Finding Shortest Paths in Weighted Graphs
Stores shortest known distance from source to each node
Stores unvisited nodes, always picks node with minimum distance
Updating distance if shorter path found through current node
Tracks processed nodes to avoid rechecking
function Dijkstra(Graph, source):
for each vertex v in Graph:
dist[v] ← INFINITY
prev[v] ← UNDEFINED
dist[source] ← 0
Q ← priority queue with all vertices
while Q is not empty:
u ← vertex in Q with min dist[u]
remove u from Q
for each neighbor v of u:
alt ← dist[u] + weight(u, v)
if alt < dist[v]:
dist[v] ← alt
prev[v] ← u
return dist, prev
| Node | Distance | Previous |
|---|
| Node | Distance | Previous |
|---|
| Implementation | Time Complexity | Space Complexity |
|---|---|---|
| Array | O(V²) | O(V) |
| Binary Heap | O((V+E) log V) | O(V) |
| Fibonacci Heap | O(E + V log V) | O(V) |
Note: Most common is binary heap implementation