JOVANA
Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
Back to the library
Algorithms 1959

A Note on Two Problems in Connexion with Graphs

Edsger W. Dijkstra

Always extend the cheapest route found so far, and the shortest path to everywhere falls out.

Choose your version
In depth · the introduction

How does your phone pick the single fastest road out of a country's worth of them — in a heartbeat, and without checking them all?

The big idea

Suppose you want the shortest route from your town to every other town on a map of roads. The brute-force way — try every possible route — explodes into impossibly many combinations. Dijkstra found a shortcut built on one disciplined habit: always extend the cheapest route you have found so far.

Start at home with a distance of zero, and mark every other town as “unknown distance.” Then repeatedly do the same small thing: among the towns you have not yet locked in, pick the one you can currently reach most cheaply, lock in that distance as final, and use it to update its neighbours. Because roads never have negative length, the cheapest town you can reach can't later turn out to be cheaper by some other way — so once you lock it in, you're done with it. Keep going and the shortest path to everywhere falls out, each one settled exactly once.

How it came about

In 1956 Edsger Dijkstra, a young programmer at Amsterdam's Mathematical Centre, needed a vivid demo for a new computer, the ARMAC. He picked a question anyone could grasp: what is the shortest way to drive from Rotterdam to Groningen? Sitting at a café terrace, tired from shopping with his fiancée and with no pencil and paper to hand, he worked the whole method out in his head in about twenty minutes.

He didn't publish it until 1959, in a three-page note that quietly solved two graph problems at once — the shortest path, and the minimum-length tree that connects every node. Dijkstra was careful about credit: the tree algorithm had been found years earlier by Jarník and by Prim, and others had circled the shortest-path idea too. What made his version last was how clean it was, and that he could say exactly why it always works.

Why it mattered

This little routine turned out to be one of the most-used algorithms ever written. Every time a navigation app draws your route, a delivery company plans its vans, or a packet of data hops across the internet, some descendant of Dijkstra's method is choosing the cheapest way through a network. It is fast enough to run on enormous maps and simple enough to prove correct — a rare and valuable combination.

A way to picture it

Picture pouring water into the pipe network at your home town, where every pipe's length is the travel time of a road. The water spreads outward and reaches each junction first by its shortest pipe — never by a longer detour, because the short way always arrives sooner. The moment water reaches a junction, you know the quickest time to it is fixed. Dijkstra's algorithm is exactly this flood, done one junction at a time: always fill the nearest dry junction next.

Interactive map of seven Dutch cities joined by roads with distances; pressing Step locks in the nearest not-yet-settled city and updates its neighbours, until the shortest route to the chosen destination is highlighted.

Where it sits

The 1950s gave computing its grammar: Shannon had quantified information, von Neumann had laid out the stored-program machine, and now Dijkstra showed how to make such machines reason efficiently about networks. His method is the ancestor of A* (the same idea, nudged toward a goal) and of the routing protocols that knit the internet together. In this Library it stands beside Shannon and von Neumann as one of the founding ideas of computer science — and beside the cryptography papers it shares a field with, since the network it routes over is the same one they keep secret.

The original document
Original source text
E. W. Dijkstra · Numerische Mathematik 1 (1959): 269–271 · Mathematisch Centrum, Amsterdam
The setting
The note opens on a graph of n nodes in which some, or all, pairs of nodes are joined by a branch carrying a given positive length, and asks two construction problems about it. (Paraphrased; the exact statements of the two problems are quoted below.)
Problem 1 — the minimum spanning tree
Construct the tree of minimum total length between the n nodes.
Dijkstra's solution grows a single tree: start from an arbitrary node and repeatedly add the shortest branch that joins a node already in the tree to a node not yet in it, until all n nodes are connected. (This rule had in fact been found earlier — by Jarník in 1930 and Prim in 1957 — and is usually called Prim's algorithm today.)
Problem 2 — the shortest path
Find the path of minimum total length between two given nodes
Here he keeps the nodes in three groups: those whose minimal distance from the start is already known and fixed; the frontier nodes for which a tentative distance has been found; and the rest, not yet reached. At each step the frontier node of smallest tentative distance is moved into the known set, and the branches leaving it are examined to see whether they offer any of its neighbours a shorter route than the one recorded so far — the step the modern literature calls “relaxing” an edge. (Paraphrased.)
[ … ]
The whole argument fits on three printed pages, with no formal pseudocode in the modern sense; the construction is given in prose. The complete note, including Dijkstra's remarks on storage and on the number of comparisons each method needs, is available at the source below.
Mathematisch Centrum, Amsterdam · 1959