www.pudn.com > FindTheShortestPath-java.rar
To Read all the content
[file head]:
package shortestpath;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Collection;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.NoSuchElementException;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Graph {
public static final double INFINITY = Double.MAX_VALUE;
//在图上添加一条边
public void addEdge(String sourceName, String destName, double cost) {
Vertex v = getVertex(sourceName);
Vertex w = getVertex(destName);
v.adj.add(new Edge(w, cost));
}
private Vertex getVertex(String vertexName) {
Vertex v = (Vertex) vertexMap.get(vertexName);
if (v == null) {
v = new Vertex(vertexNam
... ...
[file tail]:
... ...
line;
while ( (line = graphFile.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
try {
if (st.countTokens() != 3) {
System.out.println("Skipping bad line" + line);
continue;
}
String source = st.nextToken();
String dest = st.nextToken();
double cost = Double.parseDouble(st.nextToken());
g.addEdge(source, dest, cost);
}
catch (NumberFormatException e) {
System.out.println("Skipping bad line" + line);
}
}
}
catch (IOException e) {
System.err.println(e);
}
System.out.println("File read……");
System.out.println("共有" + g.vertexMap.size() + "个地点");
try {
g.Dijkstra(startName);
path2 = g.printPath(destName);
}
catch (NoSuchElementException e) {
System.err.println(e);
}
catch (GraphException e) {
System.err.println(e);
}
return path2;
}
}