D1 2.2 - Dijkstra's algorithm

D1 2.2 - Dijkstra's algorithm

Find a shortest route through a weighted network by keeping tentative distances and fixing them in a safe order. You will show Dijkstra's working values, recover the route from the final labels, and interpret changes when a road closes.

Temporary and permanent labels

Can the shortest route begin with an edge that is not the shortest-looking line on the page? The numbers, rather than the drawn lengths, decide. Even the smallest edge from your current position need not lead towards the best overall route.

Dijkstra's algorithm works here on a connected, undirected network with nonnegative edge weights. A label is a distance from the chosen start, not the length of the most recent edge.

Temporary and permanent labels

A temporary label, also called a working value, is the shortest distance to that vertex found so far. It may decrease. A permanent label, or final value, is the established shortest distance from the start. Record its order of labelling separately: order 33 does not mean distance 33.

Set the start's permanent distance to 00, with order 11. Initially other distances are unknown, represented by ∞\infty. From the newly permanent vertex UU, calculate

candidate at V=final distance at U+weight of UV.\text{candidate at }V=\text{final distance at }U+\text{weight of }UV.

Update an unlabelled neighbour only when this improves its temporary value. Then make the smallest temporary value anywhere in the network permanent. If smallest values tie, either tied vertex may be selected next; use distinct order numbers.

[DIAGRAM: asset_name: A shortest-path network; asset_slug: dijkstra-network; caption: A working value measures the whole route from A, not just the last edge.; description: Undirected edges AB 7 AC 3 BC 2 BD 4 CD 8 CE 5 DE 1 DF 6 EF 4 BF 15 on vertices A B C D E F. A separate key shows vertex, order, final value and ordered working values.]
A working value measures the whole route from A, not just the last edge.

Worked example 1: the first update

On this network, start at AA.

Plan. Fix AA at zero and update its two neighbours.

The candidates are 0+7=70+7=7 at BB and 0+3=30+3=3 at CC. All other vertices remain unknown.

Answer. Temporary values B:7,C:3B:7,C:3; select CC next, with order 22 and final value 33.

Check. These are the two one-edge routes from AA, and 3<73<7.

Change only the vertex being processed, from AA to the newly fixed CC. This reveals an improvement to an earlier estimate.

Worked example 2: revising a working value

Process CC, whose final distance is 33.

Plan. Add each edge from CC to 33, ignoring already permanent AA.

B:3+2=5<7,D:3+8=11,E:3+5=8.B:3+2=5<7,\qquad D:3+8=11,\qquad E:3+5=8.

Keep BB's working values in the order 7,57,5; do not erase the earlier 77. Now compare all temporary values 5,11,85,11,8.

Answer. Select BB next, with order 33, final value 55.

Check. The route A,C,BA,C,B has length 3+2=53+2=5, less than direct AB=7AB=7.

Why may the smallest temporary label be fixed? Any route through an unfixed vertex must first reach an unfixed vertex at least this far away. Adding nonnegative edge lengths cannot produce a smaller arrival at the chosen vertex. Negative weights would break that argument.

Watch out

Choose the smallest total temporary distance, not the smallest outgoing edge. Keep improving working values in their actual order; a correct final value alone does not show the algorithm.

Apply one update before choosing the next vertex.

Finding a shortest distance

Continue the updates until the destination is permanent; a temporary destination label can still improve.

Use a table to make the trace readable. A dash below means no new improving value, rather than a zero distance. When a candidate is larger than or equal to a current working value, retain that current value; it need not be entered as a new working value.

Worked example 3: a complete trace

Find the shortest distance from AA to FF in the pictured network, whose edges are AB=7,AC=3,BC=2,BD=4,CD=8,CE=5,DE=1,DF=6,EF=4,BF=15AB=7,AC=3,BC=2,BD=4,CD=8,CE=5,DE=1,DF=6,EF=4,BF=15.

Plan. Process each newly permanent vertex and record every improvement before selecting the next minimum.

OrderVertex fixedFinal distanceNew improving working values
1AA00B:7;C:3B:7; C:3
2CC33B:5;D:11;E:8B:5; D:11; E:8
3BB55D:9;F:20D:9; F:20
4EE88F:12F:12
5DD99None: 9+6=15>129+6=15>12 at FF
6FF1212Stop

At order 44, the candidate at DD is 8+1=98+1=9, equal to its current 99, so that label stays unchanged.

Answer. The shortest distance is 1212. Ordered working values are B:7,5;C:3;D:11,9;E:8;F:20,12B:7,5; C:3; D:11,9; E:8; F:20,12.

Check. The final distances A:0,B:5,C:3,D:9,E:8,F:12A:0,B:5,C:3,D:9,E:8,F:12 satisfy d(V)≤d(U)+UVd(V)\le d(U)+UV in both directions on every edge. For example d(F)=12≤9+6d(F)=12\le9+6 and d(D)=9≤8+1d(D)=9\le8+1. A route A,C,E,FA,C,E,F actually attains 1212, confirming the distance.

Change just CECE from 55 to 99. This delays EE, so both the order and FF's working values change.

Worked example 4: a revised edge

Repeat Dijkstra's algorithm with CE=9CE=9, all other edges unchanged.

Plan. Recalculate the trace; do not keep the old permanent order automatically.

OrderVertex fixedFinal distanceNew improving working values
1AA00B:7;C:3B:7; C:3
2CC33B:5;D:11;E:12B:5; D:11; E:12
3BB55D:9;F:20D:9; F:20
4DD99E:10;F:15E:10; F:15
5EE1010F:14F:14
6FF1414Stop

Answer. The shortest distance is 1414, with FF's working values 20,15,1420,15,14.

Check. The route A,C,B,D,E,FA,C,B,D,E,F has length 3+2+4+1+4=143+2+4+1+4=14. All revised final labels satisfy the edge inequalities above, so no route can beat them.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account