Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 Maths - General (Unit 3 & Unit 4) Trees, minimum connectors and shortest paths

Dijkstra's algorithm

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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.

Key idea. Finalise the smallest tentative distance, then update its unfinalised neighbours: new value \(=\) the smaller of the current value and \(d(u) + w(u, v)\). Repeat until every vertex is finalised.
Shortest paths from ANetwork: AB 7, AC 3, BC 2, BD 4, CE 9, CD 8, DE 1, DF 6, EF 7 (km). The red edges form the shortest paths from A to every other vertex. A B C D E F 7 3 2 4 9 8 1 6 7
Dijkstra’s algorithm from \(A\) gives the red edges: every shortest path from \(A\) runs along them. For example, \(A\)–\(C\)–\(B\)–\(D\)–\(F\) has length \(3 + 2 + 4 + 6 = 15\).
Dijkstra's algorithm part-waySame network. A and C are finalised (green). B, D and E have tentative distances 5, 11 and 12 (amber). F has not been reached. A B C D E F 7 3 2 4 9 8 1 6 7
Part-way through: \(A\) (\(0\)) and \(C\) (\(3\)) are finalised (green). Tentative distances: \(B\) \(5\), \(D\) \(11\), \(E\) \(12\) (amber). \(B\) has the smallest, so it is finalised next.

Updating a neighbour \(v\) of the vertex \(u\) just finalised, where \(w(u, v)\) is the weight of the edge from \(u\) to \(v\):

\[\text{new tentative distance of } v = \text{smaller of } \big(\text{current value},\ d(u) + w(u, v)\big)\]
d(v)=min(d(v),d(u)+w)

Starting values:

\[d(\text{start}) = 0, \qquad d(\text{every other vertex}) = \infty\]
d(s)=0
Reading the table. The boxed value in a column is that vertex’s shortest distance. Its subscript is the vertex before it on the shortest path; keep following subscripts back to the start.

Dijkstra’s algorithm with a table

  1. Set up. One column for each vertex other than the start. Give the start distance \(0\) and finalise it in the first row.
  2. 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.
  3. 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.)
  4. Repeat steps 2 and 3 until every vertex is finalised.
  5. 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.

Example 1 — Dijkstra’s algorithm with a table
Use Dijkstra’s algorithm to find the shortest distance from \(A\) to every other vertex of the network shown (km). Write down the shortest path from \(A\) to \(F\).
Solution

Finalise the smallest tentative distance each time, then update its neighbours.

Example 1 networkAB 7, AC 3, BC 2, BD 4, CE 9, CD 8, DE 1, DF 6, EF 7 (km). A B C D E F 7 3 2 4 9 8 1 6 7
  1. Finalise the start \(A\) with \(0\). Update \(B\) to \(0 + 7 = 7\), \(C\) to \(0 + 3 = 3\).
  2. Smallest tentative distance: \(C\) (\(3\)). Finalise \(C\). Update \(B\) to \(3 + 2 = 5\), \(D\) to \(3 + 8 = 11\), \(E\) to \(3 + 9 = 12\).
  3. Smallest tentative distance: \(B\) (\(5\)). Finalise \(B\). Update \(D\) to \(5 + 4 = 9\).
  4. Smallest tentative distance: \(D\) (\(9\)). Finalise \(D\). Update \(E\) to \(9 + 1 = 10\), \(F\) to \(9 + 6 = 15\).
  5. Smallest tentative distance: \(E\) (\(10\)). Finalise \(E\). No tentative distance improves.
  6. 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}\)
15km

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\)).

Example 2 — Dijkstra’s algorithm in a digraph
The digraph shows one-way laneways in Melbourne’s CBD with walking times in minutes. Use Dijkstra’s algorithm to find the quickest time from \(S\) to \(T\), and the route.
Solution

Only follow arcs leaving the vertex just finalised.

One-way lanesArcs S to P 6, S to Q 2, Q to P 3, P to R 5, Q to U 8, U to R 2, R to T 4, U to T 9, R to Q 7 (minutes). S P Q R U T 6 2 3 5 8 2 4 9 7
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}\)
14min

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.

Example 3 — Reading a completed table
Dijkstra’s algorithm was used to find the shortest distances (km) from \(W\) in a network of bike paths in Ballarat. The completed table is shown. (i) What is the shortest distance from \(W\) to \(E\)? (ii) Write down the shortest route from \(W\) to \(E\). (iii) Which vertex comes just before \(D\) on its shortest route?
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}}\)
Solution

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\)
15km

(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\).

Example 4 — From a table of distances
The table gives the lengths (km) of roads between five places in Gippsland. A dash means no direct road. Use Dijkstra’s algorithm to find the shortest distance from \(M\) to each other place, and the shortest route from \(M\) to \(D\).
\(M\)\(A\)\(B\)\(C\)\(D\)
\(M\)–3055––
\(A\)30–2045–
\(B\)5520–1550
\(C\)–4515–25
\(D\)––5025–
Solution

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}\)
90km

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

Finalise the smallest tentative distance in the whole row, not the smallest edge from the vertex just finalised. A vertex updated several steps earlier may be the smallest.
Only replace a value if the new one is smaller. Watch out for overwriting a tentative distance with a larger one just because a new path has been found.
Read the route backwards. The subscript is the vertex just before, so start at the finish and work back to the start, then write the route forwards.

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.