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.
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.
- Give the start vertex the order of labelling 1 and the final value .
- 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.
- 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.
- 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.
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 , , , , . Start at with final value .
- From : working values , . The smallest is , so is permanent (order 2).
- From : , replacing ; . The smallest is , so is permanent (order 3).
- From : , replacing . is permanent (order 4). The shortest distance from to is .
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 In the worked example, from : , so go to . From : , so go to . From : . The route is ---. The route is written from the start to the target, with the total length. Using the arc () directly would give , which is longer than via .
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 joins vertices and , it improves the route to only when where is the shortest distance, found from the algorithm. This gives an inequality for . 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.
Use the final values already found: they give for every , 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
- A network has five vertices , , , and . The arcs and their lengths are , , , , , and . Dijkstra's algorithm is applied starting at .Find the shortest route from to and its length. Explain why the arc is not used.2 marks
- A network has six vertices to . The arcs and their lengths are , , , , , , , and . Dijkstra's algorithm is applied starting at .Find the shortest route from to and state its length.2 marks
- A courier company has six depots to . The direct roads between depots and their lengths in km are , , , , , , , and . A courier travels from to .Use Dijkstra's algorithm to find the shortest route from to . State the order in which the vertices are permanently labelled, with their final labels, and the length of the route.3 marks
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).