solving sudoku graham hutton university of nottingham (with thanks to richard bird)

36
SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

Upload: blake-watson

Post on 23-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

SOLVING SUDOKU

Graham HuttonUniversity of Nottingham

(with thanks to Richard Bird)

Page 2: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

2

What is Sudoku?

A simple but addictive puzzle, invented in the USA in 1979 and called Number Place;

Became popular in Japan in 1986, where it was renamed Sudoku (~ “single number”);

First appeared in UK newspapers in 2004, and became an international craze in 2005.

Page 3: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

3

Example

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 73 1 8 9 8 2 5 6 9 7 8 44 2 5

Page 4: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

4

Example

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 73 1 8 9 8 2 5 6 9 7 8 44 2 5

What number must go

here?

Page 5: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

5

Example

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 73 1 8 9 8 2 5 6 9 7 8 44 2 5

1, as 2 and 3 already

appear in this column.

1

Page 6: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

6

Example

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 73 1 8 9 8 2 1 5 6 9 7 8 44 2 5

Page 7: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

7

Example

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 73 1 8 9 8 2 1 5 6 9 7 8 44 2 5

3

Page 8: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

8

Example

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 73 1 8 9 8 2 1 5 3 6 9 7 8 44 2 5

Page 9: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

9

Example

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 73 1 8 9 8 2 1 5 3 6 9 7 8 44 2 5

2

Page 10: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

10

Example

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 73 1 8 9 8 2 1 5 2 3 6 9 7 8 44 2 5

And so on…

Page 11: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

11

Example

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 88 6 1 4 3 2 9 7 55 7 3 9 8 6 1 4 27 2 5 6 9 8 4 1 36 9 8 1 4 3 2 5 73 1 4 7 2 5 8 6 99 3 7 8 1 4 2 5 61 5 2 3 6 9 7 8 44 8 6 2 5 7 3 9 1

The unique solution for this easy puzzle.

Page 12: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

12

This 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.

Page 13: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

13

Representing 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.

Page 14: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

14

Examples

easy :: Grideasy = [ "2 1 38" , " 5" , " 7 6 " , " 13 " , " 981 257" , "31 8 " , "9 8 2 " , " 5 69784" , "4 25 " ]

empty :: Gridempty = replicate 9 (replicate 9 ' ')

Page 15: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

15

Extracting Rows

rows :: Matrix a [Row a]rows m = m

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Page 16: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

16

… Columns

cols :: Matrix a [Row a]cols m = transpose m

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

1 5 9 13

2 6 10 14

3 7 11 15

4 8 12 16

Page 17: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

17

… And Boxes

boxs :: Matrix a [Row a]boxs m = <omitted>

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

1 2 5 6

3 4 7 8

9 10 13 14

11 12 15 16

Page 18: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

18

Validity Checking

Let us say that a grid is valid if it has no duplicate entries in any row, column or box:

valid :: Grid Boolvalid g = all nodups (rows g) all nodups (cols g) all nodups (boxs g)

A direct implementation, without concern for

efficiency.

Page 19: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

19

Making Choices

Replace each blank square in a grid by all possible numbers 1 to 9 for that square:

choices :: Grid Matrix [Char]

1 2 34 5 6 37 8 9 1 2 3 4 4 5 6 7 8 9

3 4

Page 20: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

20

Collapsing Choices

Transform a matrix of lists into a list of matrices by considering all combinations of choices:

collapse :: Matrix [a] [Matrix a]

1 34 1

2 34 2

2 34 1

1 34 2

1 2 3

4 1 2

Page 21: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

21

A 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.

Page 22: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

22

Does It Work?

> solve easyERROR: out of memory

Simple, but impractical

!

The easy example has 51 blank squares, resulting in 951 grids to consider, which is a huge number:

4638397686588101979328150167890591454318967698009

Page 23: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

23

Reducing 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.

Page 24: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

24

Pruning

Remove all choices that occur as single entries in the corresponding row, column or box:

prune :: Matrix [Char] Matrix [Char]

1 1 2 4

1 3 3 4

1 2 4

3 3 4

Page 25: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

25

And Again

Pruning may leave new single entries, so it makes sense to iterate the pruning process:

1 2 4

3 3 4

Page 26: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

26

And Again

Pruning may leave new single entries, so it makes sense to iterate the pruning process:

1 2 4

3 4

Page 27: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

27

And Again

Pruning may leave new single entries, so it makes sense to iterate the pruning process:

1 2

3 4

Page 28: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

28

And Again

Pruning may leave new single entries, so it makes sense to iterate the pruning process:

1 2

3 4We have now

reached a fixpoint of the pruning

function.

Page 29: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

29

An 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:

> solve’ easyTerminat

es instantly!

Page 30: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

30

But…

> solve' gentle

No solution after two hours - we need to think

further!

For a gentle example, pruning leaves around 381 grids to consider, which is still a huge number:

443426488243037769948249630619149892803

Page 31: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

31

Reducing 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.

Page 32: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

32

Blocked 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.

1 1 2 1

3 4 3

Page 33: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

33

Expanding One Choice

Transform a matrix of lists into a list of matrices by expanding the first square with choices:

expand :: Matrix [a] [Matrix [a]]

1 2 3

4 1 2

2 3

4 1 2

1 3

4 1 2

Page 34: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

34

Our 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')]

Page 35: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

35

Notes

Using fix prune rather than prune makes the program run slower in this case;

No need to filter out valid grids, because they are guaranteed to be valid by construction;

This program can now solve any newspaper Sudoku puzzle in an instant!

Page 36: SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)

36

The Result This program has

saved my life - my Sudoku addiction is

finally cured!!

Subliminal Message

Haskell is the world's greatest programming

language.