Presentation is loading. Please wait.

Presentation is loading. Please wait.

THE SCIENCE OF THE SUDOKU SOLVER

Similar presentations


Presentation on theme: "THE SCIENCE OF THE SUDOKU SOLVER"— Presentation transcript:

1 THE SCIENCE OF THE SUDOKU SOLVER
By Omari N. Martin

2 Starring Masahiko SudokuGrid

3 Michael “DancingLinks” Arena

4 Karou “DancingLinks” Sudoku

5 Antonio “Latin Square” Mathematico

6 Dexter “Exact Cover” Problem

7 Sarah Sparse Matrix

8 Alotof Sets

9 Renee “Cartesian Product” Lamont

10 John ColumnNode

11 Donald Knuth’s Algorithm X
And guest starring Donald Knuth’s Algorithm X

12 AND NOW OUR STORY BEGINS…
With a phrase

13 Trivia: Meaning of Sudoku
"Sūji wa dokushin ni kagiru,"

14 A typical Sudoku game.

15 Sudoku and Mathematics
PART 1 - Sudoku and Latin Square (Child and Parent)

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

17 Sudoku and Mathematics (part 1)
6 4 7 2 9 5 1 3 8 1 2 3 4 5 6 7 8 9 Sudoku Grid (Child) Latin Square (Parent)

18 END OF PART 1

19 Sudoku and Mathematics
PART 2 – Sudoku and the Exact Cover problem.

20 Sudoku and Mathematics (Part 2)
The Sudoku game is an instance of the “Exact Cover” problem. So what is the Exact Cover problem?

21 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}.

22 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. 1 2 3 4 N O P E

23 Interpreting the Sudoku grid using set notation.
Let S = {s11, s12, …, s19, s21, …, s99} be the set of squares of the sudoku grid. S1,1 S1,2 . S1,9 S2,1 S2,2 S2,9 S8,1 S8,2 S9,8 S9,1 S9,2 S9,9

24 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. S8,1 S8,2 S8,3 S8,4 S8,5 S8,6 S8,7 S8,8 S9,8

25 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. C1 C2 . S1,5 C9 R2 R9

26 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. S1,5 S2,5 . S9,5

27 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. b1 b2 b3 b4 b5 b6 . b7 b8 b9 B6 B7 B8 B9

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

29 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 }

30 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) 8 4 6 7 1 5 9 3 2

31 Solutions to the Sudoku grid
Consider the Cartesian products R×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}

32 END OF SUDOKU AND MATHEMATICS

33 SUDOKU AND COMPUTER SCIENCE
PART 1:- The Dancing Links Algorithm

34 The “set of squares” become “link nodes”
A circular doubly-linked list( classic computer science data structure)

35 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

36 Deleting a node from a linked-list
x.left.right ← x.right; Results in…. node x 8 H 2 9

37 Deleting a node from a linked-list
x.right.left ← x.left; //removes node x Results in…. node x 8 H 2 9

38 Deleting a node from a linked-list
And x.left.right ← x; Results in…. node x 8 H 2 9

39 Deleting a node from a linked-list
x.right.left ← x; //restores x’s position in the list Thus… node x H 8 2 9

40 Algorithm X by Donald Knuth The Essence of the Dancing Links
1.If the matrix A is empty, the problem is solved; terminate successfully. Matrix A 1 2 3 4 5 6 7 A B C D E F

41 Algorithm X by Donald Knuth The 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 1 2 3 4 5 6 7 A B C D E F

42 Algorithm X by Donald Knuth The 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. 1 2 3 4 5 6 7 A B C D E F

43 Algorithm X by Donald Knuth The Essence of the Dancing Links
4. Include row r in the partial solution. PARTIAL SOLUTION = A

44 Algorithm X by Donald Knuth The 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. 1 2 3 4 5 6 7 A B C D E F

45 Algorithm X by Donald Knuth The 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. 1 2 3 4 5 6 7 A B C D E F

46 Algorithm X by Donald Knuth The Essence of the Dancing Links
5.(ii) Delete row i from matrix A; So delete these rows: Before After 1 2 3 4 5 6 7 A B C D E F 1 2 3 4 5 6 7 D

47 Algorithm X by Donald Knuth The Essence of the Dancing Links
5(iii) Delete column j from matrix A. So delete these columns: Before After 1 2 3 4 5 6 7 D 2 3 5 6 D 1

48 Algorithm X by Donald Knuth The Essence of the Dancing Links
Repeat this algorithm recursively on the reduced matrix A. After one run of the algorithm , the matrix A is 2 3 5 6 D 1

49 Algorithm X by Donald Knuth The Essence of the Dancing Links
However, this branch of the algorithm will terminate unsuccessfully.

50 Algorithm X by Donald Knuth The Essence of the Dancing Links
Row A is discarded from the partial solution. PARTIAL SOLUTION: A

51 Algorithm X by Donald Knuth The 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.

52 Algorithm X by Donald Knuth The Essence of the Dancing Links
This time it will select row B and continue. 1 2 3 4 5 6 7 A B C D E F

53 Algorithm X by Donald Knuth The Essence of the Dancing Links
And the algorithm WILL NOT STOP EVER… Until an exact cover solution is found!

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

55 Building the implementation of Algorithm X
Implementing Algorithm X using Java code involves four main classes: 1. ColumnNode 2. DancingLinksArena 3. DancingLinksSudoku 4. Sudoku

56 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…

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

58 WELCOME TO THE DANCING LINKS ARENA!
A columnNode Given values of the Sudoku puzzle go here

59 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 */ }

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

61 DancingLinksSudoku Constructor 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.

62 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] = (row * 9 + digit); //row constraint rowData[2] = (column * 9 + digit); //column constraint boxrow = row / 3; //each box has 3 rows boxcol = column / 3; //each box has 3 columns. rowData[3] = ((boxrow * 3 + boxcol) * 9 + digit);//box constraints. // Add this row to the sparse matrix newRow = dla.addInitialRow(rowData);

63 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);

64 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());

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

66 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

67 END OF SUDOKU AND COMPUTER SCIENCE PART 1
….AND NOW THE MOMENT YOU’VE BEEN WAITING FOR ….PART 2

68 THE SCIENCE OF THE SUDOKU SOLVER
Presenter OMARI N. MARTIN

69 THE SCIENCE OF THE SUDOKU SOLVER
Graphics by OMARI N. MARTIN

70 THE SCIENCE OF THE SUDOKU SOLVER
Edited by PATASON BROOKS

71 THE SCIENCE OF THE SUDOKU SOLVER
ColumnNode code by STAN CHESTNUTT

72 THE SCIENCE OF THE SUDOKU SOLVER
DancingLinksArena code by STAN CHESTNUTT

73 THE SCIENCE OF THE SUDOKU SOLVER
DancingLinksSudoku code by STAN CHESTNUTT and OMARI N. MARTIN

74 THE SCIENCE OF THE SUDOKU SOLVER
Sudoku code by OMARI N. MARTIN

75 THE SCIENCE OF THE SUDOKU SOLVER
Code Documentation by STAN CHESTNUTT and OMARI N. MARTIN

76 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

77 A SCARBOROUGH SECONDARY SCHOOL PRODUCTION


Download ppt "THE SCIENCE OF THE SUDOKU SOLVER"

Similar presentations


Ads by Google