How Dijkstra found the shortest path in twenty minutes Image: Andreas F. Borchert, CC BY-SA 4.0

How Dijkstra found the shortest path in twenty minutes

Edsger Dijkstra was 26 and a programmer at the Mathematical Centre in Amsterdam when the institute's new computer, the ARMAC, was due to be shown to the public in 1956. He wanted a demonstration that a non-mathematician could follow. A map of the Netherlands with the shortest route between any two cities seemed right, and he needed a method a computer could run. The method he found is still the one in every satellite navigation system, and he described its discovery himself, in an interview recorded in 2001 and published in Communications of the ACM in 2010.

The café terrace

Dijkstra had been shopping in Amsterdam one morning with his fiancée, Ria Debets, and the two sat down tired on a café terrace for a coffee. He was wondering whether he could do it, he said, and he designed the algorithm for the shortest path in about twenty minutes. He had no pencil and paper, which he later counted as a reason the design came out simple. The demonstration followed: a map of 64 Dutch cities, chosen so that six bits were enough to number them, and the ARMAC printing the shortest route between any two the audience named.

The paper came three years later. A Note on Two Problems in Connexion with Graphs appeared in the first volume of Numerische Mathematik in 1959 and is under three pages long. Dijkstra said he wrote it because the journal had just been founded and needed material. The first of the two problems is the minimum spanning tree, for which the paper gives what is now called Prim's algorithm (Prim had published it in 1957 and Jarník in 1930). The second is the shortest path. There is no proof in the paper beyond a few sentences, and no analysis of the running time; those came from others.

The algorithm on a small map

Take seven towns and the roads between them with their lengths in kilometres: A to B 7, A to C 9, A to F 14, B to C 10, B to D 15, C to D 11, C to F 2, D to E 6, E to F 9. We want the shortest route from A to every other town.

The algorithm keeps a tentative distance for each town, starting with 0 for A and infinity for the rest, and a set of settled towns, starting empty. At each step it settles the unsettled town with the smallest tentative distance, and then looks along every road from the newly settled town: if the settled distance plus the road length is smaller than the tentative distance at the far end, the tentative distance is replaced.

Step 1 settles A at 0. Its roads give B 7, C 9, F 14. Step 2 settles B at 7, the smallest tentative value. Via B, C would be 17 and D would be 22; C stays at 9, D becomes 22. Step 3 settles C at 9. Via C, D would be 20, which beats 22, and F would be 11, which beats 14. Step 4 settles F at 11; via F, E becomes 20. Step 5 settles D at 20 (E is also 20; ties are broken arbitrarily); via D, E would be 26, no change. Step 6 settles E at 20. The shortest distances from A are B 7, C 9, F 11, D 20, E 20, and the route to E goes A, C, F, E.

Notice that D was first reached with 22, through B, and later improved to 20, through C. A tentative distance is a promise that can be broken. A settled distance is final, and that is the theorem that makes the algorithm work.

Why a settled town is never improved

Suppose all road lengths are non-negative, and suppose town X is about to be settled with tentative distance t, the smallest among the unsettled towns. Any route from A to X leaves the settled set at some first road, from a settled town S to an unsettled town Y. The distance of S is final by induction, and the road S to Y was examined when S was settled, so the tentative distance of Y is at most the length of the route up to Y. Since t is the smallest tentative value, t is at most the tentative distance of Y, which is at most the length of the route up to Y, which is at most the length of the whole route because the remaining roads have non-negative length. So no route to X is shorter than t. That is the whole proof, and Unit 2 of the Graph Theory Masterclass writes it out in full.

The last step of the proof uses that road lengths are not negative. Allow a road of length minus 10 and the promise can be broken: a route that looks long at first may become short later. Dijkstra's algorithm then gives wrong answers, and the Bellman and Ford algorithm, which examines every road n minus 1 times, is the standard replacement.

How long it takes

On a map with n towns and m roads, a naive implementation looks through all unsettled towns at every step to find the smallest tentative distance, and that costs about n squared operations in total, which is what Dijkstra's demonstration did for 64 cities. With a priority queue that keeps the tentative distances sorted, the running time drops to about m times the logarithm of n. For the road graph of a continent, with tens of millions of roads, that is seconds. Route planners add tricks on top: searching from both ends at once, and precomputed shortcuts on motorways so that a query from Lisbon to Warsaw does not examine every village in between. The core is still the 1956 design.

What else the paper started

The shortest path problem had been studied before Dijkstra, by people working on transport and on the routing of telephone calls, and several of them found similar methods in the same years. Dijkstra's version became the standard one because it was the simplest and because it was correct in exactly the sense above. He went on to write the first ALGOL 60 compiler, to invent the semaphore for concurrent programs, and to argue, in a famous letter of 1968, that the goto statement should be considered harmful. He received the Turing Award in 1972 and spent his last years at the University of Texas at Austin. The MacTutor biography tells the rest of the story.

Unit 2 of the masterclass teaches the algorithm with the seven-town map above and the invariant proof, and the tool of that week is the public demonstration of the Open Source Routing Machine, which runs a descendant of the algorithm on the road graph of the whole planet. Drag the destination marker one street further and watch which part of the route stays fixed: the settled towns of the algorithm predict it.

Study graph theory properly

The Graph Theory Masterclass: weekly units, quizzes, a final exam, and a certificate.
About the program