The mathematics of Sudoku
A Sudoku grid is a small mathematical object, and a good deal has been proved about it. Here are the main results, simplest first, each with its proof: in full when it fits in a few lines, its idea when it took a long paper or a computer.
Direct equivalence
Sudoku is a graph colouring problem
Theorem. Solving a Sudoku is colouring a graph of 81 vertices with 9 colours, keeping the colours its clues give. Each vertex has exactly 20 neighbours.
Take a vertex for each cell, and join two vertices when their cells share a row, a column or a box. The nine digits become nine colours, and colouring the graph means giving each vertex a colour so that two neighbours never share one.
Proof
From a solution to a colouring: give each vertex the colour of its cell's digit. Two neighbouring cells share a row, a column or a box, so they hold different digits, and their vertices get different colours.
From a colouring to a solution: the nine cells of a row are all neighbours of one another, so they need nine different colours. With only nine colours, each appears exactly once in the row. The same holds for every column and every box, so reading the colours back as digits gives a solution.
Counting the neighbours
A cell has 8 other cells in its row, 8 in its column and 8 in its box. But 2 cells of its box are also in its row, and 2 in its column: they were counted twice.
8 + 8 + 8 − 2 − 2 = 20
So the graph has 81 × 20 ÷ 2 = 810 edges, each counted from both of its ends. And as a row needs nine colours and nine are enough, the graph needs exactly nine: its chromatic number is 9.
In My Sudoku
Seen this way, a Naked Single is a vertex whose 20 neighbours already use 8 of the 9 colours: only one is left for it.
Source: Wikipedia: Mathematics of Sudoku.
Logical encoding
Sudoku is an exact cover problem
Theorem. A Sudoku solution is a choice of 81 rows, among the 729 rows of a 0–1 matrix with 324 columns, that covers every column exactly once.
Every move in Sudoku places a digit d in a cell (r, c), and each placement meets four constraints at once:
- cell: the cell (r, c) holds a digit;
- row: digit d appears in row r;
- column: digit d appears in column c;
- box: digit d appears in the box of (r, c).
Each kind comes 81 times: 81 cells, and 9 digits for each of the 9 rows, the 9 columns and the 9 boxes.
4 × 81 = 324 constraints · 81 × 9 = 729 placements
Write a matrix with a row for each placement and a column for each constraint, with a 1 where the placement meets the constraint. Each row has exactly four 1s. Placing 7 in R2C5, for example, covers four columns:
- cell R2C5
- a 7 in row 2
- a 7 in column 5
- a 7 in box 2
Proof
An exact cover picks rows so that each column has exactly one 1 among them. The 81 cell constraints give each cell exactly one digit; the row, column and box constraints put each digit exactly once in each row, column and box. That is a Sudoku solution, and conversely the 81 placements of a solution meet each constraint once. The clues are rows picked in advance.
exact cover ⟺ solution: 81 rows out of 729
In My Sudoku
Many solvers work on this form, Donald Knuth's Algorithm X and its Dancing Links among them: they always branch on the constraint with the fewest options left. My Sudoku's own search does the same with the cell constraints: it tries first the cell with the fewest possible digits.
The matrix also shows that the two singles are one idea: a Naked Single is a cell constraint, and a Hidden Single a row, column or box constraint, with a single placement left. Either way, a column with a single 1.
Sources: Donald E. Knuth, Dancing Links (2000), Wikipedia: Mathematics of Sudoku.
Computer count, with symmetries
There are 6,670,903,752,021,072,936,960 grids
Theorem. There are exactly 6,670,903,752,021,072,936,960 ways to fill a 9 × 9 grid by the rules, about 6.67 × 10²¹. Setting symmetries aside, 5,472,730,538 of them are essentially different.
Bertram Felgenhauer and Frazer Jarvis counted them in 2005; Ed Russell and Frazer Jarvis counted the essentially different ones soon after.
Why not (9!)⁹
Each row can be put in order in 9! = 362,880 ways, so one might expect (9!)⁹ grids, about 1.1 × 10⁵⁰. But that counts any nine rows stacked up, with digits repeated in columns and boxes: the constraints are not independent, and the true number is far smaller.
A part you can check: a multiple of 9!
Relabel the digits of a grid: every 1 becomes a 5, every 5 a 2, and so on, by any of the 9! ways to shuffle the nine digits. Each row, column and box still holds each digit once, so the result is a grid too. Two different relabellings of a grid give two different grids, and relabelling a relabelled grid gives one of the same family. So the grids fall into families of exactly 9! grids each, and their number is a multiple of 9!:
6,670,903,752,021,072,936,960 = 9! × 18,383,222,420,692,992
How the count was made
Felgenhauer and Jarvis fixed the first box as 1 to 9 in reading order, which accounts for the 9!, then counted the ways to complete the first three rows: 2,612,736, or 56 × 6⁶. Many of these are alike, one turning into another when columns are swapped or digits relabelled, and can be completed in as many ways; grouping them, they had a computer count, for each group, the ways to fill the last six rows. The total factors as follows, its last factor being a prime number:
9! × 72² × 2⁷ × 27,704,267,971
Essentially different grids
Mirroring a grid along its diagonal, swapping two bands or two stacks of boxes, two rows within a band or two columns within a stack, or relabelling its digits gives a grid that is really the same. These moves form a group of 9! × 3,359,232 elements. Dividing the count by that gives about 5,472,448,000: close, but not right. A few grids are symmetric, left unchanged by some move, so their family is smaller than the group, and the division undercounts them. Burnside's lemma, which counts the grids each move leaves unchanged, puts it right:
5,472,730,538 essentially different grids
The same count by hand, for 4 × 4
| 1 | 2 | 3 | 4 |
| 3 | 4 | 1 | 2 |
| 2 | 1 | 4 | 3 |
| 4 | 3 | 2 | 1 |
With four digits and 2 × 2 boxes, the whole count fits in a few lines. Fill the first box: 4! = 24 ways, and by relabelling the digits it can be taken to be 1 2 / 3 4. The rest of row 1 then holds 3 and 4, the rest of row 2 holds 1 and 2, in either order: 4 ways. The same goes for the rest of columns 1 and 2: 4 ways. Of these 16 combinations, 12 leave exactly one way to fill the last box, and 4 leave none:
24 × 12 = 288 grids
Among the Latin squares
A grid is a Latin square of order 9, each digit once in every row and every column, with the box rule on top. There are 5,524,751,496,156,892,842,531,225,600 Latin squares of order 9: the boxes keep only about one in 828,000.
Sources: Frazer Jarvis, Sudoku enumeration problems, OEIS A002860, Wikipedia: Mathematics of Sudoku.
Proof in a few lines
The clues show at least eight different digits
Theorem. In a Sudoku with a single solution, at least eight of the nine digits appear among the clues.
Eight different digits, that is, not eight clues.
Proof
Suppose two digits, say 4 and 7, appear in none of the clues. Take a solution and swap 4 and 7 throughout.
Each row held one 4 and one 7, and still does: they have only traded places. The same goes for each column and each box, so the new grid is a solution too. It differs from the first wherever there was a 4 or a 7, and it keeps every clue, since none is a 4 or a 7. The puzzle would have two solutions. So at most one digit can be missing from the clues.
On the limit
The 17-clue puzzle further down sits right on it: its clues show eight digits, and no 6.
On an N × N grid, the same argument requires N − 1 different symbols among the clues.
Source: Wikipedia: Mathematics of Sudoku.
Proof by construction
Four cells can be enough to spoil a puzzle
Theorem. If four cells at the corners of a rectangle, on two rows, two columns and two boxes, hold a b / b a in a solution and none of them is a clue, the puzzle has a second solution.
Proof
| a | b |
| b | a |
| b | a |
| a | b |
Swap a and b in the four cells. Each of the two rows still holds one a and one b, and so does each of the two columns. The four cells lie two by two in two boxes, each pair holding one a and one b, so the boxes are still right after the swap. Nothing else changes: the new grid is a solution too, and it keeps every clue, since none of the four cells is one.
Unavoidable sets
Such a set of cells, at least one of which must be a clue, is called an unavoidable set. Here a puzzle with 77 clues, only four empty cells, has two solutions: the number of clues says little, their places say a lot.
Never fewer than four cells
Conversely, two different solutions of a puzzle always differ in at least four cells. So a puzzle with 78 clues or more never has two solutions, and the rectangle is the smallest ambiguity there is.
Look at the cells where the two solutions differ. A row cannot hold just one of them: its eight other cells would hold the same eight digits in both solutions, leaving the same ninth digit for the last. So a row, or a column, that holds one of these cells holds at least two. Take one, x. Its row holds another, y; its column another, z; and the row of z another, w. This w is not x, which is not in the row of z, nor y, which is in the row of x. That makes four different cells.
In My Sudoku
The Unique Rectangle technique turns this around: as a proper puzzle has only one solution, such a rectangle can never come about, and the candidates that would make one can be removed.
Source: Wikipedia: Mathematics of Sudoku.
Exhaustive computer proof
A puzzle needs at least 17 clues
Theorem. A 9 × 9 Sudoku with a single solution has at least 17 clues. Puzzles with 17 exist; none with 16 does.
Gary McGuire, Bastian Tugemann and Gilles Civario proved it in 2012, by an exhaustive computer search. Tens of thousands of 17-clue puzzles are known.
Idea of the proof
The proof rests on the unavoidable sets met above: sets of cells of a grid that the clues must hit, or their digits could be rearranged into a second solution. Clues that determine a grid must meet every one of its unavoidable sets: they form a hitting set of the family. The search went as follows:
- for each of the 5,472,730,538 essentially different grids, find many of its unavoidable sets;
- list every set of 16 cells that hits them all, the hard part, for which the authors designed a new algorithm;
- check whether the grid's digits in those 16 cells determine it;
- they never did.
It is a real proof, but one only a computer can carry out: no one could check it by hand.
The same proof in miniature
On 4 × 4 grids the same search is tiny. Among the 288 grids, none is determined by any 3 of its cells, while some are by 4: the minimum there is 4, which a computer checks in a moment by trying them all.
Minimal is not minimum
A puzzle is minimal when removing any one of its clues lets in a second solution, and minimum when no puzzle has fewer clues: 17. A puzzle can be minimal with 25 clues, none of which can go, while other puzzles make do with fewer.
Apart from those at the Very easy level, My Sudoku's puzzles are minimal: it fills a grid at random, then removes the clues one by one in random order, putting back any whose removal lets in a second solution. Its puzzles usually end up with 22 to 27 clues.
Sources: Gary McGuire, Bastian Tugemann, Gilles Civario, There is no 16-Clue Sudoku (2012), Wikipedia: Mathematics of Sudoku.
Proof by reduction
Sudoku is NP-complete
Theorem. Deciding whether a partly filled Sudoku of n² × n² cells, with boxes of n × n, has a solution is an NP-complete problem.
Takayuki Yato and Takahiro Seta proved it in 2003, starting from Latin squares. A problem is NP-complete when it has two properties: it is in NP, meaning a proposed solution can be checked quickly, and it is NP-hard, meaning every problem in NP can be translated into it in polynomial time. The proof has two parts.
Part A: Sudoku is in NP
Take a filled grid of side N = n². To check it, go through its N rows, N columns and N boxes, and compare it with the clues. Checking that a row holds each symbol from 1 to N once takes about N operations, so the rows take about N × N, and so do the columns and the boxes:
3 × N × O(N) = O(N²)
That is a number of operations proportional to the N² cells of the grid. Finding a solution may be hard; checking one is quick.
Part B: Sudoku is NP-hard
A Latin square of order N fills an N × N grid with N symbols, each once in every row and every column: the rules of Sudoku without the boxes.
| 1 | 2 | 3 |
| 2 | 3 | 1 |
| 3 | 1 | 2 |
Charles Colbourn proved in 1984 that deciding whether a partly filled Latin square can be completed is NP-complete. Yato and Seta turn any partly filled Latin square L into a partly filled Sudoku S(L), in polynomial time: the clues of S(L) are laid out so that its free cells can only be filled the way L can be completed. Each completion of L gives exactly one solution of S(L), and the other way round (a parsimonious reduction):
L can be completed ⟺ S(L) has a solution
A fast method for Sudoku would therefore give a fast method for Latin squares. As completing a Latin square is NP-complete, Sudoku is NP-hard; with part A, NP-complete.
What it does not mean
It does not mean that 9 × 9 puzzles are hard: their size is fixed, and a computer solves any of them in a moment. NP-completeness is about the whole family, as n grows: no method is known that solves every grid in a time polynomial in its size, and finding one would prove that P = NP, one of the great open questions of mathematics, with a million-dollar prize from the Clay Institute.
A second solution: ASP-complete
Yato and Seta prove more: given a Sudoku and one of its solutions, deciding whether it has another is just as hard. The problem is ASP-complete (for Another Solution Problem). It is the question every puzzle maker faces, since a proper puzzle has a single solution.
So My Sudoku's generator leaves nothing to chance: after each clue it removes, a search looks for a second solution, stopping at the first it finds, and if it finds one, the clue goes back.
Sources: Takayuki Yato, Takahiro Seta, Complexity and Completeness of Finding Another Solution and Its Application to Puzzles, IEICE Transactions on Fundamentals E86-A (2003), Charles J. Colbourn, The complexity of completing partial Latin squares (1984), Wikipedia: Mathematics of Sudoku.
In short
| Result | Kind of proof |
|---|---|
| Sudoku is a graph colouring problem | Direct equivalence |
| Sudoku is an exact cover problem | Logical encoding |
| There are 6,670,903,752,021,072,936,960 grids | Computer count, with symmetries |
| The clues show at least eight different digits | Proof in a few lines |
| Four cells can be enough to spoil a puzzle | Proof by construction |
| A puzzle needs at least 17 clues | Exhaustive computer proof |
| Sudoku is NP-complete | Proof by reduction |