iSudoku Computing Solutions to Sudoku Puzzles w/ 3 Algorithms by: Gavin Hillebrand Jamie Sparrow Jonathon Makepeace Matthew Harris
What is Sudoku? Region 1 Column 6 A logic-based puzzle game ● Heavily based in combinatorics ● ● Object: place the digits 1-9 in cells of a 9x9 grid (subdivided into 3x3 regions) such that no row , column , nor region of cells contains the digit Row 6 more than once ● Initially given at least 17 clue numbers to work with
Example Sudoku Puzzle Given: Solution:
Formal Definition A formal definition of the problem of solving a Sudoku puzzle: Given an n × n Sudoku grid, the Sudoku is successfully solved if: ● ∀ x ∈ N and i ∈ N, with x ≤ n and i ≤ n: ∃ j ∈ N (with j ≤ n) such that numberAt(i, j) = x. (“For any given number x [between 1 and n] and row number i [between 1 and n], there is a column number j [between 1 and n] such that x can be found in row i , column j .”) ● ∀ x ∈ N and j ∈ N, with x ≤ n and j ≤ n: ∃ i ∈ N (with i ≤ n) such that numberAt(i, j) = x. (“For any given number x and column number j , there is a row number i ...) ∀ x ∈ N and r ∈ N, with x ≤ n and r ≤ n: ∃ i ∈ N (with i ≤ n) and j ∈ N (with j ● ≤ n) such that regionOf(i,j) = r AND numberAt(i,j) = x.
3 Algorithms for Solving a Sudoku Backtracking ● Dancing links ● ● Crook’s Algorithm ● Generic Algorithm
Steps for the Backtracking Algorithm Iterate over the cells in the puzzle, and for each cell: 1. Place a number (in the case of a 9 × 9 grid) between 1 and 9 in that empty cell. 2. Check that the placed number does not violate any of the three constraints (duplicate number in its row, column, or region). 3. If any constraint is violated, backtrack and place the next greater number in the cell. 4. Repeat until all cells have been filled.
Results of Backtracking Algorithm Tested on 100 randomly-generated (online data source) Sudoku puzzles of ● varying difficulty ● Tested on 10 puzzles for each difficulty: easy, normal, and hard difficulty (for a total of 30 puzzles) Recorded runtime and # of iterations required for each solution, as well as the ● avg/min/max runtime across all solutions.
Backtracking Results for 100 Puzzles (runtimes) Total time taken 248.48 ms Average time per puzzle 2.48 ms Fastest solve (minimum 0.63 ms time) Slowest solve (maximum 14.31 ms time)
Backtracking Results for Easy, Normal, Hard Puzzles
Steps for Dancing Links Algorithm 1. Setup initial matrix with constraints, column headers, and node pointers directed to neighbors and their column headers 2. For a row satisfying a constraint, remove the column and the rows intersecting that column (ie covering a node/column) 3. If no solution is found, backtrack in the exact reverse order nodes were covered (ie uncover a node/column) 4. Continue covering until the solution to the constraints is found
Dancing Links Results for 100 Puzzles (runtimes) Total time taken 19.62 ms Average time per puzzle 0.1962 ms Fastest solve (minimum 0.1834 time) Slowest solve (maximum 0.7952 time)
Dancing Links Results for Easy, Normal, Hard Puzzles
Algorithm #3 - Crook’s Algorithm ● Crook’s Algorithm is a method to solve any sudoku puzzle using pen and paper. Determines what is possible in each cell by looking through all the ● possibilities, then crosses out as it test each value. ● Uses a preemptive sets of possible integers of the possibilities, and groups possible sets together for a solution.
Crook’s continued
Steps for Crook’s Algorithm 1. Mark up the cells ie. list of numbers that the cell may contain. 2. Look at each column, row and 3x3 box and break down into preemptive sets. Break down the sets and use Occupancy Theorm whenever possible. 3. Determine if puzzle is: Finished, not possible or next step. 4. Choose an empty cell and mark a number and color. Repeat step 2 until no more preemptive sets remain. 5. Go through the markup until you solve or determine as not possible.
Crook’s Results for Easy, Medium, Hard Puzzles
Crook’s Results for 100 Puzzles (Run time) Total time taken 231.64 ms Average time per puzzle 2.31 ms Fastest solve (minimum 0.74 ms time) Slowest solve (maximum 12.42 ms time)
Conclusions Based on our results, we conclude that Dancing Links will be the most ● efficient based on time constraints. This result matches with our initial estimate at the beginning of the project. ● The order of efficiency based on time(Most efficient first): Backtracking ● ● Crook’s Algorithm Dancing Links ●
Questions 1. Based on Crook’s Algorithm, what is the difference between a preemptive set and a markup? A: Markup is the list of possible values for each cell. Preemptive set are numbers that are potential occupants of the cell and can be occupied by no other value. 2. What differentiates brute-force with backtracking from a complete brute-force algorithm for finding Sudoku solutions? A: Backtracking will check if the number can legally be placed in the cell. 3. How can one tell the difference between an easy and hard Sudoku Puzzle? A: The amount of conceptual moves needed to complete the puzzle 4. Does backtracking for solving Sudoku puzzles perform a breadth-first or depth-first search down the decision tree? A: Depth-first
Questions contn 5. What is one reason that causes the crook’s algorithm to slow down during harder puzzles? A: The increase in blank spaces is one reason, because it has more cells to create the markup and preemptive sets on each one.
Genetic Algorithm A genetic algorithm is a metaheuristic algorithm inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms. 1. Create a population (vector) of random solutions 2. Pick a few solutions and sort them 3. Replace the worst solution with a new solution 4. Check if you have a new global best fitness, if so, store the solution. 5. If too many iterations go by without improvement, the entire population might be stuck in a local minimum . If so, kill the population and start over at 1. 6. Go to 2.
Recommend
More recommend