Sudoku Solutions Using Logic Equations - Institut für Informatik

6 downloads 0 Views 118KB Size Report
also be extended to larger Sudokus and to a whole class of similar discrete ... Over the last years a Japanese game with the name Sudoku became very popular.
Sudoku Solutions Using Logic Equations Christian Posthoff The University of The West Indies St. Augustine Campus Trinidad & Tobago email: [email protected] Bernd Steinbach Freiberg University of Mining and Technology Institute of Computer Science D-09596 Freiberg, Germany email: [email protected] Abstract In several previous papers and particularly in [3] we presented the use of logic equations and their solution using ternary vectors and set-theoretic considerations as well as binary codings and bit-parallel vector operations. In this paper we introduce a new and elegant model for the game of Sudoku that uses the same approach and solves this problem without any search always finding all solutions (including no solutions or several solutions). It can also be extended to larger Sudokus and to a whole class of similar discrete problems, such as Queens’ problems on the chessboard, graph-coloring problems etc. Disadvantages of known SAT approaches for such problems were overcome by our new method.

1

Introduction

Over the last years a Japanese game with the name Sudoku became very popular. It is played mostly on a board with 9 x 9 fields, but other square numbers are also possible, such as 4 x 4 or 16 x 16 or even 25 x 25. It is easy to understand and a bit challenging for human beings, and it can be used comfortably to spend waiting time on airports or similarly. But there are also mathematical and logical properties that deserve some attention. There is a quadratic board of, for instance, size 9 x 9. In each column, in each row and in nine subsquares of size 3 x 3 the values 1, . . . , 9 have to be set such that in each column, in each row and in each subsquare each value is used once and only once. 5 6

3 1 9

8 4 7

7 9

5

8

6 6 8

3 1 6

3 2

6

2 4

1 8

8

9 7

5 9

Some values already have been set, and the other values have to be found according to the existing values. We enumerate the columns from the left to the right and the rows bottom-up (in the same way as a normal planar coordinate system). The reviewers of our papers made us aware of at least two papers that are using SAT for the modeling of the game and existing SAT-solvers for the solution of the problem [2], [6]. These

papers used the following approach: a binary variable xijk describes the content of the field (i, j), 1 ≤ i, j, k ≤ 9:  1 if the value on the field (i, j) = k xijk = 0 if the value on the field (i, j) 6= k The transformation into a SAT-problem uses several steps: (xij1 ∨ xij2 ∨ xij3 ∨ xij4 ∨ xij5 ∨ xij6 ∨ xij7 ∨ xij8 ∨ xij9 ) = 1 (Kij1 ∨ Kij2 ∨ Kij3 ∨ Kij4 ∨ Kij5 ∨ Kij6 ∨ Kij7 ∨ Kij8 ∨ Kij9 ) = 1 expresses the requirement that one of the numbers 1, . . . , 9 must be used for the field (i, j). Such a disjunction must be written for each field of the board which results in 81 clauses which must be satisfied simultaneously. The second step expresses all the constraints for rows, colums and squares as clauses as well. For example for the field (1, 1) and the value 1 on this field, no other value can be on this field: x111 → x112 , x111 → x113 , . . . , x111 → x118 , x111 → x119 . The same set of clauses must be written for the other values 2, . . . , 9 on the same field. The requirements for the first column can be expressed in the same way: x111 → x121 , x111 → x131 , . . . , x111 → x181 , x111 → x191 . The constraints for the row are given as x111 → x211 , x111 → x311 , . . . , x111 → x811 , x111 → x911 , and finally we must consider the value 1 in the respective square: x111 → x221 , x111 → x321 , . . . , x111 → x231 , x111 → x331 . Again all these clauses have to be written for all numbers from 1 to 9 and finally for all fields. By using the rule x → y = x∨y the whole set of implications ca be transformed into disjunctions, all of them must be satisfied at the same time, and this is the problem in SAT-format. Each satisfying set of values for the binary variables is a solution of the Sudoku. The paper [2] considered a minimal encoding which asserts that there is at least one number in each entry - this resulted in 8829 clauses, and an extended encoding with 11988 clauses that ensures that there is precisely one number in each field. The second approach [6] uses an interactive theorem prover Isabelle/HOL for the implementation of the Sudoku rules and creates 11745 clauses. The published different numbers of clauses for the same problem show that there is no unique SAT formula for the given fixed problem. It is quite clear that advanced SAT-solvers are still able to handle these sets of clauses, but we still can see several simplifications and a much higher understandability and efficiency that results from our approach. We will show that this game easily can be modeled by using a logic equation, with ternary vectors as the most appropriate data structure. Actually, the logic equation does not even have to be written down, the ternary vectors can be generated directly.

2

The Logical Model of the Game

We are using the same encoding as the two other papers mentioned above:  1 if the value on the field (i, j) = k xijk = 0 if the value on the field (i, j) 6= k The constraints can be stated by one single conjunction for each number on each field: K111 = x111 x112 x113 x114 x115 x116 x117 x118 x119 x121 x131 x141 x151 x161 x171 x181 x191 ∧ ∧ x211 x311 x411 x511 x611 x711 x811 x911 x221 x231 x321 x331 .

This conjunction describes completely the setting of 1 on the field (1, 1) and all the consequences. There are 729 of such conjunctions which are defined uniquely. The requirements of the Sudoku can be expressed in different ways. It is important to mention that not only the requirement in terms of 9 variable xijk are taken in consideration but the conjunctions Kijk so that all the consequences resulting from a given setting are used immediately. 1. The equation (K111 ∨ K112 ∨ K113 ∨ K114 ∨ K115 ∨ K116 ∨ K117 ∨ K118 ∨ K119 ) = 1 describes that one of the 9 values must be assigned to one field (the field (11) is only an example). 2. The equation (K111 ∨ K121 ∨ K131 ∨ K141 ∨ K151 ∨ K161 ∨ K171 ∨ K181 ∨ K191 ) = 1 describes that the values 1 must be assigned to one of the fields in a row (row 1 and value 1 are only examples). 3. The equation (K111 ∨ K211 ∨ K311 ∨ K411 ∨ K511 ∨ K161 ∨ K711 ∨ K811 ∨ K911 ) = 1 describes that the values 1 must assigned to one of the fields in a column (column 1 and value 1 are only examples). 4. The equation (K111 ∨ K121 ∨ K131 ∨ K211 ∨ K221 ∨ K231 ∨ K311 ∨ K321 ∨ K331 ) = 1 describes that the values 1 must be assigned to one of the fields in a subsquare (the first subsquare and value 1 are only examples). From each type of the equations, introduced above, can be generated a system 81 equations. Each of these systems describes both the requirements and the constraints of the Sudoku completely. Such a system of logic equations can be transformed into a single equation consisting of 81 disjunctions covering at all 729 conjunctions. The free choice between the four possible systems of logic equations can be used in order to optimize the solution effort. The aim of this optimization is to reduce the remaining search space as much as possible. For that the fixed variables in the conjunctions Kijk of a single equation must overlap as less as possible. Under this condition the first system of equations is a bad choice while the other three system of equations have equivalent properties with regard of fast restriction of the remaining search space. Each given setting (such as x1,9 = 5) selects one out of 9 conjunctions. In the given example 30 conjunctions already have been selected, and ”only” 51 empty cells have to be considered which means that we have to work with 459 conjunctions altogether to solve this problem (compare this figure with the number of clauses in [2] or [6]). ′ ′ as the conjunction of all negated with K111 Hint: When we are setting K111 = x111 K111 variables of K111 , then we can see that the conjunction K111 expresses the rule ′ x111 ∧ (x111 → K111 ) ′ ) = K111 . which can be transformed into x111 (x111 ∨ K111

3

Ternary Vectors as the Main Data Structure

Now we introduce the data structure of a ternary vector. Let x = (x1 , · · · , xn ), xi ∈ {0, 1, −}, i = 1, . . . , n. Then x is called a ternary vector which can be understood as an abbreviation of a set of binary vectors. When we replace each − by 0 or 1, then we get several binary vectors

generated by this ternary vector. In this way, the vector (0−1−) represents four binary vectors (0010), (0011), (0110) and (0111). A list (matrix) of ternary vectors can be understood as the union of the corresponding sets of binary vectors. And there is a second direct relation of ternary vectors with conjunctions. When there is given a conjunction C with variables x1 , . . . , xk , then we can build a ternary vector t with the components t1 , . . . , tk according to the following coding: ti missing : ti = −.

xi : ti = 0,

xi : ti = 1,

This coding expresses directly on one side the respective conjunction, on the other side the set of all binary vectors satisfying C = 1. Example: Let be given x1 x2 x3 x5 = 1, then we have t = (101 − 0) which expresses the two binary vectors (10100) and (10110). In this way each conjunction of the logical model can be represented by one ternary vector with 729 components, one ternary value for each field. The OR operations in the equations of the requirements can be realized by unions of ternary vectors. Due to the orthogonality this union can be solved by a simple connection of the nine ternary vectors into a set of ternary vectors. The solution of the system of logic equations includes all binary vectors which solve each of the equations, hence the intersection of ternary vectors must be calculated. The intersection will be computed according to Table 1 which has to be applied in each component. The ∅ indicates that the intersection is empty and can be omitted. A sophisticated Table 1: Intersection of Ternary Values xi yi xi ∩ yi

0 0 0

0 1 ∅

0 − 0

1 0 ∅

1 1 1

1 − 1

− 0 0

− 1 1

− − −

coding of the three values 0, 1 and − allows the introduction of binary vector operations that can be executed on the level of registers (32, 64 or even 128 bits in parallel). We use the coding of Table 2. Table 2: Binary Code of Ternary Values ternary value 0 1 −

bit1 1 1 0

bit2 0 1 0

When the three-valued operations for the intersection are transferred to these binary vectors, then the intersection is empty if bit1(x) ∧ bit1(y) ∧ (bit2(x) ⊕ bit2(y)) 6= 0. If the intersection is not empty, then it can be determined by the following bit vector operations: bit1(x ∩ y) = bit1(x) ∨ bit1(y), bit2(x ∩ y) = bit2(x) ∨ bit2(y). Hint: ⊕ indicates the exclusive-or. Hence, by using some very fast and very simple bit vector operations (available on the hardware level), we can find the solution set of any logic equation. Taking the second system of equations (requirements with regards of the rows), uur solution process reached now the following state:

    

K111 K121 .. . K191





    ∩  

K112 K122 .. . K192





     ∩ ... ∩   

K919 K929 .. . K999



  . 

All the conjunctions are now represented by ternary vectors, and this representation can be generated before any real game given by special settings. Each ternary vector will have 729 components, and all intersections from the left to the right have to be calculated. Since the values which have already been set result in one vector for the given field, many of these matrices will have only one row (30 in the given example). Therefore it is advisable (however, not necessary), to intersect the single vectors first, because thereafter many intersections will be empty. This algorithm does not need any considerations for special cases. If there are no solutions, then the intersection will be empty at a given point of time, if there is a unique solution, then we will have precisely one vector in the final intersection, and more than one solution will be expressed by the respective number of vectors. The solution set S consists of all binary vectors of the length 729 that solve the SAT problem of the given Sudoku. Each solution vector includes exactly 81 values 1 that indicate the solution numbers associated to the fields. The remaining 648 components of each solution vectors carry the the value 0. Thus, by taking the index (i, j, k) of the values 1 in the solution vector, a representation of the value k in the field(i, j) of column i and row j can be established.

4

Implementation of the new Approach: Implicit TwoPhase SAT-Solver

4.1

First Phase of the Implicit SAT-Solver

The idea of our new approach was introduced in Section 2. Instead of proceeding the huge number of clauses used as basic information in [2], [6] we use a much smaller set of partial solutions. In the first phase of our new implicit two-phase SAT solver we neither prepare the huge amount of clauses nor we apply a classical SAT-solver to it. We also does not create logic equations explicitly but we generate partial solution sets that solve the constrains of the the problem directly. This first phase covers the modeling of the problem and the calculation of partial solution sets implicitly. Therefor we call this new approach Implicit Two-Phase SATSolver. Of course the algorithm of the first phase depends on the problem to be solved - in our case any Sudoku game. Basically this approach is not restricted to solve Sudoku games; it can be easily fitted to other tasks like e.g. graph coloring or others. The Algorithm 1 generates the matrix mpss that covers the partial solution sets of any Sudoku completely. Generally we get an n3 × n3 -matrix for an n × n-Sudoku as result of the first phase of the new implicit SAT-solver. This matrix includes n3 partial solution sets. Each of them includes exactly one value 1 and qsf rc values 0. The value of qsf rc is defined by the number restrictive clauses having the same single fixed premise. √ qsf rc = 3 ∗ (n − 1) + ( n − 1)2 It should be mentioned that the n3 partial solution sets are valid for any special Sudoku game. Therefore the Algorithm 1 must be executed only once while initializing a program that solves any special Sudoku game of a given size. The calculated is stored for later use.

4.2

Second Phase of the Implicit SAT-Solver

Using the ternary vectors created in the first phase, which represent partial solution sets of the restrictive laws, two tasks have to be solved in the second phase. 3

1. The complete Boolean space B (n ) must be restricted with regard of the given assignments. This task is solved in the lines 1 to 4 of Algorithm 2 be means of intersection operations.

Algorithm 1 Create the n3 × n3 matrix of partial solution sets: mpss CPSS(Size n) Require: number n of rows, columns or values of a Sudoku game Ensure: n3 × n3 matrix pss that includes for each possible local assignment the associated partial solution set 1: mpss ← ∅ 2: for all i such that 1 ≤ i ≤ n do {iterate over all rows} 3: for all j such that 1 ≤ j ≤ n do {iterate over all columns} 4: for all k such that 1 ≤ k ≤ n do {iterate over all values} 5: x[i, j, k] ← 1 6: for all v such that 1 ≤ v ≤ n do {iterate over all values} 7: if v 6= k then {other values} 8: x[i, j, v] ← 0 9: end if 10: end for 11: for all c such that 1 ≤ c ≤ n do {iterate over all columns} 12: if c 6= j then {other columns} 13: x[i, c, k] ← 0 14: end if 15: end for 16: for all r such that 1 ≤ r ≤ n do {iterate over all rows} 17: if r 6= i then {other rows} 18: x[r, j, k] ← 0 19: end if 20: end for√ 21: ssn ← n 22: dssi ← ((i − 1) mod ssn) + 1 23: bssi ← ((i − dssi)/ssn) 24: dssj ← ((j − 1) mod ssn) + 1 25: bssj ← ((j − dssj)/ssn) 26: for all r such that 1 + bssi ∗ ssn ≤ r < (bssi + 1) ∗ ssn do {rows of subsquare} 27: for all c such that 1 + bssj ∗ ssn ≤ c < (bssj + 1) ∗ ssn do {columns of subsquare} 28: if (r 6= i) ∧ (c 6= j) then {other rows and columns of the subsquare} 29: x[r, j, k] ← 0 30: end if 31: end for 32: end for 33: mpss[(((i − 1) ∗ n) + (j − 1) ∗ n) + k − 1] ← x 34: end for 35: end for 36: end for 37: return mpss{the n3 × n3 matrix of partial solution sets}

The result is a single ternary vector that includes values 1 for the given assignments and due to the restrictive law covered by the partial solution sets already many values 0. 2. The remaining search space must be restricted further by the solution of a system of n2 logic equations which take into account both the restrictive laws (from the partial solution sets) and the requirement laws expressed by unions of selected partial solution sets. Intersection operation between the remaining search space and n2 such unions calculate all existing solutions of the Sudoku game with any search procedures [1]. Each of the 4 equation system introduced in Section 2 solves this task separately. For complexity reasons [7] it is strongly suggested that one of the last three systems of logic equation enumerated in Section 2 should be used. In the lines 5 to 14 of Algorithm 2 we have implemented the second system of logic equations that requires each of the n values in each row. Due to the used partial solution sets all other requirements hold implicitly. Algorithm 2 solves the SAT problem of a Sudoku game completely. Algorithm 2 Solve SAT Problem of an n × n Sudoku: s SSP(Size n, Settings g, M P S mpss) Require: number n of rows, columns or values of a Sudoku game vector g of given settings where the value g specified the associated value in mpss matrix mpss of partial solution sets as created in Algorithm 1 Ensure: set of all solution vectors of the length n3 that hold both the given settings and the rule of the Sudoku game 1: s ← F U LL(mpss) 2: for all gi in g do {iterate over all given settings} 3: s ← ISC(s, mpss[gi ]) 4: end for 5: for all k such that 1 ≤ k ≤ n do {iterate over all values} 6: for all i such that 1 ≤ i ≤ n do {iterate over all rows} 7: rule ← ∅ 8: for all j such that 1 ≤ j ≤ n do {iterate over all columns} 9: constraint ← mpss[(((i − 1) ∗ n) + (j − 1) ∗ n) + k − 1] 10: rule ← U N I(rule, constraint) 11: end for 12: s ← ISC(s, rule) 13: end for 14: end for 15: return s{set of all solutions of the Sudoku game} The solution set s of Algorithm 2 consists of all binary vectors of the length n3 that solve the SAT problem of the given Sudoku. The advantage of the new implicit two-phase approach in comparison with the known traditional SAT-model is that the assignment of one single value does not determine only one value of the solution, but additionally qsf rc values. In case of a 9 × 9 Sudoku a single assignment specifies additionally 28 variables of the solution space, and this strongly restricts the remaining search space.

5

One Interesting Example and Experimental Results

We only deal with a subset of interesting mathematical problems and properties of the game. One other of these problems, for instance, is the question: How many settings are necessary to define one and only one unique solution? In [5] an example with 16 settings has been given that has precisely two solutions. It is not known whether there are other examples with 16 settings and one or two solutions. It is known, however, that there are many unique solutions when the number of settings is equal to 17. The concept of a unique solution still has to be defined properly because such operations like relabeling of entries, reflection, rotation, ... of a valid Sudoku give other valid Sudokus. But this will be considered at another time. We used this Sudoku as an example.

1

5 3 4

2 3

4

7 2

2

6 5

1

7

3 1

The two solutions required are as follows: Solution 2

Solution 1 1 4 7 6 5 8 2 9 3

8 6 5 2 3 9 1 7 4

3 9 2 1 4 7 6 5 8

9 5 1 4 8 2 3 6 7

6 3 4 7 1 5 8 2 9

7 2 8 3 9 6 5 4 1

4 8 6 5 7 3 9 1 2

2 1 9 8 6 4 7 3 5

5 7 3 9 2 1 4 8 6

1 4 7 6 5 9 2 8 3

9 6 5 2 3 8 1 7 4

3 8 2 1 4 7 6 5 9

8 5 1 4 9 2 3 6 7

6 3 4 7 1 5 9 2 8

7 2 9 3 8 6 5 4 1

4 9 6 5 7 3 8 1 2

2 1 8 9 6 4 7 3 5

5 7 3 8 2 1 4 9 6

The second solution can be found easily by exchanging 8 and 9 in the first solution. The program has built the game matrix (which can also be stored before any real game) in the first phase using 515 milliseconds. The two solutions have been determined by the solution program in the second phase after 16 milliseconds. In case of a 16 × 16 board the matrix of the partial solution sets required approximately 2 Megabyte. Each row of this matrix includes one value 1, 54 values 0. The remaining values of the 4096 variables are filled with dashes. Therefore we decided to store only the index values of the elements with the value 0 and 1 and to generate any vector of a partial solution set at the time when it is required. Without any other changes the problem of a 16 × 16 Sudoku that maps into a problem of 4096 variables and 111616 clauses could be solved within about two and a half minutes.

6

Conclusions

There are several valuable results presented in this paper. 1. The Sudoku game as a particular finite discrete problem has been modeled as a SATproblem implicitly. It has been shown that it is not necessary to write down the huge number of clauses of the conjunctive forms which must be solved by a SAT-solver. Based on the explored properties of the Sudoku game, it has been possible to generate partial solution sets of the restrictive laws by a generic algorithm directly. 2. A new implicit two-phase SAT-solver has been suggested. In the first phase this SATsolver creates partial solution sets which are reused for two subtasks in the second phase to calculate the solution without any further decisions. 3. The matrix of the partial solution sets describes general constraints of an n × n Sudoku without any consideration of the clauses. Algorithm 1 creates this matrix of the partial solution sets directly for a given instance size of n. 4. The reuse of the partial solution sets in the second phase of the SAT-solver allows to solve the huge SAT-problems very quickly. The partial solution sets help first to restrict 3 the enormous search space of 2n binary vectors significantly. The remaining requirement clauses of the game are replaced by unions of partial solution sets which speed up the solution procedure.

5. There are four logical equivalent systems of n2 logic equations that solve together with the constraints of the given assignments the Sudoku completely. One of these systems of equations has a bad run time complexity. 6. The SAT-instance of more than 700 variables and about 10,000 clauses could be solved in the second phase of the new implicit SAT-solver in less than 20 milliseconds.

References [1] Levin, L. Universal’nye Perebornye Zadachi. Problemy Peredachi Informatsii 9 (3), 1973, pp. 265-266. English translation: Universal Search Problems. In B.A. Trakhtenbrot: A Survey of Russian Approaches to Perebor (Brute-Force Search) Algorithms. Annals of the History of Computing 6 (4), 1984, pp. 384–400. [2] Lynce, I. and Ouaknine, J., Sudoku as a SAT Problem. 9th International Symposium on Artificial Intelligence and Mathematics, January 2006. [3] Posthoff, Ch. and Steinbach, B. Logic Functions and Equations - Binary Models for Computer Science. Springer, Dordrecht, The Netherlands, 2004. Springer, Dordrecht, The Netherlands, 2005. [4] Posthoff, Ch. and Steinbach, B. A Multi-Processor Approach to SAT-Problems. 7th International Workshop on Boolean Problems, 19th - 20th of September 2006, Freiberg University of Mining and Technology, Freiberg, Germany, 2006. [5] Royle, G. ”Minimum Sudoku.” http://www.csse.uwa.edu.au/ gordon/sudokumin.php. [6] Weber, T. A SAT-based Sudoku Solver. TU Munich, November 2005. [7] Wegener, I. Complexity Theory - Exploring the Limits of Efficient Algorithms.