solving sudoku with dancing links - beezer's home...

43
Solving Sudoku with Dancing Links Rob Beezer [email protected] Department of Mathematics and Computer Science University of Puget Sound Tacoma, Washington USA African Institute for Mathematical Sciences October 25, 2010 Available at http://buzzard.pugetsound.edu/talks.html

Upload: truongnhan

Post on 22-Feb-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Solving Sudoku with Dancing Links

Rob [email protected]

Department of Mathematics and Computer ScienceUniversity of Puget SoundTacoma, Washington USA

African Institute for Mathematical SciencesOctober 25, 2010

Available at http://buzzard.pugetsound.edu/talks.html

Example: Combinatorial Enumeration

Create all permutations of the set {0, 1, 2, 3}

Simple example to demonstrate key ideas

Creation, cardinality, existence?

There are more efficient methods for this example

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 2 / 37

Brute Force Backtracking

BLACK = Forward BLUE = Solution RED = Backtrack

root00 000 10 1 00 10 1 10 10 1 20 1 2 00 1 20 1 2 1

0 1 20 1 2 20 1 20 1 2 30 1 20 10 1 30 1 3 00 1 30 1 3 10 1 30 1 3 20 1 3

0 1 3 30 1 30 100 20 2 00 20 2 10 2 1 00 2 10 2 1 10 2 10 2 1 2

0 2 10 2 1 30 2 10 20 2 20 20 2 30 2 3 00 2 30 2 3 10 2 30 2 3 20 2 3

0 2 3 30 2 30 200 30 3 00 30 3 10 3 1 00 3 10 3 1 10 3 10 3 1 2

0 3 10 3 1 30 3 10 30 3 20 3 2 00 3 20 3 2 10 3 20 3 2 20 3 20 3 2 30 3 2

0 30 3 30 30root11 01 0 01 01 0 11 01 0 21 0 2 0

1 0 21 0 2 11 0 21 0 2 21 0 21 0 2 31 0 2...............

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 3 / 37

A Better Idea

Avoid the really silly situations, such as: 1 0 1

“Remember” that a symbol has been used already

Additional data structure: track “available” symbols

Critical: must maintain this extra data properly

(Note recursive nature of backtracking)

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 4 / 37

Sophisticated Backtracking

BLACK = Forward BLUE = Solution RED = Backtrack

root {0,1,2,3}0 {1,2,3}0 1 {2,3}0 1 2 {3}0 1 2 3 {}0 1 2 {3}0 1 {2,3}0 1 3 {2}0 1 3 2 {}0 1 3 {2}0 1 {2,3}0 {1,2,3}0 2 {1,3}0 2 1 {3}

0 2 1 3 {}0 2 1 {3}0 2 {1,3}0 2 3 {1}0 2 3 1 {}0 2 3 {1}0 2 {1,3}0 {1,2,3}0 3 {1,2}0 3 1 {2}0 3 1 2 {}0 3 1 {2}0 3 {1,2}0 3 2 {1}

0 3 2 1 {}0 3 2 {1}0 3 {1,2}0 {1,2,3}root {0,1,2,3}1 {0,2,3}1 0 {2,3}1 0 2 {3}1 0 2 3 {}1 0 2 {3}1 0 {2,3}1 0 3 {2}1 0 3 2 {}1 0 3 {2}

1 0 {2,3}1 {0,2,3}1 2 {0,3}1 2 0 {3}1 2 0 3 {}1 2 0 {3}1 2 {0,3}1 2 3 {0}1 2 3 0 {}1 2 3 {0}1 2 {0,3}1 {0,2,3}1 3 {0,2}1 3 0 {2}

1 3 0 2 {}1 3 0 {2}1 3 {0,2}1 3 2 {0}1 3 2 0 {}1 3 2 {0}1 3 {0,2}1 {0,2,3}root {0,1,2,3}2 {0,1,3}.........

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 5 / 37

Depth-First Search Tree

root

0 1 2 3

01 02 03

012 013 021 023 031 032

0123 0134 0213 0231 0312 0321

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 6 / 37

Algorithm

n=4a v a i l a b l e =[ True ]∗ n # [ True , True , True , True ]perm =[0]∗n # [0 , 0 , 0 , 0 ]

def bt ( l e v e l ) :f o r x i n ra ng e ( n ) :

i f a v a i l a b l e [ x ] :a v a i l a b l e [ x ]= F a l s eperm [ l e v e l ]=xi f l e v e l +1 == n :

p r i n t permbt ( l e v e l +1)a v a i l a b l e [ x ]= True

bt ( 0 )

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 7 / 37

Sudoku Basics

n2 symbols

n2 × n2 grid

n2 subgrids (“boxes”) each n × n

Classic Sudoku is n = 3

Each symbol once and only once in each row

Each symbol once and only once in each column

Each symbol once and only once in each box

The grid begins partially completed

A Sudoku puzzle should have a unique completion

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 8 / 37

Example

5 8 4 95 3

6 7 3 1

1 52 8

1 8

7 4 1 53 2

4 9 5 3

=⇒

5 1 3 6 8 7 2 4 98 4 9 5 2 1 6 3 72 6 7 3 4 9 5 8 1

1 5 8 4 6 3 9 7 29 7 4 2 1 8 3 6 53 2 6 7 9 5 4 1 8

7 8 2 9 3 4 1 5 66 3 5 1 7 2 8 9 44 9 1 8 5 6 7 2 3

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 9 / 37

Sudoku via Backtracking

Fill in first row, left to right, then second row, . . .

For each blank cell, maintain possible new entries

As entries are attempted, update possibilities

If a cell has just one possibility, it is forced

Lots to keep track of, especially at backtrack step

Alternate Title: “Why I Don’t Do Sudoku”

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 10 / 37

Sudoku via Backtracking

Fill in first row, left to right, then second row, . . .

For each blank cell, maintain possible new entries

As entries are attempted, update possibilities

If a cell has just one possibility, it is forced

Lots to keep track of, especially at backtrack step

Alternate Title: “Why I Don’t Do Sudoku”

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 10 / 37

Top row, second column: possibilities?

5 8 4 95 3

6 7 3 1

1 52 8

1 8

7 4 1 53 2

4 9 5 3

{1, 2, 3, 6, 7}

{1, 2, 4, 7, 8} {1, 2, 4, 7, 8} ∩ {1, 2, 3, 6, 7} = {1, 2, 7}

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 11 / 37

Suppose we try 2 first.Seventh row, second column: possibilities?

5 2 8 4 95 3

6 7 3 1

1 52 8

1 8

7 4 1 53 2

4 9 5 3

{2, 3, 6, 8, 9}

{1, 4, 7, 8} {1, 4, 7, 8} ∩ {2, 3, 6, 8, 9} = {8}

One choice!This may lead to other singletons in the affected row or column.

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 12 / 37

Exact Cover Problem

Given: matrix of 0’s and 1’s

Find: subset of rows

Condition: rows sum to exactly the all-1’s vector

Amenable to backtracking (on columns, not rows!)

Example: (Knuth)

0 0 1 0 1 1 01 0 0 1 0 0 10 1 1 0 0 1 01 0 0 1 0 0 00 1 0 0 0 0 10 0 0 1 1 0 1

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 13 / 37

Solution

Select rows 1, 4 and 5:

⇒ 0 0 1 0 1 1 01 0 0 1 0 0 10 1 1 0 0 1 0

⇒ 1 0 0 1 0 0 0⇒ 0 1 0 0 0 0 1

0 0 0 1 1 0 1

1 1 1 1 1 1 1

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 14 / 37

Sudoku as an Exact Cover Problem

Matrix rows are per symbol, per grid location (n2 ×(n2 × n2

)= n6)

Matrix columns are conditions: (3n4 total)I Per symbol, per grid row: symbol in row (n2 × n2)I Per symbol, per grid column: symbol in column (n2 × n2)I Per symbol, per grid box: symbol in box (n2 × n2)

Place a 1 in entry of the matrixif and only if

matrix row describes symbol placement satisfying matrix column condition

Example:Consider matrix row that places a 7 in grid at row 4, column 9

I 1 in matrix column for “7 in grid row 4”I 1 in matrix column for “7 in grid column 9”I 1 in matrix column for “7 in grid box 6”I 0 elsewhere

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 15 / 37

Sudoku as an Exact Cover Problem

Puzzle is “pre-selected” matrix rows

Can delete these matrix rows, and their “covered matrix columns”

n = 3: 729 matrix rows, 243 matrix columns

Previous example: Remove 26 rows, remove 3× 26 = 78 columns

Select 81− 26 = 55 rows, from 703, for exact cover (uniquely)

Selected rows describe placement of symbols into locations forSudoku solution

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 16 / 37

Dancing Links

Manage lists with frequent deletions and restorations

Perfect for descending, backtracking in a search tree

Hitotumatu, Noshita (1978, Information Processing Letters)I “pointers of each already-used element are still active while. . .

removed”I Two pages, N queens problemI Donald Knuth listed in the Acknowledgement

Popularized by Knuth, “Dancing Links” (2000, arXiv)I Algorithm X = “traditional” backtrackingI Algorithm DLX = Dancing Links + Algorithm XI 26 pages, applications to packing pentominoes in a square

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 17 / 37

Doubly-Linked List

A B C D E

x

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 18 / 37

Remove Node “C” From List

A B C D E

x

D

R[x]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 19 / 37

Remove Node “C” From List

A B C D E

x

DC

L[R[x]]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 20 / 37

Remove Node “C” From List

A B C D E

x

DCB

L[R[x]] L[x]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 21 / 37

Remove Node “C” From List

A B C D E

x

DCB

L[R[x]]←−L[x]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 22 / 37

Two Assignments to Totally Remove “C”

A B C D E

x

L[R[x]]←−L[x] R[L[x]]←−R[x]

DO NOT CLEAN UP THE MESS

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 23 / 37

Two Assignments to Totally Remove “C”

A B C D E

x

L[R[x]]←−L[x] R[L[x]]←−R[x]

DO NOT CLEAN UP THE MESS

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 23 / 37

List Without “C”, Includes Our Mess

A B C D E

x

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 24 / 37

Restore Node “C” to the List

A B C D E

x

D

R[x]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 25 / 37

Restore Node “C” to the List

A B C D E

x

DB

L[R[x]]

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 26 / 37

Restore Node “C” to the List

A B C D E

x

DB C

L[R[x]] x

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 27 / 37

Restore Node “C” to the List

A B C D E

x

DB C

L[R[x]]←−x

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 28 / 37

Restore Node “C” to the List

A B C D E

x

L[R[x]]←−x R[L[x]]←−x

WE NEED OUR MESS, IT CLEANS UP ITSELF

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 29 / 37

Restore Node “C” to the List

A B C D E

x

L[R[x]]←−x R[L[x]]←−x

WE NEED OUR MESS, IT CLEANS UP ITSELF

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 29 / 37

DLX for the Exact Cover Problem

Backtrack on the columns

Choose a column to cover, this will dictate a selection of rows

Loop over rows, for each row choice remove covered columns

Recursively analyze new, smaller matrix

Restore rows and columns on backtrack step

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 30 / 37

DLX for the Exact Cover Problem

Backtrack on the columns

Choose a column to cover, this will dictate a selection of rows

Loop over rows, for each row choice remove covered columns

Recursively analyze new, smaller matrix

Restore rows and columns on backtrack step

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 30 / 37

DLX for the Exact Cover Problem

Backtrack on the columns

Choose a column to cover, this will dictate a selection of rows

Loop over rows, for each row choice remove covered columns

Recursively analyze new, smaller matrix

Restore rows and columns on backtrack step

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 30 / 37

Exact Cover Example (Knuth, 2000)

A B C D E F G

1 0 0 1 0 1 1 0

2 1 0 0 1 0 0 1

3 0 1 1 0 0 1 0

4 1 0 0 1 0 0 0

5 0 1 0 0 0 0 1

6 0 0 0 1 1 0 1

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 31 / 37

Exact Cover Representation (Knuth, 2000)

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 32 / 37

Exact Cover Representation (Knuth, 2000)

Cover column A

Remove rows 2, 4

A B C D E F G1 0 0 1 0 1 1 02 1 0 0 1 0 0 13 0 1 1 0 0 1 04 1 0 0 1 0 0 05 0 1 0 0 0 0 16 0 0 0 1 1 0 1

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 33 / 37

Exact Cover Representation (Knuth, 2000)

Loop through rows

Row 2 covers D, G

D removes row 4, 6

G removes row 5, 6

A B C D E F G1 0 0 1 0 1 1 02 1 0 0 1 0 0 13 0 1 1 0 0 1 04 1 0 0 1 0 0 05 0 1 0 0 0 0 16 0 0 0 1 1 0 1

Recurse on 2× 4 matrixIt has no solution,so will soon backtrack

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 34 / 37

Implementation in Sage

The games module only contains code for solving Sudoku puzzles, which Iwrote in two hours on Alaska Airlines, in order to solve the puzzle in theinflight magazine. — William Stein, Sage Founder

Sage, open source mathematics software, sagemath.org

Stein (UW): naive recursive backtracking, run times of 30 minutes

Carlo Hamalainen (Turkey/Oz): DLX for exact cover problems

Tom Boothby (UW): Preliminary representation as an exact cover

RAB: Optimized backtrackingI lots of look-aheadI automatic Cython conversion of Python to C

RAB: new class, conveniences for printing, finished DLX approach

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 35 / 37

Implementation in Sage

The games module only contains code for solving Sudoku puzzles, which Iwrote in two hours on Alaska Airlines, in order to solve the puzzle in theinflight magazine. — William Stein, Sage Founder

Sage, open source mathematics software, sagemath.org

Stein (UW): naive recursive backtracking, run times of 30 minutes

Carlo Hamalainen (Turkey/Oz): DLX for exact cover problems

Tom Boothby (UW): Preliminary representation as an exact cover

RAB: Optimized backtrackingI lots of look-aheadI automatic Cython conversion of Python to C

RAB: new class, conveniences for printing, finished DLX approach

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 35 / 37

Timings in Sage

Test Examples:

Original doctest, provenance is Alaska Airlines in-flight magazine?

17-hint “random” puzzle (no 16-hint puzzle known)

Worst-case: top-row empty, top-row solution 9 8 7 6 5 4 3 2 1

All ∼48,000 known 17-hint puzzles (Gordon Royle, UWA)

Equipment: R 3500 machine, 3 GHz Intel Core Duo

Puzzle Time (milliseconds)Naive Custom DLX

Alaska 34 0.187 1.11

17 1,494,000 441.0 1.20

Worst 4,798,000 944.0 1.21

48K 17 ∼60,000

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 36 / 37

Talk available at:

buzzard.pugetsound.edu/talks.html

Rob Beezer (U Puget Sound) Solving Sudoku with Dancing Links AIMS October 2010 37 / 37