Shortest Paths: Dijkstra's Algorithm with Min-Heap & BFS for Unweighted
Shortest Paths: Dijkstra's Algorithm with Min-Heap & BFS for Unweighted
Finding the shortest path between nodes in a network is one of the foundational problems of computer science. It powers GPS navigation systems (Google Maps, Apple Maps), internet packet routing (OSPF, IS-IS protocols), and robotic motion planning.
1. Shortest Path Algorithms Overview
The choice of algorithm depends on edge weights:
| Graph Characteristic | Optimal Algorithm | Time Complexity | Notes |
|---|---|---|---|
| Unweighted Graph (all weights = 1) | BFS | $O(V + E)$ | Fastest; queue guarantees minimal hops |
| Non-Negative Weights ($w \ge 0$) | Dijkstra's Algorithm | $O((V + E) \log V)$ | Greedy; uses Min-Priority Queue |
| Negative Weights Allowed | Bellman-Ford Algorithm | $O(V \times E)$ | Slower; can detect negative weight cycles |
| All-Pairs Shortest Path | Floyd-Warshall | $O(V^3)$ | Dynamic programming matrix approach |
2. Dijkstra's Algorithm Mechanics
Dijkstra uses a Greedy Strategy with a Min-Heap (Priority Queue):
3. Step-by-Step Trace Table
Consider a weighted directed graph with 4 nodes:
- $(0 \to 1, w=4)$
- $(0 \to 2, w=1)$
- $(2 \to 1, w=2)$
- $(1 \to 3, w=1)$
- $(2 \to 3, w=5)$
Find shortest path from Node 0 to all nodes:
| Step | Pop (dist, node) | Action / Edge Relaxations | Min-Heap Contents | Distances [0, 1, 2, 3] |
|---|---|---|---|---|
| Init | - | Start node 0 | [(0, 0)] | [0, inf, inf, inf] |
| 1 | (0, 0) | Relax $0 \to 1$ ($0+4=4$), $0 \to 2$ ($0+1=1$) | [(1, 2), (4, 1)] | [0, 4, 1, inf] |
| 2 | (1, 2) | Relax $2 \to 1$ ($1+2=3 < 4$! Better!), $2 \to 3$ ($1+5=6$) | [(3, 1), (4, 1), (6, 3)] | [0, 3, 1, 6] |
| 3 | (3, 1) | Relax $1 \to 3$ ($3+1=4 < 6$! Better!) | [(4, 1), (4, 3), (6, 3)] | [0, 3, 1, 4] |
| 4 | (4, 1) | Skipped! $4 > \text{dist}[1] (3)$ (Stale entry) | [(4, 3), (6, 3)] | [0, 3, 1, 4] |
| 5 | (4, 3) | Finalized node 3. No outgoing edges. | [(6, 3)] | [0, 3, 1, 4] |
| 6 | (6, 3) | Skipped! $6 > \text{dist}[3] (4)$ | [] | [0, 3, 1, 4] |
Final Shortest Distances: dist[0]=0, dist[1]=3, dist[2]=1, dist[3]=4. Shortest path to 3: $0 \to 2 \to 1 \to 3$ with total cost $1 + 2 + 1 = 4$.
4. Dual-Language Implementation
Python 3 Implementation
Modern JavaScript (Node.js) Implementation
5. Complexity & Optimality Analysis
- Time Complexity: $O((V + E) \log V)$
- Each vertex is pushed to the heap at least once, and edge relaxations trigger heap pushes ($E$ pushes total).
- Heap insertion/extraction takes $O(\log V)$.
- Total time: $O(E \log V + V \log V) = O((V + E) \log V)$.
- Space Complexity: $O(V + E)$
- Distance array: $O(V)$.
- Adjacency list: $O(V + E)$.
- Heap entries: $O(E)$ worst case.
Multiple Choice Questions
if curr_dist > distances[u]: continue in Dijkstra's heap loop?Dynamic Programming: Overlapping Subproblems, Memoization vs Tabulation
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.