Loading
Dijkstra's algorithm finds the shortest path from one node to every other in a graph with non-negative weights: repeatedly fix the nearest unvisited node and update its neighbours.
Edges: A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10, D–E 2, D–F 6, E–F 2 (undirected). Find the shortest path from A to F.
Start with d(A) = 0 and ∞ elsewhere. Each step visits the unvisited node with the smallest d and updates its neighbours. Each cell shows d and the node it came from.
| Step | Visit | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|
| 1 | A | 0 | 4 (A) | 2 (A) | ∞ | ∞ | ∞ |
| 2 | C | ✓ | 3 (C) | 2 (A) | 10 (C) | 12 (C) | ∞ |
| 3 | B | ✓ | 3 (C) | ✓ | 8 (B) | 12 (C) | ∞ |
| 4 | D | ✓ | ✓ | ✓ | 8 (B) | 10 (D) | 14 (D) |
| 5 | E | ✓ | ✓ | ✓ | ✓ | 10 (D) | 12 (E) |
| 6 | F | ✓ | ✓ | ✓ | ✓ | ✓ | 12 (E) |
The calculator below opens on this example.
Visiting in alphabetical order.
Not updating a shorter route.
Negative weights.
Giving only the distance.
Weights must be non-negative. Separate edges with ; and write each as 'A B 4'.
Add this calculator to a new note, next to your own working. It stays live: change the numbers there any time.