Sudoku solving algorithms
A standard Sudoku contains 81 cells, in a 9×9 grid, and has 9 boxes, each box being the intersection of the first, middle, or last 3 rows, and the first, middle, or last 3 columns. Each cell may contain a number from one to nine, and each number can only occur once in each row, column, and box. A Sudoku starts with some cells containing numbers, and the goal is to solve the remaining cells. Proper Sudokus have one solution. Players and investigators use a wide range of computer algorithms to solve Sudokus, study their properties, and make new puzzles, including Sudokus with interesting symmetries and other properties.
There are several computer algorithms that will solve 9×9 puzzles in fractions of a second, but combinatorial explosion occurs as increases, creating limits to the properties of Sudokus that can be constructed, analyzed, and solved as increases.
Techniques
Backtracking
Some hobbyists have developed computer programs that will solve Sudoku puzzles using a backtracking algorithm, which is a type of brute force search. Backtracking is a depth-first search, because it will completely explore one branch to a possible solution before moving to another branch. Although it has been established that approximately 5.96 x 1026 final grids exist, a brute force algorithm can be a practical method to solve Sudoku puzzles.A brute force algorithm visits the empty cells in some order, filling in digits sequentially, or backtracking when the number is found to be not valid. Briefly, a program would solve a puzzle by placing the digit "1" in the first cell and checking if it is allowed to be there. If there are no violations then the algorithm advances to the next cell and places a "1" in that cell. When checking for violations, if it is discovered that the "1" is not allowed, the value is advanced to "2". If a cell is discovered where none of the 9 digits is allowed, then the algorithm leaves that cell blank and moves back to the previous cell. The value in that cell is then incremented by one. This is repeated until the allowed value in the last cell is discovered.
The animation shows how a Sudoku is solved with this method. The puzzle's clues remain fixed while the algorithm tests each unsolved cell with a possible solution. Notice that the algorithm may discard all the previously tested values if it finds the existing set does not fulfill the constraints of the Sudoku.
Advantages of this method are:
- A solution is guaranteed.
- Solving time is mostly unrelated to degree of difficulty.
- The algorithm is simpler than other algorithms, especially compared to strong algorithms that ensure a solution to the most difficult puzzles.
A different approach which also uses backtracking, draws from the fact that in the solution to a standard sudoku the distribution for every individual symbol must be one of only 46656 patterns.
In manual sudoku solving this technique is referred to as pattern overlay or using templates and is confined to filling in the last values only.
A library with all the possible patterns may get loaded or created at program start. Then every given symbol gets assigned a filtered set with those patterns, which are in accordance with the given clues.
In the last step, the actual backtracking part, patterns from these sets are tried to be combined or overlaid in a non-conflicting way until the one permissible combination is hit upon.
The Implementation is exceptionally easy when using bit vectors, because for all the tests only bit-wise logical operations are needed, instead of any nested iterations across rows and columns.
Significant optimization can be achieved by reducing the sets of patterns even further during filtering. By testing every questionable pattern against all the reduced sets that were already accepted for the other symbols the total number of patterns left for backtracking is greatly diminished.
And as with all sudoku brute-force techniques, run time can be vastly reduced by first applying some of the most simple solving practices which may fill in some 'easy' values.
A Sudoku can be constructed to work against backtracking. Assuming the solver works from top to bottom, a puzzle with few clues, no clues in the top row, and has a solution "987654321" for the first row, would work in opposition to the algorithm. Thus the program would spend significant time "counting" upward before it arrives at the grid which satisfies the puzzle. In one case, a programmer found a brute force program required six hours to arrive at the solution for such a Sudoku. Such a Sudoku can be solved nowadays in less than 1 second using an exhaustive search routine and faster processors.p:25
Stochastic search / optimization methods
Sudoku can be solved using stochastic algorithms. An example of this method is to:- Randomly assign numbers to the blank cells in the grid.
- Calculate the number of errors.
- "Shuffle" the inserted numbers until the number of mistakes is reduced to zero.
Constraint programming
A Sudoku may also be modelled as a constraint satisfaction problem. In his paper Sudoku as a Constraint Problem, Helmut Simonis describes many reasoning algorithms based on constraints which can be applied to model and solve problems. Some constraint solvers include a method to model and solve Sudokus, and a program may require fewer than 100 lines of code to solve a simple Sudoku. If the code employs a strong reasoning algorithm, incorporating backtracking is only needed for the most difficult Sudokus. An algorithm combining a constraint-model-based algorithm with backtracking would have the advantage of fast solving time – of the order of a few milliseconds – and the ability to solve all sudokus.Exact cover
Sudoku puzzles may be described as an exact cover problem, or more precisely, an exact hitting set problem. This allows for an elegant description of the problem and an efficient solution. Modelling Sudoku as an exact cover problem and using an algorithm such as Knuth's Algorithm X and his Dancing Links technique "is the method of choice for rapid finding of all possible solutions to Sudoku puzzles."An alternative approach is the use of Gauss elimination in combination with column and row striking.
Relations and residuals
Let Q be the 9x9 Sudoku matrix, N =, and X represent a generic row, column, or block. N supplies symbols for filling Q as well as the index set for the 9 elements of any X. The given elements q in Q represent a univalent relation from Q to N. The solution R is a total relation and hence a function. Sudoku rules require that the restriction of R to X is a bijection, so any partial solution C, restricted to an X, is a partial permutation of N.Let T =, so T has 27 elements. An arrangement is either a partial permutation or a permutation on N. Let Z be the set of all arrangements on N. A partial solution C can be reformulated to include the rules as a composition of relations A and B requiring compatible arrangements:
Solution of the puzzle, suggestions for new q to enter Q, come from prohibited arrangements, the complement of C in Qx''Z'': useful tools in the calculus of relations are residuals: