Dijkstra's algorithm
Learn the step-by-step method behind every navigation app with Dijkstra’s algorithm in Year 12 VCE General Mathematics. When an ambulance in Bendigo or a delivery van in Geelong needs the quickest route, listing every possible path is far too slow. Dijkstra’s algorithm finds the shortest route from one place to all the others in a single organised table.
You will set out the algorithm as a table of tentative distances, finalising one vertex per row and updating its neighbours, then trace shortest routes back through the subscripts. You will run it on networks, one-way digraphs and tables of distances, and read completed tables, before moving on to flow through networks.
Theory
Dijkstra’s algorithm finds the shortest path from one vertex to every other vertex of a network. This Year 12 General Mathematics guide for students in Victoria shows how to set out the algorithm as a table of tentative distances, how to run it on directed networks and distance tables, and how to read the shortest routes back from the finished table.
Dijkstra’s algorithm finds the length of the shortest path from one starting vertex to every other vertex of a weighted graph or digraph, and the paths themselves. It works for any network whose weights are not negative, and it never needs to list every possible path.
Each vertex carries a tentative distance: the length of the best path found to it so far. A vertex not yet reached has tentative distance \(\infty\). At each step the vertex with the smallest tentative distance is finalised: no later path can beat it, because every other route would already be longer when it leaves the finalised vertices. Then the vertices next to it are updated.
The working is kept in a table. Each row starts with the vertex finalised in that row and its distance. A cell such as \(7_A\) means “tentative distance \(7\), arriving from \(A\)”. The value finalised in a row is boxed, and a column is left blank after its vertex is finalised. To find a shortest path, read the subscripts backwards from the finish.
Updating a neighbour \(v\) of the vertex \(u\) just finalised, where \(w(u, v)\) is the weight of the edge from \(u\) to \(v\):
Starting values:
Dijkstra’s algorithm with a table
- Set up. One column for each vertex other than the start. Give the start distance \(0\) and finalise it in the first row.
- Update. For each unfinalised neighbour \(v\) of the vertex just finalised, work out \(d(u) + w(u, v)\). If it is smaller than the current entry (or the entry is \(\infty\)), write the new value with subscript \(u\); otherwise copy the old entry down.
- Finalise the unfinalised vertex with the smallest entry in the row: box it and start the next row with it. (If two tie, either may go first.)
- Repeat steps 2 and 3 until every vertex is finalised.
- Read off the shortest distances (boxed values) and trace each path back through the subscripts.
In a digraph, update only along arcs that leave the finalised vertex. From a table of distances, a vertex’s neighbours are the non-empty entries in its row.
Finalise the smallest tentative distance each time, then update its neighbours.
- Finalise the start \(A\) with \(0\). Update \(B\) to \(0 + 7 = 7\), \(C\) to \(0 + 3 = 3\).
- Smallest tentative distance: \(C\) (\(3\)). Finalise \(C\). Update \(B\) to \(3 + 2 = 5\), \(D\) to \(3 + 8 = 11\), \(E\) to \(3 + 9 = 12\).
- Smallest tentative distance: \(B\) (\(5\)). Finalise \(B\). Update \(D\) to \(5 + 4 = 9\).
- Smallest tentative distance: \(D\) (\(9\)). Finalise \(D\). Update \(E\) to \(9 + 1 = 10\), \(F\) to \(9 + 6 = 15\).
- Smallest tentative distance: \(E\) (\(10\)). Finalise \(E\). No tentative distance improves.
- Smallest tentative distance: \(F\) (\(15\)). Finalise \(F\).
| Finalised | \(B\) | \(C\) | \(D\) | \(E\) | \(F\) |
|---|---|---|---|---|---|
| \(A\) (\(0\)) | \(7_{A}\) | \(3_{A}\) | \(\infty\) | \(\infty\) | \(\infty\) |
| \(C\) (\(3\)) | \(5_{C}\) | \(\boxed{3_{A}}\) | \(11_{C}\) | \(12_{C}\) | \(\infty\) |
| \(B\) (\(5\)) | \(\boxed{5_{C}}\) | \(9_{B}\) | \(12_{C}\) | \(\infty\) | |
| \(D\) (\(9\)) | \(\boxed{9_{B}}\) | \(10_{D}\) | \(15_{D}\) | ||
| \(E\) (\(10\)) | \(\boxed{10_{D}}\) | \(15_{D}\) | |||
| \(F\) (\(15\)) | \(\boxed{15_{D}}\) |
| \(F\) | \(\leftarrow\) | \(D \leftarrow B \leftarrow C \leftarrow A\) |
| \(\text{length}\) | \(=\) | \(3 + 2 + 4 + 6\) |
| \(\) | \(=\) | \(15 \text{ km}\) |
Shortest distances: \(B\) \(5\), \(C\) \(3\), \(D\) \(9\), \(E\) \(10\), \(F\) \(15\) km. The shortest path to \(F\) is \(A\)–\(C\)–\(B\)–\(D\)–\(F\) (\(15\) km). Note that the direct edge \(AB\) (\(7\)) was beaten by \(A\)–\(C\)–\(B\) (\(5\)).
Only follow arcs leaving the vertex just finalised.
| Finalised | \(P\) | \(Q\) | \(R\) | \(T\) | \(U\) |
|---|---|---|---|---|---|
| \(S\) (\(0\)) | \(6_{S}\) | \(2_{S}\) | \(\infty\) | \(\infty\) | \(\infty\) |
| \(Q\) (\(2\)) | \(5_{Q}\) | \(\boxed{2_{S}}\) | \(\infty\) | \(\infty\) | \(10_{Q}\) |
| \(P\) (\(5\)) | \(\boxed{5_{Q}}\) | \(10_{P}\) | \(\infty\) | \(10_{Q}\) | |
| \(R\) (\(10\)) | \(\boxed{10_{P}}\) | \(14_{R}\) | \(10_{Q}\) | ||
| \(U\) (\(10\)) | \(14_{R}\) | \(\boxed{10_{Q}}\) | |||
| \(T\) (\(14\)) | \(\boxed{14_{R}}\) |
| \(T\) | \(\leftarrow\) | \(R \leftarrow P \leftarrow Q \leftarrow S\) |
| \(\text{time}\) | \(=\) | \(2 + 3 + 5 + 4\) |
| \(\) | \(=\) | \(14 \text{ min}\) |
The quickest route is \(S\)–\(Q\)–\(P\)–\(R\)–\(T\), taking 14 minutes. When \(R\) was finalised, the arc \(R \to Q\) was ignored because \(Q\) was already finalised.
| Finalised | \(A\) | \(B\) | \(C\) | \(D\) | \(E\) |
|---|---|---|---|---|---|
| \(W\) (\(0\)) | \(4_{W}\) | \(7_{W}\) | \(\infty\) | \(\infty\) | \(\infty\) |
| \(A\) (\(4\)) | \(\boxed{4_{W}}\) | \(6_{A}\) | \(12_{A}\) | \(18_{A}\) | \(\infty\) |
| \(B\) (\(6\)) | \(\boxed{6_{A}}\) | \(9_{B}\) | \(18_{A}\) | \(18_{B}\) | |
| \(C\) (\(9\)) | \(\boxed{9_{B}}\) | \(14_{C}\) | \(15_{C}\) | ||
| \(D\) (\(14\)) | \(\boxed{14_{C}}\) | \(15_{C}\) | |||
| \(E\) (\(15\)) | \(\boxed{15_{C}}\) |
Boxed values are the shortest distances; follow the subscripts backwards.
| \(\text{(i) boxed in column } E\) | \(:\) | \(15_C\) |
| \(\text{(ii) } E\) | \(\leftarrow\) | \(C \leftarrow B \leftarrow A \leftarrow W\) |
| \(\text{(iii) boxed in column } D\) | \(:\) | \(14_C\) |
(i) \(15\) km. (ii) \(E\) is reached from \(C\), \(C\) from \(B\), \(B\) from \(A\) and \(A\) from \(W\), so the route is \(W\)–\(A\)–\(B\)–\(C\)–\(E\). (iii) \(C\).
| \(M\) | \(A\) | \(B\) | \(C\) | \(D\) | |
|---|---|---|---|---|---|
| \(M\) | – | 30 | 55 | – | – |
| \(A\) | 30 | – | 20 | 45 | – |
| \(B\) | 55 | 20 | – | 15 | 50 |
| \(C\) | – | 45 | 15 | – | 25 |
| \(D\) | – | – | 50 | 25 | – |
The neighbours of a vertex are the entries in its row.
| Finalised | \(A\) | \(B\) | \(C\) | \(D\) |
|---|---|---|---|---|
| \(M\) (\(0\)) | \(30_{M}\) | \(55_{M}\) | \(\infty\) | \(\infty\) |
| \(A\) (\(30\)) | \(\boxed{30_{M}}\) | \(50_{A}\) | \(75_{A}\) | \(\infty\) |
| \(B\) (\(50\)) | \(\boxed{50_{A}}\) | \(65_{B}\) | \(100_{B}\) | |
| \(C\) (\(65\)) | \(\boxed{65_{B}}\) | \(90_{C}\) | ||
| \(D\) (\(90\)) | \(\boxed{90_{C}}\) |
| \(D\) | \(\leftarrow\) | \(C \leftarrow B \leftarrow A \leftarrow M\) |
| \(\text{length}\) | \(=\) | \(30 + 20 + 15 + 25\) |
| \(\) | \(=\) | \(90 \text{ km}\) |
Shortest distances: \(A\) \(30\), \(B\) \(50\), \(C\) \(65\), \(D\) \(90\) km. The shortest route to \(D\) is \(M\)–\(A\)–\(B\)–\(C\)–\(D\). The direct road \(MB\) (\(55\)) and the road \(BD\) (\(50\)) are both beaten by longer-looking routes.
Common pitfalls
Frequently asked questions
What does Dijkstra's algorithm find?
It finds the length of the shortest path from one starting vertex to every other vertex of a weighted graph or digraph, together with the paths themselves.
How do you do Dijkstra's algorithm in a table?
Each row finalises the vertex with the smallest tentative distance. Write each tentative distance with a subscript showing the vertex it came from, box the value finalised in that row, and update its neighbours in the next row.
What does a subscript like 7 sub A mean in a Dijkstra table?
It means the best path found so far has length 7 and arrives from vertex A. Following these subscripts back from any vertex gives its shortest path.
Does Dijkstra's algorithm work for directed graphs?
Yes. When a vertex is finalised, only update the vertices that its arcs point to.
Why use Dijkstra's algorithm instead of listing paths?
Listing every path quickly becomes impossible in a large network. Dijkstra's algorithm reaches the answer in one row per vertex and gives the shortest path to every vertex at once.