From 734f432013f1fdbd9a467f7b3da532feb60129ca Mon Sep 17 00:00:00 2001 From: Saurav Bhattacharya Date: Thu, 26 Mar 2026 19:21:42 -0700 Subject: [PATCH] refactor: replace double[] PQ hack in GraphUtils.dijkstra with typed DijkstraEntry The previous implementation used double[] arrays as PQ entries with a parallel vertexIndex list and vertexToIdx map for int-to-vertex lookups. This required fragile double-to-int casting on every PQ poll and O(V) index bookkeeping. Replaced with a typed DijkstraEntry class (distance + vertex String), matching the pattern already used in ShortestPathFinder. This eliminates the vertexIndex list, vertexToIdx map, and the casting, making the code clearer and less error-prone. --- Gvisual/src/gvisual/GraphUtils.java | 53 +++++++++++++++++------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/Gvisual/src/gvisual/GraphUtils.java b/Gvisual/src/gvisual/GraphUtils.java index 4b8638d..eacf1a6 100644 --- a/Gvisual/src/gvisual/GraphUtils.java +++ b/Gvisual/src/gvisual/GraphUtils.java @@ -584,24 +584,15 @@ public static DijkstraResult dijkstra(Graph graph, String source) Map prev = new HashMap(); Set visited = new HashSet(); - // PQ entries: [distance, vertexIndex] - final List vertexIndex = new ArrayList(); - vertexIndex.add(source); - final Map vertexToIdx = new HashMap(); - vertexToIdx.put(source, 0); - - PriorityQueue pq = new PriorityQueue(11, - (double[] a, double[] b) -> { - return Double.compare(a[0], b[0]); - }); + PriorityQueue pq = new PriorityQueue(); dist.put(source, 0.0); - pq.add(new double[]{0.0, 0}); + pq.add(new DijkstraEntry(0.0, source)); while (!pq.isEmpty()) { - double[] entry = pq.poll(); - double entryDist = entry[0]; - String u = vertexIndex.get((int) entry[1]); + DijkstraEntry entry = pq.poll(); + double entryDist = entry.distance; + String u = entry.vertex; if (visited.contains(u)) continue; visited.add(u); @@ -621,20 +612,38 @@ public static DijkstraResult dijkstra(Graph graph, String source) if (oldDist == null || newDist < oldDist) { dist.put(v, newDist); prev.put(v, u); - - Integer idx = vertexToIdx.get(v); - if (idx == null) { - idx = vertexIndex.size(); - vertexIndex.add(v); - vertexToIdx.put(v, idx); - } - pq.add(new double[]{newDist, idx}); + pq.add(new DijkstraEntry(newDist, v)); } } } return new DijkstraResult(dist, prev); } + /** + * Typed priority-queue entry for Dijkstra's algorithm. Replaces the + * previous {@code double[]} hack that required a parallel + * {@code vertexIndex} list and {@code vertexToIdx} map for + * int-to-vertex lookups. Eliminates O(V) index bookkeeping and + * fragile double-to-int casting on every PQ poll. + * + *

Consistent with the approach already used in + * {@link ShortestPathFinder}.

+ */ + private static final class DijkstraEntry implements Comparable { + final double distance; + final String vertex; + + DijkstraEntry(double distance, String vertex) { + this.distance = distance; + this.vertex = vertex; + } + + @Override + public int compareTo(DijkstraEntry other) { + return Double.compare(this.distance, other.distance); + } + } + /** * Reconstructs the shortest path from source to target using the * predecessor map from a Dijkstra result.