Discrete MathematicsTrees5 of 5
Trees · 5

Shortest Paths: Dijkstra's Algorithm

We want to find the cheapest route between two cities on a map of weighted roads without getting trapped by a cheap first step or checking every possible path from start to finish.

1

Why the Smallest Offer Is Safe

Before we can trace a route across a whole map, we need a way to know when the shortest path to a nearby city is guaranteed to be final, even while the rest of the map is still unexplored.

Why the Smallest Offer Is Safe
AA
22
55
BB
CC
D\htmlClass{lc-mute}{D}
E\htmlClass{lc-mute}{E}
F\htmlClass{lc-mute}{F}

A short road between BB and CC in the unexplored part of the map can make the detour A→B→CA \to B \to C cheaper than CC's direct road of 5\offer{5}, but no non-negative road out there can ever make the detour A→C→BA \to C \to B beat BB's direct road of 2\locked{2}:

Shortening the road in the fog can give C a faster detour through B, while B's direct road is already too short for any detour through C to beat.
Detour to C via B:2+w(beats 5 whenever w<3)Detour to B via C:5+w≥5>2(never beats 2 when w≥0)\displaystyle \begin{aligned} &\text{Detour to } C\text{ via }B: \\ &\qquad \locked{2} + w \quad (\text{beats } \offer{5} \text{ whenever } w < 3) \\[6pt] &\text{Detour to } B\text{ via }C: \\ &\qquad \offer{5} + w \ge \offer{5} > \locked{2} \\ &\qquad (\text{never beats } \locked{2} \text{ when } \given{w \ge 0}) \end{aligned}
2

Finding the Shortest Path Step by Step

Now let's use that rule to find the shortest path from AA to FF across the full six-city map.

AA
BB
CC
DD
EE
FF
22
55
22
66
55
55
44
22
Finding the Shortest Path Step by Step
AA
BB
CC
DD
EE
FF
22
55
22
66
55
55
44
22
Text solution
Start at AA and check the roads to its neighborsLock in AA at 0\locked{0}, write the direct road costs on BB and CC, and mark the unreached cities as ∞\infty:
A: circled 0B: 2 (via A)C: 5 (via A)D,E,F: ∞\displaystyle \begin{aligned} A:&\ \text{circled } \locked{0} \\ B:&\ \offer{2}\text{ (via }A\text{)} \\ C:&\ \offer{5}\text{ (via }A\text{)} \\ D, E, F:&\ \infty \end{aligned}
Circle BB at 2\locked{2} and update its neighborsSince 2\offer{2} is the smallest uncircled offer on the board, lock in BB and add BB's running total of 2\locked{2} to the roads leaving it:
B→C: 2+2=4<5  ⟹  5 (via A)  →  4 (via B)⏟relaxationB→D: 2+6=8  ⟹  D: 8 (via B)\displaystyle \begin{aligned} &B \to C:\ \locked{2} + 2 = \offer{4} < \offer{5} \\ &\quad \implies \underbrace{\struck{\xcancel{5\text{ (via }A\text{)}}} \;\to\; \offer{4}\text{ (via }B\text{)}}_{\text{relaxation}} \\[4pt] &B \to D:\ \locked{2} + 6 = \offer{8} \\ &\quad \implies D:\ \offer{8}\text{ (via }B\text{)} \end{aligned}
Circle CC at 4\locked{4} and update its neighborsBetween CC (4\offer{4}) and DD (8\offer{8}), lock in CC at 4\locked{4} and check the roads from CC to uncircled cities:
C→D: 4+5=9>8(keep 8 via B)C→E: 4+5=9  ⟹  E: 9 (via C)\displaystyle \begin{aligned} &C \to D:\ \locked{4} + 5 = \struck{9} > \offer{8} \\ &\quad \text{(keep } \offer{8}\text{ via }B\text{)} \\[4pt] &C \to E:\ \locked{4} + 5 = \offer{9} \\ &\quad \implies E:\ \offer{9}\text{ (via }C\text{)} \end{aligned}
Circle DD at 8\locked{8} and check its road to FFBetween DD (8\offer{8}) and EE (9\offer{9}), lock in DD at 8\locked{8} and record its offer to FF:
D→F: 8+4=12  ⟹  F: 12 (via D)\displaystyle \begin{aligned} &D \to F:\ \locked{8} + 4 = \offer{12} \\ &\quad \implies F:\ \offer{12}\text{ (via }D\text{)} \end{aligned}
Circle EE at 9\locked{9} and update FFBecause EE's 9\offer{9} is smaller than FF's 12\offer{12}, lock in EE next and check its road to FF before circling FF:
E→F: 9+2=11<12  ⟹  12 (via D)  →  11 (via E)\displaystyle \begin{aligned} &E \to F:\ \locked{9} + 2 = \offer{11} < \offer{12} \\ &\quad \implies \struck{\xcancel{12\text{ (via }D\text{)}}} \;\to\; \offer{11}\text{ (via }E\text{)} \end{aligned}
Read the "via" notes backward from FFCircle FF at 11\locked{11} and follow each city's note back to AA:
F←via EE←via CC←via BB←via AA\displaystyle \begin{aligned} F &\trace{\xleftarrow{\text{via }E}} E \trace{\xleftarrow{\text{via }C}} C \\ &\trace{\xleftarrow{\text{via }B}} B \trace{\xleftarrow{\text{via }A}} A \end{aligned}

So the shortest path from AA to FF is A→B→C→E→F\locked{A \to B \to C \to E \to F} with a total cost of 11\locked{11}, and the circled numbers on the board give the shortest distance from AA to every other city at the same time.

Halfway through the search, every route from AA to DD must take a first step across the dotted border around {A,B,C}\locked{\{A, B, C\}}, and crossing the border at EE already costs 9\struck{9} before adding any roads outside:

Every route from A to D has to cross the border around the circled cities, and any crossing other than B to D already costs at least 9 just to get across.
3

Ties and Negative Roads

Two questions come up when we pick the smallest offer: what happens if two uncircled cities are tied for the smallest number, and why does the whole guarantee break if a road has a negative length?

Ties and Negative Roads
AA
0\locked{0}
88
88
DD
EE
8 (via A)\offer{8}\ \text{(via }A\text{)}
8 (via A)\offer{8}\ \text{(via }A\text{)}

With a negative road of −4\struck{-4} from CC to BB, the detour through CC gets smaller as it goes, beating BB's direct road of 2\locked{2} after BB has already been locked in:

A→C→B:5+(−4)\displaystyle A \to C \to B:\quad \offer{5} + (\struck{-4}) =1<2\displaystyle = \struck{1} < \locked{2}

A road of length 00 causes no trouble, because adding 00 never reduces a path's running total.

4

Counting the Steps: O(n2)O(n^2)

To see how fast Dijkstra's algorithm runs on a large map, we count the total number of comparisons and neighbor updates across a graph with nn cities.

Counting the Steps: O(n²)
AA
BB
CC
DD
EE
FF
22
55
22
66
55
55
44
22
0\locked{0}
2\locked{2}
4\locked{4}
8\locked{8}
9\locked{9}
11\locked{11}

Across all nn rounds, we add up the comparisons needed to pick the smallest uncircled note in each round and the road checks needed to update uncircled neighbors:

Find smallest note across n rounds:n+(n−1)+⋯+1=n(n+1)2≈12n2Check roads to uncircled neighbors:at most n(n−1)2≈12n2\displaystyle \begin{aligned} &\text{Find smallest note across } n \text{ rounds:} \\ &\qquad n + (n - 1) + \dots + 1 \\ &\qquad\quad = \locked{\frac{n(n + 1)}{2}} \approx \frac{1}{2}n^2 \\[6pt] &\text{Check roads to uncircled neighbors:} \\ &\qquad \text{at most } \locked{\frac{n(n - 1)}{2}} \approx \frac{1}{2}n^2 \end{aligned}

Putting the two jobs together gives O(n2)\locked{O(n^2)} total steps.

Key concepts

To find the shortest paths from a start city AA on a map where every road length is 00 or positive (w≥0\given{w \ge 0}), circle AA at 0\locked{0} and repeat one loop: check the roads from the city uu you just circled, update (relax) any uncircled neighbor vv where total(u)+road(u,v)\locked{\text{total}(u)} + \text{road}(u, v) beats vv's current note (as 2+2=4\locked{2} + 2 = \offer{4} beat 5\struck{5} on CC), and then lock in whichever uncircled city has the smallest offer on the entire board. If two cities tie for the smallest offer, circle either one first. Do not stop the moment the destination gets an offer (such as FF getting 12\offer{12} via DD while EE sits uncircled at 9\offer{9}); stop only when the destination itself is circled, then read the "via" notes backward from the destination to AA to find the route. Across nn cities, scanning the notes takes n(n+1)2\locked{\frac{n(n+1)}{2}} comparisons and updating neighbors checks at most n(n−1)2\locked{\frac{n(n-1)}{2}} roads, for O(n2)\locked{O(n^2)} steps in total. Do not use Dijkstra's algorithm if any road has a negative weight (w<0\struck{w < 0}), because a negative road can make a more expensive path drop back below a distance you already locked in (like 5+(−4)=1\offer{5} + (\struck{-4}) = \struck{1} beating 2\locked{2}).

5

Practice

1

Suppose you are running Dijkstra's algorithm starting at city AA. Only AA (at 0\locked{0}) is circled so far, and the notes on the other cities are B:3 (via A)B: \offer{3}\text{ (via }A\text{)}, C:7 (via A)C: \offer{7}\text{ (via }A\text{)}, D:9 (via A)D: \offer{9}\text{ (via }A\text{)}, and E:∞E: \infty. The roads leaving BB to uncircled cities are B–CB\text{–}C of length 33, B–DB\text{–}D of length 77, and B–EB\text{–}E of length 55. Which city do you circle next, and what are the notes on CC, DD, and EE after you check the roads from that city and update its neighbors?

Pick the smallest uncircled offer on the board, then add that city's running total to each road leaving it before comparing against the neighbor's current note.

The uncircled offers are 3\offer{3}, 7\offer{7}, 9\offer{9}, and ∞\infty, so circle BB at 3 (via A)\locked{3}\text{ (via }A\text{)}.
From BB, add BB's running total of 3\locked{3} to each road leaving it (be careful not to write the bare road lengths 33, 77, and 55 without adding BB's 3\locked{3} first):
B→C:3+3=6<7  ⟹  C: 6 (via B)B→D:3+7=10>9  ⟹  D keeps 9 (via A)B→E:3+5=8<∞  ⟹  E: 8 (via B)\displaystyle \begin{aligned} &B \to C: \quad \locked{3} + 3 = \offer{6} < \offer{7} \\ &\quad \implies C:\ \offer{6}\text{ (via }B\text{)} \\[4pt] &B \to D: \quad \locked{3} + 7 = \struck{10} > \offer{9} \\ &\quad \implies D\text{ keeps } \offer{9}\text{ (via }A\text{)} \\[4pt] &B \to E: \quad \locked{3} + 5 = \offer{8} < \infty \\ &\quad \implies E:\ \offer{8}\text{ (via }B\text{)} \end{aligned}
(Watch out on DD: 10\struck{10} does not beat 9\offer{9}, so do not overwrite DD's existing note.)
Circle BB at 3 (via A)\locked{3}\text{ (via }A\text{)}; the updated notes are C:6 (via B)C: \offer{6}\text{ (via }B\text{)}, D:9 (via A)D: \offer{9}\text{ (via }A\text{)}, and E:8 (via B)E: \offer{8}\text{ (via }B\text{)}.
2

Run Dijkstra's algorithm to find the shortest path from AA to FF on the 6-city map with roads A–B=3A\text{–}B = 3, A–C=4A\text{–}C = 4, B–C=2B\text{–}C = 2, B–D=3B\text{–}D = 3, B–E=6B\text{–}E = 6, C–E=2C\text{–}E = 2, D–F=5D\text{–}F = 5, and E–F=1E\text{–}F = 1. Give the final circled distance and "via" note for every city, and read the "via" notes backward to find the shortest route from AA to FF.

Keep going until FF itself has the smallest uncircled offer, even after DD puts a first offer on FF.

Circle AA at 0\locked{0}. Checking the roads from AA gives B:3 (via A)B: \offer{3}\text{ (via }A\text{)} and C:4 (via A)C: \offer{4}\text{ (via }A\text{)}.
Circle BB at 3 (via A)\locked{3}\text{ (via }A\text{)}. Checking the roads from BB gives B→C:3+2=5>4B \to C: \locked{3} + 2 = \struck{5} > \offer{4} (so CC keeps 4 via A\offer{4}\text{ via }A), D:3+3=6 (via B)D: \locked{3} + 3 = \offer{6}\text{ (via }B\text{)}, and E:3+6=9 (via B)E: \locked{3} + 6 = \offer{9}\text{ (via }B\text{)}.
Circle CC at 4 (via A)\locked{4}\text{ (via }A\text{)}. Checking the road from CC gives C→E:4+2=6<9C \to E: \locked{4} + 2 = \offer{6} < \offer{9}, updating EE to 6 (via C)\offer{6}\text{ (via }C\text{)}.
Now D:6 (via B)D: \offer{6}\text{ (via }B\text{)} and E:6 (via C)E: \offer{6}\text{ (via }C\text{)} tie for the smallest offer and can be circled in either order. Circling DD at 6 (via B)\locked{6}\text{ (via }B\text{)} gives F:6+5=11 (via D)F: \locked{6} + 5 = \offer{11}\text{ (via }D\text{)}. (Do not stop here and answer 11\offer{11}: EE is still uncircled at 6<11\offer{6} < \offer{11}.)
Circle EE at 6 (via C)\locked{6}\text{ (via }C\text{)}. Checking the road from EE gives E→F:6+1=7<11E \to F: \locked{6} + 1 = \offer{7} < \offer{11}, updating FF to 7 (via E)\offer{7}\text{ (via }E\text{)}.
Circle FF at 7 (via E)\locked{7}\text{ (via }E\text{)}. Final circled notes: A:0A: \locked{0}, B:3 (via A)B: \locked{3}\text{ (via }A\text{)}, C:4 (via A)C: \locked{4}\text{ (via }A\text{)}, D:6 (via B)D: \locked{6}\text{ (via }B\text{)}, E:6 (via C)E: \locked{6}\text{ (via }C\text{)}, F:7 (via E)F: \locked{7}\text{ (via }E\text{)}. Reading the "via" notes backward gives F←E←C←AF \trace{\leftarrow} E \trace{\leftarrow} C \trace{\leftarrow} A, so the shortest route is A→C→E→F\locked{A \to C \to E \to F} with total cost 7\locked{7}.
3

Halfway through running Dijkstra's algorithm from AA to FF, the circled cities inside the dotted border are AA (0\locked{0}), BB (2\locked{2}), and CC (5\locked{5}), and all roads leaving them have been checked. Outside the border, the uncircled cities have notes D:7 (via B)D: \offer{7}\text{ (via }B\text{)}, E:8 (via C)E: \offer{8}\text{ (via }C\text{)}, and F:10 (via C)F: \offer{10}\text{ (via }C\text{)}. A friend sees the note 10 (via C)\offer{10}\text{ (via }C\text{)} on FF and stops, claiming the shortest path from AA to FF is 1010. Explain using the dotted border why DD's 7\offer{7} is guaranteed to be final right now while FF's 10\offer{10} is not, and find the smallest possible true shortest distance from AA to FF if every road outside the border has length at least 1\given{1}.

Compare the cost of stepping across the dotted border at DD, EE, or FF, and see where a route to FF could cross the border for less than 10\offer{10}.

Every route from AA to an uncircled city must cross the dotted border at DD (for 7\offer{7}), EE (for 8\offer{8}), or FF (for 10\offer{10}). Because 7\offer{7} is the smallest crossing cost, any route that crosses at EE or FF has already spent at least 8\offer{8} at the border and cannot beat 7\offer{7} on DD. However, a route that crosses the border at DD for 7\offer{7} or at EE for 8\offer{8} can still reach FF through the uncircled cities for less than 10\offer{10}. If every road outside the border has length at least 1\given{1}, the cheapest possible path crosses at DD (not EE, which would give 8+1=9\offer{8} + \given{1} = \struck{9}) and takes a single road of length 1\given{1} from DD to FF, giving a shortest possible distance of 7+1=8\locked{7} + \given{1} = \locked{8}.
4

On a 4-city map of one-way roads, AA has roads to BB (length 44) and CC (length 77), BB has a road to DD (length 33), and CC has a road to BB of length w\given{w}. Does Dijkstra's algorithm starting at AA still find the true shortest paths if w=0\given{w = 0}? For which values of w\given{w} does it lock in the wrong shortest distance to BB and DD?

Write the total cost of the path A→C→BA \to C \to B in terms of w\given{w} and compare it against the distance 4\locked{4} that Dijkstra's algorithm locks in for BB.

Starting at AA, Dijkstra's algorithm circles BB at 4\locked{4} and updates DD to 4+3=7\locked{4} + 3 = \offer{7} before checking the road from CC (at 7\offer{7}).
If w=0\given{w = 0}, the detour A→C→BA \to C \to B costs 7+0=7\offer{7} + \given{0} = \struck{7}, which does not beat 4\locked{4}, so Dijkstra's algorithm still finds the true shortest paths.
In general, the path A→C→BA \to C \to B costs 7+w\offer{7} + \given{w} and A→C→B→DA \to C \to B \to D costs 10+w10 + \given{w}. (Be careful not to say the algorithm fails for every w<0\given{w} < 0: when −3≤w<0-3 \le \given{w} < 0, 7+w≥4\offer{7} + \given{w} \ge \locked{4}, so BB's locked-in 4\locked{4} is still the true shortest distance.) The detour beats BB's 4\locked{4} and DD's 7\locked{7} whenever 7+w<4\offer{7} + \given{w} < \locked{4}, which is for w<−3\struck{w < -3}.
5

On a map of n=20n = 20 cities with a direct road between every pair of cities, how many total notes are checked across all 2020 rounds to pick the smallest uncircled offer (20+19+⋯+120 + 19 + \dots + 1), and how many roads between pairs of cities are checked during neighbor updates? If the map grows 100100-fold to 2,0002{,}000 cities, by roughly what factor does the total number of steps grow?

Use n(n+1)2\locked{\frac{n(n+1)}{2}} for the note comparisons and n(n−1)2\locked{\frac{n(n-1)}{2}} for the roads between pairs of cities, then apply O(n2)\locked{O(n^2)} scaling.

Note comparisons:20(20+1)2=210Road checks:20(20−1)2=190\displaystyle \begin{aligned} &\text{Note comparisons:} \\ &\qquad \frac{20(20 + 1)}{2} = \locked{210} \\[4pt] &\text{Road checks:} \\ &\qquad \frac{20(20 - 1)}{2} = \locked{190} \end{aligned}
Because the step count grows as O(n2)\locked{O(n^2)}, multiplying the number of cities by 100100 multiplies the total number of steps by roughly 1002=10,000100^2 = \locked{10{,}000} (not 100100).