Java Reference
In-Depth Information
22 { 10 , 2 , 1435 }, { 10 , 4 , 496 }, { 10 , 8 , 781 }, { 10 , 11 , 239 },
23 { 11 , 8 , 810 }, { 11 , 9 , 1187 }, { 11 , 10 , 239 }
24 };
25
26 WeightedGraph<String> graph1 =
27 new WeightedGraph<>(vertices, edges);
28 WeightedGraph<String>.ShortestPathTree tree1 =
29 graph1.getShortestPath(graph1.getIndex( "Chicago" ));
30 tree1.printAllPaths();
31
32 // Display shortest paths from Houston to Chicago
33 System.out.print( "Shortest path from Houston to Chicago: " );
34 java.util.List<String> path
35 = tree1.getPath(graph1.getIndex( "Houston" ));
36 for (String s: path) {
37 System.out.print(s + " " );
38 }
39
40 edges = new int [][] {
41 { 0 , 1 , 2 }, { 0 , 3 , 8 },
42 { 1 , 0 , 2 }, { 1 , 2 , 7 }, { 1 , 3 , 3 },
43 { 2 , 1 , 7 }, { 2 , 3 , 4 }, { 2 , 4 , 5 },
44 { 3 , 0 , 8 }, { 3 , 1 , 3 }, { 3 , 2 , 4 }, { 3 , 4 , 6 },
45 { 4 , 2 , 5 }, { 4 , 3 , 6 }
46 };
47 WeightedGraph<Integer> graph2 = new WeightedGraph<>(edges, 5 );
48 WeightedGraph<Integer>.ShortestPathTree tree2 =
49 graph2.getShortestPath( 3 );
50 System.out.println( "\n" );
51 tree2.printAllPaths();
52 }
53 }
create graph1
shortest path
create edges
create graph2
print paths
All shortest paths from Chicago are:
A path from Chicago to Seattle: Chicago Seattle (cost: 2097.0)
A path from Chicago to San Francisco:
Chicago Denver San Francisco (cost: 2270.0)
A path from Chicago to Los Angeles:
Chicago Denver Los Angeles (cost: 2018.0)
A path from Chicago to Denver: Chicago Denver (cost: 1003.0)
A path from Chicago to Kansas City: Chicago Kansas City (cost: 533.0)
A path from Chicago to Chicago: Chicago (cost: 0.0)
A path from Chicago to Boston: Chicago Boston (cost: 983.0)
A path from Chicago to New York: Chicago New York (cost: 787.0)
A path from Chicago to Atlanta:
Chicago Kansas City Atlanta (cost: 1397.0)
A path from Chicago to Miami:
Chicago Kansas City Atlanta Miami (cost: 2058.0)
A path from Chicago to Dallas: Chicago Kansas City Dallas (cost: 1029.0)
A path from Chicago to Houston:
Chicago Kansas City Dallas Houston (cost: 1268.0)
Shortest path from Houston to Chicago:
Houston Dallas Kansas City Chicago
All shortest paths from 3 are:
A path from 3 to 0: 3 1 0 (cost: 5.0)
A path from 3 to 1: 3 1 (cost: 3.0)
A path from 3 to 2: 3 2 (cost: 4.0)
A path from 3 to 3: 3 (cost: 0.0)
A path from 3 to 4: 3 4 (cost: 6.0)
 
Search WWH ::




Custom Search