CSC 103 Assignment 6:
Graph Algorithms
-- REVISED --
10PM Thursday 14 June 2001
Implement a class Graph which represents a graph as a adjacency list
of the form discussed in Section 9.1.1 of the book. Nodes in the graph have
values of type Comparable. To allow convenient construction of graphs
from test programs, the Graph class must implement the following
interface:
/****
*
* GraphInterface defines constructive methods for building a graph. The Graph
* class of Assignment 6 must implement this interface.
* <p>
* As an example of using these methods, the following sequence of calls
* constructs the directed graph in Figure 9.1 on page 292 of the textbook:
* <pre>
* Graph g = new Graph();
*
* g.addNode("1").
* addNode("2").
* addNode("3").
* addNode("4").
* addNode("5").
* addNode("6").
* addNode("7");
*
* g.addEdge("1", "2", true).
* addEdge("1", "3", true).
* addEdge("1", "4", true).
* addEdge("2", "4", true).
* addEdge("2", "5", true).
* addEdge("3", "6", true).
* addEdge("4", "3", true).
* addEdge("4", "6", true).
* addEdge("4", "7", true).
* addEdge("5", "4", true).
* addEdge("5", "7", true).
* addEdge("7", "6", true);
* </pre>
*
* @author Gene Fisher (gfisher@calpoly.edu)
* @version 5jun01
*
*/
public interface GraphInterface {
/**
* Add a node of the given value to this. Do nothing if a node of the
* given value is already in this. Return the updated or unchanged value
* of this.
*/
public GraphInterface addNode(Comparable value);
/**
* Add an unweighted edge between the nodes of the given values in this.
* Do nothing if the nodes are not in this or if there is an existing edge
* between the nodes. If the isDirected input is true, make the edge
* directed from nodeValue1 to nodeValue2. If is isDirected is false, add
* the edge as undirected. Return the updated or unchanged value of this.
*/
public GraphInterface addEdge(Comparable nodeValue1, Comparable nodeValue2,
boolean isDirected);
/**
* Add an edge of the given weight between the nodes of the given values in
* this. Do nothing if the nodes are not in this or if there is an
* existing edge between the nodes. If the isDirected input is true, make
* the edge directed from nodeValue1 to nodeValue2. If is isDirected is
* false, add the edge as undirected. Return the updated or unchanged
* value of this.
*/
public GraphInterface addEdge(Comparable nodeValue1, Comparable nodeValue2,
int weight, boolean isDirected);
}
Using the Graph class as the basic representation of a graph,
implement the two methods in the following
class:
/****
*
* Class GraphMethods contains two graph methods for generating a dependency
* list and minimum spanning tree.
*
* @author
* @version
*/
public class GraphMethods {
/**
* Generate a list of dependency lists for each node in the given directed
* graph g. For a given node n, a dependency list for n is defined as the
* list of nodes which appear before n in the directed graph, in reverse
* graph order. For nodes that have no dependencies, the dependency list
* is null. If a node has more than one dependency list, each list is
* entered separately in the outer list, per the ordering described in the
* next paragraph.
* <p>
* The output list is sorted by dependency list length, from shortest to
* longest. For nodes with the same length dependency list, the secondary
* sorting order is by ascending node value, using compareTo on values.
* <p>
* Valid output is only produced for acyclic graphs. If the input graph
* has one or more cycles, the CycleFound exception is thrown.
*/
public GeneralList dependencyLists(Graph g) throws CycleFound {}
/**
* Generate a minimum spanning tree for the given undirected, weighted
* graph g using Kruskal's algorithm. The output is the GeneralTree
* representation of the minimum spanning tree.
*/
public GeneralTree spanningTree(Graph g) {}
}
Turn in the following files to the assignment named "a6"