All revision notes topics

Dijkstra's algorithmEdexcel International A Level Maths: Revision notes

Section 1

The shortest path problem

In a network each arc has a weight, such as a distance, time or cost. The shortest path problem asks for the path between two vertices with the least total weight. Trying every path becomes impractical as the network grows, so we use Dijkstra's algorithm. Dijkstra's algorithm works from a start vertex and finds the shortest distance to every vertex, labelling them in order of increasing distance. It can be stopped as soon as the target vertex is permanently labelled. It works for any network with positive weights.

Key termsnetworkweightshortest pathDijkstra's algorithm
Exam tip

The algorithm finds the shortest distance to every vertex, so you never need to guess which route to try.

Section 2

The algorithm step by step

Give each vertex a box with three parts: the order of labelling, the final value and the working values.

  1. Give the start vertex the order of labelling 1 and the final value 00.
  2. For each vertex joined to the vertex just labelled, work out (final value ++ arc weight). Write this as a working value if it is smaller than any working value already there.
  3. Choose the vertex with the smallest working value that is not yet permanently labelled. Make that value its final value and give it the next order number.
  4. Repeat steps 2 and 3 until the target vertex is labelled. Working values that are replaced by smaller ones are crossed out but kept, so the working can be checked.
Key termsorder of labellingfinal valueworking valuepermanent label
Common mistake

Adding the arc weight to the working value of the vertex instead of its final value. Always add to the final value.

Section 3

Worked example

Arcs SA=4SA=4, SB=2SB=2, AB=1AB=1, AT=6AT=6, BT=8BT=8. Start at SS with final value 00.

  • From SS: working values A=4A=4, B=2B=2. The smallest is BB, so B=2B=2 is permanent (order 2).
  • From BB: A=2+1=3A=2+1=3, replacing 44; T=2+8=10T=2+8=10. The smallest is AA, so A=3A=3 is permanent (order 3).
  • From AA: T=3+6=9T=3+6=9, replacing 1010. T=9T=9 is permanent (order 4). The shortest distance from SS to TT is 99.
Exam tip

A working value that is replaced by a smaller one is where the shortest route changes.

Section 4

Finding the route

The final values give the length, but you must also give the route. Work backwards from the target. At each vertex, look at its neighbours and choose the one for which final value of vertex−arc weight=final value of neighbour.\text{final value of vertex}-\text{arc weight}=\text{final value of neighbour}. In the worked example, from T=9T=9: 9−6=3=A9-6=3=A, so go to AA. From A=3A=3: 3−1=2=B3-1=2=B, so go to BB. From B=2B=2: 2−2=0=S2-2=0=S. The route is SS-BB-AA-TT. The route is written from the start to the target, with the total length. Using the arc SASA (44) directly would give A=4A=4, which is longer than 33 via BB.

Key termsrouteback-tracking
Common mistake

Choosing the route from the labelling order. The order of labelling does not give the route; back-track using the final values.

Section 5

Updating and checking

If a road is closed, remove that arc and repeat the algorithm. Any vertex whose shortest route did not use that arc keeps its final value. If a new arc of weight xx joins vertices XX and YY, it improves the route to TT only when d(S,X)+x+d(Y,T)<current shortest length,d(S,X)+x+d(Y,T)<\text{current shortest length}, where dd is the shortest distance, found from the algorithm. This gives an inequality for xx. Checks: final values must increase in order of labelling, and the final value at each vertex must be no more than any route you can find by inspection.

Exam tip

Use the final values already found: they give d(S,X)d(S,X) for every XX, so a new arc needs no new working.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on Dijkstra's algorithm

  1. A network has five vertices SS, AA, BB, CC and TT. The arcs and their lengths are SA=9SA=9, SB=5SB=5, AB=3AB=3, AC=10AC=10, BC=2BC=2, BT=9BT=9 and CT=6CT=6. Dijkstra's algorithm is applied starting at SS.
    Find the shortest route from SS to AA and its length. Explain why the arc SASA is not used.2 marks
  2. A network has six vertices AA to FF. The arcs and their lengths are AB=2AB=2, AC=8AC=8, AD=9AD=9, BC=13BC=13, BE=5BE=5, CD=13CD=13, CE=14CE=14, DF=6DF=6 and EF=4EF=4. Dijkstra's algorithm is applied starting at AA.
    Find the shortest route from AA to FF and state its length.2 marks
  3. A courier company has six depots AA to FF. The direct roads between depots and their lengths in km are AB=4AB=4, AC=3AC=3, AD=11AD=11, BC=10BC=10, BE=5BE=5, CD=13CD=13, CE=7CE=7, DF=13DF=13 and EF=12EF=12. A courier travels from AA to FF.
    Use Dijkstra's algorithm to find the shortest route from AA to FF. State the order in which the vertices are permanently labelled, with their final labels, and the length of the route.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).