sudoku


sudoku, a MATLAB code which handles Sudoku puzzles.

The most interesting routine is sudoku_solve.m, which accepts a partially worked-out Sudoku, and returns a solution. It does this by using an exhaustive search. That is, it finds the first empty spot in the grid, and then tries, in turn, putting in each of the digits in that spot. Once it puts in the trial digit, it calls itself again with the slightly more filled-in puzzle. If the routine fills in the whole puzzle, it returns to the user. Otherwise, it backs up to the most recent trial digit, and increases it by one. And if that's not possible, because we got to "9", it sets that cell back to blank, and backs up to the previous trial digit.

If there is a solution, this procedure should find it. However, I don't know what the routine will do if there is no solution. I don't know what the routine will do if there are multiple solutions (I assume it just comes back with one solution.) And the routine does not check that your partially filled input puzzle is consistent (doesn't have a 12 in one cell, or two 5's in one row.)

A Sudoku has a lot of symmetry. You can permute the digits. You may swap two rows if they are in the same row of boxes. You may swap two columns if they are in the same column of boxes. You may swap two columns of boxes, or two rows of boxes. Each of these operations preserves the underlying structure of the Sudoku.

Licensing:

The computer code and data files described and made available on this web page are distributed under the MIT license

Languages:

sudoku is available in a MATLAB version.

Related Data and Programs:

combo, a MATLAB code which includes several examples of backtracking routines used to solve problems such as the placement of Queens on a chessboard.

subset, a MATLAB code which includes several examples of backtracking routines used to solve problems such as the number of derangements (complete scramblings) of a set.

sudoku_test

Author:

GM Boynton wrote sudoku_solve.m.

Reference:

  1. Brian Hayes,
    Unwed Numbers,
    American Scientist,
    Volume 94, pages 12-15, January-February 2006.
  2. Robin Wilson,
    The Sudoku Epidemic,
    Focus,
    pages 5-7, January 2006.

Source Code:


Last revised on 18 February 2019.