Title: Solving%20Sudoku
1(No Transcript)
2Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 5 6 9 7 8 4 4
2 5
3Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 5 6 9 7 8 4 4
2 5
What number must go here?
4Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 5 6 9 7 8 4 4
2 5
1, as 2 and 3 already appear in this column.
1
5Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 1 5 6 9 7 8 4 4
2 5
6Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 1 5 6 9 7 8 4 4
2 5
3
7Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 1 5 3 6 9 7 8 4 4
2 5
8Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 1 5 3 6 9 7 8 4 4
2 5
2
9Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 1 3 8 5 7 6
1 3 9 8 1 2 5 7 3 1
8 9 8 2 1 5 2 3 6 9 7 8 4 4
2 5
And so on
10Example
Fill in the grid so that every row, column and
box contains each of the numbers 1 to 9
2 4 9 5 7 1 6 3 8 8 6 1 4 3 2 9 7 5 5 7 3 9 8 6 1
4 2 7 2 5 6 9 8 4 1 3 6 9 8 1 4 3 2 5 7 3 1 4 7 2
5 8 6 9 9 3 7 8 1 4 2 5 6 1 5 2 3 6 9 7 8 4 4 8 6
2 5 7 3 9 1
The unique solution for this easy puzzle.
11This Talk
- We show how to develop a program that can solve
any Sudoku puzzle in an instant - Start with a simple but impractical program,
which is improved in a series of steps - Emphasis on pictures rather than code, plus some
lessons about algorithm design.
12Representing a Grid
type Grid Matrix Char type Matrix a
Row a type Row a a
A grid is essentially a list of lists, but
matrices and rows will be useful later on.
13Extracting Rows
rows Matrix a ? Row a rows m m
14 Columns
cols Matrix a ? Row a cols m transpose m
15 And Boxes
boxs Matrix a ? Row a boxs m ltomittedgt
16Validity Checking
Let us say that a grid is valid if it has no
duplicate entries in any row, column or box
valid Grid ? Bool valid g all nodups (rows
g) ? all nodups (cols g) ?
all nodups (boxs g)
A direct implementation, without concern for
efficiency.
17Making Choices
Replace each blank square in a grid by all
possible numbers 1 to 9 for that square
choices Grid ? Matrix Char
18Collapsing Choices
Transform a matrix of lists into a list of
matrices by considering all combinations of
choices
collapse Matrix a ? Matrix a
19A Brute Force Solver
solve Grid ? Grid solve filter valid .
collapse . choices
Consider all possible choices for each blank
square, collapse the resulting matrix, then
filter out the valid grids.
20Does It Work?
The easy example has 51 blank squares, resulting
in 951 grids to consider, which is a huge number
4638397686588101979328150167890591454318967698009
Simple, but impractical!
gt solve easy ERROR out of memory
21Reducing The Search Space
- Many choices that are considered will conflict
with entries provided in the initial grid - For example, an initial entry of 1 precludes
another 1 in the same row, column or box - Pruning such invalid choices before collapsing
will considerably reduce the search space.
22Pruning
Remove all choices that occur as single entries
in the corresponding row, column or box
prune Matrix Char ? Matrix Char
23And Again
Pruning may leave new single entries, so it makes
sense to iterate the pruning process
24And Again
Pruning may leave new single entries, so it makes
sense to iterate the pruning process
25And Again
Pruning may leave new single entries, so it makes
sense to iterate the pruning process
26And Again
Pruning may leave new single entries, so it makes
sense to iterate the pruning process
We have now reached a fixpoint of the pruning
function.
27An Improved Solver
solve Grid ? Grid solve filter valid
. collapse . fix prune . choices
For the easy example, the pruning process alone
is enough to completely solve the puzzle
Terminates instantly!
gt solve easy
28But
For a gentle example, pruning leaves around 381
grids to consider, which is still a huge number
443426488243037769948249630619149892803
No solution after two hours - we need to think
further!
gt solve' gentle
29Reducing The Search Space
- After pruning there may still be many choices
that can never lead to a solution - But such bad choices will be duplicated many
times during the collapsing process - Discarding these bad choices is the key to
obtaining an efficient Sudoku solver.
30Blocked Matrices
Let us say that a matrix is blocked if some
square has no choices left, or if some row,
column, or box has a duplicated single choice
Key idea - a blocked matrix can never lead to a
solution.
31Expanding One Choice
Transform a matrix of lists into a list of
matrices by expanding the first square with
choices
expand Matrix a ? Matrix a
32Our Final Solver
solve Grid ? Grid solve search .
prune . choices
search Matrix Char ? Grid search m
blocked m complete m collapse m
otherwise g m' ? expand m
, g ? search (prune m')
33The Result
This program can solve any newspaper Sudoku
puzzle in an instant. My addiction is cured!!