Assignment 5

CS 146
due May 14, 2002
100 points

Given an abstract class Graph, define two subclasses MatrixGraph and ListGraph, each of which extends this class, and which implement this class's abstract methods using respectively an adjacency matrix representation and an adjacency list representation. Both of the subclasses are to represent undirected graphs. The abstract methods to be implemented are described in the documentation of the Graph class.

The class Graph, a class Vertex for vertices, and the test class A5 are available from the class web site. You should not modify these files, except that you may add methods to Graph that are used both by MatrixGraph and ListGraph. You may remove an abstract method if it has the same signature as one that you are adding. Any such changes should be documented at the beginning of the Graph class.

Note that cost information for edges is to be stored with edges, and not with vertices. However marks on vertices are part of the Vertex class. Also, unweighted graphs are to be represented with edge cost values of 0 (for missing edges) or 1 (for edges that are present). In any case, attempted insertion of a negative-cost edge should be rejected -- you needn't raise an exception or print an error message in this case.

Both MatrixGraph and ListGraph should have constructors that take a Collection of String objects representing names of vertices. These constructors are to return graphs with the set of vertices given by the collection, but with no edges. If a nonVertex appears in the collection, it should be ignored, except that an error message should be printed. Similarly, if a vertex name that is given as an argument to addEdge is not the name of a vertex in the graph, you should simply print an appropriate error message and not attempt to add an edge. Finally, a duplicate vertex name should be rejected. In all three cases, your error message should print the vertex or edge that is not being added. In all cases, if you raise an exception, you should not propagate it as far as the main method.

Insertion of a duplicate edge (with perhaps a different cost) may be treated as an update or may be rejected, as you see fit, although you should document which choice you are making. In any case, adjacency lists should not contain repeated destination vertices.

The MatrixGraph class should have two additional methods. The first method, allPairsShortestPaths, should implement Floyd's algorithm, and return the all-pairs shortest path information in a new output graph. The second should take a graph all of whose components are complete graphs (e.g., as in the value returned by the first method), and return a list of the connected components in the same format as the Graph method getComponents. Both of these methods should take a (large) numerical argument that represents the infinite weight of a missing edge or path. Note that this value should be larger than any possible path cost.