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.
Section 2
The steps
- Give the start vertex the permanent label 0 and order of labelling 1.
- 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.
- Look at all working values at vertices without a permanent label. Choose the smallest, make it permanent, and give the next order of labelling.
- 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.
Making a label permanent as soon as it is first written. Only the smallest working value among all unlabelled vertices is made permanent.
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: , , , , , , , , . Find the shortest route from to .
- . Working values: , . Smallest is , permanent.
- From : replaces 7. . Smallest is , permanent.
- From : replaces 10. . Smallest is .
- From : (not better than 14). . Smallest is .
- From : (not better than 17). is permanent. Order of labelling: , , , , , .
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, , so the route comes from ; , so from ; , so from ; and from . The route is 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.
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 , add the shortest distance from the start to and the shortest distance from 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.
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
- A road network joins a depot to a warehouse through four junctions , , and . The roads and their lengths in km are , , , , , , , and . Dijkstra's algorithm is used to find the shortest route from to .Explain why the final label at is 6 and not 7.2 marks
- Six towns to are joined by roads with lengths in km: , , , , , , , and . Dijkstra's algorithm is used starting from .Use the labels to find the shortest route from to and show how you worked it out.2 marks
- A courier network has seven vertices , , , , , and . The roads and their lengths in km are , , , , , , , , , and .Apply Dijkstra's algorithm from . Show the working values at , and state the permanent labels of the next three vertices to be labelled after .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).