private void showPathWithDistAndSum(DijkstraAlgorithm dijkstra, LinkedList<Vertex> path) { int sum = 0; for (int i = 0; i < path.size(); i++) { System.out.print(path.get(i).getName()); if (i + 1 < path.size()) { int dist = dijkstra.getDistance(path.get(i), path.get(i + 1)); sum += dist; System.out.print(" dist= " + dist + " -> "); } } System.out.println("Goal!"); System.out.println("sum = " + sum); }
@Test public void testExecute() throws Exception { // Setup // Generate nodes nodes = new ArrayList<Vertex>(); edges = new ArrayList<Edge>(); for (int i = 0; i < 11; i++) { Vertex location = new Vertex("Node_" + i, "Node_" + i); nodes.add(location); } addLane("Edge_0", 0, 1, 85); addLane("Edge_1", 0, 2, 217); addLane("Edge_2", 0, 4, 173); addLane("Edge_3", 2, 6, 186); addLane("Edge_4", 2, 7, 103); addLane("Edge_5", 3, 7, 183); addLane("Edge_6", 5, 8, 250); addLane("Edge_7", 8, 9, 84); addLane("Edge_8", 7, 9, 167); addLane("Edge_9", 4, 9, 502); addLane("Edge_10", 9, 10, 40); addLane("Edge_11", 1, 10, 600); // Lets check from location Loc_1 to Loc_10 Graph graph = new Graph(nodes, edges); DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(graph); dijkstra.execute(nodes.get(0)); LinkedList<Vertex> path = dijkstra.getPath(nodes.get(10)); Assert.assertNotNull(path); Assert.assertTrue(path.size() > 0); for (int i = 0; i < 10; i++) { String theSource = "Node_" + i; System.out.println("soruce: " + theSource); for (Edge edge : edges) { if (edge.getSource().getName().equals(theSource)) { System.out.println(edge.getDestination() + " : weight= " + edge.getWeight()); } } } System.out.println("Path"); System.out.print("from "); for (Vertex vertex : path) { System.out.print(vertex + " -> "); } System.out.print("Goal!"); System.out.println(); int sum = 0; for (int i = 0; i < path.size(); i++) { System.out.print(path.get(i)); if (i + 1 < path.size()) { int dist = dijkstra.getDistance(path.get(i), path.get(i + 1)); sum += dist; System.out.print(" dist= " + dist + " -> "); } } System.out.println(); System.out.println("sum = " + sum); }