Dijkstra’s algorithm: an idea he said took 20 minutes now powers route finding

Photo: Edsger W. Dijkstra (Andreas F. Borchert · CC BY-SA 4.0 · Wikimedia Commons)

In 1956, Edsger W. Dijkstra and his young fiancée walked out of another round of shopping in Amsterdam, tired, and sat down on a café terrace. Dijkstra had no pencil in his hand and no paper on the table. By his own telling, he sat there and designed an algorithm for finding the shortest path between two points on a map. He said it took about twenty minutes.

The solution waited three years before it appeared in print.

The story comes from Dijkstra himself. Decades later he told it to Philip L. Frana, and the interview appeared in Communications of the ACM in 2001. By then the idea from the terrace had spread far beyond Amsterdam. Versions of it sit inside the systems that find routes for people and for data, and they answer in a blink.

How does a short stretch of thinking, done without a scrap of paper, grow into one of the cornerstones of routing? And what was the problem Dijkstra was trying to solve in the first place?

Photograph: Edsger W. Dijkstra
Photograph: Edsger W. Dijkstra. Photo: Andreas F. Borchert. Andreas F. Borchert. Wikimedia Commons · CC BY-SA 4.0. Resized and converted to WebP; no retouching.

A demo that non-specialists could follow

Dijkstra wanted to show off a machine. In 1956 he worked as a programmer at the Mathematical Center in Amsterdam, and the institute had a new computer, the ARMAC. He wanted people outside it to see what it could do.

That meant choosing a demonstration, and the choice carried a risk. The people he had to impress did not compute for a living. A problem that delighted a mathematician could leave them politely nodding, and a machine that impresses no one is easy to forget. So Dijkstra set himself two tasks at once: find a problem that people outside computing could understand, and find a computer solution they could follow from start to finish.

The problem had to be one that needed no explaining. Anyone who has stood in front of a map and planned a trip already knows the question: which way is shortest? Dijkstra framed it with two Dutch cities that his audience could picture without help. The question for the demonstration became the shortest way to travel from Rotterdam to Groningen.

The answer had to come from somewhere, and for now it existed only in Dijkstra's head, with no pencil and no paper behind it. The ARMAC had to carry it, and the machine was small. How does an idea sketched in a head get turned into a program that runs?

Sixty-four cities and six bits

Dijkstra sat down to put the Dutch map on the ARMAC, and the machine's smallness decided the first thing he did: he cut the map down. The version he fed it was a slightly simplified transportation map of the Netherlands with 64 cities, and the number was no accident. Sixty-four was the most cities that six bits could label, so each city got a six-bit number and the whole map fit the machine's modest memory.

A simplified transportation map of the Netherlands used 64 cities to fit the ARMAC computer's modest memory.

Then came the method itself. The task was to find the shortest path from one chosen starting city to every other city on the map, not only to Groningen. Dijkstra began from what the machine could honestly say it knew. The starting city sat at distance zero. Every other city got infinity, because no path to any of them had been found yet.

From there the work was patient bookkeeping. Whenever a shorter way to a city turned up, the new route replaced the old claim, and the longer one lost its standing. Distances shrank step by step from infinity toward real numbers, until each city held the length of its best route from the start.

The method was plain enough that a person could run it without a computer. With pencil and paper and a city map, every intersection except the start is labeled with infinity. When a road yields a shorter path to an intersection, the road gets an arrow. Any other arrow pointing into that intersection is erased.

The method also knew when to quit. Once the target city became the current city, the algorithm could stop early, with no need to chase every remaining route. And a city already visited was never rechecked, because its recorded distance was minimal and final.

Yet something about the design kept nagging at Dijkstra, and he would return to it later. He had built the whole method in his head, with no pencil and no paper to hold his thinking. Years afterward he would say that this absence had shaped the solution itself. What exactly had the missing paper done to the idea?

Three pages, three years late

The publication, he said, is nice partly because he had worked the algorithm out without pencil and paper.

Quote by Dijkstra about working out the algorithm without pencil and paper.

The arithmetic at the heart of the method is small enough for a head to hold. Say city A sits at distance 6, and the road from A to B is 2 long. Then the distance to B through A is 8. Repeat that kind of sum, and once no unvisited city has a label smaller than the destination's, the arrows that remain show the shortest path.

The idea that came out of that discipline was still not in print, though. Dijkstra would describe it as a twenty-minute invention, and for a while the invention existed only where it had been born: in his memory, and in the demonstration that had run on the ARMAC. The paper that carried it into the world appeared in 1959, three years later.

When it finally came out, it was small. The note ran from page 269 to page 271 of the first volume of Numerische Mathematik, a German journal of numerical mathematics, and it bore a plain, almost shy title: "A note on two problems in connexion with graphs."

Dijkstra, for his part, had little time to wait for applause. Engineers at his institute had a new problem for him, and it had nothing to do with roads.

The wire problem and a rediscovery

The engineers came down the hall with a question about copper. A year after the terrace, the hardware people at the institute were planning the next computer, and they needed to know how to join the pins on its back panel using as little wire as possible.

Dijkstra took the problem the way he had taken the one about Rotterdam and Groningen: as something to settle by thinking. Pins stood in for cities, wires for roads, and the cheapest set of wires that tied every pin together was the answer the engineers wanted.

He found it. The method he arrived at was the minimal spanning tree algorithm that Robert Prim had published, though Dijkstra had not known that. Vojtěch Jarník had known it earlier still, and Prim, too, had reached it by rediscovery.

Dijkstra put the wire solution into print in 1959, the same year as the shortest-path note. By then the calendar had already ruled against him: his paper came two years after Prim's and 29 years after Jarník's.

Yet his shortest-path note, three pages sitting quietly in a German journal, was in a different position. Somebody was about to put it to work somewhere far from a Dutch map or a back panel full of pins.

From a Dutch map to network routing

Network engineers read the same three pages and saw a different map. Their cities were routers, their roads were links, and their travelers were packets of data that needed the quickest way across. Shortest-path methods became a common tool for network routing, and two of the best-known routing protocols, IS-IS and OSPF, put them to work.

Dijkstra's method had no quarrel with this new terrain, but its cost grew quickly. In its original form, before anyone gave it a smarter way of picking the next city, the running time was proportional to the square of the number of nodes. Double the network and the work roughly quadrupled. A 64-city demo could shrug that off, and a growing network could not.

Others went after the cost. In 1984, Michael Fredman and Robert Tarjan proposed replacing the plain bookkeeping with a Fibonacci heap, a priority queue: a structure that keeps the unfinished cities ordered so the nearest one is always ready to hand over. The change improved the running time.

With that heap in place, the method became the asymptotically fastest known way to find shortest paths from a single starting point in a directed graph, even when the weights on its edges are unbounded, so long as none is negative.

Dijkstra had no pencil, and the method that came out of that has been polished, not replaced. Szczesniak and colleagues, looking back across half a century, claim that nobody has proposed a better algorithm than his.

Still, a method can be the best of its kind and be too slow for the job in front of it. A router can wait while it works through a network. A driver staring at a phone, waiting for directions across a continent, will not wait.

Eight milliseconds across a continent

Sanders and Schultes looked at a real road map and noticed what the plain method ignored. Roads sort themselves into ranks. A trip from one town to another climbs from side streets onto main roads, crosses most of the distance on a few great highways, and then descends again. Their highway hierarchies exploited that layering, and a computer could prepare for it ahead of time. For a map of the USA or Western Europe, the preparation took a few hours. After that, a query returned an exact shortest path in around eight milliseconds, about 2,000 times faster than plain Dijkstra.

The race did not stop there. A few years later, in 2008, Geisberger, Sanders, Schultes and Delling presented contraction hierarchies, which strip a map down by folding the minor roads into shortcuts. With the same kind of preprocessing, they can run up to seven orders of magnitude faster than the plain method. When Dijkstra learned how far it had traveled, he was amazed that the algorithm had become one of the cornerstones of his fame.

A driver somewhere on a crowded highway asks a phone for the way, and the answer is there before the thumb has left the screen. Behind it runs the same question a programmer once put to a room of people who did not compute: what is the shortest way from Rotterdam to Groningen? On a café terrace in Amsterdam, a tired man with no pencil and no paper had already walked that route in his head.

Cover: Andreas F. Borchert · CC BY-SA 4.0 · Wikimedia Commons. Layout and headline text by Signal Titans.

Source: Twenty Minutes on a Café Terrace: Edsger Dijkstra and the Shortest-Path Algorithm Designed Without Pencil · arxiv.org · arxiv.org · Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks | Springer Nature Link · Highway Hierarchies Hasten Exact Shortest Path Queries | Springer Nature Link · Properties of Labeling Methods for Determining Shortest Path Trees – PMC · Properties of Labeling Methods for Determining Shortest Path Trees – PMC · www3.cs.stonybrook.edu · Bidirectional Dijkstra | Matthew Towers’ homepage

Share this article
FacebookThreadsLinkedInX

Leave a Reply

Your email address will not be published. Required fields are marked *