7C

Shortest paths

Finding the shortest path between two vertices of a weighted network, first by inspection and then with the labelling method (Dijkstra's algorithm).

Every time a maps app suggests a route, it is solving a shortest path problem on a network with millions of vertices. You'll do the same thing by hand on networks with up to ten. The method is the same one the apps are built on.

What "shortest" means

The length of a path in a weighted network is the sum of the weights of its edges. The shortest path between two vertices is the path with the smallest total weight. If the weights are times, it's the quickest route. If they are costs, it's the cheapest.

On a small network you can often see the answer by listing the sensible paths and adding them up. That is called finding it by inspection. It works, but it is easy to miss a route, and it gets out of hand quickly as networks grow. So we use a method that can't miss anything.

A weighted network. Weights are travel times in minutes.

The labelling method

The labelling method is also called Dijkstra's algorithm, after the Dutch computer scientist who invented it in 1956, reportedly in about twenty minutes while sitting at a café in Amsterdam. The idea is to work outwards from the start, always settling the closest vertex you haven't settled yet. Each vertex gets a label: the shortest distance to it found so far.

  1. Give the start vertex the label $0$ and make it permanent (box it or circle it).
  2. For every neighbour of the vertex you just made permanent, work out its label + the edge weight. If that beats the neighbour's current label (or it has none), write the new number as the neighbour's temporary label.
  3. Of all the temporary labels, find the smallest. Make that vertex permanent.
  4. Repeat steps 2 and 3 until the destination is permanent. Its label is the shortest distance.
  5. To find the path itself, work backwards from the destination. Each time, step to the neighbour whose permanent label plus the edge weight equals the current label.

Why does it work? When a vertex has the smallest temporary label, any other way to reach it would have to pass through a vertex that is already further away. So nothing can beat that label, and it is safe to make it permanent.

Example 1●●

Use the labelling method to find the shortest path from S to T in the network above.

S $= 0$ permanent. Temporary: A $4$, B $7$
Start at S. Its neighbours are A (0 + 4) and B (0 + 7).
A $= 4$ permanent. Temporary: B $6$, C $10$, D $15$
Smallest temporary label is A. From A: B could be 4 + 2 = 6, which beats 7. C gets 4 + 6 = 10 and D gets 4 + 11 = 15.
B $= 6$ permanent. Temporary: C $9$, D $15$, E $15$
Smallest is B. From B: C could be 6 + 3 = 9, beating 10. E gets 6 + 9 = 15.
C $= 9$ permanent. Temporary: D $13$, E $14$
Smallest is C. From C: D could be 9 + 4 = 13, beating 15. E could be 9 + 5 = 14, beating 15.
D $= 13$ permanent. Temporary: E $14$, T $16$
Smallest is D. From D: T gets 13 + 3 = 16. E via D would be 13 + 2 = 15, which doesn't beat 14.
E $= 14$, T $= 16$ permanent
E becomes permanent at 14. T via E would be 14 + 4 = 18, worse than 16. Then T is the only one left.
Shortest path S–A–B–C–D–T, length $16$ minutes
Backtrack: 16 − 3 = 13 at D, 13 − 4 = 9 at C, 9 − 3 = 6 at B, 6 − 2 = 4 at A, 4 − 4 = 0 at S.
The shortest path from S to T, total weight 16.

Making it reliable

  • Only update the neighbours of the vertex you just made permanent.
  • Never change a permanent label. Once a vertex is settled, it's settled.
  • If two temporary labels tie for smallest, pick either. You'll get the same answer.
  • In a directed network, only follow arrows forwards.
  • You can stop as soon as the destination is permanent, but finishing every vertex gives the shortest path from the start to everywhere at once.
InteractiveFind the quickest drive across Sydney
Try this

Before using any buttons, tap roads to build your own route from Penrith to the CBD. Write down your time.

Now press Show the labelling method and step through it. Which vertex becomes permanent second, and why that one?

Press New trip and try Liverpool to Ryde. Did you beat the method or match it? The times are typical, not live traffic.

Your turn

Using the labels from Example 1, find the shortest path from S to E and its length.

Show the working

E's permanent label is $14$. Backtrack: $14 - 5 = 9$ at C, then C came from B ($9 - 3 = 6$), then A ($6 - 2 = 4$), then S. Path S–A–B–C–E, length $4 + 2 + 3 + 5 = 14$ minutes.

Flashcards
Length of a path in a weighted network
The sum of the weights of its edges
Labelling method: which vertex next?
The one with the smallest temporary label
Finding the path after labelling
Work backwards from the destination: label − edge weight = previous label
Practice
  1. In the Example 1 network, what is the shortest time from S to C?

  2. In the same network, what is the shortest time from S to D?

  3. In the labelling method, which vertex is made permanent next?

  4. A vertex has temporary label $12$. Its newly permanent neighbour has label $5$ and the edge between them has weight $6$. The new temporary label is:

  5. On the Example 1 network, a student always takes the shortest edge to a new vertex: S–A–B–C–D–E–T. How long is that route?

  6. Which statement is true?

  7. If the road C–D in Example 1 were closed, by how many minutes would the shortest time from S to T increase?

  8. Exam style: A courier must go from S to T in the Example 1 network. What is the minimum travel time in minutes?

  9. Exam style: Using the labelling method, after S, A and B are permanent in Example 1, the temporary labels are C $9$, D $15$ and E $15$. Which vertex becomes permanent next, and why?

Formula sheet