- Asked 2/14
- 2069 Bhadra · 5 marks
- 2069 Poush · 6 marks
Dijkstra's shortest path algorithm
Short answer
Dijkstra repeatedly selects the unvisited vertex with smallest tentative distance, finalizes it, and relaxes neighbors. From source A, distances are A=0, C=2, B=3 via C, D=8 via B, E=10 via D. Works only on non-negative weights.
Discuss Dijkstra's shortest path algorithm with suitable example.
Dijkstra's algorithm finds the shortest paths from one source vertex to all other vertices in a weighted graph with non-negative edge weights. It is greedy: it repeatedly selects the unvisited vertex with the smallest known distance, finalises it, and relaxes (updates) the distances of its neighbours.
Algorithm
DIJKSTRA(G, s)
1. for each vertex v: dist[v] = infinity, prev[v] = none
2. dist[s] = 0; S = empty set
3. while S does not contain all vertices
4. u = vertex not in S with minimum dist[u]; add u to S
5. for each neighbour v of u not in S
6. if dist[u] + w(u,v) < dist[v]
7. dist[v] = dist[u] + w(u,v); prev[v] = u
Time: O(V²) with an array, O(E log V) with a binary heap.
Example
Undirected graph with edges A-B 4, A-C 2, B-C 1, B-D 5, C-D 8, C-E 10, D-E 2. Source = A.
| Step | Vertex selected | dist A | dist B | dist C | dist D | dist E |
|---|---|---|---|---|---|---|
| Init | - | 0 | ∞ | ∞ | ∞ | ∞ |
| 1 | A (0) | 0 | 4 | 2 | ∞ | ∞ |
| 2 | C (2) | 0 | 3 (via C) | 2 | 10 | 12 |
| 3 | B (3) | 0 | 3 | 2 | 8 (via B) | 12 |
| 4 | D (8) | 0 | 3 | 2 | 8 | 10 (via D) |
| 5 | E (10) | 0 | 3 | 2 | 8 | 10 |
Shortest paths from A:
- A to C: 2 (A-C)
- A to B: 3 (A-C-B)
- A to D: 8 (A-C-B-D)
- A to E: 10 (A-C-B-D-E)
Dijkstra fails with negative edge weights (Bellman-Ford is used instead).
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Dijkstra's shortest paths from source F · Data Structure and Algorithm, chapter 10
- Dijkstra shortest path (2071 Bhadra) · Discrete Structure, chapter 4 · asked 4×
- Dijkstra shortest path (2070 Magh) · Discrete Structure, chapter 4 · asked 4×
- Dijkstra shortest path (2069 Bhadra) · Discrete Structure, chapter 4 · asked 4×
- Dijkstra shortest path (2068 Bhadra) · Discrete Structure, chapter 4 · asked 4×