/** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ // Graph operation main function. // To use: java -ea GraphTest import java.io.*; import java.util.*; public class GraphTest extends junit.framework.TestCase { static final int UNVISITED = 0; static final int VISITED = 1; // Create a graph from file static Graph createGraph(BufferedReader file, Graph G) throws IOException { String line = null; StringTokenizer token; boolean undirected = false; int i, v1, v2, weight; assert (line = file.readLine()) != null : "Unable to read number of vertices"; while(line.charAt(0) == '#') assert (line = file.readLine()) != null : "Unable to read number of vertices"; token = new StringTokenizer(line); int n = Integer.parseInt(token.nextToken()); G.Init(n); for (i=0; i (D[i][k] + D[k][j]))) D[i][j] = D[i][k] + D[k][j]; } /** * This method is automatically called once before each test case * method, so that all the variables are cleanly initialized for * each test. */ public void setUp() { out = new StringBuffer(100); } public void testGraph() throws IOException { int i,j; BufferedReader f; f = new BufferedReader(new InputStreamReader(new FileInputStream("testfile.gph"))); Graph G = new Graphm(); createGraph(f, G); int[][] D = new int[G.n()][G.n()]; for (i=0; i< G.n(); i++) for (j=0; j< G.n(); j++) D[i][j] = Integer.MAX_VALUE; for (i=0; i< G.n(); i++) D[i][i] = 0; Floyd(G, D); for (i=0; i< G.n(); i++) for (j=0; j< G.n(); j++) if (D[i][j] == Integer.MAX_VALUE) out.append("-1 "); else out.append(D[i][j] + " "); assertEquals(out.toString(), "0 1 -1 8 4 0 -1 7 2 3 0 10 7 3 -1 0 "); } }