Download - THE SCIENCE OF THE SUDOKU SOLVER
Sudoku and MathematicsSudoku and Mathematics
•PART 1 - Sudoku and Latin PART 1 - Sudoku and Latin SquareSquare
•(Child and Parent)(Child and Parent)
Sudoku and Mathematics (part Sudoku and Mathematics (part 1)1)
• So is there a difference between a So is there a difference between a Latin Square and a Sudoku grid?Latin Square and a Sudoku grid?
• YES!!YES!!
• WELL ENLIGHTEN ME!WELL ENLIGHTEN ME!
• Answer: The Sudoku grid imposes the Answer: The Sudoku grid imposes the extra restriction that each box extra restriction that each box contains 9 distinct digits.contains 9 distinct digits.
Sudoku and Mathematics (part Sudoku and Mathematics (part 1)1)
11 22 33 44 55 66 77 88 99
22 33 44 55 66 77 88 99 11
33 44 55 66 77 88 99 11 22
44 55 66 77 88 99 11 22 33
55 66 77 88 99 11 22 33 44
66 77 88 99 11 22 33 44 55
77 88 99 11 22 33 44 55 66
88 99 11 22 33 44 55 66 77
99 11 22 33 44 55 66 77 88
66 44 77 22 99 55 11 33 88
11 55 22 33 88 66 99 77 44
99 33 88 11 44 77 66 55 22
33 77 66 88 11 44 55 22 99
55 11 44 99 77 22 88 66 33
88 22 99 66 55 33 77 44 11
44 66 11 77 33 99 22 88 55
77 99 33 55 22 88 44 11 66
22 88 55 44 66 11 33 99 77
Sudoku Grid (Child) Latin Square (Parent)
Sudoku and MathematicsSudoku and Mathematics
PART 2 – Sudoku and the PART 2 – Sudoku and the Exact Cover problem.Exact Cover problem.
Sudoku and Mathematics Sudoku and Mathematics
(Part 2)(Part 2) The Sudoku game is an instance of The Sudoku game is an instance of
the “Exact Cover” problem.the “Exact Cover” problem.
So what is the Exact Cover problem?So what is the Exact Cover problem?
Illustration of Exact Cover ProblemIllustration of Exact Cover Problem Let S = {Let S = {NN, , OO, , PP, E} be a collection of subsets of , E} be a collection of subsets of
a universe a universe UU = {1, 2, 3, 4} such that: = {1, 2, 3, 4} such that: NN = { }, = { }, OO = {1, 3}, = {1, 3}, EE = {2, 4}, and = {2, 4}, and PP = {2, 3}. = {2, 3}. The subcollection {The subcollection {OO, , EE} is an exact cover of } is an exact cover of UU, ,
since the subsets since the subsets OO = {1, 3} and = {1, 3} and EE = {2, 4} are = {2, 4} are disjoint and their union is disjoint and their union is UU = {1, 2, 3, 4}. = {1, 2, 3, 4}.
Representing the Exact Cover Representing the Exact Cover using a sparse matrixusing a sparse matrix
In the matrix In the matrix representation, an representation, an exact cover is a exact cover is a selection of rows such selection of rows such that each column that each column contains a 1 in exactly contains a 1 in exactly one selected row.one selected row.
11 22 33 44
NN 00 00 00 00
OO 11 00 11 00
PP 00 11 11 00
EE 00 11 00 11
Interpreting the Sudoku grid using Interpreting the Sudoku grid using set notation.set notation.
Let Let SS = { = {ss1111, , ss1212, …, , …, ss1919, ,
ss2121, …, , …, ss9999} be the set } be the set
of squares of the of squares of the sudoku grid.sudoku grid.
SS1,11,1 SS1,21,2 .. .. .. .. .. .. SS1,91,9
SS2,12,1 SS2,22,2 .. .. .. .. .. .. SS2,92,9
.. .. ..
.. .. ..
.. .. ..
.. .. ..
.. .. ..
SS8,18,1 SS8,28,2 .. .. .. .. .. .. SS9,89,8
SS9,19,1 SS9,29,2 .. .. .. .. .. .. SS9,99,9
Interpreting the Sudoku grid using Interpreting the Sudoku grid using set notation.set notation.
Let Let rrii = {= {ssi1i1, , ssi2i2, …, , …, ssi9i9} }
⊆ ⊆ SS be the set of be the set of squares belonging to squares belonging to row row ii. The diagram . The diagram appearing on the right appearing on the right illustrates the squares illustrates the squares belonging to row 8. belonging to row 8.
SS8,18,1 SS8,28,2 SS8,38,3 SS8,48,4 SS8,58,5 SS8,68,6 SS8,78,7 SS8,88,8 SS9,89,8
Interpreting the Sudoku grid using Interpreting the Sudoku grid using set notation.set notation.
Let Let RR = { = {rr11, , rr22, …, , …, rr99} }
be the set of rows.be the set of rows. let let CC = { = {cc11, , cc22, …, , …, cc99} }
be the set of columns.be the set of columns. The diagram The diagram
illustrates these two illustrates these two sets.sets.
CC11 CC22 .. .. SS1,51,5 .. .. .. CC99
RR22
..
..
..
..
..
..
RR99
Interpreting the Sudoku grid using Interpreting the Sudoku grid using set notation.set notation.
Let Let ccjj = { = {ss1j1j, , ss2j2j, …, , …, ss9j9j} }
⊆ ⊆ SS be the set of be the set of squares belonging to squares belonging to column column j. j. The The diagram illustrates the diagram illustrates the squares which belong squares which belong to column 5.to column 5.
SS1,51,5
SS2,52,5
..
..
..
..
..
..
SS9,59,5
Interpreting the Sudoku grid using Interpreting the Sudoku grid using set notation.set notation.
Let Let bbkk be the set of be the set of
squares belonging to squares belonging to a block , and let a block , and let BB = = {{BB11, , BB22, …, , …, BB99} be the } be the
set of blocks.set of blocks.
bb11 bb22 bb33
bb44 bb55 bb66.. ..
bb77 bb88 bb99
.. .. BB66
BB77 BB88 BB99
The constraints of SudokuThe constraints of Sudoku
Every row, column, and square must Every row, column, and square must contain each value exactly once. Each of contain each value exactly once. Each of these constraints involves pairs: pairs of these constraints involves pairs: pairs of rows and columns, pairs of rows and rows and columns, pairs of rows and values, pairs of columns and values, and values, pairs of columns and values, and pairs of blocks and values. Our universe is pairs of blocks and values. Our universe is going to be made up of pairs.going to be made up of pairs.
Definition of CARTESIAN Definition of CARTESIAN PRODUCTPRODUCT
Let A and B be two non-empty sets.Let A and B be two non-empty sets.Then the Cartesian Product of A and B, Then the Cartesian Product of A and B,
written as A×B is defined as the set of written as A×B is defined as the set of ordered pairs (a, b) , where ‘a’ belongs to ordered pairs (a, b) , where ‘a’ belongs to the set A and ‘b’ belongs to the set B. i.e.the set A and ‘b’ belongs to the set B. i.e.
A×B = { (a, b) : a A and b B }∈ ∈A×B = { (a, b) : a A and b B }∈ ∈
An illustration
The highlighted cell has value = 3,row = 4,column = 5 and box = 5
Thus the pairs are: RxC = (4,5) RxV = (4,3) CxV = (5,3) BxV = (5,3)
88 44 66 77
44
11 66 55
55 99 33 77 88
77
44 88 22 11 33
55 22 99
Solutions to the Sudoku gridSolutions to the Sudoku grid Consider the Consider the Cartesian productsCartesian products R×CR×C, , R×VR×V, ,
C×VC×V, and , and B×VB×V. Each contains 81 pairs. For . Each contains 81 pairs. For example example R×CR×C = { = {(r(r11,c,c11), ..., (r), ..., (r99,c,c99) ) }. The universe }. The universe UU is the 324 element union of these four is the 324 element union of these four Cartesian products. As it happens, every valid Cartesian products. As it happens, every valid sudoku solution contains exactly these 324 sudoku solution contains exactly these 324 pairs, no more, no less. But this set of pairs pairs, no more, no less. But this set of pairs does not represent a specific solution. It does not represent a specific solution. It represents represents everyevery valid solution to the blank valid solution to the blank sudoku grid.sudoku grid.
Note: V is the set of possible values. i.e. Note: V is the set of possible values. i.e. V = {1,2,3,4,5,6,7,8,9}V = {1,2,3,4,5,6,7,8,9}
The “set of squares” become “link nodes”
A circular doubly-linked list( classic computer science data structure)
A key observation!With regards to the functionality of a
circular doubly-linked list, the removal and insertion of a node involves only altering the links. Thus if we have a linked-list like this..
H 8 2 9
node x
Deleting a node from a linked-list
x.right.left ← x.left; //removes node x
Results in….
H
8
2 9
node x
Deleting a node from a linked-list
x.right.left ← x; //restores x’s position in the list
Thus…
H 8 2 9
node x
Algorithm X by Donald KnuthThe Essence of the Dancing Links
1.If the matrix A is empty, the problem is solved; terminate successfully.
Matrix A
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
2. Otherwise choose a column c.(The general heuristic is to choose the
column with the lowest number of 1’s.)Thus we
select
Column 1
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
3. Choose a row r such that Ar,c = 1
Row A and B qualify, however row A is selected first.
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
4. Include row r in the partial solution. PARTIAL SOLUTION = 1 2 3 4 5 6 71 2 3 4 5 6 7
AA 11 00 00 11 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
5.For each column j such that Ar,J = 1, (outer loop)
Therefore the columns that the algorithm will operate on are 1, 4 and 7.
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
5(i) For each row i such that Ai, j = 1, (inside loop)
These rows are: A,C,E and F.11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
5.(ii) Delete row i from matrix A; So delete these rows:Before After
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
11 22 33 44 55 66 77
DD 00 00 11 00 11 11 00
Algorithm X by Donald KnuthThe Essence of the Dancing Links
5(iii) Delete column j from matrix A. So delete these columns:
Before After
11 22 33 44 55 66 77
DD 00 00 11 00 11 11 00
22 33 55 66
DD 00 11 11 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
Repeat this algorithm recursively on the reduced matrix A.
After one run of the algorithm , the matrix A is
22 33 55 66
DD 00 11 11 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
However, this branch of the algorithm will terminate unsuccessfully.
Algorithm X by Donald KnuthThe Essence of the Dancing Links
Row A is discarded from the partial solution.
PARTIAL SOLUTION: 1 2 3 4 5 6 71 2 3 4 5 6 7
AA 11 00 00 11 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
Another branch (or clone of the algorithm) will operate on a copy of the matrix A.
Mission Objective:Get exact cover
solution from matrix A.
Algorithm X by Donald KnuthThe Essence of the Dancing Links
This time it will select row B and continue.
11 22 33 44 55 66 77
AA 11 00 00 11 00 00 11
BB 11 00 00 11 00 00 00
CC 00 00 00 11 11 00 11
DD 00 00 11 00 11 11 00
EE 00 11 11 00 00 11 11
FF 00 11 00 00 00 00 11
Algorithm X by Donald KnuthThe Essence of the Dancing Links
And the algorithm WILL NOT STOP EVER…
Until an exact cover solution is found!
Building the implementation of Algorithm X
The Dancing Links Algorithm is essentially an implementation of Algorithm X using two main computer science data structures – one-dimensional and two dimensional arrays and circular doubly-linked lists.
Building the implementation of Algorithm X
Implementing Algorithm X using Java code involves four main classes:
1. ColumnNode2. DancingLinksArena3. DancingLinksSudoku4. Sudoku
Class1: ColumnNode
A column node is a special type of node which acts as the header nodes for the respective columns in the dancing links arena. This is what it looks like…
Class2: DancingLinksArena“Where it all happens!”
This class erects a Dancing Links Arena that will implement Algorithm X in order to solve the Sudoku puzzle. Once the arena is built, the “dancing” can begin.
Class2: DancingLinksArena The constructor code.
• DancingLinksArena (int labels[],boolean optional[]){ //Constructor for the Dancing Links Arena
• ColumnNode columns[] = new ColumnNode[labels.length];• assert(labels.length == optional.length);//Ensure that the labels array and
the optional arrays are of the same length. • for (int i = 0; i < labels. length; i++) {• assert(labels[i] > 0); //This must be true for function to operate properly.• columns[i] = new ColumnNode(labels[i],optional[i]);• columns[i].setRight(null); //At this point each columnNode exists in isolation.• • //..now link up the columnNodes.• if (i > 0){ • columns[i].setLeft(columns[i - 1]); /*With the exception of the leftmost
column, set the left pointer of this columnNode to point to the previous columnNode*/
• columns[i-1].setRight(columns[i]); /* Set the right pointer of the previous columnNode to point to the current columnNode */
• }• }
Class3: DancingLinksSudoku
This class adapts or modifies the DancingLinksArena in such a way that it becomes appropriate for the specific purpose of solving a Sudoku puzzle. The following slides will highlight some of the code which play a critical role in solving a Sudoku puzzle.
DancingLinksSudokuConstructor code. public DancingLinksSudoku(int [][]puzzle){ /* * The data row for the Sudoku sparse matrix looks like: * <1-81>: the cell position in question: (row * 9 + column) * <1-81>: the row constraint (row# * 9 + digit) * <1-81>: the column constraint (column# * 9 + digit) * <1-81>: the 3x3 box constraint (box# * 9 + digit) * * yielding a grand total of 324 columns.Each cell<1-81> has 4
constraints(cell,row,column and box) * that must be satisfied.
DancingLinksSudoku
dla = new DancingLinksArena(labels); // construct a new DancingLinksArena for Sudoku solving.
rowData = new int[4]; …… //Compute the four constraint column numbers. rowData[0] = 1 + (row * 9 + column); //cell position rowData[1] = 1 + 81 + (row * 9 + digit); //row constraint rowData[2] = 1 + 81 + 81 + (column * 9 + digit); //column
constraint boxrow = row / 3; //each box has 3 rows boxcol = column / 3; //each box has 3 columns. rowData[3] = 1 + 81 + 81 + 81 + ((boxrow * 3 + boxcol) * 9 + digit);//box constraints.
// Add this row to the sparse matrix newRow = dla.addInitialRow(rowData);
DancingLinksSudoku
/* And, if it is one of the "givens", add it to our collection of constraints */ if (isGiven){ givenList.add(newRow); } }
/* * Remove all of the "givens". They are removed after all of * the entries are added, so that the interfering rows can be * removed as well. */ dla.removeInitialSolutionSet(givenList);
Algorithm X “in the flesh”
//Perform "Algorithm X" on this sparse boolean matrix int[] solve(){ int[] solution;
if (traveller == null){ traveller =
getNextNonOptionalColumn(firstColumn).getDown(); startingCount = solutionIndex; } return(solveNonRecurse()); }
Class4: Sudoku
This is the application-level class where the puzzle data is entered, the unsolved puzzle is printed and the solved puzzle is printed.
Entering the puzzle data
public static void main(String args[]){ Sudoku sudoku = new Sudoku(); int puzzle1[][] = { {0, 0, 0, 7, 0, 0, 0, 0, 0}, {0, 0, 4, 0, 3, 0, 6, 0, 0}, {0, 0, 9, 5, 0, 0, 0, 2, 1}, {9, 0, 3, 0, 0, 8, 0, 0, 2}, {7, 0, 0, 0, 0, 0, 0, 0, 4}, {8, 0, 0, 4, 0, 0, 1, 0, 5}, {5, 6, 0, 0, 0, 3, 2, 0, 0}, {0, 0, 2, 0, 7, 0, 8, 0, 0}, {0, 0, 0, 0, 0, 5, 0, 0, 0} }; sudoku.printUnsolvedPuzzle(puzzle1); //prints the unsolved puzzle sudoku.solvePuzzle(puzzle1); //prints the solved puzzle
THE SCIENCE OF THE SUDOKU SOLVER
• CAST OF CHARACTERS
Masahiko SudokuGrid…………………………. Sudoku Grid Michael “DancingLinks” Arena……………….The DancingLinksArena Karou “DancingLinks” Sudoku……………….The DancingLinksSudoku
Antonio “Latin Square” Mathematico…….. The Latin Square Dexter “Exact Cover” Problem………………The Exact Cover Problem Sarah Sparse Matrix……………………………. Sparse Matrix Alotof Sets………………………….......All the sets in the
presentation! Renee “Cartesian Product” Lamont………….Cartesian Product John ColumnNode………………………………….All ColumnNodes! Donald Knuth’s Algorithm X……………………. Itself T-800…………………………………. Clone 1 & 2