All revision notes topics

Dijkstra's algorithmEdexcel A-Level Further Maths: Revision notes

Section 1

What Dijkstra's algorithm does

Dijkstra's algorithm finds the shortest path from a start vertex to every other vertex in a weighted network, or to one chosen target. The weights must not be negative. It is used for routes such as the shortest road distance or the least travel time. Each vertex is drawn as a box (or listed in a table) with four parts:

  • the vertex name,
  • the order of labelling: 1 for the start, 2 for the next vertex given a permanent label, and so on,
  • the final label: the shortest distance from the start,
  • the working values that are tried and replaced on the way. A label is permanent once the vertex has been chosen, and it never changes.
Key termsDijkstra's algorithmworking valuepermanent label

Section 2

The steps

  1. Give the start vertex the permanent label 0 and order of labelling 1.
  2. For each vertex joined to the vertex just labelled, work out its label plus the edge length. If the vertex has no working value, or this value is smaller than its current one, record it as the new working value. Cross out a larger value.
  3. Look at all working values at vertices without a permanent label. Choose the smallest, make it permanent, and give the next order of labelling.
  4. Repeat steps 2 and 3. Stop when the target has a permanent label, or when every vertex does. The smallest working value is final because every other route to that vertex must pass through a vertex with at least as large a working value, and adding a non-negative weight cannot make it shorter.
Common mistake

Making a label permanent as soon as it is first written. Only the smallest working value among all unlabelled vertices is made permanent.

Exam tip

Keep the working values in the order they were found, and cross out replaced values neatly. Examiners award marks for them.

Section 3

Worked example

Roads: SA=4SA=4, SB=7SB=7, AB=2AB=2, AC=6AC=6, BC=3BC=3, BD=8BD=8, CD=6CD=6, CT=8CT=8, DT=4DT=4. Find the shortest route from SS to TT.

  • S=0S=0. Working values: A=4A=4, B=7B=7. Smallest is A=4A=4, permanent.
  • From AA: B=4+2=6B=4+2=6 replaces 7. C=4+6=10C=4+6=10. Smallest is B=6B=6, permanent.
  • From BB: C=6+3=9C=6+3=9 replaces 10. D=6+8=14D=6+8=14. Smallest is C=9C=9.
  • From CC: D=9+6=15D=9+6=15 (not better than 14). T=9+8=17T=9+8=17. Smallest is D=14D=14.
  • From DD: T=14+4=18T=14+4=18 (not better than 17). T=17T=17 is permanent. Order of labelling: SS, AA, BB, CC, DD, TT.

Section 4

Finding the route

The labels give the length. To find the route, work backwards from the target. At each vertex, find the neighbour whose final label plus the edge length equals the label of the vertex you are at. In the example, T=17=9+8T=17=9+8, so the route comes from CC; C=9=6+3C=9=6+3, so from BB; B=6=4+2B=6=4+2, so from AA; and A=4A=4 from SS. The route is S−A−B−C−TS-A-B-C-T with length 17. If a question gives a route from the start labelled by the algorithm, you must show the sums or the working values that justify each step.

Common mistake

Choosing the route by looking for the smallest edges. A short first edge can lead to a longer total.

Section 5

Applications and variations

  • Several targets: the final labels give the shortest distance to every labelled vertex.
  • Route through a vertex: if the route must call at XX, add the shortest distance from the start to XX and the shortest distance from XX to the target.
  • A road closed: vertices labelled before the closed edge was used keep their labels. Repeat from the point where the closed edge mattered, using the next best working values.
  • Directed networks: only follow arcs in the direction of the arrow. A vertex's shortest route may leave a long edge unused. Compare all the working values at the target before you stop.
Exam tip

For a route that must call at a vertex, find the shortest distance from the start to it and from it to the target, then add the two.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on Dijkstra's algorithm

  1. A road network joins a depot SS to a warehouse TT through four junctions AA, BB, CC and DD. The roads and their lengths in km are SA=4SA=4, SB=7SB=7, AB=2AB=2, AC=6AC=6, BC=3BC=3, BD=8BD=8, CD=6CD=6, CT=8CT=8 and DT=4DT=4. Dijkstra's algorithm is used to find the shortest route from SS to TT.
    Explain why the final label at BB is 6 and not 7.2 marks
  2. Six towns AA to FF are joined by roads with lengths in km: AB=5AB=5, AC=8AC=8, BC=2BC=2, BD=9BD=9, CD=4CD=4, CE=9CE=9, DE=3DE=3, DF=10DF=10 and EF=6EF=6. Dijkstra's algorithm is used starting from AA.
    Use the labels to find the shortest route from AA to FF and show how you worked it out.2 marks
  3. A courier network has seven vertices SS, AA, BB, CC, DD, XX and TT. The roads and their lengths in km are SA=8SA=8, SC=12SC=12, AC=3AC=3, AX=11AX=11, BC=2BC=2, BD=6BD=6, BT=10BT=10, CD=7CD=7, CT=5CT=5, DX=9DX=9 and XT=4XT=4.
    Apply Dijkstra's algorithm from SS. Show the working values at CC, and state the permanent labels of the next three vertices to be labelled after SS.3 marks
See the full worksheet

Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).