sudoku


sudoku, a Python code which seeks a single solution of a Sudoku puzzle using backtracking.

The function sudoku_solve() accepts a partially completed Sudoku puzzle, and tries to compute a full solution. It does this by using an exhaustive search. That is, it finds the first empty cell in the grid, and then tries, in turn, putting in each of the digits in that cell. 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. If there are multiple solutions, this procedure only finds a single solution.

Licensing:

The information on this web page is distributed under the MIT license.

Languages:

sudoku is available in a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

sudoku, a data directory which contains examples of Sudoku puzzles. A 9x9 grid of 3x3 boxes and 81 cells is presented. In a completed Sudoku, the digits 1 through 9 are placed in the 81 cells, in such a way that each row, column and box contains each digit exactly once. A Sudoku puzzle is a partially completed Sudoku in which the user must figure out how to complete the pattern.

Author:

The original Python version of sudoku_solve() was written by Parineeth MR. This version by John Burkardt.

Reference:

  1. Brian Hayes,
    Unwed Numbers,
    American Scientist,
    Volume 94, pages 12-15, January-February 2006.
  2. Parineeth MR,
    The Big Book of Coding Interviews in Python,
    Third Edition, 2018,
    ISBN13: 978-1983861185
  3. Robin Wilson,
    The Sudoku Epidemic,
    Focus,
    pages 5-7, January 2006.

Source Code:


Last revised on 03 August 2025.