You are on page 1of 2

Sudoku solver using csp

Using the backtracking algorithm, which is a form of constraint satisfaction problem (CSP)
solving technique.

Initial State: The starting state of the Sudoku board, typically with some cells filled and others
empty.

Final State: The completed Sudoku board where all cells are filled according to Sudoku rules.

• Backtracking:
• The solve_sudoku function is a recursive function that tries to fill the Sudoku board
cell by cell.
• It chooses the cell to fill by looking for an empty cell (0). The algorithm then tries to
assign a valid number to this cell.
• If placing a number in a cell leads to a situation where no further valid numbers can
be placed in later cells, the function backtracks by resetting the number to 0 and
trying a different number. This is the essence of the backtracking algorithm -
revising decisions when they lead to a dead-end.

• Forward Checking:
• After assigning a number to a cell, the forward_checking function updates the
possibilities data structure. This function removes the assigned number from the
set of possible values for all cells in the same row, column, and 3x3 square.
• This process reduces the number of potential values for these cells, which makes the
next steps more efficient as the algorithm does not need to consider numbers that
would at once violate the Sudoku rules.
• Forward checking helps to prune the search space, meaning the backtracking
algorithm has fewer paths to explore before finding a solution or concluding that no
solution is possible.

Combining backtracking with forward checking makes the Sudoku solver more efficient
than using simple backtracking alone. The backtracking method systematically explores
practical solutions, while forward checking reduces the number of unnecessary
explorations by eliminating invalid choices at each step.

You might also like