Navigation systems must find the quickest route from one place to every other. Dijkstra's algorithm does this for graphs with non-negative weights.
The Single-Source Shortest Path Problem
Given a weighted graph and a source vertex s, find the shortest path from s to every other vertex, where the length of a path is the sum of its edge weights. Applications include road and network routing (OSPF routers use it), flight planning and games. The algorithm requires all edge weights to be non-negative; with negative edges it can give wrong answers and Bellman-Ford is used instead.
The Algorithm
Keep for every vertex a distance estimate d[v], initially ∞, with d[s] = 0, and a set of finished vertices. Repeat: choose the unfinished vertex u with the smallest d[u], mark it finished, and relax every edge (u, v): if d[u] + w(u, v) < d[v], set d[v] = d[u] + w(u, v) and remember u as the predecessor of v. When all vertices are finished the distances are final, and following the predecessors back from any vertex gives the shortest path. The greedy choice is justified because, with non-negative weights, the closest unfinished vertex cannot be reached more cheaply through any other unfinished vertex.
Complexity
With an array to find the minimum, each of the V selections costs Θ(V), so the total is Θ(V²). With a binary heap (priority queue) and an adjacency list, the time is Θ((V + E) log V); a Fibonacci heap gives Θ(E + V log V). The space is Θ(V) apart from the graph.
Definition: Relaxation. The step of testing whether the path to a vertex v through u is shorter than the best known so far, and updating the distance of v if it is.
Table 2.8: Dijkstra's complexity
| Implementation | Time | Best for |
|---|---|---|
| Array | Θ(V²) | Dense graphs |
| Binary heap | Θ((V + E) log V) | Sparse graphs |
| Fibonacci heap | Θ(E + V log V) | Theoretical best |
Example 2.8: Dijkstra from A
Problem: Undirected edges: A-B 4, A-C 1, C-B 2, B-D 1, C-D 5, D-E 3. Find shortest distances from A.
Solution:
- Start d(A) = 0; relax A: d(B) = 4, d(C) = 1.
- Pick C (1); relax: via C, B = 1 + 2 = 3 < 4 so d(B) = 3; D = 1 + 5 = 6.
- Pick B (3); relax: D = 3 + 1 = 4 < 6 so d(D) = 4.
- Pick D (4); relax: E = 4 + 3 = 7. Pick E (7).
Result: d = A 0, C 1, B 3, D 4, E 7; the path to E is A-C-B-D-E.
import heapq
g = {"A": [("B", 4), ("C", 1)], "B": [("A", 4), ("C", 2), ("D", 1)],
"C": [("A", 1), ("B", 2), ("D", 5)],
"D": [("B", 1), ("C", 5), ("E", 3)], "E": [("D", 3)]}
dist = {v: float("inf") for v in g}
dist["A"], pq = 0, [(0, "A")]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for v, w in g[u]:
if d + w < dist[v]:
dist[v] = d + w
heapq.heappush(pq, (dist[v], v))
print(dist)
Output
{'A': 0, 'B': 3, 'C': 1, 'D': 4, 'E': 7}
Common mistakes
- Using Dijkstra's algorithm on graphs with negative edge weights.
- Forgetting to update the distance of neighbours (relaxation) after finishing a vertex.
- Confusing the shortest path tree with a minimum spanning tree; they differ in general.
- Not recording predecessors, and so being unable to print the actual path.
Key takeaways
- Dijkstra finds shortest paths from one source in graphs with non-negative weights.
- Repeatedly finalise the closest unfinished vertex and relax its edges.
- Time Θ(V²) with an array; Θ((V + E) log V) with a binary heap.
- Predecessors give the actual path.
Practice questions
- What condition must edge weights satisfy for Dijkstra's algorithm?
- What is relaxation?
- Give the time complexity using a binary heap.
Answers to the practice questions
- They must all be non-negative.
- Updating d[v] to d[u] + w(u, v) when that is smaller than the current estimate.
- Θ((V + E) log V).
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II
