solved Task 4
This commit is contained in:
parent
812dd384ce
commit
31f8e30a58
|
@ -22,7 +22,7 @@ public class ADS7_4_test {
|
||||||
String fileToTest = "RouteServer.java";
|
String fileToTest = "RouteServer.java";
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
private void init() throws Exception {
|
void init() throws Exception {
|
||||||
String swiss = "Winterthur Zürich 25\n" +
|
String swiss = "Winterthur Zürich 25\n" +
|
||||||
"Zürich Bern 126\n" +
|
"Zürich Bern 126\n" +
|
||||||
"Zürich Genf 277\n" +
|
"Zürich Genf 277\n" +
|
||||||
|
|
|
@ -27,6 +27,34 @@ public class RouteServer implements CommandExecutor {
|
||||||
apply the dijkstra algorithm
|
apply the dijkstra algorithm
|
||||||
*/
|
*/
|
||||||
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
|
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
|
||||||
|
for(DijkstraNode node : graph.getNodes()){
|
||||||
|
node.setMark(false);
|
||||||
|
node.setDist(Double.MAX_VALUE);
|
||||||
|
node.setPrev(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
DijkstraNode currentNode = graph.findNode(from);
|
||||||
|
DijkstraNode goalNode = graph.findNode(to);
|
||||||
|
Queue<DijkstraNode> redNodes = new PriorityQueue<>();
|
||||||
|
currentNode.setDist(0);
|
||||||
|
redNodes.add(currentNode);
|
||||||
|
while(!redNodes.isEmpty()){
|
||||||
|
currentNode = redNodes.remove();
|
||||||
|
if(currentNode == goalNode){ return; }
|
||||||
|
currentNode.setMark(true);
|
||||||
|
for(Edge edge : currentNode.getEdges()){
|
||||||
|
DijkstraNode neighbour = (DijkstraNode) edge.getDest();
|
||||||
|
if(!neighbour.getMark()){
|
||||||
|
double dist = currentNode.getDist() + edge.getWeight();
|
||||||
|
if(dist < neighbour.getDist()) {
|
||||||
|
neighbour.setDist(dist);
|
||||||
|
neighbour.setPrev(currentNode);
|
||||||
|
redNodes.remove(neighbour);
|
||||||
|
redNodes.add(neighbour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO implement
|
// TODO implement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue