A sudoku grid has 81 cells. In a typical puzzle about 25 are filled in and 56 are empty. Each empty cell could hold any digit from 1 to 9.
The crudest possible method is to try every combination. That is 9 multiplied by itself 56 times, a number with 54 digits. A computer checking a billion grids a second would not finish in the lifetime of the universe.
Yet a few dozen lines of code will solve the hardest published sudoku in milliseconds. The gap between those two facts is explained by two ideas.
The puzzle as constraints
First, the rules, stated the way a programmer would.
There are 81 variables, the cells. Each has a set of possible values, initially 1 to 9. There are 27 groups, comprising nine rows, nine columns and nine boxes, and the rule is that no digit may appear twice within a group.
A problem stated as variables, possible values and rules restricting combinations is called a constraint satisfaction problem. Sudoku is a tidy example of a very large family.
Idea one: propagation
When a cell's value is known, that digit can be removed from the possibilities of every other cell in its row, column and box. Each cell has twenty such neighbours, which solvers call peers.
Removing possibilities has knock-on effects, and two simple rules capture most of them.
If a cell has only one possible value left, it must be that value. Enter it, and remove it from all twenty peers. Players call this a naked single.
If a digit has only one possible place in a row, column or box, it goes there. Players call this a hidden single.
Apply the two rules repeatedly, each deduction triggering others, until nothing more changes. This is constraint propagation.
It is surprisingly powerful. Most puzzles printed as "easy" or "medium" are solved completely by propagation, with no guessing at all. Human solvers extend the idea with a catalogue of further patterns, which have names such as pairs, pointing pairs, X-wings and swordfish. Each is a more elaborate way of ruling values out.
For harder puzzles, propagation runs dry. Every remaining cell has two or more candidates and none of the rules applies.
Idea two: search with backtracking
At that point a program does what a person does reluctantly. It guesses.
- Choose an empty cell.
- Try one of its possible values.
- Propagate the consequences.
- If a contradiction appears, meaning some cell has no possible values left, the guess was wrong. Undo everything since the guess and try the next value.
- If no contradiction appears, continue: choose another cell and repeat.
This is depth-first search with backtracking. It will always find the solution, because in the worst case it tries every possibility.
It is fast because of two refinements.
Guess where there is least choice. Always pick the cell with the fewest remaining candidates. A cell with two candidates gives a one in two chance of being right first time, and a wrong guess there is usually exposed quickly. This is called the minimum remaining values heuristic. Experienced human solvers do the same when they reach for a cell with only two options.
Propagate after every guess. A wrong guess generally produces a contradiction within a few deductions. Vast branches of the search are cut off almost at once, without being explored.
With these two together, even the puzzles specially constructed to be hard for computers need only a few thousand guesses. Ordinary hard puzzles need a handful.
The computer scientist Peter Norvig published a celebrated short essay showing the whole method in about a hundred lines of Python. He ran it on a million randomly generated puzzles. Nearly all were solved in a few hundredths of a second.
A different trick: exact cover
There is another famous approach, due to Donald Knuth.
Sudoku can be restated as follows. There are 729 possible moves: each of 9 digits in each of 81 cells. There are 324 requirements: each cell must be filled, each row must contain each digit, each column likewise and each box likewise. Every move satisfies exactly four requirements. The task is to choose 81 moves that satisfy all 324 requirements exactly once.
This is an exact cover problem. Knuth devised an elegant method for such problems, using linked lists in which removing and restoring items is extremely cheap, and named it dancing links. It is among the fastest ways known to solve sudoku, and the same code solves pentomino tilings and the problem of placing eight queens on a chessboard so that none attacks another.
What computers have found out about sudoku
Fast solvers made it possible to answer questions about the puzzle itself.
How many valid completed grids are there? In 2005 Bertram Felgenhauer and Frazer Jarvis calculated the number: 6,670,903,752,021,072,936,960, or about 6.7 thousand billion billion. If grids that are merely rotations, reflections or relabellings of each other are counted once, there are 5,472,730,538 essentially different ones.
What is the smallest number of clues a puzzle can have? A proper sudoku must have exactly one solution. Many puzzles with 17 clues were known, and nobody had found one with 16. In 2012 a team led by Gary McGuire in Dublin settled it by exhaustive search. Using about seven million hours of processor time, they checked every possibility and showed that no 16-clue puzzle with a unique solution exists. The minimum is 17.
That is a proof by computation: the argument consists of a program and its output. Such proofs were once viewed with suspicion and are now an accepted part of mathematics.
How hard is sudoku, really?
Ordinary nine-by-nine sudoku is a finite problem and easy for a machine. The theoretical interest lies in the generalised version, on grids of n² by n², such as 16 by 16 or 25 by 25.
That general problem was shown in 2003 to be NP-complete. Loosely, this means that a proposed solution is easy to check, but no method is known that finds a solution efficiently in every case as the grid grows, and if one were found it would solve thousands of other hard problems as well. Whether such a method exists is the most famous open question in computer science.
Rating difficulty
Since a computer finds every puzzle easy, how do publishers decide what to label "fiendish"?
Mostly by simulating a human. A grading program attempts the puzzle using only the solving techniques a person would use, ordered from simplest to most advanced, and records which were needed and how often. A puzzle that falls to singles alone is easy. One that requires an X-wing is hard. The number of given clues is a poor guide: some 17-clue puzzles are straightforward and some with 30 are brutal.
By convention, a well-made puzzle can be solved by logic alone, with no guessing. Checking that convention is itself a job for a program.
Why this is more than a pastime
The two ideas, propagation and guided search, are the working core of a whole branch of computing called constraint programming. Swap the digits and boxes for other variables and rules, and the same machinery does this:
- builds school timetables in which no teacher or room is double-booked
- assigns aircraft and crews to flights within legal working hours
- schedules jobs on factory machines
- allocates radio frequencies so that neighbouring transmitters do not interfere
- arranges fixtures for sports leagues
- lays out components on a circuit board
These problems are much larger and messier than sudoku. They usually have many acceptable solutions and the aim is to find a good one. The method is recognisably the same: rule out what is impossible, then choose where the options are fewest and back out of dead ends.
For the pencil solver
Three habits carry over directly from the algorithm.
- Write in the candidates. A program keeps the possibilities for every cell explicitly. Small pencil marks do the same job.
- After every entry, sweep its row, column and box. That is propagation. Most errors come from skipping it.
- When stuck, look at the cells with two candidates. If you must test a value, test it there, in light pencil, and follow the consequences until they confirm or contradict.
Purists consider that last step cheating, since a proper puzzle never requires it. A program is not troubled by that, and it finishes sooner.



