Example Solution for Programming Assignment 7, Advanced Programming I

Advanced Programming I, Spring 2006

Programming Assignment 7 - An Example Solution


Table of Contents

Main

You know the drill: read in the problem, solve it, print out the answer. The solution is straightforward brute force. For that we'll need to keep track of the current solution as well as the best one found so far. A linear ordering of the vertices of a graph is a permutation of the list 0, 1, ... |V| - 1. A brute-force search is a search of through all permutations of the list.

If the list has v < 2 vertices, then the problem is solved and current solution can be compared with the best one.

So much for main().

Reading Graphs

For reading use the recursive trick of reading input lines until there's no more, creating the apropriately sized data structure, then unwind storing each input line in the data strcture. If an error is detected at any point, bail by throwing a string error message. The data structure is a graph sized to contain vcnt vertices. Each non-empty input line should be a node adjacency list:

v n0 n1 ... ni

for i ≥ -1, where v is a vertex and nj is adjacent to v in the graph. All vertices should be numeric labels.

The Solution

The Graph

A straightforward representation of an undirected graph. Internally, a graph with V vertices is represented as an edge list stored in a V × V - 1 matrix rather than a linked list. The neighbors of vertex i are stored in row i; the number of neighbors are stored in the first element and the neighbors are stored in succeeding elements.

The neighbor matrix is stored as a vector and accessed with the usual matrix-address calcuation:

The first element in the ith neighbor-matrix row is the number of neighbors for vertex i. The rest of the graph ADT is straightforward.

Index


This page last modified on 10 May 2006.