From 421ec17af505299b3b213fe5d4d7d4efe6363a0a Mon Sep 17 00:00:00 2001 From: Sugam Adhikari Date: Sun, 30 Aug 2020 17:30:09 +0545 Subject: [PATCH] Fixed PriorityQueue Logic I have added a class at the end for the comparator to fulfill the comparison needed to arrange the nodes in the priority queue. --- src/roadgraph/MapGraph.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/roadgraph/MapGraph.java b/src/roadgraph/MapGraph.java index 0437c8f..c0d0e3d 100644 --- a/src/roadgraph/MapGraph.java +++ b/src/roadgraph/MapGraph.java @@ -322,6 +322,7 @@ private List searchOnWeightedGraph(GeographicPoint start, Geogr MapNode endNode = pointNodeMap.get(goal); HashMap parentMap = new HashMap(); + DistanceComparator comparator = new DistanceComparator(getNumEdges(), comparator); PriorityQueue toExplore = new PriorityQueue(); HashSet visited = new HashSet(); @@ -461,3 +462,19 @@ public static void main(String[] args) { } } + +class DistanceComparator implements Comparator{ + @Override + public int compare(MapNode a, MapNode b) { + double distanceA = a.getActualDistance() + a.getDistance(); + double distanceB = b.getActualDistance() + b.getDistance(); + + if(distanceA < distanceB) { + return -1; + } + if(distanceB < distanceA) { + return 1; + } + return 0; + } +}