7.04a - Dijkstra's algorithm
Find a shortest route without trying every possible journey. You will use temporary and permanent labels, recover a route from its distances, adapt the calculation to restrictions, and explain the algorithm's conditions and quadratic order.
Temporary and permanent labels
Can the first route you find to a destination be too long, even when every choice so far looked sensible? In the network below, the direct arc from to has weight , but travelling through and costs only . A distance must be allowed to improve before you trust it.
You already know
A network has weights on its arcs. The weight of a route is the sum of its arc weights. A directed arc can be used only in its arrow's direction; a dash in a weight table means that no direct arc exists.
Dijkstra's algorithm grows a region whose shortest distances from the start are known. A temporary label is the weight of the best route found so far; a permanent label is a shortest distance that has been settled. A label of means that no route has yet been found, not that a direct arc has infinite length.
[DIAGRAM: asset_name: Improving a route before settling it; asset_slug: h245_7_04_dijkstra_network; caption: Dijkstra's algorithm improves the temporary labels at D, E and F before making them permanent; description: Undirected network on A B C D E F with arcs AB 4 AC 2 BC 1 BD 5 CD 8 CE 10 DE 2 DF 6 EF 3. The shortest A to F route A C B D E F is highlighted. A separate inset shows the order and final distance at each vertex.]

Dijkstra's algorithm
For a finite network with non-negative arc weights:
- Give the start permanent label and order label .
- From the vertex just made permanent, with label , calculate along each permitted arc to a non-permanent neighbour. Replace its temporary label only if this improves it.
- Make the smallest temporary label permanent. Break a tie either way, unless a rule is specified.
- Repeat until the target is permanent, or all required distances are permanent. Trace back to recover the route.
In the formula booklet.
The choice in step 3 is global: compare all remaining temporary labels, rather than following the smallest arc from the last vertex. When showing labels on a diagram, put the permanence order at the upper left of the box, the permanent distance at the upper right, and the temporary labels below, as in the booklet. A table is also a clear way to preserve the updates.
Worked example 1: settling a shortest route
Question. Use Dijkstra's algorithm to find the shortest route from to in the diagram.
Plan. Start at , update neighbours, and repeatedly settle the smallest temporary distance.
The table records the best labels after updating from the vertex in the first column. A star marks a permanent label; the next vertex is chosen from the unstarred entries.
| Updated from | ||||||
|---|---|---|---|---|---|---|
For example,
Now make permanent. The permanence order is .
Why this step? A temporary is not a stopping condition: the still-unsettled vertex can improve it.
Answer. Trace the updates backwards: . The shortest route is , of weight .
Check. Its actual arcs sum to .
The second example changes just the weight of . A local change can alter several later distances, so rerun the updates rather than adjusting only the final total.
Worked example 2: one changed arc
Question. The same network now has , with every other weight unchanged. Use Dijkstra's algorithm to find the shortest route from to .
Plan. Repeat the labels, preserving the improvement through the changed arc.
Why this step? The route through is now worse for , so its existing temporary label must survive.
Answer. , of weight .
Check. . The old shortest route still has weight , so the new route is a genuine improvement.
Permanent and temporary labels do different jobs. Keep the update that produced each best label, or you may know the distance but lose the route.
Watch out
Wrong: stop when the target first receives a label. Right: stop when the target becomes permanent. In example 1, its first label is , but its shortest distance is .
The following table defines a different undirected network completely; only the listed arcs exist.
Arrows and restrictions
A shortest route is shortest among the routes the model permits, so remove a closed road or respect a one-way street before running the algorithm.
For a directed network, update only along arrows leaving the newly permanent vertex. A connection pointing into it does not let you travel backwards. An unreachable vertex keeps label ; if no finite temporary label remains, there is no permitted route to it. With equal smallest temporary labels, either can become permanent first. Equal distances may also correspond to different shortest routes, so do not claim uniqueness without checking.
Worked example 3: a directed network
Question. The only directed arcs are of weight , of weight , of weight , of weight and of weight . Find the shortest route from to using Dijkstra's algorithm.
Plan. Update only along arrows pointing away from each settled vertex.
Why this step? lets the route reach more cheaply; it does not permit a journey from to .
Answer. , of weight .
Check. ; the alternatives and cost and .
Now change only the availability of . The physical layout is unchanged, but the set of allowed journeys is smaller.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account