// Source code example for "A Practical Introduction // to Data Structures and Algorithm Analysis" // by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer // Generic graph operation main function: adjacency matrix version // To use: Gmain import java.io.*; import java.util.*; // Driver class for generic graph tester: adjacency matrix version public class Gmain { static final int UNVISITED = 0; static final int VISITED = 1; // Create a graph from a file static Graph createGraph(DataInputStream file) throws IOException { String line; // We will read in the file one line at a time StringTokenizer token; // Then we will use the tokenizer on the line boolean undirected = false; // The file will tell us which it really is int i, v1, v2, weight; // First get number of vertices // Start with a priming read Assert.notNull(line = file.readLine(), "Unable to read number of vertices"); while(line.charAt(0) == '#') // Skip comment lines, if any Assert.notNull(line = file.readLine(), "Unable to read number of vertices"); token = new StringTokenizer(line); // Start parsing line int n = Integer.parseInt(token.nextToken()); // Number of vertices Graph G = new Graphm(n); // Now we can make a graph // Now, get the graph type Assert.notNull(line = file.readLine(), "Unable to read graph type"); if (line.charAt(0) == 'U') undirected = true; else if (line.charAt(0) == 'D') undirected = false; else Assert.notFalse(false, "Bad graph type: " + line); // Read in a series of edges while((line = file.readLine()) != null) { token = new StringTokenizer(line); v1 = Integer.parseInt(token.nextToken()); v2 = Integer.parseInt(token.nextToken()); if (token.hasMoreTokens()) weight = Integer.parseInt(token.nextToken()); else // No weight given -- set at 1 weight = 1; G.setEdge(v1, v2, weight); if (undirected) // Also put in edge in other direction G.setEdge(v2, v1, weight); } return G; } // What follows is the book code for whatever graph // algorithm we are trying to test. // The algorithm itself will get called from something named "graphOp" // so that a generic main routine can be used. static void PreVisit(Graph G, int v) { System.out.println("PreVisit vertex " + v); } static void PostVisit(Graph G, int v) { System.out.println("PostVisit vertex " + v); } static void graphOp(Graph G) { BFS(G, 0); } static void BFS(Graph G, int start) { // Breadth first search Queue Q = new AQueue(G.n()); // Use a Queue Q.enqueue(new Integer(start)); G.setMark(start, VISITED); while (!Q.isEmpty()) { // Process each vertex on Q int v = ((Integer)Q.dequeue()).intValue(); PreVisit(G, v); // Take appropriate action for (Edge w = G.first(v); G.isEdge(w); w = G.next(w)) if (G.getMark(G.v2(w)) == UNVISITED) { // Put neighbors on Q G.setMark(G.v2(w), VISITED); Q.enqueue(new Integer(G.v2(w))); } PostVisit(G, v); // Take appropriate action } } // Generic main function for graph tests -- // both adjacency list and adjacency graph public static void main(String args[]) throws FileNotFoundException, IOException { DataInputStream f; if (args.length == 0) // Read graph from standard input f = new DataInputStream(System.in); else // Data file entered as parameter f = new DataInputStream(new FileInputStream(args[0])); Graph G = createGraph(f); for (int i=0; i