Sudoku Solving
norvig.com
norvig.com
Why did I do this? As computer security expert Ben Laurie
has stated, Sudoku is "a denial of service attack on human
intellect". My wife was infected by the virus, and I wanted
to convince her that the problem had been solved and didn't
need any more of her time.
Very true.The "Why?" at the end of the article pretty much sums up why I can't play most board games and puzzles. Once you 'solve' sudoku, chess, connect 6, ect. it really takes the fun out of it, even if your brain doesn't calculate the solution as quick as your code.
Sudoku Susser can solve sudoku puzzles not only the brute-force way shown in the article, but also using “human” reasoning, and show you all the steps.
He acknowledges the possibility of using more "reasoning" in the article, but dismisses it:
"We could try to code more sophisticated strategies. For example, the naked twins strategy looks for two squares in the same unit that both have the same two possible digits. [...] Coding up strategies like this is a possible route, but would require hundreds of lines of code (there are dozens of these strategies), and we'd never be sure if we could solve every puzzle."
If you want to read more about constraint programming, there is an excellent overview chapter in CTM (http://www.info.ucl.ac.be/~pvr/book.html). "The Art of the Propagator" (http://dspace.mit.edu/handle/1721.1/44215) is also good, though it focuses more on arithmetic value propagation than set propagation problems like sudoku. For more advanced material, look at the clp(FD) papers by Danial Diaz (http://cri-dist.univ-paris1.fr/diaz/publications/cv-short.ht...); familiarity with Prolog terminology will be helpful.
Growing up, we had a favorite game. That is until my sister solved it in that going first you could always win. It wasn't as fun after that.
Ours was a different game: Mühle. Also Mill or Nine Men's Morris.
I just discovered that Ralph Gasser solved it in 1996 using retrograde analysis and an 18-ply alpha- beta search. [1] Becoming "the first non-trivial game to be solved that does not seem to benefit from knowledge-based methods."
http://corp.galois.com/blog/2009/3/18/solving-sudoku-using-c...
It's a sudoku solver based on Cryptol, which is "... a language tailored for cryptographic algorithms." built on top of Haskell. The amazing this is that all you need to define is a function that checks whether a given board is solved. Cryptol does the searching for you!
For instance, I just rendered "In the Year 2525" 593 times faster than a human can listen to it on a single thread of a core i3.
https://github.com/biilmann/Ruby-DLX-Sudoku-Solver
It's not hyper-fast (for speed I actually implemented it as a c-extention to ruby, but it's a long time ago and I don't think I have the code around by now) but being ruby it's fairly easy to read.
Can really recommend reading the paper on Dancing Links and playing around with the algorithm, its such a great feeling once you start visualizing how the linked list trick works :)
http://www.amrittuladhar.com/projects/sudokusolver/
EDIT: Just realized the "load puzzle" feature doesn't seem to work on Chrome, but it does in other browsers.
I think the complexity class of Sudoko is NP-Complete (a quick google confirms it's not exactly NP but very close: http://11011110.livejournal.com/23221.html?thread=19381 -- complexity is the same as solving SAT problems which have only one unique solution)
They're typically designed to be unambiguous (only one correct solution), so in that sense there is always a 'correct' move.
However, finding that correct move is exactly as hard, in the computational sense, as finding the solution to the entire puzzle.
> . . . | . . . | . 1 2
> . . . | . . . | . . 3
> . . 2 | 3 . . | 4 . .
> ------+-------+------
> . . 1 | 8 . . | . . 5
> . 6 . | . 7 . | 8 . .
> . . . | . . 9 | . . .
> ------+-------+------
> . . 8 | 5 . . | . . .
> 9 . . | . 4 . | 5 . .
> 4 7 . | . . 6 | . . .
There should only be one solution. Enjoy yourself, and remember: No guessing! (Source: http://en.wikipedia.org/wiki/Algorithmics_of_sudoku#Exceptio...)
It isn't computationally relevant but even if trial and error were a simpler approach I feel like that is not in the spirit of the game. Sudoku is a number maze, the goal is to backtrack.
Also I refuse to attempt something that is supposedly too difficult for humans to solve, I'm incapable of quitting puzzles and don't relish spending the next X hours proving it can be done :)
Within the set of initial grids that have unambiguous solutions there are ones which require guessing or backtracking, these are generally not considered proper Sudokus and are not presented to humans to solve.
A Sudoku is not a destination, it is a journey.
He discusses the strategies used here: http://www2.research.att.com/~gsf/sudoku/
> The solver uses depth first and/or breadth first with constraint propagation to prune the search
That would be backtracking.
Further, the purpose of many the constraint techniques seem to be aimed at creating puzzles that are amenable to humans, not solving them. The article itself states that for solving, backtracking with fewer constraints performs better.
I shared the link because I found his sudoku generator and the constraint methods interesting.
> That would be backtracking.
Let me quote the first para in full:
The solver uses depth first and/or breadth first tree search
with constraint propagation to prune the search for the next
best move (forms of forward checking.) There are space/time
tradeoffs between depth/breadth first search and the constraints
used; sudoku(1) has options to control the combinations.
The common characteristic for all constraints, here and elsewhere,
is that they avoid trial and error. Its fine for a computer
to guess and backtrack but a definite breach of puzzle manners
to require a human to do so.
I could be wrong but, yes, it does use backtracking to find the constraint that can give a number for an empty cell but it never has to change a number it has put in a cell. That differs from the trial and error approach that moves forward by guessing values and checking if it leads to a valid solution.I'd be very interested to see what an algorithm that solves any valid sudoku puzzle looks like. (apart from pathologic solutions like querying a database of all valid 9x9 sudoku combinations).
Edit: using the symmetries described in the same Wikipedia article, the size of the database could be reduced significantly to roughly 5*10^9, which is quite manageable.
You might be interested in the article linked to by this post.
In some cases you might want the algorithm that solves them to also give you some kinda hint on how hard it would be for a human to solve it.
The most efficient algorithm I know of to solve these kind of problems is Knuth's Dancing Links X algorithm. It's beautiful and very fun to implement.
I have a simple ruby implementation of it geared to solve sudokus online on github: https://github.com/biilmann/Ruby-DLX-Sudoku-Solver
http://isaksky.wordpress.com/2010/10/30/objected-oriented-so...
"Did you know that in order to generate a sudoku you need first to solve it?"
another php version