Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit 73a38c9

Browse files
Merge pull request #126 from sauravbhattacharya001/refactor/graphutils-dijkstra-typed-pq
refactor: replace double[] PQ hack in GraphUtils.dijkstra with typed DijkstraEntry
2 parents fdd81c2 + 734f432 commit 73a38c9

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

Gvisual/src/gvisual/GraphUtils.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -584,24 +584,15 @@ public static DijkstraResult dijkstra(Graph<String, Edge> graph, String source)
584584
Map<String, String> prev = new HashMap<String, String>();
585585
Set<String> visited = new HashSet<String>();
586586

587-
// PQ entries: [distance, vertexIndex]
588-
final List<String> vertexIndex = new ArrayList<String>();
589-
vertexIndex.add(source);
590-
final Map<String, Integer> vertexToIdx = new HashMap<String, Integer>();
591-
vertexToIdx.put(source, 0);
592-
593-
PriorityQueue<double[]> pq = new PriorityQueue<double[]>(11,
594-
(double[] a, double[] b) -> {
595-
return Double.compare(a[0], b[0]);
596-
});
587+
PriorityQueue<DijkstraEntry> pq = new PriorityQueue<DijkstraEntry>();
597588

598589
dist.put(source, 0.0);
599-
pq.add(new double[]{0.0, 0});
590+
pq.add(new DijkstraEntry(0.0, source));
600591

601592
while (!pq.isEmpty()) {
602-
double[] entry = pq.poll();
603-
double entryDist = entry[0];
604-
String u = vertexIndex.get((int) entry[1]);
593+
DijkstraEntry entry = pq.poll();
594+
double entryDist = entry.distance;
595+
String u = entry.vertex;
605596

606597
if (visited.contains(u)) continue;
607598
visited.add(u);
@@ -621,20 +612,38 @@ public static DijkstraResult dijkstra(Graph<String, Edge> graph, String source)
621612
if (oldDist == null || newDist < oldDist) {
622613
dist.put(v, newDist);
623614
prev.put(v, u);
624-
625-
Integer idx = vertexToIdx.get(v);
626-
if (idx == null) {
627-
idx = vertexIndex.size();
628-
vertexIndex.add(v);
629-
vertexToIdx.put(v, idx);
630-
}
631-
pq.add(new double[]{newDist, idx});
615+
pq.add(new DijkstraEntry(newDist, v));
632616
}
633617
}
634618
}
635619
return new DijkstraResult(dist, prev);
636620
}
637621

622+
/**
623+
* Typed priority-queue entry for Dijkstra's algorithm. Replaces the
624+
* previous {@code double[]} hack that required a parallel
625+
* {@code vertexIndex} list and {@code vertexToIdx} map for
626+
* int-to-vertex lookups. Eliminates O(V) index bookkeeping and
627+
* fragile double-to-int casting on every PQ poll.
628+
*
629+
* <p>Consistent with the approach already used in
630+
* {@link ShortestPathFinder}.</p>
631+
*/
632+
private static final class DijkstraEntry implements Comparable<DijkstraEntry> {
633+
final double distance;
634+
final String vertex;
635+
636+
DijkstraEntry(double distance, String vertex) {
637+
this.distance = distance;
638+
this.vertex = vertex;
639+
}
640+
641+
@Override
642+
public int compareTo(DijkstraEntry other) {
643+
return Double.compare(this.distance, other.distance);
644+
}
645+
}
646+
638647
/**
639648
* Reconstructs the shortest path from source to target using the
640649
* predecessor map from a Dijkstra result.

0 commit comments

Comments
 (0)