Computer solutions

For most computer programmers, coding the search for cell values based on elimination, contingencies and multiple contingencies (required for harder Sudoku) is relatively straightforward. These programs emulate the human logic to solve a puzzle without resorting to guesses. Given the self-imposed constraints of most Sudoku publishers, this method generally succeeds.

It is also fairly simple to build a backtracking search. Typically this involves assigning a value (say, 1, or the nearest available number to 1) to the first available cell (say, the top left hand corner) and then moves on to assign the next available value (say, 2) to the next available cell. This continues until a conflict occurs, in which case the next alternative value is used for the last cell changed. If a cell cannot be filled, the program backs up one level (from that cell) and tries the next value at the higher level (hence the name backtracking). Although far from computationally efficient, this "brute force" method will find a solution, given sufficient computation time (even a fairly naive implementation will typically not take a noticeable amount of time). A more efficient program could keep track of potential values for cells, eliminating impossible values until only one value remains for a cell, then filling that cell in and using that information for more eliminations, and so on until the puzzle is solved.

Another alternative uses finite domain constraint programming. A constraint program specifies the constraints of the puzzle (the fact that every number in each row, each column, and each 3×3 region must be unique, and the provided "givens"); a finite domain solver applies the constraints successively to narrow down the solution space until a solution is found. Backtracking may be applied when alternate values cannot otherwise be excluded.

A highly efficient way of solving such constraint problems is Donald Knuth's Dancing Links Algorithm. This method can be directly applied to solving Sudoku problems, counting all possible solutions for most puzzles rapidly. This is the method now preferred by many Sudoku programmers, mainly by virtue of its speed. A very fast solver is usually required for most trial-and-error puzzle-creation algorithms.


Next: Difficulty ratings »