improved dijkstra output

This commit is contained in:
navid.sassan 2020-11-09 17:38:49 +01:00
parent 727e1d7c2b
commit aa6dc394cd

View File

@ -1,8 +1,11 @@
package ch.zhaw.ads;
import java.util.Scanner;
import java.util.Queue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class RouteServer implements CommandExecutor {
Graph<DijkstraNode, Edge> graph;
@ -14,10 +17,12 @@ public class RouteServer implements CommandExecutor {
for (Node node : graph.getNodes()) {
result.append(node.getName() + "\n");
}
result.append("\"\n");
this.dijkstra("Winterthur", "Lugano");
List<DijkstraNode> path = this.dijkstra("Winterthur", "Lugano");
result.append("\n\nShortest path:\n");
for (DijkstraNode node : path) {
result.append(node.getName() + "\n");
}
return result.toString();
}
@ -44,7 +49,7 @@ public class RouteServer implements CommandExecutor {
return graph;
}
private void dijkstra(String from, String to) {
private List<DijkstraNode> dijkstra(String from, String to) {
for (DijkstraNode<Edge> node : graph.getNodes()) {
node.setMark(false);
node.setDist(Double.POSITIVE_INFINITY);
@ -75,14 +80,14 @@ public class RouteServer implements CommandExecutor {
}
}
System.out.println("\n\nShortest path:\n");
// print the shortest path
List<DijkstraNode> path = new ArrayList<DijkstraNode>();
DijkstraNode<Edge> current = goal;
while (current.getPrev() != null) {
System.out.println(current.getName());
path.add(current);
current = current.getPrev();
}
System.out.println(current.getName());
System.out.println("\n\n");
Collections.reverse(path);
return path;
}
}