SlideShare a Scribd company logo
4
Most read
6
Most read
7
Most read
PROJECT REPORT : SUDOKU SOLVER
Submitted in partial fulfillment of the requirements of
COMPSCI 271 A FALL 2016
By
CHAITANYA KSHIRSAGAR 15244643
MADHUR BAJAJ 36562594
PRATIK SHETTY 39844769
SOHAM KULKARNI 20005264
PROJECT GUIDE:
PROF. KALEV KASK
UNIVERSITY OF CALIFORNIA, IRVINE
Table of Contents
1. Introduction................................................................................................................................ 1
1.1. Problem Statement.................................................................................................................. 1
1.2. Purpose .................................................................................................................................... 1
1.3. Data Structure/Sudoku Jargon................................................................................................. 2
2. Constraint Statisfaction Problem .............................................................................................. 4
2.1. Sudoku as CSP .......................................................................................................................... 4
2.2. Constraint Propogation............................................................................................................ 4
2.2.1. Backtracking Search ......................................................................................................... 4
2.2.2. Forward Checking............................................................................................................. 5
2.2.3. Most Constrained Value................................................................................................... 5
2.2.4. Arc Consistency ................................................................................................................ 5
3. Results and Analysis................................................................................................................... 7
3.1. Test Cases................................................................................................................................. 7
3.2. Statistics................................................................................................................................... 8
4. Appendix................................................................................................................................... 10
1. Introduction
Currently, Sudoku puzzles are becoming increasingly popular among the people all over the
world. The game has become popular now in a large number of countries and many developers
have tried to generate even more complicated and more interesting puzzles. Today, the game
appears in almost every newspaper, in books and in many websites.
In this project we present a Sudoku Solver algorithm using AI Techniques to solve the puzzles.
The algorithm is formulated based on human techniques. This means that the algorithm is
implemented based on human perceptions indeed giving the feel of artificial Intelligence. The
Algorithms making use of Various Constraint Satisfaction Problem techniques are then
implemented incrementally to compare with this algorithm in order to evaluate the efficiency
of the proposed algorithm. The brute force is a general algorithm than can be applied to any
possible problem. This algorithm generates any possible solutions until the right answer is
found.
The following subsections describe the problem statement, the purpose of this project, and
the abbreviations and the definitions.
1.1 Problem Statement
Sudoku’s are challenging is a logic-based, combinatorial number placement puzzle, that have
been a source of entertainment for many years now. People who like brain teasers have always
enjoyed solving the classic 9X9 sudoku puzzle. However, Sudoku Puzzles could be of 4x4 matrix
and 16x16 matrix as well. Sudoku puzzles can be solved incorporating many different Artificial
Intelligence techniques like Brute force technique, Most Constrained Variable, Least
Constrained Variable, Forward Checking, ARC Consistency etc. We have used these techniques
in incremental order to get the sense of improvement in accuracy of Sudoku Solver after
addition of each of these individually.
1.2 Purpose
The aim of the project is to investigate the efficiency of CSP Techniques like backtracking, MCV,
LCV, ARC Consistency with respect to Classic 9x9 Sudoku Solver. Later these methods are
compared analytically to examine which accounts for the faster execution of Sudoku. In this
project we have implemented four variations of Sudoku Solver Implementation that simulate
how humans would solve the puzzle by using some simple strategies that can be employed to
solve the majority of Sudoku.
1.3 Data Structure Used / Sudoku Jargons
Before we proceed, following is the list of some definitions to the jargons used with respect to
Sudoku Implementation:
Fig. 1
 Cell:
A cell is a 1x1 'square' in a Sudoku grid, which is the minimum unit of the Sudoku board
defined by the x (row no.) and y (column no.) co-ordinate in the Grid.
 Square:
A collection of small squares, mostly sqrt(N) x sqrt(N) sized. Thus, 3x3 for a 9x9 Sudoku.
 Grid:
A grid represents the Sudoku board.
 Domain:
The number of possible values that can be placed into an empty square.
There already exist a number of digits in the board that make the puzzle solvable. It means
that some numbers are already placed in the Sudoku board before starting playing. The board
consists of 81 cells, which is divided into nine 3X3 sub boards and each 3X3 sub board is called
“box/square”. The main concept of the game is to place numbers from 1 to 9 on a 9X9 board
so that every row, column and box contains any numbers but once. This means that no number
is repeated more than once. An example of start state of this game is illustrated in Fig.1.
First, we need to define the Sudoku problem as a Constraint Satisfaction Problem.
2. Constraint Satisfaction Problem
Constraint satisfaction problems (CSPs) are mathematical problems defined as a set of objects
whose state must satisfy a number of constraints or limitations. CSPs represent the entities in
a problem as a homogeneous collection of finite constraints over variables, which is solved
by constraint satisfaction methods.
Formally, a constraint satisfaction problem is defined as a triple <X, D, C>
X = {X1, X2. . . Xn} is a set of variables,
D = {D1, D2. . . Dn} is a set of the respective domains of values, and
C = {C1, C2. . . Cm} is a set of constraints.
2.1 Sudoku as Constraint Satisfaction Problem
 Variables:
The variables will be each cell on the grid i.e 81 cells.
 Domain:
The domain will be any digit from 1 to 9.
 Constraints: The constraints are:
o Same digit can't appear twice (or more) in the same row.
o Same digit can't appear twice (or more) in the same column.
o Same digit can't appear twice (or more) in the same square.
2.2 Constraint Propagation
Constraint propagation techniques are methods used to modify a constraint satisfaction
problem. More precisely, they are methods that enforce a form of local consistency, which are
conditions related to the consistency of variables and constraints. Constraint propagation has
various uses. First, it turns a problem into one that is equivalent but is usually simpler to solve.
Second, it may prove satisfiability or unsatisfiability of problems. This is not guaranteed to
happen in general; however, it always happens for some forms of constraint propagation
and/or for certain kinds of problems. The most popular constraint propagation method is
the AC-3 algorithm, which enforces arc consistency.
2.2.1 Backtracking Search
Perhaps the most obvious way to solve a Sudoku Puzzle is to just pick the first empty cell and
assign one to that cell. If the cell conflicts with another value, then change it to a number that
does not conflict. Do this until a value that does not conflict is found. Once such value is found,
pick another cell and repeat this process. If a cell has no possible values, then return to
previously assigned cell and change its value. This method is guaranteed to find a solution if
exists, but will eventually end up doing too much computations. There are several other means
by which this algorithms can be Improved. Hence, Search is possibly the last resort we employ
to find Solution to given Sudoku.
2.2.2 Forward Checking
The most elementary constraint propagation is forward checking. It means eliminating, in
advance, the possibilities that do not match the constraints from the domains of unassigned
variables. For example, if we assign the digit 'd' to cell c1, we eliminate 'd' from all the domains
of this cell's peers.
2.2.3 Most Constraint Value
In the aforementioned search algorithm, we say, we assign value to a variable using a function
but it does not say anything about the criterion. Various kinds of heuristics can be used for
selection of a cell/variable viz. the classic ones are Most Constrained Value, Least Constrained
Value, Lexicographic Order Heuristic, etc. We employed Most Constrained Value heuristic,
since it results in maximum reduction of domain size of the dependent cells. After introducing
Most Constrained value heuristic, the variable/ cell selected is not random, instead is the one
with maximum constraints imposed. The implementation makes use of Min-Heap to keep
track of cells with least domain size, implies the cells that are most constrained. In case of tie,
for equal number of constraints imposed, We worked with degree of cell associated with every
cell as the tie-breaker. Degree associated with cell x is defined as the total number of cells, the
assignment of cell x will impact.
2.2.4 ARC Consistency
Establishing arc consistency for a constraint roughly means removing from each variable, all
values that belong to no solution of that constraint regardless of the rest of the model. It
depends of the filtering algorithm one uses for the constraint. What we have done is types of
reductions, some for reducing domains and some just to see a mistake in advance for saving
time:
1. When we want to assign the digit 'd' to cell x,y, we use assign Cell(x, y, d). By doing that,
we want to assign (x, y) the value d, but we also want to eliminate this possibility from its
peers. If the elimination causes one (or some) of the peers going down to only one
possibility, which we call d2, we want to assign d2 to it, and by doing that, eliminate d2
from all of its peer's peers, and that could make another chain reaction. This chain reaction
is simply called constraint propagation: placing a constraint on one cell can cause further
constraints to be placed on other cells.
2. The second type: let's say we just eliminate 8 as a possible value of cell [0,0], and we would
look on the first unit (the rows unit) of [0,0] and see that the only cell who has 6 as a
possibility is [0,6]. We will assign 8 to [0,6], and again, a chain reaction happens.
3. The last one is a pre-check for mistakes, because Sudoku is declared as an AllDiff constraint.
We can check in advance for a valid solution by doing the following simple form of
inconsistency detection: if there are M variables involved in the constraint and if they have
N possible distinct values altogether, and M>N, then the constraint cannot be satisfied and
thus we don't have to spend time on this branch that will eventually go wrong.
3. Results and Analysis
In Implementation of Sudoku Solver, the above discussed approaches for Constraint
Satisfaction Problem, were implemented on incremental basis, thereby leading to four
versions of Sudoku Solvers, with A1 being the most basic Solver and A4 being the most
advanced.
A1 : Brute Force (Backtracking Search)
A2 : Backtracking Search + Forward Checking
A3 : Backtracking Search + Forward Checking + Most Constrained Value (MCV)
A4 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) + AC – 3
3.1 Test Cases
The aforementioned Algorithm verisons were tested across 82 Testcases. Test Case 0 being
already solved Sudoku as input. For every subsequent Test Case, one cell from the previous
test case is set to 0 at random. Test Case 0 having 1 cell set to 0, Test Case 1 having 1 more
cell set to 0 in addition to the one from Test Case 0. Likewise, Test Case 81 will be completely
empty and hence set to 0.
3.2 Runtime Statistics
A1 : Brute Force (Backtracking Search)
A2 : Backtracking Search + Forward Checking
0
200
400
600
800
1000
1200
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79
Brute-force(Backtracking Search)
Brute-force(Backtracking Search)
0
0.001
0.002
0.003
0.004
0.005
0.006
0.007
0.008
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79
Backtracking Search + Forward Checking
Backtracking Search + Forward Checking
A3 : Backtracking Search + Forward Checking + Most Constrained Value (MCV)
A4 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) + AC – 3
In conclusion, The Average running time for A4 across all the test cases is optimal. Hence, all the
subsequent Benchmarking Test cases are examined using A4 version of Sudoku Solver
0
0.0002
0.0004
0.0006
0.0008
0.001
0.0012
0.0014
0.0016
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79
Backtracking Search + Forward Checking + Most
Constrained Value (MCV)
Backtracking Search + Forward Checking + Most Constrained Value (MCV)
0
0.0002
0.0004
0.0006
0.0008
0.001
0.0012
0.0014
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82
Backtracking Search + Forward Checking + MCV + AC-
3
Backtracking Search + Forward Checking + MCV + AC-3
4. Appendix
4.1 Benchmark Test Cases
1) Easy
Runtime 0.000875 sec
Number of Search Space Node 54
Number of Backtracks 20
2) Medium 1
Runtime 0.000792 sec
Number of Search Space Node 76
Number of Backtracks 27
3) Medium 2
Runtime 0.000687 sec
Number of Search Space Node 38
Number of Backtracks 10
4) Hard 1
Runtime 0.000814 sec
Number of Search Space Node 58
Number of Backtracks 20
5) Hard 2
Runtime 0.000573 sec
Number of Search Space Node 44
Number of Backtracks 9
6) Evil1
Runtime 0.013683 sec
Number of Search Space Node 918
Number of Backtracks 862
7) Evil 2
Runtime 0.007062 sec
Number of Search Space Node 513
Number of Backtracks 458
8) Evil3
Runtime 0.00548 sec
Number of Search Space Node 428
Number of Backtracks 375

More Related Content

PPSX
Intelligent Sudoku Solver
PPTX
final presentation of sudoku solver project
PPTX
Sudoku solver ppt
PDF
Sudokureport
PDF
9. chapter 8 np hard and np complete problems
PPTX
Realibity design
PPTX
Graph coloring problem(DAA).pptx
PPT
Introduction to Design Algorithm And Analysis.ppt
Intelligent Sudoku Solver
final presentation of sudoku solver project
Sudoku solver ppt
Sudokureport
9. chapter 8 np hard and np complete problems
Realibity design
Graph coloring problem(DAA).pptx
Introduction to Design Algorithm And Analysis.ppt

What's hot (20)

PPTX
PPTX
8 queen problem
PPTX
Sudoku solver
PPTX
Bellman ford algorithm
PPT
PPTX
01 Knapsack using Dynamic Programming
PDF
Time and Space Complexity
PPTX
8 QUEENS PROBLEM.pptx
PPTX
Dijkstra's Algorithm
PPT
Backtracking Algorithm.ppt
PPTX
Greedy algorithms
PPTX
Knapsack Problem
PPT
Backtracking
PPTX
8 queens problem using back tracking
PPTX
Dijkstra’S Algorithm
PDF
If then rule in fuzzy logic and fuzzy implications
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Artificial Intelligence- TicTacToe game
PDF
Sudoku Solving with Computational Intelligence
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
8 queen problem
Sudoku solver
Bellman ford algorithm
01 Knapsack using Dynamic Programming
Time and Space Complexity
8 QUEENS PROBLEM.pptx
Dijkstra's Algorithm
Backtracking Algorithm.ppt
Greedy algorithms
Knapsack Problem
Backtracking
8 queens problem using back tracking
Dijkstra’S Algorithm
If then rule in fuzzy logic and fuzzy implications
Greedy Algorithm - Knapsack Problem
Artificial Intelligence- TicTacToe game
Sudoku Solving with Computational Intelligence
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Ad

Similar to Sudoku Solver (20)

PPTX
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
PPT
Sudoku
PPTX
AIw10_Backtracking.pptx
PPTX
Sudoku solver ppt with exquisite example and build.pptx
PDF
Andrew Daws - Final Project Report
PDF
INTELLIGENTSUDOKUSOLVERWITH AI-BASEDOPTIMIZATION.pdf
PPTX
Digi sudoku
PDF
P1
PPTX
python presentation.pptx which includes project details
PDF
Constructionof heuristicsforasearch basedapproachtosolvingsudoku
PPTX
Constraint Satisfaction Problems - Copy.pptx
PPTX
dsa project.pptx
PDF
Kakuro: Solving the Constraint Satisfaction Problem
PPT
05-constraint-satisfaction-problems-(us).ppt
PDF
Constraint Satisfaction.pdf
PPTX
Genetic operators
PPTX
Sudoku Solver using Convolutional Neural networks.pptx
PDF
Artificial Intelligence JNTUH Syllabusss
PPTX
Constraint satisfaction problems (csp)
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
Sudoku
AIw10_Backtracking.pptx
Sudoku solver ppt with exquisite example and build.pptx
Andrew Daws - Final Project Report
INTELLIGENTSUDOKUSOLVERWITH AI-BASEDOPTIMIZATION.pdf
Digi sudoku
P1
python presentation.pptx which includes project details
Constructionof heuristicsforasearch basedapproachtosolvingsudoku
Constraint Satisfaction Problems - Copy.pptx
dsa project.pptx
Kakuro: Solving the Constraint Satisfaction Problem
05-constraint-satisfaction-problems-(us).ppt
Constraint Satisfaction.pdf
Genetic operators
Sudoku Solver using Convolutional Neural networks.pptx
Artificial Intelligence JNTUH Syllabusss
Constraint satisfaction problems (csp)
Ad

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Geodesy 1.pptx...............................................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Well-logging-methods_new................
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
composite construction of structures.pdf
PPTX
web development for engineering and engineering
Foundation to blockchain - A guide to Blockchain Tech
Geodesy 1.pptx...............................................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
573137875-Attendance-Management-System-original
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Well-logging-methods_new................
Lecture Notes Electrical Wiring System Components
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Automation-in-Manufacturing-Chapter-Introduction.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Operating System & Kernel Study Guide-1 - converted.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Model Code of Practice - Construction Work - 21102022 .pdf
composite construction of structures.pdf
web development for engineering and engineering

Sudoku Solver

  • 1. PROJECT REPORT : SUDOKU SOLVER Submitted in partial fulfillment of the requirements of COMPSCI 271 A FALL 2016 By CHAITANYA KSHIRSAGAR 15244643 MADHUR BAJAJ 36562594 PRATIK SHETTY 39844769 SOHAM KULKARNI 20005264 PROJECT GUIDE: PROF. KALEV KASK UNIVERSITY OF CALIFORNIA, IRVINE
  • 2. Table of Contents 1. Introduction................................................................................................................................ 1 1.1. Problem Statement.................................................................................................................. 1 1.2. Purpose .................................................................................................................................... 1 1.3. Data Structure/Sudoku Jargon................................................................................................. 2 2. Constraint Statisfaction Problem .............................................................................................. 4 2.1. Sudoku as CSP .......................................................................................................................... 4 2.2. Constraint Propogation............................................................................................................ 4 2.2.1. Backtracking Search ......................................................................................................... 4 2.2.2. Forward Checking............................................................................................................. 5 2.2.3. Most Constrained Value................................................................................................... 5 2.2.4. Arc Consistency ................................................................................................................ 5 3. Results and Analysis................................................................................................................... 7 3.1. Test Cases................................................................................................................................. 7 3.2. Statistics................................................................................................................................... 8 4. Appendix................................................................................................................................... 10
  • 3. 1. Introduction Currently, Sudoku puzzles are becoming increasingly popular among the people all over the world. The game has become popular now in a large number of countries and many developers have tried to generate even more complicated and more interesting puzzles. Today, the game appears in almost every newspaper, in books and in many websites. In this project we present a Sudoku Solver algorithm using AI Techniques to solve the puzzles. The algorithm is formulated based on human techniques. This means that the algorithm is implemented based on human perceptions indeed giving the feel of artificial Intelligence. The Algorithms making use of Various Constraint Satisfaction Problem techniques are then implemented incrementally to compare with this algorithm in order to evaluate the efficiency of the proposed algorithm. The brute force is a general algorithm than can be applied to any possible problem. This algorithm generates any possible solutions until the right answer is found. The following subsections describe the problem statement, the purpose of this project, and the abbreviations and the definitions. 1.1 Problem Statement Sudoku’s are challenging is a logic-based, combinatorial number placement puzzle, that have been a source of entertainment for many years now. People who like brain teasers have always enjoyed solving the classic 9X9 sudoku puzzle. However, Sudoku Puzzles could be of 4x4 matrix and 16x16 matrix as well. Sudoku puzzles can be solved incorporating many different Artificial Intelligence techniques like Brute force technique, Most Constrained Variable, Least Constrained Variable, Forward Checking, ARC Consistency etc. We have used these techniques in incremental order to get the sense of improvement in accuracy of Sudoku Solver after addition of each of these individually. 1.2 Purpose The aim of the project is to investigate the efficiency of CSP Techniques like backtracking, MCV, LCV, ARC Consistency with respect to Classic 9x9 Sudoku Solver. Later these methods are compared analytically to examine which accounts for the faster execution of Sudoku. In this project we have implemented four variations of Sudoku Solver Implementation that simulate how humans would solve the puzzle by using some simple strategies that can be employed to solve the majority of Sudoku.
  • 4. 1.3 Data Structure Used / Sudoku Jargons Before we proceed, following is the list of some definitions to the jargons used with respect to Sudoku Implementation: Fig. 1
  • 5.  Cell: A cell is a 1x1 'square' in a Sudoku grid, which is the minimum unit of the Sudoku board defined by the x (row no.) and y (column no.) co-ordinate in the Grid.  Square: A collection of small squares, mostly sqrt(N) x sqrt(N) sized. Thus, 3x3 for a 9x9 Sudoku.  Grid: A grid represents the Sudoku board.  Domain: The number of possible values that can be placed into an empty square. There already exist a number of digits in the board that make the puzzle solvable. It means that some numbers are already placed in the Sudoku board before starting playing. The board consists of 81 cells, which is divided into nine 3X3 sub boards and each 3X3 sub board is called “box/square”. The main concept of the game is to place numbers from 1 to 9 on a 9X9 board so that every row, column and box contains any numbers but once. This means that no number is repeated more than once. An example of start state of this game is illustrated in Fig.1. First, we need to define the Sudoku problem as a Constraint Satisfaction Problem.
  • 6. 2. Constraint Satisfaction Problem Constraint satisfaction problems (CSPs) are mathematical problems defined as a set of objects whose state must satisfy a number of constraints or limitations. CSPs represent the entities in a problem as a homogeneous collection of finite constraints over variables, which is solved by constraint satisfaction methods. Formally, a constraint satisfaction problem is defined as a triple <X, D, C> X = {X1, X2. . . Xn} is a set of variables, D = {D1, D2. . . Dn} is a set of the respective domains of values, and C = {C1, C2. . . Cm} is a set of constraints. 2.1 Sudoku as Constraint Satisfaction Problem  Variables: The variables will be each cell on the grid i.e 81 cells.  Domain: The domain will be any digit from 1 to 9.  Constraints: The constraints are: o Same digit can't appear twice (or more) in the same row. o Same digit can't appear twice (or more) in the same column. o Same digit can't appear twice (or more) in the same square. 2.2 Constraint Propagation Constraint propagation techniques are methods used to modify a constraint satisfaction problem. More precisely, they are methods that enforce a form of local consistency, which are conditions related to the consistency of variables and constraints. Constraint propagation has various uses. First, it turns a problem into one that is equivalent but is usually simpler to solve. Second, it may prove satisfiability or unsatisfiability of problems. This is not guaranteed to happen in general; however, it always happens for some forms of constraint propagation and/or for certain kinds of problems. The most popular constraint propagation method is the AC-3 algorithm, which enforces arc consistency. 2.2.1 Backtracking Search Perhaps the most obvious way to solve a Sudoku Puzzle is to just pick the first empty cell and assign one to that cell. If the cell conflicts with another value, then change it to a number that does not conflict. Do this until a value that does not conflict is found. Once such value is found,
  • 7. pick another cell and repeat this process. If a cell has no possible values, then return to previously assigned cell and change its value. This method is guaranteed to find a solution if exists, but will eventually end up doing too much computations. There are several other means by which this algorithms can be Improved. Hence, Search is possibly the last resort we employ to find Solution to given Sudoku. 2.2.2 Forward Checking The most elementary constraint propagation is forward checking. It means eliminating, in advance, the possibilities that do not match the constraints from the domains of unassigned variables. For example, if we assign the digit 'd' to cell c1, we eliminate 'd' from all the domains of this cell's peers. 2.2.3 Most Constraint Value In the aforementioned search algorithm, we say, we assign value to a variable using a function but it does not say anything about the criterion. Various kinds of heuristics can be used for selection of a cell/variable viz. the classic ones are Most Constrained Value, Least Constrained Value, Lexicographic Order Heuristic, etc. We employed Most Constrained Value heuristic, since it results in maximum reduction of domain size of the dependent cells. After introducing Most Constrained value heuristic, the variable/ cell selected is not random, instead is the one with maximum constraints imposed. The implementation makes use of Min-Heap to keep track of cells with least domain size, implies the cells that are most constrained. In case of tie, for equal number of constraints imposed, We worked with degree of cell associated with every cell as the tie-breaker. Degree associated with cell x is defined as the total number of cells, the assignment of cell x will impact. 2.2.4 ARC Consistency Establishing arc consistency for a constraint roughly means removing from each variable, all values that belong to no solution of that constraint regardless of the rest of the model. It depends of the filtering algorithm one uses for the constraint. What we have done is types of reductions, some for reducing domains and some just to see a mistake in advance for saving time: 1. When we want to assign the digit 'd' to cell x,y, we use assign Cell(x, y, d). By doing that, we want to assign (x, y) the value d, but we also want to eliminate this possibility from its peers. If the elimination causes one (or some) of the peers going down to only one possibility, which we call d2, we want to assign d2 to it, and by doing that, eliminate d2 from all of its peer's peers, and that could make another chain reaction. This chain reaction is simply called constraint propagation: placing a constraint on one cell can cause further constraints to be placed on other cells.
  • 8. 2. The second type: let's say we just eliminate 8 as a possible value of cell [0,0], and we would look on the first unit (the rows unit) of [0,0] and see that the only cell who has 6 as a possibility is [0,6]. We will assign 8 to [0,6], and again, a chain reaction happens. 3. The last one is a pre-check for mistakes, because Sudoku is declared as an AllDiff constraint. We can check in advance for a valid solution by doing the following simple form of inconsistency detection: if there are M variables involved in the constraint and if they have N possible distinct values altogether, and M>N, then the constraint cannot be satisfied and thus we don't have to spend time on this branch that will eventually go wrong.
  • 9. 3. Results and Analysis In Implementation of Sudoku Solver, the above discussed approaches for Constraint Satisfaction Problem, were implemented on incremental basis, thereby leading to four versions of Sudoku Solvers, with A1 being the most basic Solver and A4 being the most advanced. A1 : Brute Force (Backtracking Search) A2 : Backtracking Search + Forward Checking A3 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) A4 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) + AC – 3 3.1 Test Cases The aforementioned Algorithm verisons were tested across 82 Testcases. Test Case 0 being already solved Sudoku as input. For every subsequent Test Case, one cell from the previous test case is set to 0 at random. Test Case 0 having 1 cell set to 0, Test Case 1 having 1 more cell set to 0 in addition to the one from Test Case 0. Likewise, Test Case 81 will be completely empty and hence set to 0.
  • 10. 3.2 Runtime Statistics A1 : Brute Force (Backtracking Search) A2 : Backtracking Search + Forward Checking 0 200 400 600 800 1000 1200 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 Brute-force(Backtracking Search) Brute-force(Backtracking Search) 0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 Backtracking Search + Forward Checking Backtracking Search + Forward Checking
  • 11. A3 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) A4 : Backtracking Search + Forward Checking + Most Constrained Value (MCV) + AC – 3 In conclusion, The Average running time for A4 across all the test cases is optimal. Hence, all the subsequent Benchmarking Test cases are examined using A4 version of Sudoku Solver 0 0.0002 0.0004 0.0006 0.0008 0.001 0.0012 0.0014 0.0016 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 Backtracking Search + Forward Checking + Most Constrained Value (MCV) Backtracking Search + Forward Checking + Most Constrained Value (MCV) 0 0.0002 0.0004 0.0006 0.0008 0.001 0.0012 0.0014 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 Backtracking Search + Forward Checking + MCV + AC- 3 Backtracking Search + Forward Checking + MCV + AC-3
  • 12. 4. Appendix 4.1 Benchmark Test Cases 1) Easy Runtime 0.000875 sec Number of Search Space Node 54 Number of Backtracks 20 2) Medium 1 Runtime 0.000792 sec Number of Search Space Node 76 Number of Backtracks 27
  • 13. 3) Medium 2 Runtime 0.000687 sec Number of Search Space Node 38 Number of Backtracks 10 4) Hard 1 Runtime 0.000814 sec Number of Search Space Node 58 Number of Backtracks 20
  • 14. 5) Hard 2 Runtime 0.000573 sec Number of Search Space Node 44 Number of Backtracks 9 6) Evil1 Runtime 0.013683 sec Number of Search Space Node 918 Number of Backtracks 862
  • 15. 7) Evil 2 Runtime 0.007062 sec Number of Search Space Node 513 Number of Backtracks 458 8) Evil3 Runtime 0.00548 sec Number of Search Space Node 428 Number of Backtracks 375