SlideShare a Scribd company logo
Sudoku Solving with Computational
           Intelligence




           Dr. Harald Hiss

            June 30, 2010
Summary
Computers can create convincing results – the cinemas are presenting the
latest overwhelming 3D multimedia spectacles. The results and benefits
aren’t restricted to the amusement and movies, modern methods for our
machines bring further considerable improvements to reality. Programs and
algorithms evolve in bioinformatics and public health. Computational intel-
ligence can also be utilized in solving brainteasing tasks. This article is
presenting a computational intelligence approach for solving Sudoku brain-
teasers.
A scanning strategy introduces Sudoku and solves the first example. The
article presents an estimation of the complexity with stochastic methods.
Sudoku-specific advanced strategies refine the scanning technique from
the introduction. A software-architecture for efficiently implementing a rule-
based solver is integrating the strategies and is using annotations that are
updated with notifications. When no rule can be applied, a backtracking
search is started. The parametrized solver offers policies for searching. A
logging mechanism is presented supporting the search saving memory for
the efficient implementation.
The author, Dr. Harald Hiss, has obtained the diploma in software-engineering
in 2004 and he has graduated in databases in 2008 at the university of
Freiburg supported by the interdisciplinary graduate programme logic and
applications. This article is presenting a new Sudoku solver that performs
best. The computer scientist is presenting Sudoku-specific strategies and
a solver architecture for the fast and simple implementation using the latest
technologies.
Contents

1 Solving Sudoku with Computational Intelligence   1

2 Sudoku and a Scanning Strategy                   1

3 Possibilities Estimation                         3

4 Sudoku-specific Strategies and Techniques         4

5 Solver Software-Architecture                     6

6 Integrating AI Search Strategies                 7
2 Sudoku and a Scanning Strategy                                                         1


1 Solving Sudoku with Computational Intelligence
Computers can create convincing results – the cinemas are presenting the latest over-
whelming 3D multimedia spectacles. The results and benefits aren’t restricted to the
amusement and movies, modern methods for our machines bring further consider-
able improvements to reality. Programs and algorithms evolve in bioinformatics and
public health. Computational intelligence can also be utilized in solving brainteasing
tasks. This article is presenting a computational intelligence approach for solving Su-
doku brainteasers. The example is illustrating specific game strategies, estimating the
complexity and again proving both power and expressiveness of computational intelli-
gence.
At the turn of the millenium, the number puzzle became very popular. The enjoyable
quest for filling numbers in fields comes in some variants and difficulty levels. Simple
Sudokus can be solved very fast, the next chapter introduces a scanning strategy that is
sufficient for automatically solving a first example. After the description, an estimation of
the complexity is given with stochastic methods showing a huge amount of possibilities
that a solver has to face. For the development of an automatic solver, some advanced
strategies are presented. The aspects are integrated in a flexible software-architecture
that can be easily adapted to Sudoku variants. Finally, the strategies are integrated
with the architecture. Computational intelligence extracts information for choosing the
appropriate rules to fill the next field and for choosing optimal fields and numbers for
backtracking when no rule applies.
The article presents an approach in computational intelligence for Sudoku. The con-
venient example is illustrating many aspects of computational intelligence. The num-
ber quest is formalized, stochastical methods estimate the possibilities, the specific
advanced strategies integrate with the software architecture and a rule-based logical
system. Methods from artificial intelligence and learning optimize the search choosing
the right fields and numbers – a very promising approach for constructing a success-
ful Sudoku solver with computational intelligence. The author has acquired very much
knowledge in this field. The huge power of computational intelligence can impressively
be shown with the Sudoku example presented in this article.




2 Sudoku and a Scanning Strategy
The challenge of Sudoku is posed in filling fields with fitting numbers so that some
number constraints are satisfied. The fields are arranged in groups. Within a group,
only numbers of a defined domain may be inserted. Numbers may not occur more than
once in a group. Solutions have filled all fields so that each number occurs exactly



                                            1
2 Sudoku and a Scanning Strategy                                                      2


once in each group. The standard Sudoku variant considers a 9 × 9 array of fields and
defines the domain 1, 2, ..., 9 for the numbers. As groups, the number puzzle considers
rows, columns and the 9 blocks formed of the 3 × 3 squares that are tiling the game.
The constraints have to be satisfied for each group. For instance, a row can contain
numbers in 1, 2, ..., 9 and a row of a solution represents a permutation of the domain.
The section shows a simple scanning strategy for solving some Sudokus. Figure 1

Figure 1 An easy Sudoku example.


                          1          2          5     9          4

                                     4     3    6           1    5

                               5           1                3

                    1                           2     5

                    7          2     6                      9    3

                                     3     4          2     7    1

                    3     6                     7     1          9

                               7     1          4           2    8

                          8          5     9                     7


presents the example that is illustrating the strategy. For the numbers of the domain,
the rows and columns are scanned to find the appropriate field in a block. The scan
starts with 1. The second field of the first row, abbreviated with S12 , has a 1. In the
block below, the field S41 stores this number. The first two columns may not contain the
number anymore, the left bottom block has the 1 in S73 or S93 . Then the scan considers
the relevant rows for the bottom block. The seventh row has a 1 in S77 , the 1 can be
filled in S93 . Using scans and filling obvious fields the solution in figure 2 is obtained.
The next section estimates the amount of possible games. Then again the article turns
to solving strategies and presents some advanced techniques.




                                           2
3 Possibilities Estimation                                                             3


Figure 2 The solution for the previous example.


                     6       1   3    2    7      5    9    8     4

                     9       2   8    4    3      6    7    1     5

                     4       7   5    9    1      8    6    3     2

                     1       3   9    7    8      2    5    4     6

                     7       4   2    6    5      1    8    9     3

                     8       5   6    3    4      9    2    7     1

                     3       6   4    8    2      7    1    5     9

                     5       9   7    1    6      4    3    2     8

                     2       8   1    5    9      3    4    6     7



3 Possibilities Estimation
A first naive estimation of the possibilities for choosing the numbers in the 9 × 9 square
considers filling each field with any number. The 981 combinations compute a total
of 1.966 · 1077 . The first improvement considers the symbols that have been inserted
previously in the same row. For nine rows, 9!9 combinations are obtained, a total of
1.091 · 1050 . Taking into account more information considering relevant blocks the num-
ber can be improved to 1.528 · 1031 . The huge amount suggests to invest some time
in software design considerations and to integrate both efficient game-specific strate-
gies and intelligent computational methods instead of implementing a trial-and-error
program. The following sections are going to present an efficient approach for the de-
velopment of an automatic solver.
The final estimation presented in this section is considering sequentially filling numbers,
from the left to the right and from the top to the bottom. The estimation uses the max-
imal restriction that is available in the context of the focussed field. The maximum of
the present numbers in the same row, column or block reduces the possible choice.
Figure 3 shows the result. The field in line 5, column 4 is regarding the numbers above
and to the left. In the same line, three numbers have been inserted. The column has
four numbers, and the middle block three. The column offers the maximal restriction of



                                           3
4 Sudoku-specific Strategies and Techniques                                               4


Figure 3 Possibilities for choosing a number for a field starting at the top-left corner
continuing from the left to the right and downward.


                     9     8     7     6    5     4     3     2     1

                     6     5     4     6    5     4     3     2     1

                     3     2     1     3    2     1     3     2     1

                     6     6     6     6    5     4     3     2     1

                     5     5     4     5    5     4     3     2     1

                     3     2     1     3    2     1     3     2     1

                     3     3     3     3    3     3     3     2     1

                     2     2     2     2    2     2     2     2     1

                     1     1     1     1    1     1     1     1     1



four different numbers. At most five numbers remain for choosing, the possibilities are
inserted in the field. With this approach, an estimation of 9! · 6!6 · 3!10 · 5 is obtained.
The number of possible valid Sudoku instances can be bound with 1.528 · 1031 . Another
aspect of Sudoku brainteasing is presented in figure 3. The last column and the bottom
row have one possibility left for each field. Often, many fields are still empty although
the game has been solved. Some clean-up work completes a Sudoku game filling num-
bers in fields that are restricted already having 8 numbers in the same group.
The next section is showing some improvements for the simple scanning solver in-
troduced at the beginning and is presenting advanced Sudoku-specific strategies and
techniques.


4 Sudoku-specific Strategies and Techniques
Easy instances can be solved with simple strategies as shown in section 2. For each
number, the possible positions within a block have been scanned excluding correspond-
ing rows and columns in the blocks of the same column and row. Obvious fields remain-
ing as the single gap in a group can be filled. For each field, the numbers of all groups




                                            4
4 Sudoku-specific Strategies and Techniques                                                  5


it belongs to can be removed from the candidates. Again, if one number is left the gap
can be filled. This section is considering advanced strategies for more complex Sudoku
solving.
Side-effects of groups can be used for improving the scanning approach presented in

Figure 4 First lines of the example for the improvement.


                                                                1

                                        1     1     1

                      1     8     9     2     3     4


section 2. Some group restrictions suffice to determine fields in depending groups. Fig-
ure 4 shows an example, the possible positions for a 1 in the middle block have blue
entries. The Information that the middle block has the 1 in the middle row suffices for
filling a 1 in S31 . In this way, the 1 in the first block is restricted to the two bottom fields
when the middle and last block share the 1 in the first two rows as shown in figure 5.
The section presents a strategy that is not necessarily needed to solve the example of

Figure 5 A variant of the improvement.


                                        1     1     1     1     1     1

                                        1     1     1     1     1     1

                      1     1     9     2     3     4     5     6     7


the introduction. The last row of the instance shown in figure 6 contains 9, 6, 7 and 8.
Other fields in this row can be filled with 1, ..., 5. The bottom middle block contains 4, 2
and is restricting its bottom row to 1, 3, 5. Considering columns is proving S94 , S95 ∈ {1,
5} and S96 ∈ {1, 3}. The constraints for S94 and S95 consume 1 and 5, the three fills S96 .
Constraints consuming numbers can be used sometimes similarly to symbols of filled
fields. The information just derived together with the 1 in S77 restricts possible positions
for filling a 1 in the bottom left block to S81 and S82 . Furthermore, the remaining fields
of the bottom middle block may only be filled with 6, ..., 9, possibly resulting in more
consequences.



                                              5
5 Solver Software-Architecture                                                         6


Figure 6 A number consuming example with S94 and S95 consuming the pair (1, 5).


                     7    6      1    3

                     2    8           7          9     3

                                                       7

                     6                           5

                                 9    4                           6

                                           3           8    2

                                           4           1    5

                                 6               2

                     9                                 6    7     8



The strategies introduced here are comfortably integrated in a software-architecture for
a Sudoku solver. The flexible approach makes the integration simple and it is open for
integrating new strategies. Section 3 has shown the importance with the huge amount
of possibilities that make backtracking costly.


5 Solver Software-Architecture
The section presents an object-oriented architecture for a Sudoku solver. A class
hierarchy is structuring the logical groups of the game, annotations provide a flexible
mechanism for rule-based game solving integrating Sudoku-specific strategies.
The solver architecture defines a hierarchy for groups and offers annotations for the
integration of strategies. Fields can be filled with numbers, groups represent fields that
belong to the same logical unit of the game. In particular, a block represents one of the
3 × 3 squares tiling the game. Rows and columns also represent groups. Each field is
associated with the groups it belongs to – a block, a row and a column. Vice versa, the
groups are associated with their fields. Furthermore, the groups are associated to the
groups they intersect.
Empty fields are annotated with possible numbers. Initially, the annotations get the



                                           6
6 Integrating AI Search Strategies                                                       7


numbers that aren’t in the groups of the field. Numbers that don’t occur in a group de-
fine annotations. The annotated group initially stores the empty fields. With constraints
of these annotations, fields can be filled as immediate consequence. In fields with one
possible number, the annotation left can be filled. When the group annotation for a
number leaves one field, the number may fill the gap.
For filling fields, the annotations are updated. Filling a number generates notifications,
the change at a position informs depending fields and groups. In that block, row and
column the number can’t occur again, there are fewer possible numbers. The anno-
tations of the fields in these groups are updated. Depending groups get notifications.
In this way, a generalization of the scanning strategy presented in section 2 is imple-
mented. Depending groups are defined having a non-empty intersection with a group of
the considered field. For example, filling a 1 in the top left field notifies some depending
groups. The first row intersects the top middle and right block – they can fill the only 1 in
the rows two and three. In addition, row one is intersecting the first field of all columns.
The first column intersects all rows and restricts the left middle and bottom blocks to the
columns two and three. Finally, the top left block removes the first three fields from the
group annotations of the first three columns and rows.
The annotations provide a flexible mechanism for integrating the Sudoku-specific strate-
gies presented in section 4. For instance, when the top left block restricts the 1 to the
first column, the blocks below are notified in the same way. Fields consuming con-
straints like exclusive pairs can have the same side-effect. Finding these constraints
always generates notifications within the groups the constraint shares. The field anno-
tations remove the numbers of such a pair, for example.
When no rules apply, the solver uses backtracking for searching a solution. The object-
oriented architecture and the annotations of this section are integrated in the intelligent
search that the next section is presenting.


6 Integrating AI Search Strategies
As section 3 has been showing, the huge amount of possibilities can cause consider-
able costs and so a simple scanning approach and advanced Sudoku-specific strate-
gies have been introduced in the sections 2 and 4. The previous section has integrated
the strategies in a rule-based solver using annotations for fields and groups. When no
rule can be applied, the solver is starting a backtracking search that is presented in this
section. The annotations can be used for optimizing the search.
The solver stores the progress for a Sudoku instance. A history remembers the fields
that are filled and the changes that are computed for the annotations. Logging the steps
is supporting an optimized backtracking search. When no rule or strategy can be ap-




                                            7
6 Integrating AI Search Strategies                                                        8


plied, the solver is introducing a choice-point and is starting a search. Occassionally
choosing a field and a number can be wrong, a guess can cause a clash. Each time
after filling further fields, the solver is checking clashes. Empty fields must have non-
empty annotations that suggest possible numbers for filling. Numbers that don’t occur
in a group may not have empty annotations in that particular group. Such numbers are
then missing in the group that can’t form a solution anymore. Checking clashes can be
limited to changes of annotations.
The backtracking is integrating the annotations. The solver makes a guess and then
continues with rule-based solving. New choice-points can be introduced and clashes
revise a guess. A choice-point is choosing a number for a field or the place for a number
within a group. The annotations decrease the considered possibilities and can be used
to optimize the decisions regarding the basis of a choicepoint. Furthermore, the history
is reducing the required resources storing the changes that can simply be undone. A
choice-point stores the parent state and the history for reaching a descendant. When
breadth-first search is desired, the ways to other choice-points can also be logged. The
choice-point stores the possible choices within its basis and the choices that can al-
ready be disregarded. A field is determined, if only one possibility is left. One more
clash propagates the change to the parent.
The approach offers policies for the parametrized search. The standard depth-first
search method can be replaced with breadth-first search and the approach is open for
further strategies. Choosing the basis for a choice-point is using annotations. For exam-
ple, a test would like to give high priority to choice-points with group annotations storing
two or three fields for a certain number. Another test might prefer field annotations
providing few possible numbers for filling the gap. The solver is open for fine-tuning in
accordance with the software-architecture presented in section 5. Finally, the parameter
can be optimized with statistical methods. Further methods of artificial intelligence can
be utilized for optimizing the solver. Training with Sudoku instances is very promising
for finding the best basis for the choice-point and for at fastest finding the solution.




                                             8

More Related Content

PDF
Sudoku Solver
PPTX
Sudoku solver ppt
PPTX
PDF
Solving sudoku
PPT
5.1 mining data streams
PPSX
Intelligent Sudoku Solver
PPTX
And or graph
PPT
Tree and Binary Search tree
Sudoku Solver
Sudoku solver ppt
Solving sudoku
5.1 mining data streams
Intelligent Sudoku Solver
And or graph
Tree and Binary Search tree

What's hot (20)

PPTX
Ai 8 puzzle problem
PPTX
rain technology
PPT
Backtracking
PDF
PPTX
0 1 knapsack using branch and bound
PDF
Cs6702 graph theory and applications Anna University question paper apr may 2...
PPTX
PPTX
Computer Networks - Error Detection & Error Correction
PPT
5.3 mining sequential patterns
PPTX
Asymptotic Notations
PPTX
Clipping
PPTX
Sudoku solver
PPTX
Vector quantization
PPTX
Graph clustering
PPTX
8 queens problem using back tracking
PPTX
Puzzle Solving Using Model Checking
PPTX
Recurrent Neural Networks (RNNs)
PPTX
DAA-Floyd Warshall Algorithm.pptx
PPT
3.9 external sorting
Ai 8 puzzle problem
rain technology
Backtracking
0 1 knapsack using branch and bound
Cs6702 graph theory and applications Anna University question paper apr may 2...
Computer Networks - Error Detection & Error Correction
5.3 mining sequential patterns
Asymptotic Notations
Clipping
Sudoku solver
Vector quantization
Graph clustering
8 queens problem using back tracking
Puzzle Solving Using Model Checking
Recurrent Neural Networks (RNNs)
DAA-Floyd Warshall Algorithm.pptx
3.9 external sorting
Ad

Viewers also liked (20)

PPTX
Sudoku
PPTX
Presentation - Sudoku Assignment
PDF
Breadth-First Search, Depth-First Search and Backtracking Depth-First Search ...
PPT
BackTracking Algorithm: Technique and Examples
PPTX
The Sudoku Cipher
PDF
PDF
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
PDF
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
PPT
Sudoku puzzles
PDF
Clinical Practice Guideline for Dementia 2008
PDF
Sudoku valencia 2
PDF
L1 Sudoku
PDF
ซูโดกุ สสวท
PDF
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
PPTX
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
PDF
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
PPTX
Sudoku powerpoint
PDF
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
Sudoku
Presentation - Sudoku Assignment
Breadth-First Search, Depth-First Search and Backtracking Depth-First Search ...
BackTracking Algorithm: Technique and Examples
The Sudoku Cipher
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
Sudoku puzzles
Clinical Practice Guideline for Dementia 2008
Sudoku valencia 2
L1 Sudoku
ซูโดกุ สสวท
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
Sudoku powerpoint
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
Ad

Similar to Sudoku Solving with Computational Intelligence (20)

PPTX
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
DOCX
The sexagesimal foundation of mathematics
PDF
Grade 10 tutorials
PDF
Sudoku solve rmain
PPTX
GRADE 4 SESSION 4.pptx
PDF
Andrew Daws - Final Project Report
PDF
Z And T Tests
PPTX
Lattice multiplication
PDF
Exponents and radicals in Algebra
PDF
iGridd puzzles, Demo book
PDF
[ACM-ICPC] Sort
PDF
Solving Multiple Square Jigsaw Puzzles with Missing Pieces
PPTX
Presentation- How To Sudoku
PPT
L 12 give and take
PDF
Module 1 answer key
PDF
Sudokureport
PDF
Overview of sparse and low-rank matrix / tensor techniques
PDF
Dan Stefanica - A Primer for the Mathematics of Financial Engineering-FE Pres...
PPTX
FINAL DEFENCE
PDF
Precalculus 6th edition blitzer test bank
Solve Sudoku using Constraint Propagation- Search and Genetic Algorithm
The sexagesimal foundation of mathematics
Grade 10 tutorials
Sudoku solve rmain
GRADE 4 SESSION 4.pptx
Andrew Daws - Final Project Report
Z And T Tests
Lattice multiplication
Exponents and radicals in Algebra
iGridd puzzles, Demo book
[ACM-ICPC] Sort
Solving Multiple Square Jigsaw Puzzles with Missing Pieces
Presentation- How To Sudoku
L 12 give and take
Module 1 answer key
Sudokureport
Overview of sparse and low-rank matrix / tensor techniques
Dan Stefanica - A Primer for the Mathematics of Financial Engineering-FE Pres...
FINAL DEFENCE
Precalculus 6th edition blitzer test bank

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Approach and Philosophy of On baking technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Approach and Philosophy of On baking technology
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx

Sudoku Solving with Computational Intelligence

  • 1. Sudoku Solving with Computational Intelligence Dr. Harald Hiss June 30, 2010
  • 2. Summary Computers can create convincing results – the cinemas are presenting the latest overwhelming 3D multimedia spectacles. The results and benefits aren’t restricted to the amusement and movies, modern methods for our machines bring further considerable improvements to reality. Programs and algorithms evolve in bioinformatics and public health. Computational intel- ligence can also be utilized in solving brainteasing tasks. This article is presenting a computational intelligence approach for solving Sudoku brain- teasers. A scanning strategy introduces Sudoku and solves the first example. The article presents an estimation of the complexity with stochastic methods. Sudoku-specific advanced strategies refine the scanning technique from the introduction. A software-architecture for efficiently implementing a rule- based solver is integrating the strategies and is using annotations that are updated with notifications. When no rule can be applied, a backtracking search is started. The parametrized solver offers policies for searching. A logging mechanism is presented supporting the search saving memory for the efficient implementation. The author, Dr. Harald Hiss, has obtained the diploma in software-engineering in 2004 and he has graduated in databases in 2008 at the university of Freiburg supported by the interdisciplinary graduate programme logic and applications. This article is presenting a new Sudoku solver that performs best. The computer scientist is presenting Sudoku-specific strategies and a solver architecture for the fast and simple implementation using the latest technologies.
  • 3. Contents 1 Solving Sudoku with Computational Intelligence 1 2 Sudoku and a Scanning Strategy 1 3 Possibilities Estimation 3 4 Sudoku-specific Strategies and Techniques 4 5 Solver Software-Architecture 6 6 Integrating AI Search Strategies 7
  • 4. 2 Sudoku and a Scanning Strategy 1 1 Solving Sudoku with Computational Intelligence Computers can create convincing results – the cinemas are presenting the latest over- whelming 3D multimedia spectacles. The results and benefits aren’t restricted to the amusement and movies, modern methods for our machines bring further consider- able improvements to reality. Programs and algorithms evolve in bioinformatics and public health. Computational intelligence can also be utilized in solving brainteasing tasks. This article is presenting a computational intelligence approach for solving Su- doku brainteasers. The example is illustrating specific game strategies, estimating the complexity and again proving both power and expressiveness of computational intelli- gence. At the turn of the millenium, the number puzzle became very popular. The enjoyable quest for filling numbers in fields comes in some variants and difficulty levels. Simple Sudokus can be solved very fast, the next chapter introduces a scanning strategy that is sufficient for automatically solving a first example. After the description, an estimation of the complexity is given with stochastic methods showing a huge amount of possibilities that a solver has to face. For the development of an automatic solver, some advanced strategies are presented. The aspects are integrated in a flexible software-architecture that can be easily adapted to Sudoku variants. Finally, the strategies are integrated with the architecture. Computational intelligence extracts information for choosing the appropriate rules to fill the next field and for choosing optimal fields and numbers for backtracking when no rule applies. The article presents an approach in computational intelligence for Sudoku. The con- venient example is illustrating many aspects of computational intelligence. The num- ber quest is formalized, stochastical methods estimate the possibilities, the specific advanced strategies integrate with the software architecture and a rule-based logical system. Methods from artificial intelligence and learning optimize the search choosing the right fields and numbers – a very promising approach for constructing a success- ful Sudoku solver with computational intelligence. The author has acquired very much knowledge in this field. The huge power of computational intelligence can impressively be shown with the Sudoku example presented in this article. 2 Sudoku and a Scanning Strategy The challenge of Sudoku is posed in filling fields with fitting numbers so that some number constraints are satisfied. The fields are arranged in groups. Within a group, only numbers of a defined domain may be inserted. Numbers may not occur more than once in a group. Solutions have filled all fields so that each number occurs exactly 1
  • 5. 2 Sudoku and a Scanning Strategy 2 once in each group. The standard Sudoku variant considers a 9 × 9 array of fields and defines the domain 1, 2, ..., 9 for the numbers. As groups, the number puzzle considers rows, columns and the 9 blocks formed of the 3 × 3 squares that are tiling the game. The constraints have to be satisfied for each group. For instance, a row can contain numbers in 1, 2, ..., 9 and a row of a solution represents a permutation of the domain. The section shows a simple scanning strategy for solving some Sudokus. Figure 1 Figure 1 An easy Sudoku example. 1 2 5 9 4 4 3 6 1 5 5 1 3 1 2 5 7 2 6 9 3 3 4 2 7 1 3 6 7 1 9 7 1 4 2 8 8 5 9 7 presents the example that is illustrating the strategy. For the numbers of the domain, the rows and columns are scanned to find the appropriate field in a block. The scan starts with 1. The second field of the first row, abbreviated with S12 , has a 1. In the block below, the field S41 stores this number. The first two columns may not contain the number anymore, the left bottom block has the 1 in S73 or S93 . Then the scan considers the relevant rows for the bottom block. The seventh row has a 1 in S77 , the 1 can be filled in S93 . Using scans and filling obvious fields the solution in figure 2 is obtained. The next section estimates the amount of possible games. Then again the article turns to solving strategies and presents some advanced techniques. 2
  • 6. 3 Possibilities Estimation 3 Figure 2 The solution for the previous example. 6 1 3 2 7 5 9 8 4 9 2 8 4 3 6 7 1 5 4 7 5 9 1 8 6 3 2 1 3 9 7 8 2 5 4 6 7 4 2 6 5 1 8 9 3 8 5 6 3 4 9 2 7 1 3 6 4 8 2 7 1 5 9 5 9 7 1 6 4 3 2 8 2 8 1 5 9 3 4 6 7 3 Possibilities Estimation A first naive estimation of the possibilities for choosing the numbers in the 9 × 9 square considers filling each field with any number. The 981 combinations compute a total of 1.966 · 1077 . The first improvement considers the symbols that have been inserted previously in the same row. For nine rows, 9!9 combinations are obtained, a total of 1.091 · 1050 . Taking into account more information considering relevant blocks the num- ber can be improved to 1.528 · 1031 . The huge amount suggests to invest some time in software design considerations and to integrate both efficient game-specific strate- gies and intelligent computational methods instead of implementing a trial-and-error program. The following sections are going to present an efficient approach for the de- velopment of an automatic solver. The final estimation presented in this section is considering sequentially filling numbers, from the left to the right and from the top to the bottom. The estimation uses the max- imal restriction that is available in the context of the focussed field. The maximum of the present numbers in the same row, column or block reduces the possible choice. Figure 3 shows the result. The field in line 5, column 4 is regarding the numbers above and to the left. In the same line, three numbers have been inserted. The column has four numbers, and the middle block three. The column offers the maximal restriction of 3
  • 7. 4 Sudoku-specific Strategies and Techniques 4 Figure 3 Possibilities for choosing a number for a field starting at the top-left corner continuing from the left to the right and downward. 9 8 7 6 5 4 3 2 1 6 5 4 6 5 4 3 2 1 3 2 1 3 2 1 3 2 1 6 6 6 6 5 4 3 2 1 5 5 4 5 5 4 3 2 1 3 2 1 3 2 1 3 2 1 3 3 3 3 3 3 3 2 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 four different numbers. At most five numbers remain for choosing, the possibilities are inserted in the field. With this approach, an estimation of 9! · 6!6 · 3!10 · 5 is obtained. The number of possible valid Sudoku instances can be bound with 1.528 · 1031 . Another aspect of Sudoku brainteasing is presented in figure 3. The last column and the bottom row have one possibility left for each field. Often, many fields are still empty although the game has been solved. Some clean-up work completes a Sudoku game filling num- bers in fields that are restricted already having 8 numbers in the same group. The next section is showing some improvements for the simple scanning solver in- troduced at the beginning and is presenting advanced Sudoku-specific strategies and techniques. 4 Sudoku-specific Strategies and Techniques Easy instances can be solved with simple strategies as shown in section 2. For each number, the possible positions within a block have been scanned excluding correspond- ing rows and columns in the blocks of the same column and row. Obvious fields remain- ing as the single gap in a group can be filled. For each field, the numbers of all groups 4
  • 8. 4 Sudoku-specific Strategies and Techniques 5 it belongs to can be removed from the candidates. Again, if one number is left the gap can be filled. This section is considering advanced strategies for more complex Sudoku solving. Side-effects of groups can be used for improving the scanning approach presented in Figure 4 First lines of the example for the improvement. 1 1 1 1 1 8 9 2 3 4 section 2. Some group restrictions suffice to determine fields in depending groups. Fig- ure 4 shows an example, the possible positions for a 1 in the middle block have blue entries. The Information that the middle block has the 1 in the middle row suffices for filling a 1 in S31 . In this way, the 1 in the first block is restricted to the two bottom fields when the middle and last block share the 1 in the first two rows as shown in figure 5. The section presents a strategy that is not necessarily needed to solve the example of Figure 5 A variant of the improvement. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 2 3 4 5 6 7 the introduction. The last row of the instance shown in figure 6 contains 9, 6, 7 and 8. Other fields in this row can be filled with 1, ..., 5. The bottom middle block contains 4, 2 and is restricting its bottom row to 1, 3, 5. Considering columns is proving S94 , S95 ∈ {1, 5} and S96 ∈ {1, 3}. The constraints for S94 and S95 consume 1 and 5, the three fills S96 . Constraints consuming numbers can be used sometimes similarly to symbols of filled fields. The information just derived together with the 1 in S77 restricts possible positions for filling a 1 in the bottom left block to S81 and S82 . Furthermore, the remaining fields of the bottom middle block may only be filled with 6, ..., 9, possibly resulting in more consequences. 5
  • 9. 5 Solver Software-Architecture 6 Figure 6 A number consuming example with S94 and S95 consuming the pair (1, 5). 7 6 1 3 2 8 7 9 3 7 6 5 9 4 6 3 8 2 4 1 5 6 2 9 6 7 8 The strategies introduced here are comfortably integrated in a software-architecture for a Sudoku solver. The flexible approach makes the integration simple and it is open for integrating new strategies. Section 3 has shown the importance with the huge amount of possibilities that make backtracking costly. 5 Solver Software-Architecture The section presents an object-oriented architecture for a Sudoku solver. A class hierarchy is structuring the logical groups of the game, annotations provide a flexible mechanism for rule-based game solving integrating Sudoku-specific strategies. The solver architecture defines a hierarchy for groups and offers annotations for the integration of strategies. Fields can be filled with numbers, groups represent fields that belong to the same logical unit of the game. In particular, a block represents one of the 3 × 3 squares tiling the game. Rows and columns also represent groups. Each field is associated with the groups it belongs to – a block, a row and a column. Vice versa, the groups are associated with their fields. Furthermore, the groups are associated to the groups they intersect. Empty fields are annotated with possible numbers. Initially, the annotations get the 6
  • 10. 6 Integrating AI Search Strategies 7 numbers that aren’t in the groups of the field. Numbers that don’t occur in a group de- fine annotations. The annotated group initially stores the empty fields. With constraints of these annotations, fields can be filled as immediate consequence. In fields with one possible number, the annotation left can be filled. When the group annotation for a number leaves one field, the number may fill the gap. For filling fields, the annotations are updated. Filling a number generates notifications, the change at a position informs depending fields and groups. In that block, row and column the number can’t occur again, there are fewer possible numbers. The anno- tations of the fields in these groups are updated. Depending groups get notifications. In this way, a generalization of the scanning strategy presented in section 2 is imple- mented. Depending groups are defined having a non-empty intersection with a group of the considered field. For example, filling a 1 in the top left field notifies some depending groups. The first row intersects the top middle and right block – they can fill the only 1 in the rows two and three. In addition, row one is intersecting the first field of all columns. The first column intersects all rows and restricts the left middle and bottom blocks to the columns two and three. Finally, the top left block removes the first three fields from the group annotations of the first three columns and rows. The annotations provide a flexible mechanism for integrating the Sudoku-specific strate- gies presented in section 4. For instance, when the top left block restricts the 1 to the first column, the blocks below are notified in the same way. Fields consuming con- straints like exclusive pairs can have the same side-effect. Finding these constraints always generates notifications within the groups the constraint shares. The field anno- tations remove the numbers of such a pair, for example. When no rules apply, the solver uses backtracking for searching a solution. The object- oriented architecture and the annotations of this section are integrated in the intelligent search that the next section is presenting. 6 Integrating AI Search Strategies As section 3 has been showing, the huge amount of possibilities can cause consider- able costs and so a simple scanning approach and advanced Sudoku-specific strate- gies have been introduced in the sections 2 and 4. The previous section has integrated the strategies in a rule-based solver using annotations for fields and groups. When no rule can be applied, the solver is starting a backtracking search that is presented in this section. The annotations can be used for optimizing the search. The solver stores the progress for a Sudoku instance. A history remembers the fields that are filled and the changes that are computed for the annotations. Logging the steps is supporting an optimized backtracking search. When no rule or strategy can be ap- 7
  • 11. 6 Integrating AI Search Strategies 8 plied, the solver is introducing a choice-point and is starting a search. Occassionally choosing a field and a number can be wrong, a guess can cause a clash. Each time after filling further fields, the solver is checking clashes. Empty fields must have non- empty annotations that suggest possible numbers for filling. Numbers that don’t occur in a group may not have empty annotations in that particular group. Such numbers are then missing in the group that can’t form a solution anymore. Checking clashes can be limited to changes of annotations. The backtracking is integrating the annotations. The solver makes a guess and then continues with rule-based solving. New choice-points can be introduced and clashes revise a guess. A choice-point is choosing a number for a field or the place for a number within a group. The annotations decrease the considered possibilities and can be used to optimize the decisions regarding the basis of a choicepoint. Furthermore, the history is reducing the required resources storing the changes that can simply be undone. A choice-point stores the parent state and the history for reaching a descendant. When breadth-first search is desired, the ways to other choice-points can also be logged. The choice-point stores the possible choices within its basis and the choices that can al- ready be disregarded. A field is determined, if only one possibility is left. One more clash propagates the change to the parent. The approach offers policies for the parametrized search. The standard depth-first search method can be replaced with breadth-first search and the approach is open for further strategies. Choosing the basis for a choice-point is using annotations. For exam- ple, a test would like to give high priority to choice-points with group annotations storing two or three fields for a certain number. Another test might prefer field annotations providing few possible numbers for filling the gap. The solver is open for fine-tuning in accordance with the software-architecture presented in section 5. Finally, the parameter can be optimized with statistical methods. Further methods of artificial intelligence can be utilized for optimizing the solver. Training with Sudoku instances is very promising for finding the best basis for the choice-point and for at fastest finding the solution. 8