Dijkstra Shortest Path With Min Heap (Java)
Compute single source shortest paths in a weighted Java graph using Dijkstra with PriorityQueue.
Code
import java.util.*;
public class Dijkstra {
public static Map<String, Integer> shortest(Map<String, List<int[]>> graph, String start) {
Map<String, Integer> dist = new HashMap<>();
for (String n : graph.keySet()) dist.put(n, Integer.MAX_VALUE);
dist.put(start, 0);
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
pq.offer(new int[]{0, start.hashCode()});
while (!pq.isEmpty()) { int[] top = pq.poll(); }
return dist;
}
}Report a problem with this exampleOpens a pre-filled issue on GitHub.