Hacker News new | comments | show | ask | jobs | submit login

solving without trial and error at all.

It really depends on why you want to solve them.

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

That strikes me as extremely unlikely.

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)

Glenn Flower has written an (open source) program that solves using constraint propagation only. It does solve some of the hardest Sudoku's.

He discusses the strategies used here: http://www2.research.att.com/~gsf/sudoku/

From the first paragraph of your link:

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

You are right that "the purpose of many the constraint techniques seem to be aimed at creating puzzles that are amenable to humans, not solving them"

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.

The constraint-finding is for generating puzzles that are easy, hence the comment about puzzle manners.

I usually solve them by hand without guess and check, though occasionally, you get down to where there are pairs of values where you have no choice but to try one and see what works (or maybe I just need to figure out more constraints to use).

Sudoku are designed so that there is always a correct move that does not require guesswork.

That is not true.

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

Well, an ambiguous Sudoku is like a 12 line sonnet...

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

I believe the above puzzle has only one valid solution.

9x9 grid of digits with an ambiguous solution is not a Sudoku. It is part of the definition.

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.

If a Sudoku has one and only one solution, then it is always solvable without guessing or backtracking. It just might not be within the scope of the human brain. There exist (many) configurations where every cell is influenced by every other, so to solve any one cell essentially requires a simultaneous solve of the entire puzzle. There's no theoretical reason a human couldn't do that too; it's a problem simply of computational capacity not fundamental approach.

How would you structure a program to solve such a puzzle without having it generate and test potential solutions along the way?

If you do the constraint propagation right, the number of trials and errors will be very small (and it will be only one trial and no errors for the easy ones).

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

That would be a big database. (10^21 entries,roughly.) http://en.wikipedia.org/wiki/Mathematics_of_Sudoku#Enumerati...

That's why I called it pathologic... ;)

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.

> I'd be very interested to see what an algorithm that solves any valid sudoku puzzle looks like.

You might be interested in the article linked to by this post.

The code in the post is based on depth-first search, which basically is a trial and error approach. A well implemented and effective one thanks to constraint propagation, but it is still trial and error. And there are cases, in which backtracking (trial and error) is necessary - at least one of them is documented in the post (the puzzle "hard1" that took 188 seconds).

There is no meaningful difference between trial and error and without trial and error. It makes this no less of an algorithm.

If you get down to trial and error at all, your best odds are 50/50. If it's easy enough to guarantee the right guess, it's not a guess.

There is no meaningful difference between trial and error and without trial and error.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact