Sudoku Solving Algorithms - Wikipedia
Sudoku Solving Algorithms - Wikipedia
There are several computer algorithms that will solve most 9×9 puzzles
(n=9) in fractions of a second, but combinatorial explosion occurs as n
increases, creating limits to the properties of Sudokus that can be
A typical Sudoku puzzle
constructed, analyzed, and solved as n increases.
Contents
Techniques
Backtracking
Stochastic search / optimization methods
Constraint programming
Exact cover
Developing (searching for) Sudokus
See also
References
External links
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.[2] Backtracking is a depth-first search (in contrast to a breadth-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 6.67 x 1021 final grids exist, a brute force algorithm can be a practical method to solve Sudoku puzzles.
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms 1/5
18.03.2018 Sudoku solving algorithms - Wikipedia
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.[3][4][5][6] 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 (checking row, column, and box constraints) 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 (81st) cell is discovered.
The animation shows how a Sudoku is solved with this method. The
puzzle's clues (red numbers) remain fixed while the algorithm tests each
A Sudoku (top) being solved by
unsolved cell with a possible solution. Notice that the algorithm may backtracking. Each cell is tested for a
discard all the previously tested values if it finds the existing set does valid number, moving "back" when there is
not fulfil the constraints of the Sudoku. a violation, and moving forward again until
the puzzle is solved.
Advantages of this method are:
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms 2/5
18.03.2018 Sudoku solving algorithms - Wikipedia
A solution to the puzzle is then found. Approaches for shuffling the numbers include simulated annealing, genetic
algorithm and tabu search. Stochastic-based algorithms are known to be fast, though perhaps not as fast as deductive
techniques. Unlike the latter however, optimisation algorithms do not necessarily require problems to be logic-solvable,
giving them the potential to solve a wider range of problems. Algorithms designed for graph colouring are also known to
perform well with Sudokus.[11] It is also possible to express a Sudoku as an integer linear programming problem. Such
approaches get close to a solution quickly, and can then use branching towards the end. The simplex algorithm is able to
solve non-proper Sudokus, indicating if the Sudoku is not valid (no solution), or providing the set of answers when there is
more than one solution.
Constraint programming
A Sudoku may also be modelled as a constraint satisfaction problem. In his paper Sudoku as a Constraint Problem,[12]
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 less than
100 lines of code to solve a simple Sudoku.[13][14] 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, and the ability to solve all sudokus.
Exact cover
Sudoku puzzles may be described as an exact cover 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 dancing links will typically
solve a Sudoku in a few milliseconds.
One common method of searching for A Sudoku with 17 clues and An automorphic Sudoku with 18
Sudokus with a particular diagonal symmetry.[15] clues and two-way diagonal
characteristic is called neighbor symmetry.[16]
searching. Using this strategy, one or
more known Sudokus which satisfy or
nearly satisfy the characteristic being searched for is used as a starting point, and these Sudokus are then altered to look
for other Sudokus with the property being sought. The alteration can be relocating one or more clue positions, or removing
a small number of clues, and replacing them with a different number of clues. For example, from a known Sudoku, a
search for a new one with one fewer clues can be performed by removing two clues and adding one clue in a new location.
(This can be called a {-2,+1} search). Each new pattern would then be searched exhaustively for all combinations of clue
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms 3/5
18.03.2018 Sudoku solving algorithms - Wikipedia
See also
Sudoku
Mathematics of Sudoku
Mathematics of Sudoku (Minimum number of givens section)
Mathematics of Sudoku (Automorphic Sudokus section)
Combinatorial explosion - (with summary of grid count of Sudoku compared to Latin squares)
Glossary of Sudoku
References
1. "Star Burst - Polar Graph" (https://www.flickr.com/photos/npcomplete/2361922699) A polar chart showing a solution
path for a Sudoku (Star Burst) using an exhaustive search routine and comment about 17-clue Sudoku.
2. http://intelligence.worldofcomputing/brute-force-search (http://intelligence.worldofcomputing.net/ai-search/brute-force-
search.html#.WPWK6oWcEiQ) Brute Force Search, December 14th, 2009.
3. "Backtracking - Set 7 (Sudoku)" (https://web.archive.org/web/20160828164622/http://www.geeksforgeeks.org/backtra
cking-set-7-suduku/). GeeksforGeeks. GeeksforGeeks. Archived from the original (http://www.geeksforgeeks.org/back
tracking-set-7-suduku/) on 2016-08-28. Retrieved 24 December 2016.
4. Norvig, Peter. "Solving Every Sudoku Puzzle" (http://www.norvig.com/sudoku.html). Peter Norvig (personal website).
Archived from the original (http://www.norvig.com/sudoku.html) on 20 Oct 2016. Retrieved 24 December 2016.
5. "Chart of Cells Visited for Solution" (https://www.flickr.com/photos/npcomplete/2341937186) A chart showing a
solution path to a difficult Sudoku.
6. Zelenski, Julie (July 16, 2008). Lecture 11 | Programming Abstractions (Stanford) (https://www.youtube.com/watch?v=
p-gpaIGRCQI). Stanford Computer Science Department.
7. "Star Burst Leo - Polar Graph" (https://www.flickr.com/photos/npcomplete/2384354604) A polar chart showing a
solution path for a Sudoku (Star Burst Leo) using an exhaustive search routine.
8. "Chart of Cells Visited for Solution" (https://www.flickr.com/photos/npcomplete/2341937186) A chart showing a
solution path for a difficult Sudoku using an exhaustive search routine.
9. Lewis, R (2007) Metaheuristics Can Solve Sudoku Puzzles Journal of Heuristics, vol. 13 (4), pp 387-401.
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms 4/5
18.03.2018 Sudoku solving algorithms - Wikipedia
10. Perez, Meir and Marwala, Tshilidzi (2008) Stochastic Optimization Approaches for Solving Sudoku arXiv:0805.0697.
11. Lewis, R. A Guide to Graph Colouring: Algorithms and Applications. Springer International Publishers, 2015.
12. Simonis, Helmut (2005). "Sudoku as a Constraint Problem" (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.
1.88.2964) (PDF). Cork Constraint Computation Centre at University College Cork: Helmut Simonis. Retrieved
8 December 2016. "paper presented at the Eleventh International Conference on Principles and Practice of
Constraint Programming"
13. Multiple Authors. "Java Constraint Programming solver" (http://jacop.osolpro.com) (Java). JaCoP. Krzysztof
Kuchcinski & Radoslaw Szymanek. Retrieved 8 December 2016.
14. Rhollor. "Sudokusolver" (https://github.com/Rhollor/SudoKuSolver) (C++). GitHub. Rhollor. Retrieved 8 December
2016.
15. "Symmetrical 17 Clue Puzzle" (https://www.flickr.com/photos/npcomplete/2599486458) Symmetrical 17 Clue Puzzle.
16. "18 Clue Automorphic Sudoku" (https://www.flickr.com/photos/npcomplete/2691474926) 18 Clue Automorphic
Sudoku.
17. 17 Clue Sudoku "R929-3E01" (https://www.flickr.com/photos/npcomplete/3469184185) A 17 clue sudoku on a
symmetrical pattern of 20 positions.
18. 18 Clue Sudoku "R828-S09" (https://www.flickr.com/photos/npcomplete/3469184183) An 18 clue sudoku with
horizontal symmetry.
19. Royle, Gordon. "Minimum Sudoku" (http://www.csse.uwa.edu.au/~gordon/sudokumin.php). Retrieved October 20,
2013.
20. http://forum.enjoysudoku.com (http://forum.enjoysudoku.com/no-new-17s-within-3-3-t33171.html) The New Sudoku
Players' Forum "No new 17s within {-3+3}".
External links
http://diuf.unifr.ch/pai/people/juillera/Sudoku/Sudoku.html Sudoku Explainer by Nicolas Juillerat (Popular for rating
Sudokus in general)
http://www2.research.att.com/sw/download/man/man1/sudoku.html sudoku by gsf (Popular for rating the hardest
Sudokus amongst other things)
A Pencil-and-Paper Algorithm for Solving Sudoku Puzzles (http://www.ams.org/notices/200904/rtx090400460p.pdf)
quoted also in a popular British newspaper the Daily Mail (http://www.dailymail.co.uk/news/article-1163925/Puzzling-b
ehaviour-Maths-professor-finds-formula-solve-ANY-Sudoku.html)
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia
Foundation, Inc., a non-profit organization.
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms 5/5