From aa6dc394cd23d24b32337a6e085413eb1551b517 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 9 Nov 2020 17:38:49 +0100 Subject: [PATCH] improved dijkstra output --- 07/src/main/java/ch/zhaw/ads/RouteServer.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/07/src/main/java/ch/zhaw/ads/RouteServer.java b/07/src/main/java/ch/zhaw/ads/RouteServer.java index 343b858..40f1426 100644 --- a/07/src/main/java/ch/zhaw/ads/RouteServer.java +++ b/07/src/main/java/ch/zhaw/ads/RouteServer.java @@ -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 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 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 dijkstra(String from, String to) { for (DijkstraNode 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 path = new ArrayList(); DijkstraNode 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; } }