Dijkstra's algorithm
Dijkstra's algorithm builds shortest distances from one source in a weighted graph with positive edge weights. Add each edge weight to the permanent distance already reached, retain the smaller label and make the globally smallest temporary label permanent
- Start at the source Give the source a distance label of zero before relaxing any edges \(\displaystyle d(S)=0\)
- Use cumulative labels Add an edge weight to the distance label at the permanent vertex \(\displaystyle d(P)=4,\;w(PQ)=3\;\Rightarrow\;4+3=7\)
- Retain the smaller value Compare a proposed label with the existing temporary label \(\displaystyle \min\{9,7\}=7\)
- Choose the global minimum Make the temporary vertex with the smallest label permanent next \(\displaystyle d(B)=5<d(C)=8\;\Rightarrow\;B\text{ becomes permanent}\)
Worked example
A weighted road network has the edges and weights shown below. Apply Dijkstra's algorithm from S to find the shortest distance to R and recover a route
- \(\displaystyle d(S)=0,\quad d(P)=0+4=4,\quad d(Q)=0+10=10\)Set the source label to 0 and relax the two edges leaving S.
- \(\displaystyle 4<10\;\Rightarrow\;P\text{ is next},\quad d(T)=4+3=7\)P has the smaller temporary label, so relax the edge from P to T.
- \(\displaystyle 7<10,\quad d(Q)=\min\{10,7+2\}=9,\quad d(R)=7+5=12\)T is next, and its relaxations improve Q and label R.
- \(\displaystyle d(Q)=9<d(R)=12,\quad 9+6=15>12\;\Rightarrow\;d(R)=12\)Make Q permanent and check its edge to R; R keeps its smaller label of 12
- \(\displaystyle 0+4=4,\quad 4+3=7,\quad 7+5=12\;\Rightarrow\;S\!-!P\!-!T\!-!R,\quad 4+3+5=12\text{ km}\)Exact predecessor differences recover the route S to P to T to R, with total distance 12 km
Common mistakes
Difficulty levels
Questions about this topic
Why are Dijkstra labels cumulative?+
A label records the distance from the source to a vertex, so a new edge weight must be added to the distance already travelled
When does a temporary label become permanent?+
The vertex with the globally smallest temporary label becomes permanent because no later route can improve that label when all edge weights are positive
How is a shortest route recovered?+
Start at the destination and trace predecessors backwards. Each predecessor must have a label that plus the connecting edge weight equals the current label
Can I use a calculator?+
A calculator is allowed unless a question is marked no calculator
Which year level and chapter is this?+
Year 12, VCE Units 3 & 4, General Mathematics, chapter 13: Graphs and networks