How a Computer Solves Sudoku (and Why It Does It Backwards)

September 4, 20267 min readBen Miller

Any modern laptop solves any 9×9 Sudoku ever published in less time than your screen takes to refresh. That fact can sound deflating — why struggle over what a machine dispatches in microseconds? But look inside how the machine actually does it and the feeling reverses. The algorithms are beautiful, their history runs through one of the great computer scientists of the century, and the differences between silicon solving and human solving explain — precisely — what your daily struggle is made of, and why the machine's hardest puzzle is not yours.

Method one: the patient brute

The simplest Sudoku program is recursion with a rulebook. Find the first empty cell. Try a 1: legal so far? Move to the next cell and repeat. Hit a dead end — a cell with no legal digit — and backtrack: undo the latest guess, try its next value.

This is depth-first search, and its honesty is its charm: no cleverness, no insight, just exhaustive bookkeeping performed without boredom or despair. It is also, in the worst case, catastrophically wasteful — early wrong guesses can spawn millions of doomed descendants before the contradiction surfaces. A bare backtracker still eats typical newspaper puzzles alive, because their clues prune the tree fast. But it isn't how serious solvers — silicon or human — actually work.

Method two: your technique, industrialized

The upgrade is one you practice every morning: constraint propagation. Keep, for every empty cell, its candidate set — the digits still legal there. Every placement erases candidates from its row, column, and box; every erasure can trigger more. A cell down to one candidate: place it (your naked single). A digit with one home left in a unit: place it (your hidden single). Each placement propagates, and easy puzzles simply collapse, no search required.

Peter Norvig's famous essay on Sudoku solving demonstrated the canonical recipe: propagate until quiet; if unsolved, pick the cell with the fewest candidates, try one, and propagate the consequences — undoing cleanly on contradiction. Note the heuristic, because it is exactly your instinct formalized: guess where the options are narrowest. A two-candidate cell is a coin flip; a wrong flip dies fast and teaches much. Between propagation's shears and disciplined guessing, the search tree that terrified the brute shrinks to almost nothing — hard puzzles fall in a few dozen guesses total.

Here is the strange mirror: the machine's propagation phase is precisely your technique arsenal — singles, pairs, eliminations — run at nanosecond speed. The machine's search phase is what you refuse to do: bifurcating, holding a hypothetical world in memory, ready to unwind it. Humans hate that — working memory can't hold the tree — which is why "no guessing required" is a design promise made to humans, not machines. The machine never needed it.

Method three: the dance

The most elegant chapter belongs to Donald Knuth. In 2000 he published a paper on a technique he called dancing links, attacking a general problem called exact cover: given a universe of requirements and a menu of options each satisfying several, choose options so every requirement is met exactly once.

Sudoku is exact cover wearing a costume. The requirements number 324: each of 81 cells must be filled; each of 9 rows needs each of 9 digits; likewise 81 column-digit and 81 box-digit demands. The options number 729: "place digit d at cell (r,c)," each satisfying exactly four requirements. A solved grid is a choice of 81 options covering all 324 requirements exactly once — no digit twice in a row, no cell empty. One matrix formulation, and suddenly Sudoku, Nonograms of a certain species, polyomino packings, and the n-queens problem are all the same animal, solvable by the same engine.

Knuth's contribution was making the engine fast, with a data-structure trick of pure loveliness. Represent the matrix as circular doubly-linked lists. Removing a row or column during search is a few pointer redirections — and here is the magic — restoring it on backtrack is the same redirections reversed, cost nearly zero. No copying, no rebuilding: the links "dance" apart and back together, millions of times per second. Watching a visualization of DLX on Sudoku is genuinely balletic: hypotheses splice out of existence and spring back, until 81 survivors remain.

Why the machine's "hard" isn't yours

Now the payoff insight. Puzzle difficulty, for a computer, is roughly how much search survives propagation. For a human, difficulty is which techniques the logical path demands — a puzzle needing an X-wing is hard even with zero search, because the pattern is rare in your library, not because the tree is deep.

The two scales genuinely diverge. Famous "world's hardest" Sudokus — like the Finnish mathematician Arto Inkala's compositions that circulate every few years — are hard for humans because their solving chains are long and exotic; a DLX solver shrugs them off in milliseconds. Conversely, one can construct near-empty grids that make naive backtrackers thrash for ages yet contain no interesting human logic at all — just a badly shaped search tree. When our generators grade a puzzle, this is why they don't time a solver and call it a day: they simulate your toolkit, technique by technique, and ask what the puzzle demands of a person. The machine is the cartographer of difficulty, not its measure.

And uniqueness — the sacred promise that exactly one solution exists — is a solver running twice: find a solution, then continue searching; any second solution kills the puzzle before publication. Every "this cell must be 5" you've ever confidently written rests on a completed exhaustive search you never saw.

Two kinds of solving, one grid

So should the microsecond machines deflate you? Consider what each party actually extracts from the grid. The computer extracts a certificate: these 81 digits satisfy 324 constraints. It learns nothing, enjoys nothing, and would not notice if the puzzle were beautiful. You extract the other thing — the walk itself: the noticing, the near-miss, the pattern that snaps into place and joins your permanent library. The grid is a course, and the machine merely proves the course is fair. That division of labor is not a consolation prize; it is the design. We build the solver so the solver can promise you a clean mountain. The climbing was never its job.

← Back to Blog