5
International Journal of Advance Foundation and Research in Computer (IJAFRC) Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853 1 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org Efficient Algorithm Comparison To Solve Sudoku Puzzle Anuj Thaker, Chinmay Sawant, Nikhil Patel,Prof. Avani Sakhapara Dept. of Information Technology, K.J.Somaiya College of Engineering [email protected] , [email protected] , [email protected] , [email protected] Dept. of Information Technology, K.J.Somaiya College of Engineering, Mumbai A B S T R A C T Sudoku puzzles are logic-based number-placement puzzles and each has a unique solution. These Sudoku puzzles are entertaining and are good exercise of the brain. There are number of algorithms available to solve such Sudoku. In this paper we are comparing two different algorithms Backtracking and Constraint propagation and also we have proposed a new method improved version of constraint propagation. The improved constraint propagation solves the Sudoku more efficientl and has less time and number of iterations. General Terms: Cell, Grid, Domain. Keywords: Sudoku, Puzzle, Backtracking, Constraint Propagation, Improved Constraint Propagation. I. INTRODUCTION In real life we come across Sudoku puzzles of varying difficulty levels in newspapers and other text and digital media. It is a common leisure activity for a lot of people. However, it is observed that the solution is not always immediately available for the user’s verification. In most cases, people have to wait till the next day to check the solutions of the Sudoku they just solved. Hence our motivation for this project was to develop an application for it. In this method the user has to enter the values from the given Sudoku problem and the algorithm returns the complete solution of the same. Sudoku is a logic based puzzle with the goal to complete a 9x9 grid so that every row, every column and all of the nine 3x3 boxes contain the numbers 1 through 9, given a partial filing to a unique solution. The problem itself is a popular brainteaser but can easily be used as an algorithmic problem. The latest version of Sudoku was first introduced in Japan in the eighties and has since then attracted a lot of programmers and therefore there are a great deal of different Sudoku solving algorithms available on the Internet, both implementations and construction methods. Sudoku solvers are interesting because they are practical applications of theoretical problems, and most people are familiar with it. Different algorithms have been used to solve this logical problem. We are using two different approaches to solve the puzzle and will compare them. We will implement the Recursive Backtracking and Constraint Propagation algorithms. Our task is to find the efficient algorithm between the two. The rest of the paper is Section 2 the review of Literature, Section 3 Algorithms, Section 4 Result, Section 5 Conclusion and Section 6 the References. II. LITERATURE REVIEW A. OVERVIEW Approach used here is that the users are required to manually enter the Sudoku puzzle [1] and this numbers are used in Backtracking algorithm [1] to generate the solution. The algorithm will randomly

Efficient Algorithm Comparison To Solve Sudoku Puzzle

  • Upload
    ijafrc

  • View
    7

  • Download
    5

Embed Size (px)

DESCRIPTION

Sudoku puzzles are logic-based number-placement puzzles and each has a unique solution. TheseSudoku puzzles are entertaining and are good exercise of the brain. There are number ofalgorithms available to solve such Sudoku. In this paper we are comparing two differentalgorithms Backtracking and Constraint propagation and also we have proposed a new methodimproved version of constraint propagation. The improved constraint propagation solves theSudoku more efficientl and has less time and number of iterations.

Citation preview

International Journal of Advance Foundation and Research in Computer (IJAFRC)

Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853

1 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org

Efficient Algorithm Comparison To Solve Sudoku Puzzle Anuj Thaker, Chinmay Sawant, Nikhil Patel,Prof. Avani Sakhapara

Dept. of Information Technology, K.J.Somaiya College of Engineering

[email protected], [email protected], [email protected],

[email protected]

Dept. of Information Technology, K.J.Somaiya College of Engineering, Mumbai

A B S T R A C T

Sudoku puzzles are logic-based number-placement puzzles and each has a unique solution. These

Sudoku puzzles are entertaining and are good exercise of the brain. There are number of

algorithms available to solve such Sudoku. In this paper we are comparing two different

algorithms Backtracking and Constraint propagation and also we have proposed a new method

improved version of constraint propagation. The improved constraint propagation solves the

Sudoku more efficientl and has less time and number of iterations.

General Terms: Cell, Grid, Domain.

Keywords: Sudoku, Puzzle, Backtracking, Constraint Propagation, Improved Constraint

Propagation.

I. INTRODUCTION

In real life we come across Sudoku puzzles of varying difficulty levels in newspapers and other text and

digital media. It is a common leisure activity for a lot of people. However, it is observed that the solution

is not always immediately available for the user’s verification. In most cases, people have to wait till the

next day to check the solutions of the Sudoku they just solved. Hence our motivation for this project was

to develop an application for it. In this method the user has to enter the values from the given Sudoku

problem and the algorithm returns the complete solution of the same. Sudoku is a logic based puzzle with

the goal to complete a 9x9 grid so that every row, every column and all of the nine 3x3 boxes contain the

numbers 1 through 9, given a partial filing to a unique solution. The problem itself is a popular

brainteaser but can easily be used as an algorithmic problem. The latest version of Sudoku was first

introduced in Japan in the eighties and has since then attracted a lot of programmers and therefore there

are a great deal of different Sudoku solving algorithms available on the Internet, both implementations

and construction methods. Sudoku solvers are interesting because they are practical applications of

theoretical problems, and most people are familiar with it.

Different algorithms have been used to solve this logical problem. We are using two different approaches

to solve the puzzle and will compare them. We will implement the Recursive Backtracking and Constraint

Propagation algorithms. Our task is to find the efficient algorithm between the two. The rest of the paper

is Section 2 the review of Literature, Section 3 Algorithms, Section 4 Result, Section 5 Conclusion and

Section 6 the References.

II. LITERATURE REVIEW

A. OVERVIEW

Approach used here is that the users are required to manually enter the Sudoku puzzle [1] and this

numbers are used in Backtracking algorithm [1] to generate the solution. The algorithm will randomly

International Journal of Advance Foundation and Research in Computer (IJAFRC)

Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853

2 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org

enter a number from 1 to 9 in the blank grid location and in case of some conflict in the Sudoku rules, we

will go back and change the number that we inserted before. Another method used here is Constrain

Propagation, where we check for possible entries for a blank location using a possibility array[2].

B. Algorithms

1. Backtracking algorithm

Here, for solving the Sudoku we use recursive backtracking tactics. After obtaining the empty virtual grid

and storing all the numbers from the image, we put zeroes for all the blank spaces we need to fill. To

obtain solution we identify all those spaces where there are zeroes and fill those spaces with numbers

from 1-9 based on Sudoku rules. We use this technique to find assignments at all the blank grid locations

recursively. Here what the algorithm does is assign a number to a blank location from 1-9 based on the

Sudoku rules and go to the next blank location, if there is some conflict in assigning value to a particular

blank space then the algorithm will go back to the previous location where we inserted the value, change

the assignment keeping the rules satisfied. When all the blank grid locations are filled we will display the

solved Sudoku to the user, if we fail to find solution for different blank grid locations we will use different

combination of numbers and try to solve the Sudoku. If all the combinations fail to give a solution, we

conclude that the particular grid is unsolvable.

2. Pseudo code

Procedure SUDOKUSOLUTION (row,col)

Begin

if row > 8

return true

if grid [row,col] is set

return NEXT(row,col)

else if grid[row,col] is zero

for i=1 to 9

if it does not violate constraints then

grid[row,col]=i

if NEXT(row,col)==true

return true

grid[row,col]=0

return false

End

Procedure NEXT(row,col)

Begin

if col==8

return SUDOKUSOLUTION(row+1,0)

else

return SUDOKUSOLUTION(row, col+1)

International Journal of Advance Foundation and Research in Computer (IJAFRC)

Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853

3 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org

End

C. Constraint Propagation

Constraint propagation is an algorithm that solves Sudoku problems by using possibility array and

multiple iterations. For this algorithm we will initially fill all the space with the values given in the

question, after that we will put zeroes in the blank locations. In this we will find all the possible numbers

for a particular blank location and we will have an array for each blank location, if for a particular blank

location there is only one possibility then we will directly insert the value in that location, otherwise we

will have multiple iterations following to complete the Sudoku problem. In some cases where the Sudoku

problem is of hard or extreme level we won't have a possible value after multiple iterations. For most of

the cases constraint propagation will be enough to fill the grid completely and without any conflict.

Limitations:

When single possible value is not available for some grids then the constraint propagation algorithm

enters infinite loop and consequently is unable to solve the Sudoku problem.

Modified Constraint Propagation:

The modified version that we are developing will be able to solve such Sudoku problems efficiently.

1. Pseudo code

Procedure init (grid[][])

Begin

for i=0 to 8 for j= 0 to 8

Cell[i][j] =grid[i][j]

Cell[i][j].domain.clear()

for i=0 to 8

for j= 0 to 8

if grid [i][j] is NOT 0

for k= 0 to 8

Cell[i][k].domain.remove(grid[i][j])

assignIfOnlyOne(i,k)

for k= 0 to 8

Cell[k][j].domain.remove(grid[i][j])

assignIfOnlyOne(k,j)

for k = 0 to 3

for kk = 0 to 3

Cell[floor(i/3)*3+k ][ floor(j/3)*3+kk]

.d

omain.remove(grid[i][j])

assignIfOnlyOne(k, kk)

End

Procedure assignIfOnlyOne (row, col)

Begin

International Journal of Advance Foundation and Research in Computer (IJAFRC)

Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853

4 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org

if(Cell[row][col].domain.size() ==1

Cell[row][col] = Cell[row][col].domain.get(0)

Cell[row][col].domain.clear()

for k= 0 to 8

Cell[i][k].domain.remove(values[i][j])

assignIfOnlyOne(i,k)

for k= 0 to 8

Cell[k][j].domain.remove(values[i][j])

assignIfOnlyOne(k,j)

for k = 0 to 3

for kk = 0 to 3

Cell[floor(i/3)*3+k ][ floor(j/3)*3+kk]

.domain.remove(values[i][j])

assignIfOnlyOne(k, kk)

End

III. RESULT

Comparing the time and number of iterations of Backtracking and Constraint Propagation.

Name of the

Algorithm

Time and Iterations

for Easy level sudoku

Time and Iterations

for Medium level

sudoku

Time and Iterations

for Hard level

sudoku

Backtracking 327 iterations

65 milliseconds

1501 iterations

187 milliseconds

12058 iterations

1040 milliseconds

Constraint

Propagation

1630 iterations

272 milliseconds

7386 iterations

580 milliseconds

60464 iterations

6052 milliseconds

Improved Constraint

Propagation

455 iterations

77 milliseconds

2823 iterations

250 milliseconds

23083 iterations

631 milliseconds

IV. CONCLUSION

The simulation result shows us that Backtracking algorithm give better solution for the 9x9 grid Sudoku

puzzle with no violation of constraints and it requires less time, number of iterations and has better

performance. On the other hand Constraint Propagation algorithm is more time consuming. We have

found certain limitations while executing constraint propagation i.e. When single possible value is not

available for some grids then the constraint propagation algorithm enters infinite loop and consequently

is unable to solve the Sudoku problem, so we have developed an improved version of constraint

propagation that solves the Sudoku problem more effectively.

V. ACKNOWLEDGMENT

We would like to thank K.J. Somaiya College of Engineering for the platform that they have provided us

and knowledge base, and most importantly we would like to acknowledge the help and efforts of Prof.

AvaniSakhapara.

International Journal of Advance Foundation and Research in Computer (IJAFRC)

Volume 2, Issue 5, May - 2015. ISSN 2348 – 4853

5 | © 2015, IJAFRC All Rights Reserved www.ijafrc.org

VI. REFERENCE

[1] Arnab Kumar Maji and Rajat Kumar Pal, “Sudoku Solving using Minigrid based Backtracking”,

University of Kolkata and north eastern hill university, INDIA, 2014

[2] Arnab Kumar Maji and Rajat Kumar Pal,“ Sudoku Solving using Minigrid based Backtracking”,

University of Kolkata and north eastern hill university, INDIA, 2014

[3] Ali Fatih Gunduz, Larasmoyo Nugroho, Afşar Saranli," Sudoku Problem Solving using

Backtracking, Constraint Propagation, Stochastic Hill Climbing and Artificial Bee Colony

Algorithms",

[4] http://en.wikipedia.org/wiki/Backtracking(accessed on 12/10/2014)

[5] http://en.wikipedia.org/wiki/Sudoku_solving_algorithms#Backtracking (accessed on 15 /10 /

14}