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

Commit b6d83b7

Browse files
refactor: deduplicate DijkstraEntry between GraphUtils and ShortestPathFinder
ShortestPathFinder had its own private DijkstraEntry class that was identical to the one in GraphUtils. Made GraphUtils.DijkstraEntry package-visible and updated ShortestPathFinder to reuse it. This eliminates a duplicated inner class (~15 lines) and ensures any future improvements to the PQ entry type only need to happen in one place.
1 parent 0ef3b7d commit b6d83b7

2 files changed

Lines changed: 8 additions & 31 deletions

File tree

Gvisual/src/gvisual/GraphUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ public static DijkstraResult dijkstra(Graph<String, Edge> graph, String source)
626626
* int-to-vertex lookups. Eliminates O(V) index bookkeeping and
627627
* fragile double-to-int casting on every PQ poll.
628628
*
629-
* <p>Consistent with the approach already used in
630-
* {@link ShortestPathFinder}.</p>
629+
* <p>Package-visible so that {@link ShortestPathFinder} can reuse
630+
* the same entry type instead of maintaining its own duplicate.</p>
631631
*/
632-
private static final class DijkstraEntry implements Comparable<DijkstraEntry> {
632+
static final class DijkstraEntry implements Comparable<DijkstraEntry> {
633633
final double distance;
634634
final String vertex;
635635

Gvisual/src/gvisual/ShortestPathFinder.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public PathResult findShortestByHops(String source, String target) {
141141
/**
142142
* Finds the shortest path by total Edge weight (Dijkstra) between source and target.
143143
*
144-
* <p>Uses a typed {@link DijkstraEntry} in the priority queue for clean
144+
* <p>Uses a typed {@link GraphUtils.DijkstraEntry} in the priority queue for clean
145145
* vertex lookups without index indirection or double-to-int casting.</p>
146146
*
147147
* @param source source vertex ID
@@ -168,16 +168,16 @@ public PathResult findShortestByWeight(String source, String target) {
168168
Map<String, String> predecessor = new HashMap<String, String>();
169169
Map<String, Edge> predecessorEdge = new HashMap<String, Edge>();
170170

171-
PriorityQueue<DijkstraEntry> pq = new PriorityQueue<DijkstraEntry>();
171+
PriorityQueue<GraphUtils.DijkstraEntry> pq = new PriorityQueue<GraphUtils.DijkstraEntry>();
172172

173173
dist.put(source, 0.0);
174174
predecessor.put(source, null);
175-
pq.add(new DijkstraEntry(0.0, source));
175+
pq.add(new GraphUtils.DijkstraEntry(0.0, source));
176176

177177
Set<String> visited = new HashSet<String>();
178178

179179
while (!pq.isEmpty()) {
180-
DijkstraEntry entry = pq.poll();
180+
GraphUtils.DijkstraEntry entry = pq.poll();
181181
double entryDist = entry.distance;
182182
String current = entry.vertex;
183183

@@ -210,7 +210,7 @@ public PathResult findShortestByWeight(String source, String target) {
210210
predecessor.put(neighbor, current);
211211
predecessorEdge.put(neighbor, e);
212212

213-
pq.add(new DijkstraEntry(newDist, neighbor));
213+
pq.add(new GraphUtils.DijkstraEntry(newDist, neighbor));
214214
}
215215
}
216216
}
@@ -282,29 +282,6 @@ public boolean areConnected(String source, String target) {
282282
return false;
283283
}
284284

285-
// --- Dijkstra priority-queue entry ---
286-
287-
/**
288-
* Typed PQ entry replacing the previous {@code double[]} hack that required
289-
* a parallel {@code vertexIndex} list and {@code vertexToIndex} map for
290-
* int-to-vertex lookups. This eliminates the O(V) index bookkeeping and
291-
* the fragile double-to-int casting on every PQ poll.
292-
*/
293-
private static final class DijkstraEntry implements Comparable<DijkstraEntry> {
294-
final double distance;
295-
final String vertex;
296-
297-
DijkstraEntry(double distance, String vertex) {
298-
this.distance = distance;
299-
this.vertex = vertex;
300-
}
301-
302-
@Override
303-
public int compareTo(DijkstraEntry other) {
304-
return Double.compare(this.distance, other.distance);
305-
}
306-
}
307-
308285
// --- private helpers ---
309286

310287
private void validateVertex(String vertex, String name) {

0 commit comments

Comments
 (0)