On a Galway road map, can taking the shortest road first make your trip longer?
You’re planning a trip across Galway. The first junction gives you two choices: a two kilometre road and a five kilometre road. Which one starts the shorter trip? The answer depends on what happens after that first turn.
On this map, letters mark junctions, or vertices, and lines mark roads, or edges. The numbers are road lengths in kilometres. You can travel either way along each road, and your goal is to find the shortest route from A to D.
Write zero beside A because you’re already there. Write infinity beside B, C and D because you haven’t recorded a route to any of them yet. These are tentative labels, meaning they can change as you find routes.
Use the same road map, but this time start at C. Before you look at any roads, what labels should you give C and its neighbour D? Pick the pair that fits.
Make the least tentative label permanent. Follow each allowed road out to a still-tentative neighbour. Add the road length to the permanent label. If the total is smaller, replace the neighbour’s label and record its predecessor. Repeat until you fix the destination or run out of finite tentative labels.
Fix A at zero. From A, label B two and C five. Fix B at two. Via B, C would cost six, so keep five. Label D ten via B. Fix C at five. Via C, D drops to eight. Now fix D at eight.
From S, you can go to B in four kilometres or straight to D in nine. The road from B to D is three kilometres, and the road back is one. What distance and predecessor should D have when it becomes permanent? Pick a pair.
Read D’s predecessor, C, then C’s predecessor, A. Reverse that chain to get A, C, D, an eight-kilometre route. If you take the cheapest road to an unvisited junction instead, you go A, B, C, D. Two plus four plus three is nine kilometres. Dijkstra compares totals from the source.
Roads point to later letters, so there are no directed cycles. At each decision stage, your state is your junction. Bellman’s principle says the rest of an optimal route is optimal too. Minimise road length plus best remaining distance. Solve D, C, B, A in order. Winning turns form your policy.
To justify Dijkstra’s greedy choice, assume permanent labels are correct. A rival route first leaves that set along a road you checked. Its cost so far is at least the label there, which is at least the least tentative label. Non-negative roads cannot lower that cost, so fixing the minimum is safe.