Group 136 - Sudoku Solver Report
Group 136 - Sudoku Solver Report
A PROJECT REPORT
Submitted by
BACHELOR OF TECHNOLOGY
in
PROGRAM OF STUDY
DECEMBER 2021
VIT BHOPAL UNIVERSITY, KOTHRIKALAN,
SEHORE
MADHYA PRADESH – 466114
BONAFIDE CERTIFICATE
Certified that this project report titled “SUDOKU SOLVER USING
Certified further that to the best of my knowledge the work reported at this time
does not form part of any other project/research work based on which a degree
i
ACKNOWLEDGEMENT
First and foremost I would like to thank the Lord Almighty for His presence and immense
blessings throughout the project work.
I wish to express my heartfelt gratitude to Dr. SANDIP MAL, Head of the Department,
School of Computing Sciences and Engineering for much of his valuable support
encouragement in carrying out this work.
I would like to thank my internal guide Mr. HARIHARAN.R for continually guiding and
actively participating in my project, giving valuable suggestions to complete the project
work.
I would like to thank all the technical and teaching staff of the School of Computing Sciences
and Engineering, who extended directly or indirectly all support.
Last, but not least, I am deeply indebted to my parents who have been the greatest support
while I worked day and night for the project to make it a success.
ii
LIST OF ABBREVIATIONS
ABBREVIATION MEANING
Candidates The number of possible values that can be placed into an empty
square.
Clues The given numbers are in the grid at the beginning.
UI User Interface
iii
LIST OF FIGURES AND GRAPHS
2 Sudoku Example 5
v
ABSTRACT
The Results have proven that Crook’s Algorithm is faster than the Normal
Backtracking Solution, But In Some Cases Fails to Reach the Right Results.
vi
TABLE OF CONTENTS
CHAPTER TITLE PAGE NO.
NO.
NOVELTY
4
4.1 Methodology and goal
4-6
4.1.1 Backtracking
7-8
4.1.2 Crook’s Algorithm
9
4.2 Functional modules design and analysis
9
vii
4.2.1 Generating a Random Sudoku 9
4.2.2 Read the input from the API. 9
4.2.3 Solve by the chosen Algorithm 9
4.2.4 Display the Solved Grid 9
4.2.5 Show the respective run-time.
10
4.3 Architectural designs
11
4.4 User Interface designs
5 CHAPTER-5:
12
ANALYSIS OF ALGORITHMS
5.1 Backtracking Approach - Analysis
12-13
5.2 Comparative Performance Analysis 13-14
6 CHAPTER-6:
15
PROJECT OUTCOME AND APPLICABILITY
15
6.1 Outline
6.2 Key implementations outlines of the System 15
6.3 Significant project outcomes 15
6.3.1 Valid Sudoku Generation (API) 15-16
6.3.2 Backend API 16
6.3.3 Front End Webpage 16
6.5 Inference 17
7 CHAPTER-7:
18
CONCLUSIONS AND RECOMMENDATION
7.1 Outline 18
7.2 Limitation/Constraints of the System 18
7.3 Future Enhancements 18
7.4 Inference 18
References 19
CHAPTER-1
PROJECT DESCRIPTION AND OUTLINE
1.1. Introduction
the objective is to fill a square grid of size ’n’ with numbers between 1 to ’n’.
1) each column,
3) each of the sub-grids (if any) contains all of the numbers from 1 to ‘n’.
1.2. Motivation for the work
A way to solve sudoku accurately through a computer in the least amount of time serves as a
way to check the correctness of the solved puzzle obtained through conventional methods.
The sudoku puzzle would be solved using Backtracking and Crook’s algorithm.
We are given a Sudoku puzzle and need to fill the empty cells without violating any rules.
1. Each of the digits [1-9] must occur exactly once in each row.
2. Each of the digits [1-9] must occur exactly once in each column.
3. Each of the digits [1-9] must occur exactly once in each of the 3x3 sub-boxes of the grid.
The purpose is to develop a more effective algorithm for solving sudoku to reduce the
computing time and utilize lower memory space.
1
CHAPTER-2
RELATED WORK INVESTIGATION
2.1 Introduction
We have noticed that there is a large volume of published studies describing Sudoku
problems. Furthermore, several research has been made to solve Sudoku problems more
efficiently. It has conclusively been shown that solving the puzzle, by using different
algorithms are possible but most developers seek optimizations techniques such
as genetic algorithms, simulated annealing, etc.
2.3 Existing Approaches/Methods
2
CHAPTER-3
REQUIREMENT ARTIFACTS
While conducting this project, We focused a lot on the requirements set by this project to
account for as many possible human errors as possible, also known as designing for usability
(requiring as little thinking for the user as possible).
Hardware Requirements:
Any device capable of running Python script i.e, Windows 7 or later.
Minimum 2GB Ram
x86 64-bit CPU
5 GB free disk space
Software Requirements:
Python 3.8
Flask 2.0.1(pip install flask)
Flask-CORS(pip install -U flask-cors)
3
CHAPTER-4:
DESIGN METHODOLOGY AND ITS NOVELTY
Sudoku puzzles are NP-complete problems and as such, their solution can be found by
performing an exhaustive search.
Crook’s Algorithm
4
We will now create a Sudoku solver using backtracking by encoding our problem, goal, and
constraints in a step-by-step algorithm. A brute force algorithm visits the empty cells in
sequential order, filling in digits sequentially, or backtracking when no digit can be filled
without violating any rule.
Initially, the program would solve the puzzle by placing the digit “1” in the first empty cell
and checking if it is allowed to be there. If there are no violations (checking row, column, and
box constraints) then the algorithm advances to the next cell and places a “1” in that cell.
When checking for violations, if it is discovered that the “1” is not allowed, the value is
changed to “2”. If a cell is discovered where none of the 9 digits is allowed, then the
algorithm leaves that cell blank and moves back to the previous cell. The value in that cell is
then incremented by one. This is repeated until an allowed value in the last empty cell is
discovered.
If we try to draw the recursion tree then each step will branch into 9 branches and at each
level, the problem size is reduced by 1.
5
The unique missing method and the naked single method can solve all puzzles with an easy
and medium level of difficulty. To solve puzzles with even more difficult levels such as hard
and evil backtracking, the method has been used to complete the algorithm. A human player
solves the puzzle by using simple techniques. If the puzzle is not solvable by using the
techniques the player then tries to fill the rest of the empty squares by guessing. The
backtracking method, which is similar to the human strategy (guessing), is used as a helpful
method to the pencil-and-paper algorithm. In other words, if the puzzle cannot be filled when
using the unique missing method and the naked single method, the backtracking method will
take the puzzle and fill the rest of the empty squares. Generally, the backtracking method
finds an empty square and assigns the lowest valid number in the square once the content of
other squares in the same row, column, and box are considered. However, if none of the
numbers from 1 to 9 are valid in a certain square, the algorithm backtracks to the previous
square, which was filled recently. The above-mentioned methods are an appropriate
combination to solve any Sudoku puzzles. The naked single method can find quickly single
candidates to the empty squares that need only one single value. Since the puzzle comes to its
end solution the unique missing method can be used to fill the rest of the puzzles. Finally, if
either method fills the board the algorithm calls the backtracking method to fill the rest of the
board. The time needed for the algorithm to run is not directly related to the difficulty of the
puzzle. It is dependent on the number of times the recursion is repeated (the function calls
itself).
6
4.1.2 Crook’s Algorithm
Step1: Markup
The first step is easy, list out all possible numbers in each cell. If you have used an app to
play Sudoku, there should already be a function allowing you to write down some possible
numbers in a cell. In the first step of the algorithm, for each cell, you follow the rules and
write down all possible numbers.
The team “singleton” seems so complicated. However, the concept is easy, finding if there is
a row, column, or box with only one possible value throughout the row, column, or box.
Since there is one and only one number in each row, column, and box, if there is only a cell
in the row, column, or box with the respective number, this number must be in this cell. So
you fill in this cell with this number and then update markups in an affected row, column, or
box.
7
This step is quite difficult as I spent a lot of time understanding this part. Below is the
definition of preemptive sets from Crook’s paper.
To simplify it, if m numbers are a set or a superset of a markup of m cells within a row,
column, box, [row, box] or [column, box], then this combination of numbers and cells are a
preemptive set.
This step is the most important part of the algorithm and this step helps reduce the
possinumberbers of cells.
In this step, for all numbers in a preemptive set, they cannot be possible numbers for a cell
outside the preemptive set. The reason here is simple, as each number can be once only in
each row, column or box. If this number is in the preemptive set, this number must be placed
8
in those cells in the set. As a result, all cells in the same row, column, and box cannot have
this number as a possible number in the markup.
9
4.2 Functional modules design and analysis
10
4.3 Architectural designs
In Artificial Intelligence, an Architectural System is a computer system that emulates the
decision-making ability of a human expert.
The following are the components of the expert system in our approach:
A knowledge base is a set of data structures that keep track of previous information
needed to go forward.
The Knowledge Base Editor would be a simple text editor that could modify or add
to the code's built-in rules.
Another feature is the Rule Engine, which examines each pattern to see if it adheres
to a set of rules.
11
4.4 User Interface designs
With several design tweaks along the process, we strived to make our design a little different,
user-friendly, usable, and efficient. We've done a lot of changes to get something that our
12
CHAPTER-5
ANALYSIS OF ALGORITHMS
Time Complexity: O(nm) where n is the number of possibilities for each square (i.e., 9 in
classic Sudoku) and m is the number of blank spaces. The problem can be designed for a grid
size of N*N where N is a perfect square. For such an N, let M = N*N, the recurrence
equation can be written as
T(M) = 9*T(M-1) + O(1)
where T(N) is the running time of the solution for a problem size of N. Solving this
recurrence will yield, O(9M).
Explanation: This can be seen by working backward from only a single empty spot. If there
is only one empty spot, then you have ‘n’ possibilities and you must work through all of them
in the worst case. If there are two empty spots, then you must work through n possibilities for
the first spot and n possibilities for the second spot for each of the possibilities for the first
spot. If there are three spots, then you must work through n possibilities for the first spot.
Each of those possibilities will yield a puzzle with two empty spots that now have n²
possibilities.
You may also say that this algorithm performs a depth-first search through the possible
solutions. Each level of the graph represents the choices for a single square. The depth of the
graph is the number of squares that need to be filled. With a branching factor of n and a depth
of m, finding a solution in the graph has a worst-case performance of O(nm).
13
Space complexity: It’s the recursion stack that is used as an auxiliary space which is N*N
step deep. Remember we need to fill in 81 cells in a 9*9 sudoku and at each level, only one
cell is filled. So, space complexity would be O(M).
This diagram depicts the differences between the pencil-and-paper algorithm and the brute
force algorithm based on how long it takes to solve the puzzles by a computer. The Crooks
algorithm solves the puzzle quicker than the brute force algorithm. The given data is based on
the averaging of the computing time for several puzzles that have been tested with the same
difficulty levels such as easy, medium, hard, and evil respectively. For instance, the time
obtained for the easy level is the result of averaging several computing times with the easy
level. In the given diagram the vertical axis represents the running time of the puzzles in
14
milliseconds and the horizontal axis the difficulty levels. Generally, the backtracking method,
which is similar to the brute force algorithm, can solve the puzzles quicker than the pencil-
and-paper algorithm. The question now is why should we use backtracking and human
methods together? There are three reasons to do so. Firstly, the purpose of this work is to
implement an algorithm applying human strategies. Secondly, human players also use the
backtracking method when they get stuck. It means that players check different alternatives
and place the numbers in the empty squares by guessing when there are no options left.
Finally, employing human strategies makes the algorithm more efficient based on the number
of comparisons. In other words, the naked single method fills the empty squares by
performing fewer comparisons in a short time as 13 uses a better technique. However, the
backtracking method runs more analytical circulation while solving the puzzles resulting in
the consumption of memory space.
15
CHAPTER-6:
PROJECT OUTCOME AND APPLICABILITY
6.1 Outline
Here We have Successfully Learned the Mathematical Logic Working Behind the Sudoku
and How to Computationally Solve this Problem in Step-By-Step Manner.
In this project, a website will be created that will generate random sudoku based on the
difficulty level desired by the user and give their solutions while showing the amount of time
taken to solve it. Currently, the website is more than halfway done and generates the random
sudoku while not solving it at the moment.
First, generate a full grid of numbers. This step is more complex as it seems as we cannot just
randomly generate numbers to fill in the grid. We have to make sure that these numbers
follow the Sudoku rules. We will do this in 2 parts:
We will take an empty grid and then fill all the diagonal sub-grids. We can observe that in the
above matrix, the diagonal matrices are independent of other empty grids initially. So we
only need to check that the inserted numbers are not repeated in the respective subgrid.
Then the next step will be to fill the remaining 6 sub-grids which are not part of the diagonal.
We will do this using backtracking, i.e, try all numbers until a safe number to fill space is
found, check for the next space, and if no possible number is found we backtrack.
Once the grid is filled, remove K elements randomly in such a way that after removing the
grid will have a unique solution. It can be done using a backtracking sudoku solver.
Each time a value is removed we will apply the sudoku solver algorithm to see if the grid can
still be solved and to count the number of solutions it leads to. If the resulting grid only has
one solution we can carry on the process from step 3. If not we will have to put the value we
16
took away back in the grid. We have to continue doing this until k values are removed from
the grid
Based on the number of removals from the completed grid, the resulting grid will be more
difficult to solve.
The Flask API used in this project takes requests from the User to:
It does the job of generating sudoku by using the generator algorithm, then sending the
sudoku board as a JSON object to the User. Similarly, when the user selects to solve sudoku
using any of the two available methods (Backtracking or Crook's Algorithm), the incomplete
sudoku board is received by the Flask API, solves it using the respective algorithm, and sends
the solved board as a JSON object. This Flask API also calculates the time taken to solve the
received sudoku by the given method and sends it within the JSON object so that the user can
view and compare the durations.
This gives a UI Interface to the user with buttons for generating and solving sudoku and
displays the time taken to solve the sudoku by the algorithm.
Sudoku puzzles have been translated into coloring problems that link the game to a class of
important mathematical problems.
17
Sudoku puzzles are used in data hiding techniques like Steganography. The Sudoku is used as
a key to hide data behind images.
6.5 Inference
The algorithm seems to be a useful method to solve any Sudoku puzzles and it can guarantee
to find at least one solution. However, this algorithm is not efficient because the level of
difficulties is irrelevant to the algorithm. In other words, the algorithm does not adopt
intelligent strategies to solve the puzzles.
18
CHAPTER-7:
7.1 Outline
It has been shown that solving the puzzle, by using different algorithms is desirable but most
developers seek optimizations techniques.
Users are not given the option of solving the Sudoku on their own on the website.
It does not provide hints; instead, it answers the entire Sudoku puzzle at once.
In some puzzles, the algorithm can get slow.
This project may be used with computer vision [AR, OpenCV, and Machine
Learning] to extract Sudoku from newspapers, solve them in real-time, and
display them out.
Provides immediate assistance by providing hints and tracking user progress while
playing.
Time and accuracy-based scoring system.
7.4 Inference
While consuming procedures result in an inefficient solver, this approach checks all possible
solutions to the puzzle until a legitimate solution is found. As previously said, the key benefit
of employing the method is the ability to solve any puzzle with the certainty of a solution.
19
REFERENCES
3. J.F. Crook, A pencil and paper algorithm for solving Sudoku Puzzles, [Cited 2013
February 24], Winthrop University, Webpage:
http://www.ams.org/notices/200904/tx090400460p.pdf
4. A.S. Showdhury, S. Skher Solving Sudoku with Boolean Algebra [Cited 2013
February 24], International Journal of Computer Applications, Peer-reviewed
Research, Webpage:
http://research.ijcaonline.org/volume52/number21/pxc3879024.pdf
6. Ch. Xu, W. Xu, The model and Algorithm to Estimate the Difficulty Levels of
Sudoku Puzzles, Journal of Mathematics Research, 2009 Webpage:
http://journal.ccsenet.org/index.php/jmr/article/viewFile/3732/3336
20