Download presentation

Presentation is loading. Please wait.

Published byAndrew Lyons Modified over 2 years ago

1

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

3
Starring Masahiko SudokuGrid

4
Michael DancingLinks Arena

5
Karou DancingLinks Sudoku

6
Antonio Latin Square Mathematico

7
Dexter Exact Cover Problem

8
Sarah Sparse Matrix

9
Alotof Sets

10
Renee Cartesian Product Lamont

11
John ColumnNode

12
And guest starring Donald Knuths Algorithm X

13
AND NOW OUR STORY BEGINS… With a phrase

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

15
A typical Sudoku game.

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

17
Sudoku and Mathematics (part 1) So is there a difference between a Latin Square and a Sudoku grid? So is there a difference between a Latin Square and a Sudoku grid? YES!! YES!! WELL ENLIGHTEN ME! WELL ENLIGHTEN ME! Answer: The Sudoku grid imposes the extra restriction that each box contains 9 distinct digits. Answer: The Sudoku grid imposes the extra restriction that each box contains 9 distinct digits.

18
Sudoku and Mathematics (part 1) Sudoku Grid (Child) Latin Square (Parent)

19
E END OF PART 1

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

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

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

23
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 N0000 O1010 P0110 E0101

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

25
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. S 8,1 S 8,2 S 8,3 S 8,4 S 8,5 S 8,6 S 8,7 S 8,8 S 9,8

26
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. C1C1C1C1 C2C2C2C2.. S 1,5... C9C9C9C9 R2R2R2R R9R9R9R9

27
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. S 1,5 S 2, S 9,5

28
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. b1b1b1b1 b2b2b2b2 b3b3b3b3 b4b4b4b4 b5b5b5b5 b6b6b6b6.. b7b7b7b7 b8b8b8b8 b9b9b9b9.. B6B6B6B6 B7B7B7B7 B8B8B8B8 B9B9B9B9

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

30
Definition of CARTESIAN PRODUCT 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, 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. 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 } A×B = { (a, b) : a A and b B }

31
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)

32
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 = {(r 1,c 1 ),..., (r 9,c 9 ) }. 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. Consider the Cartesian products R×C, R×V, C×V, and B×V. Each contains 81 pairs. For example R×C = {(r 1,c 1 ),..., (r 9,c 9 ) }. 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. 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}

33
END OF SUDOKU AND MATHEMATICS END OF SUDOKU AND MATHEMATICS

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

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

36
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.. H829 node x

37
Deleting a node from a linked-list x.left.right x.right; Results in…. H 8 29 node x

38
Deleting a node from a linked-list x.right.left x.left; //removes node x Results in…. H 8 29 node x

39
Deleting a node from a linked-list And x.left.right x; Results in…. H 8 29 node x

40
Deleting a node from a linked-list x.right.left x; //restores xs position in the list Thus… H 8 29 node x

41
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 A B C D E F

42
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 1s.) Thus we select Column A B C D E F

43
Algorithm X by Donald Knuth The Essence of the Dancing Links 3. Choose a row r such that A r, c = 1 Row A and B qualify, however row A is selected first A B C D E F

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

45
Algorithm X by Donald Knuth The Essence of the Dancing Links 5.For each column j such that A r, J = 1, (outer loop) Therefore the columns that the algorithm will operate on are 1, 4 and A B C D E F

46
Algorithm X by Donald Knuth The Essence of the Dancing Links 5(i) For each row i such that A i, j = 1, (inside loop) These rows are: A,C,E and F A B C D E F

47
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 A B C D E F D

48
5(iii) Delete column j from matrix A. So delete these columns: Before After D D0111

49
Repeat this algorithm recursively on the reduced matrix A. After one run of the algorithm, the matrix A is 2356 D0111

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

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

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

53
Algorithm X by Donald Knuth The Essence of the Dancing Links This time it will select row B and continue A B C D E F

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

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

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

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

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

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

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

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

62
DancingLinksSudoku Constructor code. public DancingLinksSudoku(int [][]puzzle){ /* * The data row for the Sudoku sparse matrix looks like: * : the cell position in question: (row * 9 + column) * : the row constraint (row# * 9 + digit) * : the column constraint (column# * 9 + digit) * : the 3x3 box constraint (box# * 9 + digit) * * yielding a grand total of 324 columns.Each cell has 4 constraints(cell,row,column and box) * that must be satisfied.

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

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

65
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()); }

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

67
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

68
END OF SUDOKU AND COMPUTER SCIENCE PART 1 ….AND NOW THE MOMENT YOUVE BEEN WAITING FOR ….PART 2

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

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

71
THE SCIENCE OF THE SUDOKU SOLVER Edited by PATASON BROOKS

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

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

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

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

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

77
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 Knuths Algorithm X……………………. Itself T-800 …………………………………. Clone 1 & 2

78
A SCARBOROUGH SECONDARY SCHOOL PRODUCTION

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google