Abstract. We classify sudoku puzzles into four types of puzzles according to their
...... Our method is also applicable for 16x16 sudoku puzzles, however, the first.
Boolean Gr¨obner Bases and Sudoku Yosuke Sato
Shutaro Inoue Akira Suzuki Katsusuke Nabeshima Abstract
We classify sudoku puzzles into four types of puzzles according to their boolean structures. We show that boolean Gr¨ obner bases computations are useful for solving sudoku puzzles with checking their types. Data of our implementations shows that our method is satisfactorily practical and that there are strong relationship between our classification and the levels of difficulty. Our approach sheds light on the boolean structures of sudoku puzzles.
1
Introduction
Combinatorial problems are often reduced to solving certain types of constraints of set. Such set constraints are described as polynomial equations over boolean rings. Sudoku that is one of the most popular puzzles in the world is one of such instances. Though there are enormous amount of mathematical papers or articles of sudoku, there does not exist any paper which deals with a sudoku puzzle as a system of polynomial equations over boolean rings of sets. In this paper, we study the boolean structure of sudoku puzzles. Every sudoku puzzle is considered as an ideal of a certain boolean polynomial ring. We categorize sudoku puzzles into essentially four types of puzzles according to the structures of their associated ideals. We show that boolean Gr¨obner bases introduced in [6, 7] are useful not only for checking the type of a given sudoku puzzle but also for solving it. The data of our computation experiments shows that there are strong relationship between the types of sudoku puzzles and the levels of their difficulty. Though our aim is not making a fast sudoku solver, our implementation shows that our method is satisfactorily practical. Our program does not employ any sophisticated heuristics, nonetheless it can solve most 9x9 sudoku puzzles at most in a minute with a standard laptop computer. We can even handle sudoku variants such as a diagonal sudoku with the same program. Our plan is as follows. In section 2, we first give a quick review of necessary classical results of boolean algebra in terms of boolean rings. More detailed comprehensive descriptions can be found in [5]. We also describe minimum background of boolean Gr¨obner bases which is necessary for this paper. The reader is referred to [8] or [5] for more details. In section 3, we first show how sudoku puzzles are translated into a systems of equations of boolean rings. Then
1
we show how we can categorize sudoku puzzles into four classes together with an algorithm to solve sudoku puzzles by boolean Gr¨obner bases computations. Some timing data of our computation experiments are also given in Section 4. We also assume the reader knows at least the rules of sudoku.
2 2.1
Boolean polynomial ring Classical results
In this section, we give several definitions and notations concerning boolean polynomial rings, then we present the extension theorem of boolean equations, an important classical result of boolean algebra. We describe it in terms of boolean polynomial rings. More detailed descriptions with proofs can be found in [5]. Definition 1 A commutative ring B with an identity 1 is called a boolean ring if every element a of B is idempotent, i.e. a2 = a. hB,∨, ∧, ¬i becomes a boolean algebra with the boolean operations ∨, ∧, ¬ defined by a ∨ b = a + b + a · b, a ∧ b = a · b, ¬a = 1 + a. Conversely, for a boolean algebra hB,∨, ∧, ¬i, if we define + and · by a + b = (¬a ∧ b) ∨ (a ∧ ¬b) and a · b = a ∧ b, hB,+, ·i becomes a boolean ring. Since −a = a in a boolean ring, we do not need to use the symbol ’−’, however, we also use − when we want to stress its meaning. Example 1 Let S be an arbitrary set and P(S) be its power set, i.e. the family of all subsets of S. Then, (P(S), ∨, ∧, ¬) becomes a boolean algebra with the operations ∨, ∧, ¬ as the union, intersection and the complement of S respectively. As a boolean ring, it is isomorphic to GFS2 that is a commutative ring of all functions from S to GF2 . Stone’s representation theorem tells us any boolean ring is isomorphic to a sub-algebra of GFS2 for some set S. Especially, when B is a finite boolean ring, it is isomorphic to a direct product GFk2 for some natural number k. Note that a computable boolean ring need not be finite. For any countable set S, any family of computable subsets S which is closed under the computable operations ∨, ∧, ¬ is a computable boolean ring. Definition 2 Let B be a boolean ring. A quotient ring B[X1 , . . . , Xn ]/hX12 − X1 , . . . , Xn2 − Xn i with an ideal hX12 − X1 , . . . , Xn2 − Xn i becomes a boolean ring. It is called a boolean polynomial ring and denoted by B(X1 , . . . , Xn ), its element is called a boolean polynomial. Note that a boolean polynomial of B(X1 , . . . , Xn ) is uniquely represented by a polynomial of B[X1 , . . . , Xn ] that has at most degree 1 for each variable Xi . In what follows, we identify a boolean polynomial with such a representation. ¯ or Multiple variables such as X1 , . . . , Xn or Y1 , . . . , Ym are abbreviated to X ¯ Y respectively. Lower small Greek letters such as a, b, c are usually used for 2
elements of a boolean ring B. The symbol a ¯ denotes an n-tuple of element of B for some n. For a ¯ = (a1 , . . . , an ) and ¯b = (b1 , . . . , bm ), (¯ a, ¯b) denotes an ¯ Y¯ ) with n + m-tuple (a1 , . . . , an , b1 , . . . , bm ). For a boolean polynomial f (X, ¯ and Y¯ , f (¯ variables X a, Y¯ ) denote a boolean polynomial in B(Y¯ ) obtained by ¯ with a specializing X ¯. Definition 3 Let I be an ideal of B(X1 , . . . , Xn ). For a subset S of B, VS (I) denotes a subset {¯ a ∈ S n |∀f ∈ If (¯ a) = 0}. When S = B, VB (I) is simply denoted by V (I) and called a variety of I. We say I is satisfiable in S if VS (I) is not empty. When S = B, we simply say I is satisfiable. Theorem 4 (The boolean extension theorem) Let I be a finitely generated ideal in a boolean polynomial ring B(Y1 , . . . , Ym , X1 , . . . , Xn ). For any ¯b ∈ V (I ∩ B(Y¯ )), there exist c¯ ∈ Bn such that (¯b, c¯) ∈ V (I). The following is a special case of the extension theorem. Corollary 5 (The boolean weak Nullstellensatz) For any finitely generated ideal I of a boolean polynomial ring B(X1 , . . . , Xn ), the variety V (I)(⊆ Bn ) of I is an empty set if and only if there exists a non-zero constant element of B in I.
2.2
Boolean Gr¨ obner bases
A boolean Gr¨obner basis is defined as a natural modification of a Gr¨obner basis in a polynomial ring over a boolean ring. Though it was introduced in [6, 7] together with a computation algorithm using a special monomial reduction, the same notion was independently discovered by V.Weispfenning in a polynomial ring over a more general coefficient ring, namely, a commutative von Neumann regular ring( [10]). In this section, we give a quick review of boolean Gr¨ obner bases. For the proofs and more detailed descriptions, refer to [10] or [8]. We concentrate on boolean rings. In what follows, we assume that some term order on a set of power products of variables is given. For a polynomial f in ¯ over a boolean ring B, we use the a polynomial ring B[X1 , . . . , Xn ] (= B[X]) notations LT (f ), LM (f ) and LC(f ) to denote the leading power product, the leading monomial and leading coefficient of f respectively. f − LM (f ) is also denoted by Rd(f ). We also use the notations LT (F ) and LM (F ) to denote the sets {LT (f )|f ∈ F } and {LM (f )|f ∈ F } for a (possibly infinite) subset F of ¯ T (X) ¯ denotes the set of power products consisting of variables X. ¯ B[X]. ¯ a finite subset G of I Definition 6 For an ideal I of a polynomial ring B[X], is called a Gr¨obner basis of I if hLM (I)i = hLM (G)i. ¯ let a = LC(f ), t = LT (f ) and Definition 7 For a polynomial f ∈ B[X], h = Rd(f ). A monomial reduction →f by f is defined as follows: bts + p →f (1 − a)bts + absh + p. 3
(Note that (bts + p) − ((1 − a)bts + absh + p) = bs(af ).) ¯ b is an element of B such that ab 6= 0 and p is Where s is a term of T (X), ¯ For a set F ⊆ B[X], ¯ we write g →F g ′ if and only any polynomial of B[X]. ∗ if g →f g ′ for some f ∈ F . A recursive closure of →F is denoted by →F , i.e. ∗ g →F g ′ if and only if g = g ′ or there exist a sequence of monomial reductions g →F g1 →F · · · →F gn →F g ′ . Theorem 8 When F is finite, →F is noetherian, that is there is no infinite sequence of polynomials g1 , g2 , . . . such that gi →F gi+1 for each i = 1, 2, . . .. ¯ Theorem 9 Let I be an ideal of a polynomial ring B[X]. ∗ A finite subset G of I is a Gr¨ obner basis of I if and only if ∀h ∈ I h →G 0. Using our monomial reductions, a reduced Gr¨obner basis is defined exactly same as in a polynomial ring over a field. A Gr¨obner basis G is reduced if each polynomial of G is not reducible by a monomial reduction of any other polynomial of G. In a polynomial ring over a field, a reduced Gr¨obner basis is uniquely determined. In our case, however, this property does not hold. Example 2 Let B = GF2 ×GF2 . In a polynomial ring B[X], {(1, 0)X, (0, 1)X} and {(1, 1)X} are both reduced Gr¨ obner bases of the same ideal. In order to have a unique Gr¨obner basis, we need one more definition. Definition 10 A reduced Gr¨ obner basis G is said to be stratified if G does not contain two polynomials which have the same leading power product. Theorem 11 If G and G′ are stratified Gr¨ obner bases of the same ideal w.r.t. some term order, then G = G′ . In the above example, {(1, 1)X} is the stratified Gr¨obner basis, but the other is not. Definition 12 For a polynomial f , LC(f )f is called a boolean closure of f , and denoted by bc(f ). If f = bc(f ), f is said to be boolean closed. Theorem 13 Let G be a Gr¨ obner basis of an ideal I, then bc(G) \ {0} is also a Gr¨ obner basis of an ideal I. Theorem 14 Let G be a reduced Gr¨ obner basis, then every element is boolean closed. S-polynomial is also defined similarly as in a polynomial ring over a field. Definition 15 Let f = atr + f ′ and g = bsr + g ′ be polynomials where a = LC(f ), b = LC(g), tr = LT (f ) and sr = LT (g) for some power product t, s, r such that GCD(t, s) = 1, i.e. t and s do not contain a common variable. The polynomial bsf + atg = bsf ′ + atg ′ is called an S-polynomial of f and g and denoted by S(f, g). 4
As in a polynomial ring over a field, the following property is crucial for the construction of Gr¨obner bases. Theorem 16 Let G be a finite set of polynomials such that each element of G ∗ is boolean closed. Then, G is a Gr¨ obner basis if and only if S(f, g) →G 0 for any pair f, g of G. For any given finite set F , using our monomial reductions, we can always construct a Gr¨obner basis of hF i with computing boolean closures and Spolynomials by the following algorithms. It is also easy to construct a stratified Gr¨obner basis from a Gr¨obner basis. Algorithm BC ¯ Input: F a finite subset of B[X] ′ Output: F a set of boolean closed polynomials such that hF ′ i = hF i begin F′ = ∅ while there exists a polynomial f ∈ F which is not boolean closed F = F ∪ {bc(f ) − f } \ {f }, F ′ = F ′ ∪ {bc(f )} end. Algorithm GBasis ¯ > a term order of T (X) ¯ Input: F a finite subset of B[X], Output: G a Gr¨obner basis of hF i w.r.t. > begin G = BC(F ) ∗ while there exists two polynomials p, q ∈ G such that S(p, q) →G h for some non-zero polynomial h which is irreducible by →G G = G∪BC({h}) end. Since any element of a boolean ring is idempotent, a boolean polynomial ring is more natural to work on. We can also define Gr¨obner bases in boolean polynomial rings. A power product X1l1 · · · Xnln is called a boolean power product if each li is ei¯ ther 0 or 1. The set of all boolean power products consisting of variables X ¯ ¯ ¯ is denoted by BT (X). A boolean polynomial f (X) in B(X) is uniquely represented by b1 t1 + · · · + bk tk with elements b1 , . . . , bk of B and distinct boolean power products t1 , . . . , tk . We call b1 t1 + · · · + bk tk the canonical representa¯ Since BT (X) ¯ is a subset of T (X), ¯ a term order ≥ on T (X) ¯ is tion of f (X). ¯ also defined on BT (X). Given such a term order ≥, we use the same notations LT (f ), LM (f ), LC(f ) and Rd(f ) as before, which are defined by using its canonical representation. We also use the same notations LT (F ) and LM (F ) for a set F of boolean polynomials as before. ¯ a finite subset Definition 17 For an ideal I of a boolean polynomial ring B(X), ¯ G of I is called a boolean Gr¨obner basis of I if hLM (I)i = hLM (G)i in B(X). 5
Using canonical representations of boolean polynomials, we can also define monomial reductions for boolean polynomials as Definition 7 and have the same property of Theorem 8. We can also define a stratified boolean Gr¨obner basis as in Definition 10, which is unique w.r.t. a term order. The boolean closure of a boolean polynomial is also similarly defined as Definition 12 and the same properties of Theorem 11,13,14 and 16 hold. Construction of a boolean Gr¨obner ¯ Combasis is very simple. Given a finite set of boolean polynomials F ⊆ B(X). ¯ w.r.t. pute a Gr¨obner basis G of the ideal hF ∪{X12 −X1 , . . . , Xn2 −Xn }i in B[X] the same term order. Then, G \ {X12 − X1 , . . . , Xn2 − Xn } is a boolean Gr¨obner ¯ If G is stratified, then G \ {X 2 − X1 , . . . , X 2 − Xn } is basis of hF i in B(X). n 1 also stratified. Example 3 The following left constraint with unknown set variables X and Y and an unknown element variable a is equivalent to the right system of equations of a boolean polynomial ring B(X, Y, A), where B is a boolean ring of sets and the variable A stands for the singleton {a}. X ∪ Y ⊆ {1, 2} (XY + X + Y ) + {1, 2}(XY + X + Y ) = 0 1∈X {1}X + {1} = 0 ⇐⇒ a∈Y AY + A = 0 X ∩Y =∅ XY = 0
The stratified boolean Gr¨ obner basis G of the ideal I = h(XY + X + Y ) + {1, 2}(XY + X + Y ), {1}X + {1}, AY + A, XY i w.r.t. a lexicographic term order X > Y > A has the following form: G = {{2}XY, {2}Y A + {2}A, (1 + {2})Y, {2}XA, (1 + {2})X + {1}, (1 + {2})A}. From this we can get the elimination ideal I∩B(A) = h(1 + {2})Ai. By boolean extension theorem, we can see that the given constraint is satisfiable if and only if the element variable a satisfies the equation (1 + {2}){a} = 0 that is a = 2. We conclude this section with the following theorem, which is essentially a special instance of Theorem 2.3 of [10]. Definition 18 Let B be a boolean ring and k be a natural number. Bk denotes a direct product, i.e. the set of all k-tuples of elements of B. For an element p of Bk , pi ∈ B denotes the i-th element of p for each i = 1, . . . , k. If we define p + q and p · q for p, q ∈ Bk by (p + q)i = pi + qi and (p · q)i = pi · qi ¯ for each i = 1, . . . , k, Bk also becomes a boolean ring. For a polynomial f (X) k ¯ ¯ in B [X] fi (i = 1, . . . , k) denotes the polynomial in B[X] obtained by replacing ¯ in Bk (X), ¯ a boolean each coefficient p of f by pi . For a boolean polynomial f (X) ¯ polynomial fi in B(X) is defined similarly. ¯ let G be a finite set of boolean closed Theorem 19 In a polynomial ring Bk [X], polynomials. Then, G is a (reduced) Gr¨ obner basis of an ideal I if and only if Gi = {gi |g ∈ G} \ {0} is a (reduced) Gr¨ obner basis of the ideal Ii = {fi |f ∈ I} ¯ for each i = 1, . . . , k. in B[X] ¯ let G be a finite set of Corollary 20 In a boolean polynomial ring Bk (X), boolean closed boolean polynomials. Then, G is a (reduced) boolean Gr¨ obner 6
basis of an ideal I if and only if Gi = {gi |g ∈ G} \ {0} is a (reduced) Gr¨ obner ¯ for each i = 1, . . . , k. basis of the ideal Ii = {fi |f ∈ I} in B(X)
3
Boolean structure of Sudoku
We first describe how a sudoku puzzle is considered as a system of equations of a boolean polynomial ring. 4 3
9 1
2
5 9
8
5 8 1
6
7 5
7 2
9
figure 3.1 Consider the above sudoku puzzle. We associate a variable Xij for each grid at the i-th row and the j-th column. This puzzle can be considered as a set constraint where each variable should be assigned a singleton set from 9 candidates {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} and {9} so that any distinct two variables which lie on a same row, column or block must be assigned different singleton sets. 17 variables are assigned singleton sets X11 = {4}, X15 = {9}, . . . , X99 = {9} as the initial conditions. This constraint is translated into a system of equations of a boolean polynomial ring B(X11 , X12 , . . . , X99 ) with B = P({1, 2, . . . , 9}) as follows: (1) X11 = {4}, X15 = {9}, . . . , X99 = {9}. (2) Xij Xi′ j ′ = 0(= ∅) for each pair of distinct variables Xij , Xi′ j ′ which lie on a same row, column or block. P (3) (i,j)∈A Xij = 1(= {1, 2, . . . , 9}) where A is a set of indices lying on a same row, column or block. (There are 27 such A’s.) This puzzle is nothing but solving the above equations with a strong restriction that is each variable must be a singleton set. Unless we have this restriction, we can solve the equations by computing a stratified boolean Gr¨obner basis of the corresponding ideal. Actually a stratified boolean Gr¨obner basis G w.r.t. a purely lexicographic term order is a canonical representation of the whole solutions. That is, the equations has a solution in B if and only if G does not contain a non-zero constant of B, in case it has a solution, we can easily obtain all solutions step by step by solving linear equations with one variable. This is an easy consequence of the boolean extension theorem and the boolean weak Nullstellensatz. Unfortunately, computation of a stratified boolean Gr¨obner ba7
sis is not enough because of the restriction. Even if a stratified boolean Gr¨obner basis does not contain a non-zero constant of B, the equations might not have solutions consisting of only singleton sets(say singleton solutions in short). After the computation of a stratified boolean Gr¨obner basis, we have to do something more for getting all singleton solutions. The following lemma is very useful for such a purpose. Before describing the lemma, we need some notations. Definition 21 Let φ be an isomorphism from P({1, 2, . . . , 9}) to (GF2 )9 such that φ({1}) = (1, 0, 0, 0, 0, 0, 0, 0, 0) ,φ({2}) = (0, 1, 0, 0, 0, 0, 0, 0, 0),. . ., φ({9}) = (0, 0, 0, 0, 0, 0, 0, 0, 1). Let φk (k = 1, . . . , 9) be a projection homomorphism from P({1, 2, . . . , 9}) to GF2 induced by this isomorphism, that is φk (S) = 1 if k ∈ S otherwise φk (S) = 0 for each S ⊆ {1, 2, . . . , 9}. We also ¯ to (GF2 )9 (X) ¯ naturally extend φ to an isomorphism from P({1, 2, . . . , 9})(X) ¯ ¯ and φk to an homomorphism from P({1, 2, . . . , 9})(X) to GF2 (X). ¯ Lemma 22 Let I be an ideal of a boolean polynomial ring P({1, 2, . . . , 9})(X) ¯ corresponding to a sudoku puzzle. If I contains a boolean polynomial f (X) such ¯ = Xij + 1, then the value of Xij of any singleton solution of the that φk (f (X)) ¯ such that sudoku puzzle must be {k}. If I contains a boolean polynomial f (X) ¯ φt (f (X)) = Xij for every t except for k, then the value of Xij of any singleton solution of the sudoku puzzle must be {k}. ¯ = φk ({k}f (X)). ¯ proof Since φk ({k}) = 1, φk (f (X)) ¯ ¯ If φk ({k}f (X)) = Xij + 1, {k}f (X) must be equal to {k}Xij + {k} since a ¯ is equal to {k}. Since {k}f (X) ¯ is also coefficient of every monomial of {k}f (X) contained in I, {k}Xij + {k} ∈ I. {k}Xij + {k} = 0 is equivalent to Xij ⊇ ¯ = Xij {t}Xij = 0 which is equivalent to {k}. By a similar reason, if φt (f (X)) {t} ∩ Xij = 0, i.e. t ∈ / Xij . ¤ In the above lemma, Xij + 1 or Xij is actually a minimal polynomial of Xij ¯ respectively. In a which lies on the projection ideal φk (I) or φt (I) of I in GF2 (X) ¯ boolean polynomial ring GF2 (X), a polynomial consisting of only one variable X is either X or X +1. Boolean Gr¨obner bases have a nice property for computing a minimal polynomial of one variable. Theorem 23 Let G be a reduced boolean Gr¨ obner basis of an ideal I in a boolean ¯ w.r.t. any term order. If I is not a trivial ideal h1i polynomial ring GF2 (X) and contains a polynomial consisting of one variable, then G contains this polynomial. proof Suppose a non-trivial ideal I contains X + a(a is 0 or 1). Then ∗ ¯ such X + a →G 0. Therefore G must contain a boolean polynomial X + h(X) ∗ ¯ = X. Since h(X) ¯ − a ∈ I, h(X) ¯ →G a. h(X) ¯ must be equal that LT (X + h(X)) to a because G is reduced. ¤ Let G be a stratified boolean Gr¨obner basis of an ideal I in a boolean polyno¯ Then, φk (G)(k = 1, . . . , 9) is a reduced boolean mial ring P({1, 2, . . . , 9})(X). 8
¯ by CorolGr¨obner basis of an ideal φk (I) in a boolean polynomial ring GF2 (X) ¯ which satisfies lary 20. Hence, we can compute every boolean polynomial f (X) the assumptions of Lemma 22 by using only G. Example 4 For the corresponding ideal I of the sudoku puzzle of figure 3.1, we have f = ({6, 7}+1)X22 +{5}X12 +{5, 9} in the stratified boolean Gr¨ obner basis G of I w.r.t. a purely lexicographic term order X99 > X98 > · · · > X12 > X11 . For this boolean polynomial f , {9}f = {9}X22 + {9}, which means φ9 (f ) = X22 + 1. In general, an element f of a stratified boolean Gr¨obner basis G such that ¯ + {k} where φk (f ) = X + 1 and LT (f ) = X has the form f = aX + h(X) ¯ a{k} = {k} and {k}h(X) = 0. An element g of G such that φt (g) = X and ¯ where a{t} = {t} and {t}h(X) ¯ = 0. LT (g) = X has the form g = aX + h(X) In case f = X + {k}, certainly φk (f ) = X + 1 holds, but the converse does not generally hold as the above example. If t 6= k, g = X + {k} also satisfies φt (g) = X. If X + {k} is not contained in G and there exists an element g of G such that LT (g) = X and φt (g) = X for any t which is different from k, then G contains a boolean polynomial (1 + {k})X(= {1, 2, . . . , k − 1, k + 1, . . . , 9}X) ¯ ∈ I for every t 6= k and G is stratified. because {t}X = {t}(aX + h(X)) A polynomial in a form of X + {k} is called a solution polynomial. We call a ¯ + {k} described above that is not a solution boolean polynomial f = aX + h(X) polynomial an almost solution polynomial and call X +{k} its associated solution polynomial. We also call a boolean polynomial (1 + {k})X an almost solution polynomial with X + {k} as its associated solution polynomial. In either case, X + {k} is denoted by Sol(f ) or Sol((1 + {k})X). Now we are ready to present an algorithm to compute every solutions of a given sudoku puzzle. ¯ are 81 variables X11 , X12 , . . . , X99 . H B is the boolean ring P({1, 2, . . . , 9}), X denotes the set of all boolean polynomials used in the equations of (2) and (3). Algorithm Sudoku1 Input: F a finite subset of boolean polynomials in a form of Xij + {k}, ¯ an term order > of T (X) Output: G a stratified boolean Gr¨obner basis w.r.t. > such that : hF ∪ Hi ⊆ hGi. V (hF ∪ Hi) and V (hGi) include the same singleton solutions. If G does not contain a non-zero constant, then F ⊆ G and G does not contain an almost solution polynomial. begin G := the stratified boolean Gr¨obner basis of hF ∪ Hi w.r.t. >. Do Sudoku1 main(G). end Sudoku1 main(G) case 1 G contains a non-zero constant, terminate. 9
case 2 G does not contain an almost solution polynomial, terminate. otherwise G := the stratified boolean Gr¨obner basis of hG ∪ {Sol(g)|g ∈ S} \ Si where S is the set of all almost solution polynomials in G Sudoku1 main(G). If Sudoku1 main terminates with case 1, the sudoku puzzle given its initial condition by F has no solution. If the output of Sudoku1 consists of only solution polynomials, then it is the unique solution of the the sudoku puzzle. Otherwise, we have to use another type of algorithm. Even if we can not directly determine singleton solutions from the output stratified boolean Gr¨obner basis, it is useful for computing all singleton solutions as far as we use a purely lexicographic term order. We can compute all singleton solutions by solving equations with one variable step by step from the smallest variable to the biggest variable. In order to write a program for such computations, we do not need any knowledge(even the rules) of sudoku. We actually implemented a simple algorithm for this purpose, essential part of which consists of less than 20 lines. We denote this simple algorithm Sudoku2, that is Sudoku2(G) computes all singleton solutions for a given stratified boolean Gr¨obner basis w.r.t. a purely lexicographic term order. Before describing our computation experiments in the next section, we categorize sudoku puzzles into four classes. Class 1. The stratified boolean Gr¨obner basis computed by Sudoku1 before calling Sudoku1 main contains a non-zero element, the translated equation does not have a solution even in B. Class 2. The translated equation has a solution in B,but not have a singleton solution. It is checked by case 1 of Sudoku1 main or Sudoku2. Class 3. Sudoku1 computes the unique singleton solution. Class 4. The singleton solutions are computed by Sudoku2. In the next section, it will be shown that there is a close relationship between this classification and the levels of difficulty of sudoku puzzles.
4
Data of computation experiment
For the computation of boolean Gr¨obner bases, we used a recent implementation ([1]) on the computer algebra system Risa/Asir([3]). As is reported in [9, 5], in a computation of a boolean Gr¨obner basis, a purely lexicographic term order is faster than the most other term orders such as a degree reverse lexicographic term order. In our computations we used only purely lexicographic term orders. We solved more than 300 sudoku puzzles which range from a kids level to an extreme level of difficulty. Computation time or consumed memory space of the first boolean Gr¨obner basis computation in Sudoku1 do not have any influence
10
from the levels of difficulty. The number of the initial conditions drastically affects both computing time and consumed memory space. Our computation experiments are done by a laptop computer(Cpu IntelCore2Duo 2.8GHZ with Memory 2GB) running on Linux(Ubuntu 8.10). When we have more than 20 initial conditions, computation time is less than 20 seconds and consumed memory space is less than 70MB in every case. When we have 18 to 20 initial conditions, computation time ranges from 15 to 40 seconds and consumed memory space is less than 70MB in every case. When we have 17 initial conditions, initial conditions are divided into two types according to whether they include every numbers. The example Kids1 in the appendix A contains all numbers whereas the example Kids2 does not contain the number 3. In the latter cases, which do not happen for any puzzle with more than 17 initial conditions in our experiment, computation time ranges from 70 to 135 seconds and consumed memory space is less than 80MB in every case. In the former case, computation time is less than 50 seconds and consumed memory space is less than 70MB in every case. In all examples, computation of Sudoku1 main is done at most within 10 seconds,Sudoku2(if necessary) is less than 1 second. For an ultimate case, i.e. when we have no initial conditions, we also computed a boolean Gr¨obner basis. Its computation time is 723second and consumed memory space is about 1GB, although we can divide it by 9 with a parallel computation of boolean Gr¨obner bases ([1]). Though our purpose is not making a fast sudoku solver, our program is satisfactorily practical. One of its merits is that we can handle many sudoku variants by the same program. For example, in order to solve diagonal sudoku, what we have to do is simply adding suitable polynomials to H of Sudoku1. Every diagonal sudoku puzzles which we have tried can be also solved by our program at most within a minute. Most important phenomenon we have found through our experiment is that there seems to exist close relationship between our classification of sudoku puzzles and their levels of difficulty. The following table is a data of 24 interesting examples of our computations. They are given in appendix A and B. They are released as free puzzles at some web site. Every puzzle has 17 initial conditions. The puzzles in A are ranked as the lowest level of difficulty, the puzzles in B are ranked as the highest level of difficulty, there are 6 levels of difficulty there. In the table, the sequence of numbers is the number of detected almost solution polynomials obtained at each recursive call of Sudoku1 main, the next number is the number of undetermined variables after Sudoku1. The last number is the computation time of the first boolean Gr¨obner basis, which is not much different from the entire time for solving the puzzle as mentioned above. There are no difference for computation time between A and B, however, all puzzles in A are in class 3 whereas 8(of 12) puzzles in B are in class 4. Of course, we can not say every easy level sudoku puzzle should be in class 3. Consider Extreme12 for example, after Sudoku1 we have a sudoku puzzle with 59 initial conditions which still lies on class 3, however it should be ranked easy level.
11
puzzle Kids1 Kids2 Kids3 Kids4 Kids5 Kids6 Kids7 Kids8 Kids9 Kids10 Kids11 Kids12 Extreme1 Extreme2 Extreme3 Extreme4 Extreme5 Extreme6 Extreme7 Extreme8 Extreme9 Extreme10 Extreme11 Extreme12
5
almost solution polynomials 10 6 9 4 5 6 2 2 2 266887954 885442222324 12 2 5 5 3 4 5 4 2 3 10 8 10 3 6 2 2 3 3 848621342223 7 11 8 10 3 2 3 2 759623535 776692422 12 9 5 4 6 6 7 12 8 11 5 3 8 867457553 12 10 6 4 1 85651424565 12 4 8 4 1 1 83112 7484645233 10 6 9 5 10 5 777223 9 5 7 6 6 5 2 10 6963 10 5 6 3 8 10 4 3 1 7361325452
unsolved grids 0 0 0 0 0 0 0 0 0 0 0 0 30 0 32 49 0 0 34 0 40 40 36 22
time(sec) 43.9 93.4 133.3 48.7 46.8 78.3 45.4 44.5 49.2 81.5 115.9 42.5 41.7 135.5 48.1 83.9 44.3 76.2 43.6 80.4 40.1 85.8 92.6 48.9
Conclusion and Remarks
We showed sudoku puzzles are divided into four classes according to the structure of their corresponding ideals. This classification is algorithm independent, therefore we can apply any algorithm to solve boolean equations such as in [2, 4]. The most important merit of using boolean Gr¨obner bases is Theorem 23. This theorem enables us both to construct a triangular system of equations and to compute all almost solution polynomials simultaneously. Our method is also applicable for 16x16 sudoku puzzles, however, the first boolean G¨obner basis computation does not terminate unless we have many initial conditions. What we actually need in Sudoku1 is computation of the elimination ideal for each variable, therefore the approach discussed in [5] seems effective for 16x16 sudoku puzzles. Our method is also applicable for any set constraint, where we want to have singleton solutions for some variables which may not be all variables. Order of variables dramatically affects the computations of Gr¨obner bases, especially when we use a purely lexicographic term order. The timing data shown in this paper is given by using only the purely lexicographic term order such that X99 > X98 > · · · > X12 > X11 . Since our method is quite effective as 12
is shown in Section 4, we have not used other term order. But, we surely can make computation time shorter by using a proper order. We have to study how to detect a proper order for more complicated problems such as 16x16 sudoku puzzles.
References [1] Inoue, Shutaro. (2009). On the computations of Boolean Gr¨obner bases and Comprehensive Boolean Gr¨obner bases. in preparation. [2] Menju, S., Sakai, K., Sato,Y. and Aiba,A. (1993). A Study on Boolean Constraint Solvers. Constraint Logic Programming Selected Research The MIT Press, pp253–267. [3] Noro, M. and Takeshima, T. (1992). Risa/Asir – A Computer Algebra System. International Symposium on Symbolic and Algebraic Computation (ISSAC 92), Proceedings, 387–396. [4] Rudeanu, S. Boolean functions and equations. North-Holland Publishing Co., Amsterdam-London; American Elsevier Publishing Co., Inc., New York, 1974. [5] Sato, Y. Nagai, A. and Inoue, S. On the Computation of Elimination Ideals of Boolean Polynomial Rings, LNCS 5081, pp 338-348, Springer-Verlag Berlin Heidelberg 2008. [6] Sakai, K. and Sato, Y. (1988). Boolean Gr¨obner bases. ICOT Technical Momorandum 488. http://www.icot.or.jp/ARCHIVE/Museum/TRTM/tm-list-E.html [7] Sakai, K., Sato, Y. and Menju, S. (1991). Boolean Gr¨obner bases(revised). ICOT Technical Report 613. http://www.icot.or.jp/ARCHIVE/Museum/TRTM/tr-list-E.html [8] Sato, Y. A new type of canonical Gr¨obner bases in polynomial rings over Von Neumann regular rings. Proceedings of ISSAC 1998, ACM Press, pp 317-32, 1998. [9] Sato, Y. et al.(1998). Set Constrains Solvers(Klic version). http://www.icot.or.jp/ARCHIVE/Museum/FUNDING/funding-98-E.html [10] Weispfenning, V. (1989). Gr¨obner bases in polynomial ideals over commutative regular rings. In Davenport Ed., editor, EUROCAL’87, pp 336-347. Springer LNCS 378, 1989.
13
A
Kids Sudoku Puzzles 3
2
9
1
8
5
6
6 9
6
3
7
1
2 2 8 7 5
5
7 4
9
1
6
5 5 3
5
4 8
2 4
2 9
4 6 Kids2
Kids1 4
5
6 Kids3 5
6 1
7 9
2 8 6
1 9 8
6 3
6 5
9
2
7 1
8 1
9 8 1
7 3 9
4
2
7 9 3 8 9
9
7
2 1
4
4 6
3
8
2 5
1 5 5
6 2
3 9 4
8 Kids4
8
3 Kids5 8 9 9 5 8
9 6 3
8 1
6 2
6
2 4
7 7 4 2
6 1 9 4 7 3 5 1 6 4 Kids7
4
3 5 8 5
7
2 9 4 1
7
3 4 9
3
9 3 7
7 1 4 6 Kids10
7 2 3
3 6
9 7
4
8 5
8
2 6 4 Kids9
Kids8 6
1
8 7 7 4
7
5
4 9
9 3 1
8
1 8
7 3 5 1 Kids6
6
8
2
8
8 4 1 5
6
6 2 5
4 5 8 6 8 7
9 7 8 Kids11
14
4
1 Kids12
5
B
Extreme Sudoku Puzzles 3 7 5
6
1 6 9
2 9
6 8 4
1
9
3
2 9 5 4
8
4 9 8 6
9
7
7 2 5
1
6 8
5 3
9 4
5 8 7
9 6
2
2 1 Extreme2
Extreme1 5
7
2
4
6
4
3
4 7 8 6
3
6
2 8
1 2 3
9
7 4 5 1
2
7 3 4 9
2
3
8
5
1 7 3 5
8
4 8
3
4
5 2 Extreme7
2
7
4 6
8
6 1 9
1
1
4 9
3 2 7 Extreme9
6 7
5
5
7
2
1 6 5
6 7
4
2
4
2 4
7 2 7
7 6 4 9
6
3
3 1 2 4 5 Extreme10
5
4
7 9 Extreme8 3 4 6 1 2
1 8 3
6 8
8 9
9
5 3 7 9
2
2 7
Extreme6
1 6
4 9 1 5
6 7 8
8 5
8 6 Extreme5
Extreme4
9
9 5 9 4
8 3
6 1 2 8
2
1
7
8 5 9 3
7 3
5
8
2 Extreme3
1
3
8 9 1 6 4
5
9 Extreme11
15
7 9 1 Extreme12