From 31f8e30a58fc108cec3648aab44b57c948747ad7 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 1 Nov 2022 13:27:55 +0100 Subject: [PATCH] solved Task 4 --- src/ch/zhaw/ads/ADS7_4_test.java | 2 +- src/ch/zhaw/ads/RouteServer.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ch/zhaw/ads/ADS7_4_test.java b/src/ch/zhaw/ads/ADS7_4_test.java index 057afb7..bb93640 100644 --- a/src/ch/zhaw/ads/ADS7_4_test.java +++ b/src/ch/zhaw/ads/ADS7_4_test.java @@ -22,7 +22,7 @@ public class ADS7_4_test { String fileToTest = "RouteServer.java"; @BeforeEach - private void init() throws Exception { + void init() throws Exception { String swiss = "Winterthur Zürich 25\n" + "Zürich Bern 126\n" + "Zürich Genf 277\n" + diff --git a/src/ch/zhaw/ads/RouteServer.java b/src/ch/zhaw/ads/RouteServer.java index 6b345ab..4ee54ec 100644 --- a/src/ch/zhaw/ads/RouteServer.java +++ b/src/ch/zhaw/ads/RouteServer.java @@ -27,6 +27,34 @@ public class RouteServer implements CommandExecutor { apply the dijkstra algorithm */ public void dijkstraRoute(Graph 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 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 }