![Page 1: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/1.jpg)
THE SCIENCE OF THE SUDOKU
SOLVER
By Omari N. Martin
![Page 2: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/2.jpg)
Starring
• Masahiko SudokuGrid
![Page 3: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/3.jpg)
•Michael “DancingLinks”
Arena
![Page 4: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/4.jpg)
•Karou “DancingLinks”
Sudoku
![Page 5: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/5.jpg)
Antonio “Latin Square”
Mathematico
![Page 6: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/6.jpg)
Dexter “Exact Cover” Problem
![Page 7: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/7.jpg)
Sarah Sparse Matrix
![Page 8: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/8.jpg)
•Alotof Sets
![Page 9: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/9.jpg)
•Renee “Cartesian Product” Lamont
![Page 10: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/10.jpg)
John ColumnNode
![Page 11: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/11.jpg)
And guest starring
•Donald Knuth’s Algorithm X
![Page 12: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/12.jpg)
•AND NOW OUR STORY BEGINS…•With a phrase
![Page 13: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/13.jpg)
Trivia: Meaning of Sudoku
"Sūji wa dokushin ni kagiru,"
![Page 14: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/14.jpg)
A typical Sudoku game.A typical Sudoku game.
![Page 15: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/15.jpg)
Sudoku and MathematicsSudoku and Mathematics
•PART 1 - Sudoku and Latin PART 1 - Sudoku and Latin SquareSquare
•(Child and Parent)(Child and Parent)
![Page 16: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/16.jpg)
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.
![Page 17: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/17.jpg)
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)
![Page 18: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/18.jpg)
• END OF PART 1END OF PART 1
![Page 19: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/19.jpg)
Sudoku and MathematicsSudoku and Mathematics
PART 2 – Sudoku and the PART 2 – Sudoku and the Exact Cover problem.Exact Cover problem.
![Page 20: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/20.jpg)
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?
![Page 21: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/21.jpg)
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}.
![Page 22: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/22.jpg)
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
![Page 23: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/23.jpg)
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
![Page 24: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/24.jpg)
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
![Page 25: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/25.jpg)
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
![Page 26: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/26.jpg)
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
![Page 27: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/27.jpg)
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
![Page 28: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/28.jpg)
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.
![Page 29: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/29.jpg)
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 }∈ ∈
![Page 30: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/30.jpg)
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
![Page 31: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/31.jpg)
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}
![Page 32: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/32.jpg)
END OF SUDOKU END OF SUDOKU AND MATHEMATICSAND MATHEMATICS
![Page 33: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/33.jpg)
SUDOKU AND COMPUTER
SCIENCEPART 1:- The Dancing Links Algorithm
![Page 34: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/34.jpg)
The “set of squares” become “link nodes”
A circular doubly-linked list( classic computer science data structure)
![Page 35: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/35.jpg)
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
![Page 36: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/36.jpg)
Deleting a node from a linked-list
x.left.right ← x.right;
Results in….
H
8
2 9
node x
![Page 37: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/37.jpg)
Deleting a node from a linked-list
x.right.left ← x.left; //removes node x
Results in….
H
8
2 9
node x
![Page 38: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/38.jpg)
Deleting a node from a linked-list
And x.left.right ← x;Results in….
H
8
2 9
node x
![Page 39: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/39.jpg)
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
![Page 40: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/40.jpg)
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
![Page 41: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/41.jpg)
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
![Page 42: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/42.jpg)
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
![Page 43: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/43.jpg)
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
![Page 44: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/44.jpg)
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
![Page 45: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/45.jpg)
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
![Page 46: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/46.jpg)
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
![Page 47: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/47.jpg)
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
![Page 48: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/48.jpg)
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
![Page 49: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/49.jpg)
Algorithm X by Donald KnuthThe Essence of the Dancing Links
However, this branch of the algorithm will terminate unsuccessfully.
![Page 50: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/50.jpg)
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
![Page 51: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/51.jpg)
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.
![Page 52: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/52.jpg)
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
![Page 53: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/53.jpg)
Algorithm X by Donald KnuthThe Essence of the Dancing Links
And the algorithm WILL NOT STOP EVER…
Until an exact cover solution is found!
![Page 54: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/54.jpg)
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.
![Page 55: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/55.jpg)
Building the implementation of Algorithm X
Implementing Algorithm X using Java code involves four main classes:
1. ColumnNode2. DancingLinksArena3. DancingLinksSudoku4. Sudoku
![Page 56: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/56.jpg)
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…
![Page 57: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/57.jpg)
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.
![Page 58: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/58.jpg)
WELCOME TO THE DANCING LINKS ARENA!
Given values of theSudoku puzzle go here
A columnNode
![Page 59: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/59.jpg)
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 */
• }• }
![Page 60: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/60.jpg)
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.
![Page 61: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/61.jpg)
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.
![Page 62: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/62.jpg)
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);
![Page 63: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/63.jpg)
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);
![Page 64: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/64.jpg)
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()); }
![Page 65: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/65.jpg)
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.
![Page 66: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/66.jpg)
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
![Page 67: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/67.jpg)
END OF SUDOKU AND COMPUTER SCIENCE PART 1
….AND NOWTHE MOMENT YOU’VE BEEN WAITING
FOR….PART 2
![Page 68: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/68.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• Presenter• OMARI N. MARTIN
![Page 69: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/69.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• Graphics by
• OMARI N. MARTIN
![Page 70: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/70.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• Edited by
• PATASON BROOKS
![Page 71: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/71.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• ColumnNode code by
• STAN CHESTNUTT
![Page 72: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/72.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• DancingLinksArena code by
• STAN CHESTNUTT
![Page 73: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/73.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• DancingLinksSudoku code by
• STAN CHESTNUTT and OMARI N. MARTIN
![Page 74: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/74.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• Sudoku code by
• OMARI N. MARTIN
![Page 75: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/75.jpg)
THE SCIENCE OF THE SUDOKU SOLVER
• Code Documentation by
• STAN CHESTNUTT and OMARI N. MARTIN
![Page 76: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/76.jpg)
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
![Page 77: THE SCIENCE OF THE SUDOKU SOLVER By Omari N. Martin](https://reader035.vdocuments.mx/reader035/viewer/2022062511/551420e8550346e7488b57d3/html5/thumbnails/77.jpg)
•A SCARBOROUGH SECONDARY SCHOOL PRODUCTION