Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CSE 2003 DATASTRUCTURE AND

ALGORITHMS REVIEW 1
SOLVING SUDOKU TABLE USING BACK
TRACKING AND ALGRORITHMS

ABSTRACT:
we are going to develop an algorithm to solve a Sudoku table using
backtracking methodology. Through backtracking we can solve ithis
problem easily.

Keywords: Sudoku,backtracking.

Introduction:

What is backtracking?

Backtracking is finding the solution of a problem whereby the


solution depends on the previous steps taken. For example,
in a maze problem, the solution depends on all the steps you
take one-by-one. If any of those steps is wrong, then it will
not lead us to the solution. In a maze problem, we first
choose a path and continue moving along it. But once we
understand that the particular path is incorrect, then we just
come back and change it. This is what backtracking basically
is.

In backtracking, we first take a step and then we see if this


step taken is correct or not i.e., whether it will give a correct
answer or not. And if it doesn’t, then we just come back and
change our first step. In general, this is accomplished by
recursion. Thus, in backtracking, we first start with a partial
sub-solution of the problem (which may or may not lead us
CSE 2003 DATASTRUCTURE AND
ALGORITHMS REVIEW 1
to the solution) and then check if we can proceed further
with this sub-solution or not. If not, then we just come back
and change it.

Thus, the general steps of backtracking are:

 start with a sub-solution

 check if this sub-solution will lead to the solution or not

 If not, then come back and change the sub-solution and


continue again

ABOUT SUDOKO AND HOW WE ARE GOING TO


SOLVE THIS PROBLEM
Sudoku is a 9x9 matrix filled with numbers 1 to 9 in
such a way that every row, column and sub-matrix
(3x3) has each of the digits from 1 to 9. We are
provided with a partially filled 9x9 matrix and have to
fill every remaining cell in it. For example, a Sudoku
problem is given below.
CSE 2003 DATASTRUCTURE AND
ALGORITHMS REVIEW 1

You can see that every row, column, and sub-matrix


(3x3) contains each digit from 1 to 9. Thus, we can
also conclude that a Sudoku is considered wrongly
filled if it satisfies any of these criteria:
1 Any row contains the same number more than once.
2 Any column contains the same number more than
once.
3 Any 3x3 sub-matrix has the same number more than
once.
In Backtracking, we first start with a sub-solution and if
this sub-solution doesn't give us a correct final answer,
then we just come back and change our sub-solution.
We are going to solve our Sudoku in a similar way.
The steps which we will follow are:
• If there are no unallocated cells, then the Sudoku is
already solved. We will just return true.
• Or else, we will fill an unallocated cell with a digit
between 1 to 9 so that there are no conflicts in any
of the rows, columns, or the 3x3 sub-matrices.
CSE 2003 DATASTRUCTURE AND
ALGORITHMS REVIEW 1
• Now, we will try to fill the next unallocated cell and if
this happens successfully, then we will return true.
Else, we will come back and change the digit we used
to fill the cell. If there is no digit which fulfills the need,
then we will just return false as there is no solution of
this Sudoku.

By
SUPRATIM
S A HARIPRASAD 18BCE0686
IPPILI AKARSH 18BCB0042

You might also like