A levels Further Mathematics · Decision mathematics
Shortest paths
1The shortest path problem
The shortest path problem asks for the route of least total weight between two vertices of a network.
For small networks you can check by inspection, but Dijkstra's algorithm solves it systematically for any network with non-negative weights.
By inspection
- In the network, from A to D the possible routes include A–B–D (9), A–C–D (11) and A–C–B–D (10).
- The shortest is A–B–D with length 9.
For large networks there are too many routes to check by hand, which is why an algorithm is needed.
Check yourself
Why is an algorithm needed for shortest paths in large networks?
Show the answer
There are far too many routes to check one by one
The number of routes grows very fast.
2Dijkstra's algorithm
Dijkstra's algorithm:
- Give the start vertex the permanent label 0.
- For each vertex joined to the vertex just made permanent, work out (permanent label + edge weight). If this is smaller than its current temporary label, replace it.
- Choose the vertex with the smallest temporary label and make it permanent.
- Repeat until the destination is permanent.
Each vertex's box records: order of becoming permanent, permanent label, and working (temporary) values.
Final Dijkstra labels from A (red numbers). The shortest route to F is A–B–D–F, length 13.
Check yourself
In Dijkstra's algorithm, which vertex becomes permanent next?
Show the answer
The one with the smallest temporary label
Its distance cannot be improved.
3Carrying out Dijkstra
From A to F
- A: permanent 0. Temporary: B 4, C 3.
- C smallest: permanent 3. Update B: $3 + 2 = 5$, not better than 4. D: 11, E: 9.
- B: permanent 4. Update D: $4 + 5 = 9$, better than 11.
- D and E both 9: make D permanent (either can go first). Update E: $9 + 3 = 12$, no; F: 13.
- E: permanent 9. Update F: $9 + 7 = 16$, no.
- F: permanent 13.
Check yourself
A vertex has temporary label 11. A new route gives 9. What happens?
Show the answer
The label is replaced by 9
Always keep the smaller value.
4Finding the route
Dijkstra gives the length of the shortest path. To find the route, trace back from the destination:
- An edge $XY$ is on the route if label of Y − label of X = weight of XY.
Tracing back from F
- F 13: $13 - 9 = 4 = $ DF, so the route comes from D.
- D 9: $9 - 4 = 5 = $ BD, so it comes from B.
- B 4: $4 - 0 = 4 = $ AB, so it comes from A.
- Route: A–B–D–F, length 13.
Check yourself
How do you find the route after Dijkstra's algorithm?
Show the answer
Trace back, using edges whose weight equals the difference in labels
Work backwards from the destination.
5Properties and limitations
- Dijkstra's algorithm finds the shortest path from the start to every vertex that becomes permanent, not just the destination.
- It requires non-negative weights.
- It works on directed networks if you only follow edges in their allowed direction.
Shortest distances from A
- From the labels: B 4, C 3, D 9, E 9, F 13.
- So the shortest path from A to E has length 9, via A–C–E.
Floyd's algorithm is another method, which finds shortest distances between all pairs of vertices using a matrix updated once for each vertex.
Check yourself
Dijkstra's algorithm from A is complete. What do the permanent labels give?
Show the answer
The shortest distance from A to each vertex
One run gives many answers.
6Shortest paths with conditions
Questions often add a condition.
Passing through a vertex
- Find the shortest route from A to F that passes through E.
- Shortest A to E is 9 (A–C–E). Shortest E to F is 7 (direct), as no route via D is shorter ($3 + 4 = 7$ also).
- Total $= 9 + 7 = 16$.
An edge closed
- If edge DF is closed, F can only be reached via E.
- Shortest A to F $= 9 + 7 = 16$.
Check yourself
How do you find the shortest route from A to F via E?
Show the answer
Add the shortest A–E distance and the shortest E–F distance
Split into two shortest path problems.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera