🧩 Constraint Solving POTD:Problem of the Day: Sudoku Solving via Constraint Propagation #53356
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 #53648. |
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 a classic puzzle that challenges solvers to fill a 9×9 grid with digits 1–9 such that:
Small Instance (4×4 Sudoku):
Fill the 4×4 grid using digits {1, 2, 3, 4} under the same rules.
Input: A partially filled grid (clues).
Output: A complete grid satisfying all constraints, or "UNSAT" if no solution exists.
Why It Matters
Mathematical recreation & education: Sudoku demonstrates the power of declarative constraint solving—modeling a problem is often easier than writing search algorithms by hand.
Propagation algorithms in practice: Real-world scheduling, resource allocation, and product configuration problems use the same constraint propagation and search techniques that solve Sudoku efficiently.
Solver benchmarking: Sudoku instances are used to evaluate propagation strength, variable ordering heuristics, and branching strategies in constraint solvers.
Modeling Approaches
Approach 1: Constraint Programming (CP)
Decision variables:
grid[i][j] ∈ {1..9}for each cell(i,j)in the 9×9 grid.Constraints:
grid[i][0], ..., grid[i][8]) for each rowigrid[0][j], ..., grid[8][j]) for each columnjTrade-offs:
AllDifferentglobal constraint prune the search space aggressively.Approach 2: SAT Encoding
Decision variables:
For each cell
(i,j)and digitd, introduce boolean variablex_{i,j,d}(true if cell(i,j)contains digitd).Constraints:
∑_d x_{i,j,d} = 1(or via "at-most-one" clauses).¬(x_{i,j,d} ∧ x_{i,k,d})for allj ≠ k(no two cells in rowicontain digitd).Trade-offs:
Example CP Model (MiniZinc)
Key Techniques
1. Arc Consistency (AC-3 Algorithm)
Before searching, repeatedly remove values from cell domains that cannot participate in any valid solution. For Sudoku, if a row already contains the digit 5, remove 5 from all other cells in that row. Repeat until no more reductions occur. This "constraint propagation" phase often solves easy to moderate puzzles without search.
2. Global Constraint:
AllDifferentThe
AllDifferentconstraint on a set of variables enforces that all take distinct values. Modern solvers use:This is far more powerful than propagating pairwise inequality constraints.
3. Search Strategy: Minimum Remaining Values (MRV)
When branching, choose the cell with the fewest possible values. Sudoku solvers often branch on the most constrained cells first, dramatically reducing the search tree. Combine MRV with a good value ordering (e.g., try digits that appear least in neighboring boxes first).
Challenge Corner
1. Symmetry Breaking:
Sudoku puzzles have rotations, reflections, and digit permutations that preserve solutions. Can you add constraints to break these symmetries and speed up solving?
2. Minimal Puzzles:
A valid Sudoku puzzle has exactly one solution and uses the minimum number of clues (typically 17 for 9×9). Given a completed grid, how would you find or verify a minimal puzzle?
3. Hardness Classification:
What properties of clue placement (distribution across boxes, row/column density) correlate with puzzle difficulty? Can you design a heuristic to predict which instances will require deep search?
References
Rossi, F., van Beek, P., & Walsh, T. (eds.). Handbook of Constraint Programming. Elsevier, 2006.
→ Chapter on arc consistency algorithms and global constraints.
Stuckey, P. J. et al. The MiniZinc Handbook. (www.minizinc.org/redacted)
→ MiniZinc models and solver integration for puzzles.
Gomes, C. P., Sellmann, M. "Streamlined constraint reasoning." In Recent Advances in Constraints, 2004.
→ Techniques for boosting propagation in puzzles.
Sudoku Wikipedia: (en.wikipedia.org/redacted)
→ History, variants, and mathematical theory.
Next time: Scheduling problems and the power of job-shop formulations.
All reactions