Dijkstra’s algorithm
Dijkstra finds shortest paths from a start vertex as long as no weight is negative.
Rules
- Start with distance 0
- Always pick the open vertex with the smallest distance
- Relax its neighbours
Your turn
Three problems on this topic. Separate lists with commas.
- 1 · Dijkstra
- 2
- 3
Dijkstra from A on the undirected graph (ties alphabetically). Find the shortest distances and the order in which the vertices are finalised.
| Edge | Weight |
|---|---|
| A – B | 2 |
| B – C | 1 |
| A – D | 9 |
| D – E | 2 |
| E – F | 1 |
| B – F | 7 |
| B – E | 6 |
| A – E | 1 |
Distances A, B, C, D, E, F (comma-separated)
Order (comma-separated)
Worked example
Dijkstra from A on the undirected graph (ties alphabetically). Find the shortest distances and the order in which the vertices are finalised.
| Edge | Weight |
|---|---|
| A – B | 3 |
| A – C | 5 |
| C – D | 4 |
| B – E | 1 |
| A – F | 7 |
| D – E | 2 |
| A – E | 3 |
| B – F | 7 |
Worked solution
- 1A: 0
- 2B: 3
- 3E: 3
- 4C: 5
- 5D: 5
- 6F: 7
Typical mistake: A vertex is only final once it has the smallest tentative distance, not when first discovered.
Common questions
Why no negative edges?
A vertex already finalised could otherwise get cheaper later.
