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
A short road between B and C in the unexplored part of the map can make the detour A→B→C cheaper than C's direct road of 5, but no non-negative road out there can ever make the detour A→C→B beat B's direct road of 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)
2
Finding the Shortest Path Step by Step
Now let's use that rule to find the shortest path from A to F across the full six-city map.
Finding the Shortest Path Step by Step
Text solution
Start at A and check the roads to its neighborsLock in A at 0, write the direct road costs on B and C, and mark the unreached cities as ∞:
A:B:C:D,E,F:circled 02 (via A)5 (via A)∞
Circle B at 2 and update its neighborsSince 2 is the smallest uncircled offer on the board, lock in B and add B's running total of 2 to the roads leaving it:
Circle C at 4 and update its neighborsBetween C(4) and D(8), lock in C at 4 and check the roads from C to uncircled cities:
C→D:4+5=9>8(keep 8 via B)C→E:4+5=9⟹E:9 (via C)
Circle D at 8 and check its road to FBetween D(8) and E(9), lock in D at 8 and record its offer to F:
D→F:8+4=12⟹F:12 (via D)
Circle E at 9 and update FBecause E's 9 is smaller than F's 12, lock in E next and check its road to F before circling F:
E→F:9+2=11<12⟹12 (via D)→11 (via E)
Read the "via" notes backward from FCircle F at 11 and follow each city's note back to A:
Fvia EEvia CCvia BBvia AA
So the shortest path from A to F is A→B→C→E→F with a total cost of 11, and the circled numbers on the board give the shortest distance from A to every other city at the same time.
Halfway through the search, every route from A to D must take a first step across the dotted border around {A,B,C}, and crossing the border at E already costs 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
With a negative road of −4 from C to B, the detour through C gets smaller as it goes, beating B's direct road of 2 after B has already been locked in:
A→C→B:5+(−4)=1<2
A road of length 0 causes no trouble, because adding 0 never reduces a path's running total.
4
Counting the Steps: O(n2)
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 n cities.
Counting the Steps: O(n²)
Across all n 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=2n(n+1)≈21n2Check roads to uncircled neighbors:at most 2n(n−1)≈21n2
Putting the two jobs together gives O(n2) total steps.
Key concepts
To find the shortest paths from a start city A on a map where every road length is 0 or positive (w≥0), circle A at 0 and repeat one loop: check the roads from the city u you just circled, update (relax) any uncircled neighbor v where total(u)+road(u,v) beats v's current note (as 2+2=4 beat 5 on C), 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 F getting 12 via D while E sits uncircled at 9); stop only when the destination itself is circled, then read the "via" notes backward from the destination to A to find the route. Across n cities, scanning the notes takes 2n(n+1) comparisons and updating neighbors checks at most 2n(n−1) roads, for O(n2) steps in total. Do not use Dijkstra's algorithm if any road has a negative weight (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 beating 2).
5
Practice
1
Suppose you are running Dijkstra's algorithm starting at city A. Only A (at 0) is circled so far, and the notes on the other cities are B:3 (via A),C:7 (via A),D:9 (via A), and E:∞. The roads leaving B to uncircled cities are B–C of length 3,B–D of length 7, and B–E of length 5. Which city do you circle next, and what are the notes on C,D, and E 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,7,9, and ∞, so circle B at 3 (via A).
From B, add B's running total of 3 to each road leaving it (be careful not to write the bare road lengths 3,7, and 5 without adding B's 3 first):
(Watch out on D:10 does not beat 9, so do not overwrite D's existing note.)
Circle B at 3 (via A); the updated notes are C:6 (via B),D:9 (via A), and E:8 (via B).
2
Run Dijkstra's algorithm to find the shortest path from A to F on the 6-city map with roads A–B=3,A–C=4,B–C=2,B–D=3,B–E=6,C–E=2,D–F=5, and E–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 A to F.
Keep going until F itself has the smallest uncircled offer, even after D puts a first offer on F.
Circle A at 0. Checking the roads from A gives B:3 (via A) and C:4 (via A).
Circle B at 3 (via A). Checking the roads from B gives B→C:3+2=5>4 (so C keeps 4 via A), D:3+3=6 (via B), and E:3+6=9 (via B).
Circle C at 4 (via A). Checking the road from C gives C→E:4+2=6<9, updating E to 6 (via C).
Now D:6 (via B) and E:6 (via C) tie for the smallest offer and can be circled in either order. Circling D at 6 (via B) gives F:6+5=11 (via D). (Do not stop here and answer 11:E is still uncircled at 6<11.)
Circle E at 6 (via C). Checking the road from E gives E→F:6+1=7<11, updating F to 7 (via E).
Circle F at 7 (via E). Final circled notes: A:0,B:3 (via A),C:4 (via A),D:6 (via B),E:6 (via C),F:7 (via E). Reading the "via" notes backward gives F←E←C←A, so the shortest route is A→C→E→F with total cost 7.
3
Halfway through running Dijkstra's algorithm from A to F, the circled cities inside the dotted border are A(0), B(2), and C(5), and all roads leaving them have been checked. Outside the border, the uncircled cities have notes D:7 (via B),E:8 (via C), and F:10 (via C). A friend sees the note 10 (via C) on F and stops, claiming the shortest path from A to F is 10. Explain using the dotted border why D's 7 is guaranteed to be final right now while F's 10 is not, and find the smallest possible true shortest distance from A to F if every road outside the border has length at least 1.
However, a route that crosses the border at D for 7 or at E for 8 can still reach F through the uncircled cities for less than 10.The cheapest possible path crosses at D (not E, which would give 8+1=9) and takes a single road of length 1 from D to F.
Compare the cost of stepping across the dotted border at D,E, or F, and see where a route to F could cross the border for less than 10.
Every route from A to an uncircled city must cross the dotted border at D (for 7), E (for 8), or F (for 10). Because 7 is the smallest crossing cost, any route that crosses at E or F has already spent at least 8 at the border and cannot beat 7 on D. However, a route that crosses the border at D for 7 or at E for 8 can still reach F through the uncircled cities for less than 10. If every road outside the border has length at least 1, the cheapest possible path crosses at D (not E, which would give 8+1=9) and takes a single road of length 1 from D to F, giving a shortest possible distance of 7+1=8.
4
On a 4-city map of one-way roads, A has roads to B (length 4) and C (length 7), B has a road to D (length 3), and C has a road to B of length w. Does Dijkstra's algorithm starting at A still find the true shortest paths if w=0? For which values of w does it lock in the wrong shortest distance to B and D?
Write the total cost of the path A→C→B in terms of w and compare it against the distance 4 that Dijkstra's algorithm locks in for B.
Starting at A, Dijkstra's algorithm circles B at 4 and updates D to 4+3=7 before checking the road from C (at 7).
If w=0, the detour A→C→B costs 7+0=7, which does not beat 4, so Dijkstra's algorithm still finds the true shortest paths.
In general, the path A→C→B costs 7+w and A→C→B→D costs 10+w. (Be careful not to say the algorithm fails for every w<0: when −3≤w<0,7+w≥4, so B's locked-in 4 is still the true shortest distance.) The detour beats B's 4 and D's 7 whenever 7+w<4, which is for w<−3.
5
On a map of n=20 cities with a direct road between every pair of cities, how many total notes are checked across all 20 rounds to pick the smallest uncircled offer (20+19+⋯+1), and how many roads between pairs of cities are checked during neighbor updates? If the map grows 100-fold to 2,000 cities, by roughly what factor does the total number of steps grow?
Use 2n(n+1) for the note comparisons and 2n(n−1) for the roads between pairs of cities, then apply O(n2) scaling.