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