(Shortest Path)
Dijkstra’s algorithm is a classic method for finding the shortest path from a starting node to every other node in a weighted graph (with non-negative edge weights).
dist
where every node’s distance is set to infinity (∞
), except the start node which is set to 0.previous
map to keep track of the best preceding node for each node (useful for reconstructing the shortest path).u
with the smallest distance from the queue.v
of u
:
alt = dist[u] + weight(u, v)
alt
is less than dist[v]
:
dist[v]
to alt
.previous[v]
to u
.v
in the priority queue with the new distance.dist
object contains the shortest distances from the start node to every other node.previous
map.Consider the following graph:
Goal: Find the shortest path from node A
to node D
.
dist = { A: 0, B: ∞, C: ∞, D: ∞ }
previous = {}
[(A, 0)]
alt = 0 + 1 = 1
→ Update dist[B] = 1
, previous[B] = A
.alt = 0 + 4 = 4
→ Update dist[C] = 4
, previous[C] = A
.[(B, 1), (C, 4)]
.alt = 1 + 2 = 3
→ Update dist[C] = 3
, previous[C] = B
.alt = 1 + 5 = 6
→ Update dist[D] = 6
, previous[D] = B
.[(C, 3), (C, 4), (D, 6)]
alt = 3 + 1 = 4
→ Update dist[D] = 4
, previous[D] = C
.[(D, 4), (D, 6)]
.previous[D] = C
previous[C] = B
previous[B] = A
https://drawtocode.vercel.app/problems/dijkstras-algorithm
Loading component...
Loading component...
INPUT : graph = { 'A': [('B', 1), ('C', 4)], 'B': [('A', 1), ('C', 2), ('D', 5)], 'C': [('A', 4), ('B', 2), ('D', 1)], 'D': [('B', 5), ('C', 1)] }
OUTPUT: Shortest paths from A: A : 0 B : 1 C : 3 D : 4
public static void main(String[] args) {
Map<String, List<Node>> graph = new HashMap<>();
graph.put("A", Arrays.asList(new Node("B", 1), new Node("C", 4)));
graph.put("B", Arrays.asList(new Node("A", 1), new Node("C", 2), new Node("D", 5)));
graph.put("C", Arrays.asList(new Node("A", 4), new Node("B", 2), new Node("D", 1)));
graph.put("D", Arrays.asList(new Node("B", 5), new Node("C", 1)));
String start = "A";
Map<String, Integer> shortestPaths = dijkstra(graph, start);
System.out.println("Shortest paths from " + start + ":");
for (Map.Entry<String, Integer> entry : shortestPaths.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}//Loop End
}//function end
public static Map<String, Integer> dijkstra(Map<String, List<Node>> graph, String start) {
Map<String, Integer> distances = new HashMap<>();
for (String vertex : graph.keySet()) {
distances.put(vertex, Integer.MAX_VALUE);
} //Loop End
distances.put(start, 0);
PriorityQueue<Node> queue = new PriorityQueue<>();
queue.add(new Node(start, 0));
while (!queue.isEmpty()) {
Node current = queue.poll();
String currentVertex = current.vertex;
int currentDistance = current.distance;
if (currentDistance > distances.get(currentVertex)) {
continue;
}
for (Node neighbor : graph.get(currentVertex)) {
int distanceThroughCurrent = currentDistance + neighbor.distance;
if (distanceThroughCurrent < distances.get(neighbor.vertex)) {
distances.put(neighbor.vertex, distanceThroughCurrent);
queue.add(new Node(neighbor.vertex, distanceThroughCurrent));
}
}//Loop end
}//Loop end
return distances;
}//function end
static class Node implements Comparable<Node> {
public String vertex;
public int distance;
public Node(String vertex , int distance) {
this.vertex = vertex;
this.distance = distance;
}
@Override
public int compareTo(Node other) {
return this.distance - other.distance;
}
} // Class End