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 intelligence can also be utilized in solving brainteasing tasks. This article is presenting a computational intelligence approach for solving Sudoku brainteasers.
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.
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