12 Backtracking Algorithm for solving Sudoku Problem
12 Backtracking Algorithm for solving Sudoku Problem
Sudoku Problem
Sudoku
Sudoku is a number-placement puzzle where the objective is to fill a square grid of
size ‘n’ with numbers between 1 to ‘n’. The numbers must be placed so that each
column, each row, and each of the sub-grids (if any) contains all of the numbers
from 1 to ‘n’.
The most common Sudoku puzzles use a 9x9 grid. The grids are partially filled
(with hints) to ensure a solution can be reached.
And here’s the solution. Notice how each row, each column and each sub-grid have
all numbers from 1 to 9. Some puzzles may even have multiple solutions.
Sudoku & Backtracking
We will now create a Sudoku solver using backtracking by encoding our problem,
goal and constraints in a step-by-step algorithm.
Problem
Given a, possibly, partially filled grid of size ‘n’, completely fill the grid with
number between 1 and ‘n’.
Goal
Goal is defined for verifying the solution. Once the goal is reached, searching
terminates. A fully filled grid is a solution if:
Constraints
Constraints are defined for verifying each candidate. A candidate is valid if:
Termination conditions
Typically, backtracking algorithms have termination conditions other than reaching
goal. These help with failures in solving the problem and special cases of the
problem itself.
1. There are no empty spots left to fill and the candidate still doesn’t qualify as a
the solution.
2. There are no empty spots to begin with, i.e., the grid is already fully filled.
Step-by-step algorithm
Here’s how our code will “guess” at each step, all the way to the final solution:
Now let’s try all this in practice with a simple 3x3 grid.
We start off by listing all the empty spots. If we label each cell in the grid with a
pair of numbers (x,y) and mark the first cell (1,1), then our empty spots will be at
locations:
One more spot is filled. Also, it might not look like it, but we did just perform
backtracking on a single spot. We abandoned a candidate solution (1 at spot (2,2)),
visited a previous stage (empty spot (2,2)) and explored a new candidate solution
(number 2 at spot (2,2)).
When we move on to spot (2,3), we have another problem. As you can see, we are
all out of options. None of the possible numbers fit in. This means that we must
now abandon candidate and repeat step 2 with the next number. Only this time, we
must visit spot (2,2) first to fix spot (2,3).
We need to fill number 3 in spot (2,2) and that will resolve the issue.
We now repeat this process until with either reach the goal or we hit one of the
termination conditions.
Since this was a demo problem, it should be obvious that we’d arrive at the solution
without any further complications.
However, consider the same grid with one small change. Replacing the 1 in
cell (3,3) with a 2 renders the grid unsolvable. Similarly, removing hints from
cells (2,1) and (3,3) allows for multiple solutions. But since this algorithm has a
single goal, it stops after the first solution is reached.