← The Museum of Algorithms
4.2

Wing IV · The Ocean of Possibilities

The Dance of
the Pointers

Dancing Links, Algorithm X, and exact cover

VOLUME IV — Combinatorial Algorithms SECTION 7.2.2.1 2000 Knuth · Dancing Links

Every hard search in the last room ended the same way: try something, discover it fails, and undo it. Backtracking — the method of exploring a tree of choices, retreating whenever a branch proves hopeless — is the workhorse of combinatorial computing, the engine behind Sudoku solvers, puzzle packers, and constraint systems of every kind. And its secret cost is almost never in the trying. It is in the undoing: to back up cleanly, you must restore the world to exactly the state it was in before you meddled, and doing that quickly, correctly, millions of times, is where naïve programs bog down and careful ones fly.

In 2000 Donald Knuth published a short paper on a data-structure trick so pretty he gave it a name that stuck. Store your problem as a grid of items linked into doubly-linked lists — every element pointing to its left, right, up, and down neighbours. To remove an element, splice it out by making its neighbours point past it. The trick is what happens next: do not erase the removed element's own pointers. It still remembers exactly who its neighbours were. So to put it back — to undo the removal — you simply tell those neighbours to point at it again, and it slots into precisely its old place, in constant time, with no bookkeeping at all. Knuth watched the links flicker out and back as the search advanced and retreated, and called it dancing links.

"The reverse operation puts x back into its position, as if it had never left. This makes it possible to backtrack extremely efficiently. I can't resist calling the resulting technique 'dancing links.'" — Donald E. Knuth, "Dancing Links," 2000

I · Exact Cover & Algorithm XOne problem to rule the puzzles

The dance solves one abstract problem, and the abstract problem is a chameleon. Exact cover asks: given a set of options and a set of requirements, choose a subset of options so that every requirement is met by exactly one chosen option — no gaps, no overlaps. It sounds narrow. It is not. Tiling a region with shapes is exact cover (each cell is a requirement, each placement an option). Sudoku is exact cover. The N-queens problem, crosswords, protein folding models — all of them melt into this one grid of ones and zeros. Solve exact cover well and you solve them all.

Knuth's Algorithm X is the natural recursive method: pick the requirement hardest to satisfy — the column with the fewest options — and cover it, deleting that column and every option that conflicts with your choice. Recurse on the smaller problem. If you reach a dead end, uncover — dance the deleted rows and columns back into place — and try the next option. Dancing links is what makes the covering and uncovering instantaneous. Below, watch it tile a board: each column is a cell to fill, each row a way to place a piece, and the matrix of ones on the right is the real linked structure, unlinking and dancing back as the search breathes in and out.

Instrument 4.2
The Living Board
DANCING LINKS · Algorithm X · cover → recurse → uncover
Board (left) & the live 1-matrix (right) — rows dance out on cover, back on uncoverready
Solve runs Algorithm X; Step advances one link operation. Gold cells are the current partial tiling; faded matrix rows have been covered — watch them dance back on every backtrack.
Board
The ledgeridle
Covers
0
Uncovers
0
Solutions
0
Press Solve to set the links dancing.

Every cover that pulls a column and its conflicting rows out of the matrix is matched, on backtracking, by an uncover that splices them back into exactly their old positions — because the removed nodes never forgot their neighbours. The counts of covers and uncovers end equal, and the structure returns identical to how it began. That perfect reversibility is the whole magic of the dance.

II · Why the Dance Is FastUndoing without remembering

A naïve backtracking solver, to undo a step, must either copy the entire state before each move — ruinously expensive — or laboriously reconstruct what it deleted. Dancing links does neither. Because a removed node keeps its own L, R, U, D pointers intact, restoration needs no saved history and no allocation: the four assignments that re-link it are the exact mirror of the four that unlinked it. Deletion and its perfect inverse each cost a handful of pointer writes, independent of how large the matrix is. This is why Algorithm X on dancing links can rip through search trees with millions of nodes that would strangle a copying solver — and why the counts you watch tick upward, covers and uncovers, must end precisely equal when the search returns home.

The removed node is not destroyed; it is only stepped over. It waits in the wings, still pointing at the place it belongs, ready to dance back the instant the search changes its mind.

Knuth pairs the technique with one more idea that the instrument quietly uses: always cover the column with the fewest remaining options first. This "minimum remaining values" heuristic prunes the tree brutally — it fails fast, discovering dead ends before wasting effort — and on structured problems it turns an astronomically large search into a brief flurry of dancing. It is the same instinct a human puzzler has, filling the most constrained square first; Knuth simply made it exact, and made the undo free.

III · The Method Behind the PuzzlesFrom Sudoku to satisfiability

Give Algorithm X the 729-row, 324-column exact-cover matrix of a Sudoku and it disgorges the solution in milliseconds; the world's fastest Sudoku solvers are dancing links underneath. Feed it the placements of the twelve pentominoes and it enumerates every way to tile a box. Knuth extended the idea in Volume 4B into colour-controlled covering and generalized it toward the ZDD-based methods of the exhibits' near neighbours — a whole family of exact, exhaustive combinatorial search built on this one reversible trick. Backtracking is the engine of Wing IV, and dancing links is its most beautiful bearing: frictionless, exact, and endlessly reusable.

"An algorithm must be seen to be believed." — D. E. Knuth

There is a particular joy, Knuth has said, in watching the links dance — the way a solved constraint dissolves the rows that conflict with it, the way a wrong guess is undone as cleanly as it was made, the way the whole matrix breathes as the search descends and rises. It is a rare thing in programming: a technique that is at once maximally practical and frankly gorgeous. Slow the speed down, step through a single cover and its later uncover, and watch a node leave and return to precisely its place. Then let it run, and let the pointers dance.