780 likes | 1.14k Views
THE SCIENCE OF THE SUDOKU SOLVER. By Omari N. Martin. Starring. Masahiko SudokuGrid. Michael “DancingLinks” Arena. Karou “DancingLinks” Sudoku. Antonio “Latin Square” Mathematico. Dexter “Exact Cover” Problem. Sarah Sparse Matrix . Alotof Sets. Renee “Cartesian Product” Lamont.
E N D
THE SCIENCE OF THE SUDOKU SOLVER By OmariN.Martin
Starring • Masahiko SudokuGrid
And guest starring • Donald Knuth’s Algorithm X
AND NOW OUR STORY BEGINS… With a phrase
Trivia: Meaning of Sudoku • "Sūji wa dokushin ni kagiru,"
Sudoku and Mathematics • PART 1 - Sudoku and Latin Square • (Child and Parent)
Sudoku and Mathematics (part 1) • So is there a difference between a Latin Square and a Sudoku grid? • YES!! • WELL ENLIGHTEN ME! • Answer: The Sudoku grid imposes the extra restriction that each box contains 9 distinct digits.
Sudoku and Mathematics (part 1) Sudoku Grid (Child) Latin Square (Parent)
Sudoku and Mathematics PART 2 – Sudoku and the Exact Cover problem.
Sudoku and Mathematics (Part 2) • The Sudoku game is an instance of the “Exact Cover” problem. • So what is the Exact Cover problem?
Illustration of Exact Cover Problem • Let S = {N, O, P, E} be a collection of subsets of a universe U = {1, 2, 3, 4} such that: • N = { }, • O = {1, 3}, • E = {2, 4}, and • P = {2, 3}. • The subcollection {O, E} is an exact cover of U, since the subsets O = {1, 3} and E = {2, 4} are disjoint and their union is U = {1, 2, 3, 4}.
Representing the Exact Cover using a sparse matrix • In the matrix representation, an exact cover is a selection of rows such that each column contains a 1 in exactly one selected row.
Interpreting the Sudoku grid using set notation. • Let S = {s11, s12, …, s19, s21, …, s99} be the set of squares of the sudoku grid.
Interpreting the Sudoku grid using set notation. • Let ri= {si1, si2, …, si9} ⊆ S be the set of squares belonging to row i. The diagram appearing on the right illustrates the squares belonging to row 8.
Interpreting the Sudoku grid using set notation. • Let R = {r1, r2, …, r9} be the set of rows. • let C = {c1, c2, …, c9} be the set of columns. • The diagram illustrates these two sets.
Interpreting the Sudoku grid using set notation. • Let cj = {s1j, s2j, …, s9j} ⊆ S be the set of squares belonging to column j. The diagram illustrates the squares which belong to column 5.
Interpreting the Sudoku grid using set notation. • Let bk be the set of squares belonging to a block , and let B = {B1, B2, …, B9} be the set of blocks.
The constraints of Sudoku • Every row, column, and square must contain each value exactly once. Each of these constraints involves pairs: pairs of rows and columns, pairs of rows and values, pairs of columns and values, and pairs of blocks and values. Our universe is going to be made up of pairs.
Definition of CARTESIAN PRODUCT • Let A and B be two non-empty sets. • Then the Cartesian Product of A and B, written as A×B is defined as the set of ordered pairs (a, b) , where ‘a’ belongs to the set A and ‘b’ belongs to the set B. i.e. • 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)
Solutions to the Sudoku grid • Consider the Cartesian productsR×C, R×V, C×V, and B×V. Each contains 81 pairs. For example R×C = {(r1,c1), ..., (r9,c9) }. The universe U is the 324 element union of these four Cartesian products. As it happens, every valid sudoku solution contains exactly these 324 pairs, no more, no less. But this set of pairs does not represent a specific solution. It represents every valid solution to the blank sudoku grid. • Note: V is the set of possible values. i.e. • V = {1,2,3,4,5,6,7,8,9}
SUDOKU AND COMPUTER SCIENCE • PART 1:- The Dancing Links Algorithm
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.. node x H 8 2 9
Deleting a node from a linked-list • x.left.right ← x.right; • Results in…. node x 8 H 2 9
Deleting a node from a linked-list • x.right.left ← x.left; //removes node x • Results in…. node x 8 H 2 9
Deleting a node from a linked-list • And x.left.right ← x; • Results in…. node x 8 H 2 9
Deleting a node from a linked-list • x.right.left ← x; //restores x’s position in the list • Thus… node x 8 H 2 9
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
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
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.
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 7 A 1 0 0 1 0 0 1
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.
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.
Algorithm X by Donald KnuthThe Essence of the Dancing Links • 5.(ii) Delete row i from matrix A; • So delete these rows: • Before After
Algorithm X by Donald KnuthThe Essence of the Dancing Links • 5(iii) Delete column j from matrix A. • So delete these columns: • Before After
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
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 7 A 1 0 0 1 0 0 1