2024 · completed
Counting and solving Sudoku
How many Sudoku grids exist, and can a grid be solved by projecting onto convex sets instead of searching? The counts come from the literature; the TIPE rebuilt the symmetry group behind the second one, then built and compared three solvers: alternating projections, Douglas–Rachford and backtracking.
Context
TIPE research project, preparatory classes, Lycée Sainte-Geneviève.
Stack
- Python
- GAP
- TypeScript (browser demo)
Pipeline
- 1Grid as a 729-dim binary vector
- 2324 linear rules A·v = 1
- 3Projections or Douglas–Rachford reflections
- 4Round and check the grid
- 5Benchmark against backtracking
- complete grids (Felgenhauer & Jarvis, 2005)
- 6.67 × 10²¹
- essentially different grids (Russell & Jarvis, 2006, Burnside’s lemma)
- 5,472,730,538
- minimum clues for a unique solution (McGuire et al., 2012)
- 17
How it works
A grid is a binary vector of 729 entries and the rules are 324 linear equations A·v = 1. The first solver alternates projections between that affine set (via the pseudo-inverse of A) and the cube [0,1]⁷²⁹; it only works when the relaxed problem has no fractional solution besides the true grid. The second, Douglas–Rachford, averages reflections through the exact constraints instead: every cell, row, column and box holds exactly one of each digit, a projection that keeps the largest value of each group of nine.
Results
Alternating projections on the convex relaxation solve all 100 easy and intermediate grids but none of the 32 grids with 17 clues. Douglas–Rachford on the exact constraints solves all 132, the 17-clue grids in a median of 970 iterations, about 100 ms. Backtracking that picks the most constrained cell remains the fastest.
Technical details
Encoding
A grid becomes a vector v ∈ {0,1}⁷²⁹, one coordinate per (cell, digit) pair, at index 81·row + 9·column + digit. The rules are 324 linear equations A v = 1 in four blocks of 81 rows: each cell holds one digit, each row, column and box holds each digit once. A has rank 249, so its pseudo-inverse is computed from an SVD with a relative cutoff.
- Unknowns
- 729 binary variables
- Rule matrix A
- 324 × 729, rank 249
- Affine projection
- x ↦ x − A⁺(A x − 1)
- Box projection
- clip to [0,1], given cells pinned
- Stopping
- rounded grid valid, or 20,000 iterations
- Douglas–Rachford
- 4 copies (cells, rows, columns, boxes), reflect through each, average; each projection keeps the largest of 9
The three solvers, step by step
1. Alternating projections. Start from any vector. Project it onto the affine set A v = 1 (the closest vector that satisfies all 324 rules, computed once with the pseudo-inverse of A), then onto the cube [0,1]⁷²⁹ (clip every coordinate to [0,1], pin the given cells), and repeat. Both sets are convex, so the iterates converge to a point of their intersection. On easy grids that intersection is the solution alone. On hard ones it also contains fractional points, a cell that is half a 3 and half a 7, and the iterates stall there.
2. Douglas–Rachford. Keep four copies of the vector, one per family of rules (cells, rows, columns, boxes). The set of allowed values is now exact and not convex: in every group of nine, one coordinate is 1 and the others 0, so projecting just keeps the largest value of each group. One step reflects each copy through that set (go to the projection, then as far again), reflects the result through the set where the four copies agree (their average), and moves halfway between the start and that double reflection. Fractional points are no longer allowed, and the reflections keep the iterates from settling in a wrong corner.
3. Backtracking. Pick the empty cell with the fewest possible digits, try each one in turn, and continue; when some cell has no possible digit left, undo the last choice. It is an exact search, and choosing the most constrained cell first keeps the tree small.
Solvers compared
The TIPE compared alternating projections, the same with naked-single propagation after each step (every cell left with one candidate is filled), Douglas–Rachford on the exact constraints (after Elser, and Aragón Artacho, Borwein and Tam, 2014), naive backtracking (first empty cell, digits 1 to 9) and backtracking that branches on the cell with the fewest candidates. Simulated annealing was also coded (energy = violated constraints, T₀ = 50,000, cooling 0.999) but left out of the final results.
Douglas–Rachford is what fixes the projections' failure on hard grids. The timings on this page are measured with the page's own TypeScript port of the solvers, on the TIPE's 50 easy, 50 intermediate and 32 seventeen-clue grids. In the original Python runs on 7 seventeen-clue grids, read from the defence charts, projections with naked-single propagation solved 2 of them, in roughly 1,500 and 10,000 s, and fewest-candidates backtracking all 7, in roughly 100 to 3,000 s.
- Cost of one projection step
- ≈ 236,000 multiply-adds (729 × 324 matrix)
- Cost of one backtracking node
- ≈ 20 cell checks
- Projections, easy / intermediate
- 25 / 200 iterations (median), 100% solved
- Projections, 17 clues
- 0 of 32 within 20,000 iterations
- Douglas–Rachford, 17 clues
- 32 of 32, median 970 iterations (≈ 100 ms)
- Naive backtracking, 17 clues
- 20 of 32 within 20 million nodes
- Fewest-candidates backtracking
- all 132 grids, < 0.2 s each
Larger grids
The TIPE code was written for any size N = b². The same methods were run on 16 × 16 and 25 × 25 grids, generated at random with a given share of cells filled (not checked for a unique solution), and on the 132 TIPE grids, changing one thing at a time. Projecting onto the exact constraints instead of the convex relaxation solves nothing; adding the reflections of Douglas–Rachford solves almost everything. It scales better than backtracking on large, sparse grids. Guiding the backtracking with projected values was also tried and dropped: fewer nodes on some grids, failures on others, and every node costs 400 projection steps.
- Grids solved
- projections · exact projections · Douglas–Rachford · backtracking
- 9 × 9, 100 easy and intermediate
- 100 · 0 · 100 · 100
- 9 × 9, 32 with 17 clues
- 0 · 0 · 32 · 32
- 16 × 16, 20 at 55% given
- 2 · 0 · 20 · 20
- 16 × 16, 20 at 45% given
- 0 · 0 · 20 · 18
- 25 × 25, 20 at 60% given
- 5 · 0 · 20 · 20
- 25 × 25, 20 at 50% given
- 0 · 0 · 19 · 5
- Limits
- 20,000 iterations (5,000 above 9 × 9); backtracking on the most constrained cell with naked singles, 200,000 nodes
Counting
The symmetry group of the grid (permutations of rows within a band, of bands, of columns within a stack, of stacks, and transposition) was built in GAP: order 3,359,232, 275 conjugacy classes. Burnside's lemma averages the number of grids fixed by each class, which gives the 5,472,730,538 essentially different grids of Russell and Jarvis.
- Group order
- 3,359,232
- Conjugacy classes
- 275
- Tool
- GAP