package jimDij; import java.io.*; /***** * ReadData builds the Graph from a given file.

* * The nodes and edges of the graph are defined in * the file g5.obj, which is the same data file used * in the applet in ../applet_one/. * (http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/Dijkstra.shtml) *

* * Most of the code here is very close to that in the applet, * with minor modifications to fit my data structures.

* * If I had been doing this as a project for the intro Java class, * I would probably have just put the data right into this file, * with code something like * *

 *    Node brattleboro = new Node("Boston");
 *    Node wilmington = new Node("Wilmington");
 *    Edge Route9 = new Edge(brattleboro, wilmington, 20);
 *    theGraph.addNode(brattleboro);
 *    theGraph.addNode(wilmington);
 *  
* * and so on to build up the nodes and the connections and distances * between them. * * @author Jim Mahoney (mahoney@marlboro.edu) * @version 0.1 Nov 26 2002 ***/ public class ReadData { /******* * initData defines a single instance of a Graph * by reading in Node and Edge definitions * from a given file. * * This should be called once to set everything up. * * @param startNodeName the name of the node where the search starts * @param fileName the name of the data file * @return the Graph object that holds the Nodes and Edges. * *****/ /////////////////////////////////////////////////////////////////// // The data file has the following form, where "#" marks comments. // (Though I'm not using (x,y) here, nor the names of the edges.) // // 2 1 graph # 2 nodes, 1 edge, not a digraph // Boston 100 200 # name x y // Chicago 600 700 # // # // Rt9 Boston Chicago 23 # name fromNode toNode distance // # /////////////////////////////////////////////////////////////////// public static Graph initData(String fileName, String startNodeName){ int n,m; String s; Graph theGraph = new Graph(); boolean isdigraph; // Since we're doing file input and output, which // might fail, all the work happens in a "try" block. try { InputStream is = new FileInputStream(fileName); Reader r = new BufferedReader(new InputStreamReader(is)); StreamTokenizer st = new StreamTokenizer(r); st.commentChar('#'); // read in the number of vertices and edges from the data file st.nextToken(); n = (int)st.nval; st.nextToken(); m = (int)st.nval; st.nextToken(); s = st.sval; isdigraph = "digraph".equals(s); // read in nodes and their names from the data file for (int i = 0; i