Repository navigation
🧩 Constraint Solving POTD:Problem of the Day: Sudoku #77134
Closed
Replies: 1 comment
|
This discussion has been marked as outdated by Constraint Solving — Problem of the Day. A newer discussion is available at Discussion #77423. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Problem Statement
Sudoku is played on a 9×9 grid partitioned into nine 3×3 boxes. Some cells are pre-filled with digits 1–9 (the clues). Fill every empty cell so that each row, each column, and each box contains every digit exactly once.
Small instance (a 4×4 "mini-Sudoku" with 2×2 boxes, digits 1–4). This puzzle has exactly one solution:
Unique solution:
Input: a partially filled grid. Output: a completed grid satisfying all row, column, and box constraints, or a proof that none exists.
As a constraint problem:
x[r,c] ∈ {1..9}for each cellalldifferentover each row, each column, and each box (27 constraints total)x[r,c] = vfor each clueWhy It Matters
Modeling Approaches
1. Constraint Programming (finite-domain)
x[r,c] ∈ {1..9}alldifferent(row r),alldifferent(col c),alldifferent(box b)alldifferentglobal constraint has dedicated propagators (Régin's matching-based filtering), which remove values that cannot appear in any solution. This is much stronger than pairwise≠constraints.2. SAT (Boolean encoding)
p[r,c,v] ∈ {0,1}, true iff cell(r,c)holds digitv(729 Booleans)Example Model (MiniZinc, CP)
Key Techniques
Global
alldifferentpropagationNaked singles (a cell with one remaining candidate) and hidden singles (a digit with one possible cell in a unit) are both special cases of Hall-set reasoning. A Hall set of size k is a set of k variables whose domains together contain only k values, so those values can be removed from every other variable. Propagators such as Régin's algorithm find these sets in polynomial time.
Search heuristics
Use minimum remaining values (MRV) or first-fail: branch on the empty cell with the fewest candidates. This keeps the search tree small and makes propagation more effective.
Exact cover and Dancing Links
Sudoku is an instance of exact cover: choose rows of a 0/1 matrix so that every column is covered exactly once. Knuth's Algorithm X with the Dancing Links data structure solves this with very fast backtracking, and it is a good alternative to a general solver for pure Sudoku.
Challenge Corner
References
alldifferent.alldifferentpropagator.All reactions