Roads and Wires
Real links are not all equal: one road takes five minutes, another fifty. Give every link a number and two classic questions appear: what is the quickest way from here to there, and what is the cheapest way to connect everything?
A navigation app rarely sends you along the route with the fewest roads. It sends you along the route with the smallest total time, even if that means more turns. The ripple from Chapter 3 cannot do that: it counts links, and treats a short lane and a long motorway as one step each.
So give each link a number, its weight: minutes of driving, kilometres of cable, cost in money. The length of a path is now the sum of its weights, and the question becomes: which path has the smallest sum?
Dijkstra’s bubble of certainty
The trick that answers it is to grow a bubble of towns whose quickest time you already know for sure. At the start the bubble holds only your starting town, at time 0. Every town just outside the bubble gets a best guess so far: the quickest time found by stepping out of the bubble along one road. Then comes the key move: the town with the smallest guess joins the bubble, and its guess becomes certain. Its roads are checked to improve the guesses of its neighbours, and the step repeats.
Dijkstra on a road map
Press Step to grow the bubble one town at a time. Drag a road’s minutes up or down (or click it and use the slider) to change the travel time.
Why can the smallest guess be trusted? Every other way of reaching that town would have to leave the bubble through some other town just outside it, and every one of those already has a guess at least as large. Since no road takes a negative amount of time, a detour can only add minutes, never remove them. So the smallest guess can never be beaten.
Dijkstra’s algorithm grows a bubble of certainty outward in order of distance: the closest town outside the bubble is always safe to lock in, as long as no link has a negative weight.
The Dutch computer scientist Edsger Dijkstra devised the method in 1956 and published it in 1959. He later recalled designing it in about twenty minutes, without pencil and paper, while sitting at a café terrace in Amsterdam. Modern route planners add many speed-ups and tricks, but the bubble of certainty is still at their heart, and the same idea helps the internet decide where to send your data next.
The guarantee needs every weight to be zero or positive. If some link had a negative weight (imagine a road that pays you time back), a long detour could end up cheaper than a town already locked in, and Dijkstra’s method can give the wrong answer. Other algorithms exist for that case.
Connecting everything, cheaply
Now a different job. You are laying cable (electricity, water, fibre) between nine towns. Every town must be connected to every other, at least indirectly, and every metre of cable costs money. You don’t care how long any particular trip is; you care only about the total length of cable.
The first insight is that the cheapest answer never contains a loop. If your cable network had a loop, you could cut the most expensive cable in it and every town would still be connected the other way around the loop. A connected network with no loops is called a tree. A tree that reaches every node is a spanning tree, and the one with the smallest total weight is the minimum spanning tree.
Cheapest wiring
Click the dashed lines to lay (or remove) cable. Connect all nine towns as cheaply as you can, then let the greedy method try.
Greedy rules (“always grab the best-looking option right now”) usually fail in life and in mathematics, so it is remarkable that this one never does. The method above is known as Kruskal’s algorithm, published in 1956. One of the earliest solutions to the problem, by the Czech mathematician Otakar Borůvka in 1926, was motivated by exactly this job: planning an efficient electrical network.
A tree connects n nodes with exactly n − 1 links and no loops, so there is exactly one path between any two of its nodes; the cheapest network that connects everything is always such a tree.
Trees are everywhere once you look: family trees, the folders on your computer, river systems flowing to the sea, the shortest-path tree that the ripple left behind in Chapter 3. They are the leanest possible way to hold a set of nodes together. That leanness has a price, though. Cut any single link of a tree and it falls into two pieces, and the route between two towns may be a long way round. Real road and power networks add extra links, and with them loops, to buy shorter trips and resilience.
Intuition check
You have built the minimum spanning tree for a region’s towns, using road length as the weight. Does it also give the shortest route between any two towns?
What you now see
- In a weighted network, a path’s length is the sum of its weights, and the fewest links is not always the quickest.
- Dijkstra’s algorithm grows a bubble of certainty, locking in the closest town each step; it needs non-negative weights.
- A tree connects n nodes with n − 1 links and no loops; the minimum spanning tree connects everything at the least total cost.
- Cheapest-to-build and quickest-to-travel are different goals, and real networks trade one against the other.