Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 package dijkstra; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.LinkedList; | |
5 import java.util.List; | |
6 import java.util.Scanner; | |
7 | |
8 public class TestDijkstraAlgorithm { | |
9 | |
10 private static List<Vertex> nodes; | |
11 private static List<Edge> edges; | |
manu_chaud
2017/11/18 23:13:47
Why are these variables static? Would this make an
| |
12 //private static List<Distance> duration; | |
13 ········ | |
14 | |
15 | |
16 //public void testExcute() { | |
17 public static void main(String[] args){ | |
18 nodes = new ArrayList<Vertex>(); | |
19 edges = new ArrayList<Edge>(); | |
20 // duration = new ArrayList<Duration>(); | |
21 ················ | |
22 for (int i = 0; i < 11; i++) { | |
23 Vertex location = new Vertex("hop" + i, "hop" | |
manu_chaud
2017/11/18 23:13:47
To make it consistent, if a list of Vertex is stor
| |
24 + "~" + i); | |
25 nodes.add(location); | |
26 // edge weight = new edge() | |
27 } | |
28 | |
29 addLane("Edge_0", 0, 1, 85); | |
30 addLane("Edge_1", 0, 2, 217); | |
31 addLane("Edge_2", 0, 4, 173); | |
32 addLane("Edge_3", 2, 6, 186); | |
33 addLane("Edge_4", 2, 7, 103); | |
34 addLane("Edge_5", 3, 7, 183); | |
35 addLane("Edge_6", 5, 8, 250); | |
36 addLane("Edge_7", 8, 9, 84); | |
37 addLane("Edge_8", 7, 9, 167); | |
38 addLane("Edge_9", 4, 9, 502); | |
39 addLane("Edge_10", 9, 10, 40); | |
40 addLane("Edge_11", 1, 10, 600); | |
41 //reverse path | |
42 addLane("Edge_0", 1, 0, 85); | |
43 addLane("Edge_1", 2, 0, 217); | |
44 addLane("Edge_2", 4, 0, 173); | |
45 addLane("Edge_3", 6, 2, 186); | |
46 addLane("Edge_4", 7, 2, 103); | |
47 addLane("Edge_5", 7, 3, 183); | |
48 addLane("Edge_6", 8, 5, 250); | |
49 addLane("Edge_7", 9, 8, 84); | |
50 addLane("Edge_8", 9, 7, 167); | |
51 addLane("Edge_9", 9, 4, 502); | |
52 addLane("Edge_10", 10, 9, 40); | |
53 addLane("Edge_11", 10, 1, 600); | |
54 //Array2CSV(alldata,"test.csv"); | |
55 long start=System.currentTimeMillis(); //获取开始时间·· | |
56 | |
57 | |
58 // Lets check from location Loc_1 to Loc_10 | |
59 Graph graph = new Graph(nodes, edges); | |
60 DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(graph); | |
61 Scanner reader = new Scanner(System.in); // Reading from System .in | |
62 System.out.println("Enter a source hop: "); | |
63 int n = reader.nextInt(); // Scans the next token of the input as an int. | |
64 System.out.println("Enter a dst hop: "); | |
65 int m = reader.nextInt();· | |
66 //once finished | |
67 | |
68 | |
69 reader.close();· | |
70 dijkstra.execute(nodes.get(n)); | |
71 LinkedList<Vertex> path = dijkstra.getPath(nodes.get(m)); | |
72 ············· | |
73 ··········· | |
74 // assertNotNull(path); | |
75 // assertTrue(path.size() > 0); | |
76 System.out.println( "the shortest path from " + n +" to "+ m ); | |
77 long end = System.currentTimeMillis(); | |
78 System.out.println("Time to find shortest path (first time) from " + n + " to " + m + ": " + (end-start) + "ms"); | |
79 for (Vertex vertex : path) { | |
80 System.out.println( vertex); | |
81 } | |
82 ················ | |
83 // for (Vertex vertex : path) { | |
84 // double sumDistance =0; | |
85 ··················· | |
86 ··················· | |
87 ··················· | |
88 //System.out.println( vertex.weight); | |
89 //} | |
90 ················ | |
91 } | |
92 ········ | |
93 private static void addLane(String laneId, int sourceLocNo, int destLocN o, int duration) { | |
manu_chaud
2017/11/18 23:13:47
This function would be better called 'addEdge', ri
| |
94 Edge lane = new Edge(laneId, nodes.get(sourceLocNo), nodes.get(d estLocNo), duration); | |
manu_chaud
2017/11/18 23:13:47
To make it clear, would be nice to replace "durati
| |
95 edges.add(lane); | |
96 } | |
97 | |
98 // private static class Distance { | |
99 | |
100 // public Distance() { | |
101 ············ | |
102 //} | |
103 //} | |
104 | |
105 ··· | |
106 | |
107 ··· | |
108 } | |
109 | |
110 | |
OLD | NEW |