impera Open Impera

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.

Worked example

By inspection

  1. 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).
  2. 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?

  • Edge weights in large networks can be negative

  • Large networks often have no paths between vertices

  • Inspection always gives the wrong shortest path

  • There are far too many routes to check one by one

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:

  1. Give the start vertex the permanent label 0.
  2. 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.
  3. Choose the vertex with the smallest temporary label and make it permanent.
  4. 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?

  • The destination

  • The one with the smallest temporary label

  • The one joined by the shortest edge

  • The one with the highest degree

Show the answer

The one with the smallest temporary label

Its distance cannot be improved.

3Carrying out Dijkstra

Worked example

From A to F

  1. A: permanent 0. Temporary: B 4, C 3.
  2. C smallest: permanent 3. Update B: $3 + 2 = 5$, not better than 4. D: 11, E: 9.
  3. B: permanent 4. Update D: $4 + 5 = 9$, better than 11.
  4. D and E both 9: make D permanent (either can go first). Update E: $9 + 3 = 12$, no; F: 13.
  5. E: permanent 9. Update F: $9 + 7 = 16$, no.
  6. F: permanent 13.
Check yourself

A vertex has temporary label 11. A new route gives 9. What happens?

  • Both are kept and averaged

  • The vertex becomes permanent immediately

  • The label stays 11

  • The label is replaced by 9

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.
Worked example

Tracing back from F

  1. F 13: $13 - 9 = 4 = $ DF, so the route comes from D.
  2. D 9: $9 - 4 = 5 = $ BD, so it comes from B.
  3. B 4: $4 - 0 = 4 = $ AB, so it comes from A.
  4. Route: A–B–D–F, length 13.
Check yourself

How do you find the route after Dijkstra's algorithm?

  • Trace back, using edges whose weight equals the difference in labels

  • Read off the order in which vertices were labelled

  • Use Kruskal's algorithm on the labelled network

  • Follow the smallest edges forwards from the start

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.
Worked example

Shortest distances from A

  1. From the labels: B 4, C 3, D 9, E 9, F 13.
  2. 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?

  • The degree of each vertex in the network

  • The minimum spanning tree of the network

  • The longest route from A to each vertex

  • The shortest distance from A to each vertex

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.

Worked example

Passing through a vertex

  1. Find the shortest route from A to F that passes through E.
  2. 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).
  3. Total $= 9 + 7 = 16$.
Worked example

An edge closed

  1. If edge DF is closed, F can only be reached via E.
  2. Shortest A to F $= 9 + 7 = 16$.
Check yourself

How do you find the shortest route from A to F via E?

  • Use the minimum spanning tree of the network

  • Run Dijkstra's algorithm once, from F only

  • Add up the weights of all the edges at E

  • Add the shortest A–E distance and the shortest E–F distance

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