Sudoku Report
Sudoku Report
Sudoku Report
Sudoku explained
Sudoku is a number placement puzzle. In this puzzle you are given an N x N grid
of cells. The grid itself is composed of M x K sub-grids. You can place a single
digit, drawn from 1 to N, in any cell. Initially the grid will have some of its cells
partially filled. The objective of the puzzle is to complete the grid so that:
1. Every cell contains a digit.
2. No digit appears twice in any row, column of the N x N grid or in any row,
column of any of the M x K sub-grid.
Backtracking
SudokuGridDFS.java
Backtracking is a recursive algorithm. Using it without any heuristic is a nave
approach for solving any constraint satisfaction problem.
From the above pseudo code it can be seen that the backtracking approach
makes recursive calls until it finds the board inconsistent. When it finds the board
inconsistent it backtracks to find another assignment of variables for which the
boards will be consistent. Pseudo code for this has been given above.
Implementation specifics:
SudokuGridFwdChecking.java
After assigning a value to the most restricted cell we check the constraint list of
each of its neighbours that have not been assigned a value as yet. If the
constraint list of any of these neighbours is equal to N i.e. if the neighbouring cell
cannot take any more values then we backtrack.
Results:
For Test 1
6 2 3
_,_,6,_,5,_
_,1,_,2,_,_
_,_,1,_,_,_
_,_,_,3,_,_
_,_,4,_,1,_
_,2,_,4,_,_
Method
Consistency Checks
Time(ms)
Memory(MB
)
Backtracking
474
~0
1.23
Backtracking + MRV
31
~0
0.62
30
~0
0.62
Backtracking + MRV + CP
27
~0
1.25
Method
Consistency Checks
Time(ms)
Memory(MB)
Backtracking
61386
~0
3.7
Backtracking +
MRV
50
~0
0.62
Backtracking +
MRV + Fwd Check
51
~0
0.62
Backtracking +
MRV + CP
51
~0
3.7
Method
Consistency
Checks
Time(ms)
Memory(MB)
Backtracking
163104392
~54596
0.62
Backtracking +
MRV
11576
~0
4,32
Backtracking +
MRV + Fwd Check
12990
~0
6.16
Backtracking +
MRV + CP
6278
5041
21.5
Running Tests
Run the code in the following way
Change the method as required in TestSudoku.METHOD java code and call the
java code in the following manner
java ai.TestSudoku test_case_file
where test_case_file has entry like
6 2 3
_,_,6,_,5,_
_,1,_,2,_,_
_,_,1,_,_,_
_,_,_,3,_,_
_,_,4,_,1,_
_,2,_,4,_,_
000