Научная статья на тему 'Resolution of sudokus using Groebner basis'

Resolution of sudokus using Groebner basis Текст научной статьи по специальности «Математика»

CC BY
231
76
i Надоели баннеры? Вы всегда можете отключить рекламу.
Ключевые слова
СУДОКУ / ОБОБЩЁННОЕ СУДОКУ / БАЗИС ГРЁБНЕРА / АЛГЕБРАИЧЕСКОЕ МНОГООБРАЗИЕ / SUDOKU / GENERALIZED SUDOKU / GRöBNER BASE / ALGEBRAIC MANIFOLD

Аннотация научной статьи по математике, автор научной работы — Gonzalez-Dorrego Maria Rosario

We study the resolution of sudokus and generalized sudokus using Groebner basis. Let x1,..., x81 the 81 squares which form the sudoku, arranged from left to right and from top to bottom. Its solution will be (a1,..., a81), where ai is the number in the square associated to the variable xi. Let S be a sudoku with preassigned data {ci}i in L, for L subset {1,..., 81}. All the necessary information to solve the sudoku is contained in the algebraic set V (I+ <{xi-ci}i in L>). We shall use Groebner basis to find a solution and give a SAGE code for that purpose.

i Надоели баннеры? Вы всегда можете отключить рекламу.
iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.
i Надоели баннеры? Вы всегда можете отключить рекламу.

Решение судоку помощью базисов Гребнерa

Мы изучаем решение судоку и обобщенного судоку, используя технику базисов Грёбнера. Пусть x1,..., x81 переменные, связанные с 81 квадратами, которые образует головоломку судоку и линейно упорядочены сначала по строкам, затем по столбцам. Решение судоку есть набор чисел (a1,..., a81), где ai число в квадрате, ассоциированном с переменной xi. Пусть также S судоку с предварительно заполненными данными {ci}i принадлежит L для L включено в {1,..., 81}. Вся необходимая информация для решения такого судоку содержится в алгебраическом множестве V (I+< {xi-ci}i принадлежит L>). Мы используем технику базисов Грёбнера для поиска такого решения и приводим соответсвующий код в~системе компьютерной алгебры SAGE для программы, решающей эту задачу.

Текст научной работы на тему «Resolution of sudokus using Groebner basis»

Computer tools in education, 2018 № 3: 5-21

http://ipo.spb.ru/journal doi:10.32603/2071-2340-3-5-21

RESOLUTION OF SUDOKUS USING GROEBNER BASIS

We study the resolution of sudokus and generalized sudokus using Groebner basis. Let x1,...,x81 the 81 squares which form the sudoku, arranged from left to right and from top to bottom. Its solution will be (ai,...,a81), where ai is the number in the square associated to the variable x^.

Let S be a sudoku with preassigned data [ci};eL, for L c {1,___,81}. All the necessary

information to solve the sudoku is contained in the algebraic set V(I + < [xi - ci}ieL >). We shall use Groebner basis to find a solution and give a SAGE code for that purpose.

Keywords: Sudoku, generalized sudoku, Grobner base, algebraic manifold.

Citation: M. R. Gonzalez-Dorrego, "Resolution of Sudokus Using Groebner Basis," Computer tools in education, no. 3, pp. 5-21, 2018.

A sudoku puzzle is a 9 x 9 grid divided into nine 3 x 3 boxes where there are numbers between 1 and 9 in some of the squares in the grid. To solve the puzzle we have to find the remaining numbers in such a way that every row and every column only contain the digits 1 to 9 and also every 3 x 3 box only contains those digits, without repetitions.The puzzle derives its name from the japanese words Su, which means number and Doku, which means solitary. This puzzle was popular in Japan since 1986 and it was in 2005 that it became internationally known. This mathematical game had it origin in New York at the end of 1970 where it was known as 'Number Place'. It was published in a magazine called: "Math Puzzles and Logic Problems".

A sudoku is a particular case of what is called a Euler square which is an n xn grid such that each row and column of this grid must be filled with the n distinct numbers without repetitions.

A magic square is a 3 x 3 grid such that the sum of every row, column and diagonal equals 15. One can discuss the maximum number of magic squares that can appear in a unique solution sudoku; for a discussion on this topic see [5].

The resolution of the sudoku can be seen as a problem of coloring the vertices of a plane graph with 81 vertices such that two of them are adjacent if they belong to the same row, column or 3 x 3 block. If we assign a different color to each of the 9 digits, vertices with the same color cannot be adjacent which means that a digit cannot be twice in each row or each column or each 3 x 3 block.

Let x1,...,x81 the 81 squares which form the sudoku, arranged from left to right and from top to bottom. Its solution will be (a1,..., a81), where at is the number in thesquare associated to the variable x{. Let S be a sudoku with preassigned data [c{}ieL,

Gonzalez-Dorrego M. R. Universidad Autónoma de Madrid, Madrid, SPAIN

Abstract

1. INTRODUCTION

for L c {1,..., 81}. All the necessary information to solve the sudoku is contained in the algebraic set V(I + < {xi - Ci}ieL >). We also consider generalized sudokus. We shall use Groebner basis to find a solution. Some of the results are based on the Masters' Thesis of A. Delgado Latournerie [1].

The minimum number of preassigned data C[ for a unique solution sudoku is 17. This result was proven by McGuire-Tugemann-Civario [3] using computer software. For an interesting reading on the subject, see [4].

The rate of difficulty of a sudoku is ranked using stars. One-star sudokus are very easy to solve while a 5-star sudoku is very hard to solve. There is a famous sudoku, created by the Finnish mathematician Arto Inkala in 2012, whose difficulty is ranked with 11 stars and it is called Everest sudoku. We are not going to discuss here the definition of difficulty for a sudoku.

First we introduce some basic concepts on Algebraic Geometry and on Groebner basis. In 3 we solve sudokus using Groebner basis. In 4 we give SAGE codes for its resolution. In 5, 6 and 7 we study generalizations of sudoku puzzles.

2. BASIC CONCEPTS

Definition 1. Let R be a commutative ring. Let I c R be an ideal of R.

• Let I + R. I is a prime ideal of R if whenever ab e I, either a e I or b e I.

• Let J be an ideal of R such that I c J. I is a maximal ideal if I = J or J = R.

• The radical of I is the ideal y/I = {a e R : an e I, for some n e N}.

• I is a radical ideal if I = \[I.

Definition 2. Let k [xi,..., xn ] be a polynomial ring with coefficients in the field k. Let Akn denote the affine space of dimension n over k. For J c k [xi,..., xn ] we define

V(J) = {P e Akn : f (P) = 0, Vf e J}.

V(J) is called affine algebraic set.

Definition 3. Let S c Akn. We define

D(S) = {f e k[xi,..., xn]: f (P) = 0, VP e S}.

Properties

• V(0) = Akn, V(k [xi,..., xn ]) = 0.

• D(0) = k[xi,...,xn], l(Akn) = 0.

• For Ji c J2, V(J2) c V(Ji).

• For Si c S2, D(S2) c D(Si).

• Let {Sj}, j e J, be a collection of subsets of Akn. £ jej D(Sj) = D(n jejSj).

• Let {Ji}, i e I, be a collection of subsets of k [xi,..., xn]. n ;eIV(J;) = V£ ieiJi).

Definition 4. We define the Zariski topology in Akn the topology whose closed sets are affine algebraic sets.

Definition 5. An affine algebraic set S c Akn is irreducible if, whenever S = Si u S2, for Si c Akn, i < i < 2, S = Si or S = S2. An irreducible affine algebraic set is called an algebraic variety.

Theorem 6. Hilbert's Nullstellensatz Let k be an algebraically closed field. Let A be an ideal in

k[xi,..., xn]. D(V(A)) = vA.

Proof. See [2;Th. 13A]. □

Proposition 7. Let k be an algebraically closed field. There is a one-to-one correspondence between the ideals of k [xi,..., xn ] and the sets of Akn such that radical ideals of k[x1(...,xn] correspond to affine algebraic sets; the prime ideals of k[xi,...,xn] correspond to algebraic varieties and the maximal ideals of k[xi,..., xn] correspond to the points of Akn.

Proof. See [2; 1.4,1.4.4]. □

Proposition 8. Let I c k [xi,..., xn ] be an ideal such that V(I) is finite. Then,

• (a) |V(I) |< dimk(k[xi"f'xn]),

• (b) Let k be an algebraically closed field. If I is a radical ideal, |V(I)| = dimk(k[xiyxn]); that is, the number of points of | V (I) |, counted with its multiplicity, is exactly dimk (k[xi"j"x" ]).

Proof. See [1; Prop. 5]. □

Definition 9. A monomial in the variables xi,..., xn is a product of the form xa _ xiai,..., xn an, with a _ (ai,..., an) e Z>0n. An ideal I c k [xi,..., xn ] is a monomial ideal if it admits a system ofgenerators which are monomials.

Definition 10. Let f _ £a aaxa a nonzero polynomial in k[xi,...,xn]. Let > be a monomial ordering.

• The multidegree of f is multideg(f) _ max[a e Z>0n : aa + 0}.

• The leading coefficient of f is LC(f) _ amultideg( f).

• The leading monomial of f is LM(f) _ xmultideg(f) with coefficient 1.

• The leading term of f is LT(f) = amultideg(f) xmultideg(f)

Definition 11. Let I c k [xi,..., xn ] be a nonzero ideal. LT (I) denotes the set of the leading terms of the elements of I.

Definition 12. Let us fix a monomial ordering. A finite subset G _ {g^..., gt} of an ideal I c k [xi,..., xn ] is a Groebner basis if < LT (gi),..., LT (gt) >=< LT (I) >, where LT denotes the leading term as defined in Definition 10.

Definition 13. Let a = (ai,...,an) and ¡3 = (pi,...,¡5n) e Z>0n. We say that a >lex ¡3 and, thus, that xa >lex x3, if the first nonzero term of the vector a - 3 is posittive.

Definition 14. A Groebner basis G is reduced, for a polynomial ideal I, if

• LC(f) = i, Vf e G.

• For every f e G, no monomial of f is in < LT(G - {f}) > .

Lemma 15. Let m, n e N, aij e N. For each j, i < j < m, let [xij - aij}n= i, be a reduced Groebner basis of the ideal Ij c k[[x{j}n=i]. Then um=i({xij - aij}n=i), aij e N, i < i < n, i < j < m, is a reduced Groebner basis of the idealY. 'Jl=i Ij c k [{{xij }1l=i}rn_i].

Proof. Let m, n e N, a{j e N. By hypothesis, for each j, 1 < j < m, let {x{j - a^j}n= r a^j e N, be a reduced Groebner basis of the ideal Ij ; thus it satisfies the conditions of Definition 14. Since all the monomials are linear, um=1{{xij - aijin=1), 1 < i < n, 1 < j < m, is a reduced Groebner basis of the ideal £m=1 Ij c k [{{xj\ni=l]m=l\. □

Example 16. See [1; Ej. 12]. Let k = C.

Let I =< x2 + y + z - 1,x + y2 + z - 1,x + y + z2 -1 > .

A Groebner basis for I with respect to the lexicographical order can be calculated using the mathematical software SAGE using the command .grobner- basis()

R. < x, y, z >= PolynomialRing(CC, 3, order =' lex') I = ideal(x2 + y + z - 1, x + y2 + z - 1, x + y + z2 -1)

We get

I.grobner- basis ()

{z2 + y + x -1, y2 - y - z2 + z,2yz2 + z4 - z2, z 6 -4z4+4z 3-z 2}

(1)

If we just want a Groebner basis obtained from the Buchberger we should use

I.grobner-basis(toy: buchberger')

Thus, we obtain

2 2 2 2 2 {x2 + y + z -1, x + y2 + z -1, x + y + z2 - 1, y2 - y - z2 + z,

-yz4 - yz2 - 2z4 + 2z3, -2yz2 - z4 + z2, z6 - 4z4 + 4z3 - z2} (1) and (2) generate the same ideal I.

Definition 17. Let f, g e k[xi,...,xn] be two nonzero polynomials. Let multideg(f) multideg(g) = ¡3. Let ji = max(ai,¡3i), 1 < i < n.

• The monomial xr is the least common multiple of LM(f) and LM(g).

• The S-polynomial of f and g is

(2)

= a,

S( f, g ) =

:f -

LT ( f)J LT (g)

g •

Proposition 18. Let I c k[xi,..., xn] be a nonzero ideal. There exists a Groebner basis of I with respect to every monomial order. Moreover, any Groebner basis of I is a system of generators of I.

Proof. See [1; Cor. 2]. □

3. RESOLUTION OF SUDOKUS USING GROEBNER BASIS

Let us consider the sudoku grid

Y

Y

x

x

Let us denote by x1,..., x81 the 81 squares which form the sudoku, arranged from left to right and from top to bottom. Its solution will be (a1,..., a81), where at is the number in the square associated to the variable xi. Let us consider the polynomial ring C[x1,..., x81].

Definition 19. • (a) For 1 < i < 81, let Ff (xi) = n9k=1(xi - k).

• (b) For 1 < i < j < 81, let

Fi (xi) - Fj (x;) Gij(xi,x;) =-_ e Q[x;,x;], with i + j.

xi - x j

Remark20. Notethat Fi (xi )-Fj (xj) isOin V(x;-xj); thus (xi-xj) isafactorofFi (xi )-Fj (xj) for i + j. Moreover Gij (xi, xj) is not divisible by (xi - xj), since, for xi = xj, Gij (xi, xi) = Fj (xi) which is not 0 since Fi is not constant. Also, if ai, aj are such that Gij (ai, aj) = 0 and Fi(ai) = 0 = Fj (aj) we would have that ai + aj, for, otherwise, Gij (ai, ai) = F'f(ai) + 0 because there would be a summand of Fj which would not be 0 in ai e {1,. ..,9}.

Notation 21. Let E = {(i, j) e T}

(i, j) e T when 1 < i < j < 81 and the ith andjth cells belong to the same row, column or 3 x 3 block.

iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.

We consider the ideal I generated by the polynomials Fi, 1 < i < 81, and Gij, (i, j) e E. Let S be a sudoku with preassigned data {c{}ieL, for L c {1,.. .,81}.

Remark 22. The ideal I + < {xi - Ci}ieL > is the ideal generated by Iu < {xi - Ci}ieL > and

V(I + < {xi - Ci},• eL >) = V(I) f~l V(< {xi - Ci},• eL >). Proposition 23. The following statements are equivalent:

• (1) Let L c {1,.. .,81}. We have a = (a1,..., a81) e V(I + < {xi - Ci}ieL >, where Ci are certain constants.

• (2) ai e {1,...,9}, for i e {1,...,81}, with ai + aj, for (i, j) e E, and ai = Ci, for all i e L. Proof. See [1; Prop. 4].

(1) (2) Let a e V(I + < {x;- - Ct}teL >). Then Ft(ai) = 0, Vi e {1,...,81} and ai = Cj, for all i e L. Let us prove that ai + aj, for (i, j) e E. Let us assume that there is a pair (k, l) e E such that ak = b = at. Since Fk(xk) = Ft(xt) + (xk -xt)GW(xk,xt), we obtain Fk(xk) = (xk - b)Gkl(xk, b). If Gki (b, b) = 0, we would obtain that b is a zero of Fk of multiplicity at least 2 which is impossible.

(2) (1) If for all i e {1,..., 81} ai satisfies the first condition of (2) and also ai = Ci, for all i e L, then every Fi, i e {1,...,81}, would be 0 on them and xi - Ci = 0, for all i e L. If also ai, i e {1,.. .,81}, satisfies the second condition of (2), then Gij would be 0 on a = (a1,..., a81). □

Remark 24. Let S be a sudoku with preassigned data {Ci}ieL, for L c {1,...,81}. Let IS = I + < {xi - Ci}ieL > be the ideal associated to the sudoku S. Since IS c C[x1,...,x81]. We have | V(IS) | < 981 < ro and IS is a radical ideal. By Proposition 8,

C[x1,..., x81] |V(Is )|= dime( 1' 81 ).

IS

We want to find out when the solution is unique. Proposition 25. The following statements are equivalent:

• (1) a = (ai,..., a8i) is the unique solution of the sudoku S.

• (2) Is =< {xi - ai,...,xsi - asil > .

• (3) {xi - ai,.x8i - a8i} is a reduced Groebner basis of IS. Proof. See [1; Prop. 6].

(1) (2) Since a is a solution a e V(IS). Since a is the unique solution a = V(IS) because if there would exist b e V(IS), such that b + a, b would also be a solution of S which would contradict the unicity of a.

By Theorem 6, \/TS = l(V(IS)). Thus, \/TS = 1(a) =< {xi - ai,...,x8i - a8i} > . In particular, for i e {i,___,8i}, there exists mi e N such that (x; - ai)m' e IS. Therefore, IS n k[x;] =

< (x; - ai )t >, for some t e N. But t = i since the polynomials Fi (xi ) are free of squares. Thus, Is =< {xi - ai,...,xsi - asi} > .

(2) (3) We have to see that {xi - ai,...,x8i - a8i} is a reduced Groebner basis. Let G := {xi - ai,...,x8i - a8i}. It is easy to see that G is a Groebner basis since, for each pair (i, j), with i + j, the remainder of the division of S(x; - ai, xj - aj) by G is 0. Moreover, for each i e {i,___, 8i}, LC (xi - ai ) = i and no monomial of xi - ai belongs to < LT (G - {x; - ai}) >,

< LT (G - {x; - ai}) >=< {xj ,i < j < 8i, i + j} >. Thus, {xi - ai,..., x8i - a8i} is a reduced Groebner basis of IS.

(3) (2) Since {xi - ai,...,x8i - a8i} is a reduced Groebner basis of IS, by Proposition 18, Is =< {xi - ai,...,x8i - a8i} > .

(2) (i) By Proposition 23 we know that the solutions of a sudoku are the zeroes of the ideal IS. Thus, if IS =< {xi - ai,...,x8i - a8i} >, then (ai,..., a8i) is the unique solution. □

4. SAGE CODES TO SOLVE A SUDOKU

See also [1, pp. 54-56].

(1) Let us construct E, the polynomials Fi and Gij and the ideal I generated by them.

# the set E:

R _ Integers (3) E = []

for j in[i,.,9] : forkin[i,.,9] :

i _ (j - i) * 9 + k a _ R (j) if a __ 0: a _ 3 b _ R(k) if b __ 0: b _ 3

# Pairs (i, j) such that xi and xj belong to the same row:

for lin[k + i,., 9] : E ■ append((i, i + l - k)

# Pairs (i, j) such that Xi and xj belong to the same column:

for l in[ j + 1,9] : E• append((i,(l -1) * 9 + k))

# Pairs (i, j) such that X[ and Xj belong to the same 3 x 3 block:

for min[a, .,3] : for nin[1, .,3] : if i < (( j - 1) + (3 - m)) * 9 + k - b + n : E • append((i, ((j -1) + (3 - m)) * 9 + k - b + n) print(E)

# Polynomials Fi : F = []

variable = [0]

P(x) = prod([(x - j)for j in [1,.,9]])

for j in[1,.,81] : variable.append(var('x' + str(j))) printP (variable[j])

# Polynomials Gij : var('y')

G(x,y) = (P(x) - P(y))/(x- y)

TheGij = [G(variable[a], variable[b]) for a, b in E]

(2) Now, we are going to construct the ideal IS, generated by the polynomials Fi,

i e {1,___,81}, and Gij, (i, j) e E, and by the polynomials corresponding to the preassigned

values to the sudoku. In a matrix 9 x 9, the values not preassigned will be written as 0.

# Polynomials corresponding to the preassigned values:

def Preassigned(matrix) polynomial = [ ] for i in[0,.,8] : for j in[0,.,8] : if matrix[i,j] ! = 0 k = i * 9 + ( j + 1) p = variable[k] - matrix[i, j] polynomial.append(p) return(polynomial)

T = PolynomialRing(QQ; ['x' + str(j) for j in [1,.,81]]) M = matrix(QQ; matrix[i, j] for i,j in [1,.,9]) L = Preassigned(M)

TheGij = [G(variable[a]; variable[b]) for a, b in E]

I = T.ideal(L + TheGij)

show(I • groebner-basis())

U = I • groebner -basis ()

N1=U[54:56]

N2=U[63:65]

N3=U[72:74]

N=N1+N2+N3

x='x' + str(j) for j in [1,.,81]

a='a' + str(j) for j in [1,.,81]

def g(x-a)=a

show g(x-a) for x-a in N

Let us consider the following sudoku grid S

6 4

9 8 7

8 2 3

8 4

6 5

1 5 7

4 1 6 9

1 4

2 7 3 8

Let us calculate the Groebner basis of the ideal IS R = PolynomialRing(QQ; ['x' + str(j) for j in [1,..,81]])

M = matrix(QQ; [[0, 0, 6, 0, 4, 0, 0, 0, 0], [0, 9, 0, 8, 0, 7, 0, 0, 0],

[0, 8, 2, 0, 0, 0, 3, 0, 0], [0, 0, 0, 0, 0, 0, 8, 4, 0], [0, 0, 0, 0, 0, 0, 0, 6, 5]

[1, 0, 5, 0, 0, 0, 0, 0, 7], [4, 0, 0,1, 6, 0, 0, 9, 3], [0, 0,1, 4, 0, 0, 0, 0, 0],

[0, 2, 0, 7, 0, 3, 0, 0, 8]])

L = Preassigned(M)

TheGij = [G(variable[a]; variable[b]) for a; b in E]

I = R:ideal(L + TheGij) Once L added, the polynomials F are redundant. show(I • groebner -basis())

[xi - 3; x2 - i; x3 - 6; x4 - 2 ; x5 - 4; x6 - 5; x7 - 7; x8 - 8; x9 - 9; x10- 5

xii - 9; xi2 - 4; xi3 - 8; X14 - 3; x15 - 7; x16 - 6; x17- 2; x18- 1; x19- 7;

X20 - 8; X21 - 2; x22 - 9; x23 -1; x24 - 6; x25 - 3; x26 - 5; x27 - 4; x28 - 9;

X29 - 6; x30 - 3; X31 - 5; x32 - 7; x33 -1; x34 - 8; x35 - 4; x36 - 2; x37 - 2;

X38 - 7; x39 - 8; x40 - 3; X41 - 9; x42 - 4; x43 -1; x44 - 6; x45 - 5; x46 - 1;

X47 - 4; x48 - 5; x49 - 6; x50 - 8; x51 - 2; x52 - 9; x53 - 3; x54 - 7; x55 - 4;

X56 - 5; x57 - 7; x58 -1; x59 - 6; x60 - 8; x61 - 2; x62 - 9; x63 - 3; x64 - 8;

X65 - 3; x66 -1; x67 - 4; x68 - 2; x69 - 9; x70 - 5; x71 - 7; x72 - 6;

x73 - 6; x74 - 2; x75 - 9; x76 - 7; x77 - 5; x78 - 3; x7g - 4;

X80 - 1; X81 - 8]

The unique solution is:

(3,1, 6, 2, 4, 5, 7, 8, 9, 5, 9, 4, 8, 3, 7, 6, 2,1, 7, 8, 2, 9,1, 6, 3, 5, 4, 9, 6, 3, 5, 7, 1, 8, 4, 2, 2, 7, 8, 3, 9, 4, 1, 6, 5, 1, 4, 5,

6, 8, 2, 9, 3, 7, 4, 5, 7, 1, 6, 8, 2, 9, 3, 8, 3, 1, 4, 2, 9, 5, 7, 6, 6, 2, 9, 7, 5, 3, 4,1, 8)

Notice that the solution is unique as it is known, by Proposition 8, (b), that

C[x1,..., x81] |V(Is )| = dimc( 1 ' 81 ) = 1.

Is

If we solve it using the command "Sudoku( )" of SAGE we obtain sudoku(M) S = Sudoku(M)

print('The number of solutions of this sudoku is:')

len(list(S.dlx())) # It computes the number of solutions of this sudoku.

3 1 6 2 4 5 7 8 9

5 9 4 8 3 7 6 2 1

7 8 2 9 1 6 3 5 4

9 6 3 5 7 1 8 4 2

iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.

2 7 8 3 9 4 1 6 5

1 4 5 6 8 2 9 3 7

4 5 7 1 6 8 2 9 3

8 3 1 4 2 9 5 7 6

6 2 9 7 5 3 4 1 8

5. GENERALIZATIONS OF SUDOKU PUZZLES

Definition 26. We call a generalized sudoku an (mn) x (mn) grid, m, n e N, m, n > 2, divided into m x n boxes where there are numbers between 1 and mn in some of the squares in the grid. To solve the puzzle we have to find the remaining numbers in such a way that every row and every column only contain the digits 1 to mn and also every m xn box only contains those digits, without repetitions

Remark 27. • When m = n = 2 the generalized sudoku is called Shidoku (see VII).

• When m = 2 and n = 3 the generalized sudoku is called Roku sudoku.

• When m = 2 and n = 4 the generalized sudoku is called Hachi sudoku.

• When m = 3 and n = 4 there is a type of generalized sudoku which uses the digits from 1 to 9 and the letters A, B and C instead of the digits 10, 11 and 12. This type is called Juuni sudoku.

• When m = n = 4 there is a type ofgeneralized sudoku which uses the digits from 1 to 9 and the letters A, B, C, D, E, F and G instead of the digits 10,11,12,13,14,15 and 16. This type is called Supersudoku.

Let r = (mn)2. Let x1,..., xr the r squares which form the sudoku, arranged from left to right and from top to bottom. Its solution will be (a^...,ar), where ai is the number in the square associated to the variable Xi. Let S be a generalized sudoku with preassigned data {c;};eL, for L c {1,..., r}. All the necessary information to solve the sudoku is contained in the algebraic set

V(I + < {X; - C{};eL >).

Definition28. • (a) For 1 < i < r, let F{(x;) = n^x - k), k e {1,...,mn}, i e {1,..., r}.

• (b) For 1 < i < j < r, let

Fi (x;) - Fj (x;) Gij(xt,x;) =-_ e Q[x;,x;],with i + j.

xi - x j

Remark29. Notethat Fi (xi )-Fj (x; ) is0in V(x;-x; ); thus (xi-x; ) is a factor of Fi (xi )-Fj (x; ) for i + j. Moreover Gij (xi, xj ) is not divisible by (xi - xj ), since, for xi = xj, Gij (xi, xi ) = Fj (x; ) which is not 0 since Fi is not constant. Also, if ai, aj are such that Gij (ai, aj ) = 0 and Fi (ai ) = 0 = Fj (aj ) we would have that ai + aj, for, otherwise, Gij (ai, ai ) = Fj(a; ) + 0 because there would be a summand of Fj which would not be0in ai e {1,..., mn}.

Notation 30. Let r = (mn)2. Let E = {(i, j) e T}.

(i, j) e T when 1 < i < j < r and the ith andjth cells belong to the same row, column or m x n block.

We consider the ideal I generated by the polynomials Fi, 1 < i < r, and Gij, (i, j) e E. Let S be a sudoku with preassigned data {c; }i eL, for L c {1,..., r}, r = (mn )2.

Remark 31. The ideal I + < {x; - Ci};eL > is the ideal generated by Iu < {x; - Ci};eL > and

V(I + < {x; - Ci};eL >) = V(I) n V(< {x; - Ct};eL >) Proposition 32. Let r = (mn)2. The following statements are equivalent:

• (1) Let L c {1,..., r}. We have a = (a1,..., ar) e V(I + < {x; - Ci};eL >, where Ci are certain constants.

• (2) ai e {1,..., mn}, for i e {1,..., r}, with ai + aj, for (i, j) e E, and ai = Ci, for all i e L. Proof. See Prop. 23.

(1) (2) Let a e V(I + < {x; -Ct};eL >). Then Ft(a;) = 0, Vi e {1,...,r} and at = Cj, for all i e L. Let us prove that ai + aj, for (i, j) e E. Let us assume that there is a pair (k, l) e E such that ak = b = ai. Since Fk (xk) = Ft (xt) + (xk - xi)Gki (xk, xi), we obtain Fk (xk) = (xk - b)Gki (xk, b). If Gki (b, b) = 0, we would obtain that b is a zero of Fk of multiplicity at least 2 which is impossible.

(2) (1) If for all i e {1,..., r} ai satisfies the first condition of (2) and also ai = Ci, for all i e L, then every Fi, i e {1,..., r}, would be 0 on them and xi - c; = 0, for all i e L. If also ai, i e {1,..., r}, satisfies the second condition of (2), then Gij would be 0 on a = (a1,..., ar). □

Remark 33. Let S be a sudoku with preassigned data {c;};eL, for L c {1,...,r}. Let IS = I + < {x; - Ci};eL > be the ideal associated to the sudoku S. Since IS c C[x1,...,xr]. We have |V(IS) | < (mn)r < œ and IS is a radical ideal. By Proposition 8,

C[x1,..., xr ] |V(Is)| = dimc( 1 ' r ).

IS

We want to find out when the solution is unique.

Proposition 34. Let r = (mn)2. The following statements are equivalent:

• (1) a = (ai,..., ar ) is the unique solution of the sudoku S.

• (2) IS =< {xi - ai,...,xr - ar} >.

• (3) {xi - ai,...,xr - ar} is a reduced Groebner basis of IS. Proof. See Prop. 25.

(1) (2) Since a is a solution a e V(IS). Since a is the unique solution a = V(IS) because if there would exist b e V(IS), such that b + a, b would also be a solution of S which would contradict the unicity of a.

By Theorem 6, \fI$ = D(V(IS)). Thus, \fI$ = I(a) =< {xi - ai,...,xr - ar} >. In particular, for i e {i,...,mn}, there exists mi e N such that (x; - ai)mi e IS. Therefore, IS n k[x;] =

< (x; - ai )t >, for some t e N. But t = i since the polynomials Fi (xi ) are free of squares. Thus, IS =< {xi - ai,...,xr - ar} > .

(2) (3) We have to see that {xi - ai,...,xr - ar} is a reduced Groebner basis. Let G := {xi - ai,...,xr - ar}. It is easy to see that G is a Groebner basis since, for each pair (i, j), with i + j, the remainder of the division of S(x; - ai, xj - aj) by G is 0. Moreover, for each i e {i,..., r}, LC (xi - ai ) = i and no monomial of x; - ai belongs to < LT (G - {x; - ai}) >,

< LT (G - {x; - ai}) >=< {xj, i < j < r, i + j} >. Thus, {xi - ai,..., xr - ar} is a reduced Groebner basis of IS.

(3) (2) Since {xi - ai,...,xr - ar} is a reduced Groebner basis of IS, by Proposition 18, IS =< {xi - ai,...,xr - ar} > .

(2) (i) By Proposition 32 we know that the solutions of a sudoku are the zeroes of the ideal IS. Thus, if IS =< {xi - ai,...,xr - ar} >, then (ai,..., ar) is the unique solution. □

6. SAGE CODES TO SOLVE A GENERALIZED SUDOKU

Let S be a generalized sudoku which is an (mn) x (mn) grid, m, n e N, m, n > 2, m < n. (1) Let us construct E, the polynomials Fi and Gij and the ideal I generated by them.

• the set E:

R i = Integers (m) t = mn E=[]

for j in[i,., t] : forkin[i,., t] : i = (j - i) * t + k a = R i( j ) if a == 0: a = m

R 2 = Integers (n ) b = R2(k)

if b == 0: bm

# Pairs (i, j) such that Xi and xj belong to the same row:

for lin[k + 1,., t] : E ■ append((i, i + l - k)

# Pairs (i, j) such that X[ and Xj belong to the same column:

for lin[j + 1,., t] : E■ append((i,(l -1) * t + k))

# Pairs (i, j) such that x; and Xj belong to the same m x n block:

for uin[a,m] : for win[1,.,m] :

if i < ((j -1) + (m - u)) * t + k - fo + w :

E ■ append((i, ((j -1) + (m - u)) * t + k - fo + w) print(E)

# Polynomials Fi : r = t * t

F = []

variable = [0]

P(x) = prod([(x - j)for j in [1,.,t]])

for j in[1,., r ] : variable.append(var('x' + str(j))) printP (variable[j])

# Polynomials Gij : var('y')

G(x,y) = (P(x) - P(y))/(x- y)

TheGij = [G(variable[a], variable[b]) for a, b in E]

(2) Now, we are going to construct the ideal IS, generated by the polynomials Fi, i e {1,...,mn}, and Gij, (i, j) e E, and by the polynomials corresponding to the preassigned values to the sudoku. In a matrix (mn) x (mn), the values not preassigned will be written as 0.

# Polynomials corresponding to the preassigned values:

def Preassigned(matrix) polynomial = [ ] t = mn r=t*t

for i in[0,t -1] :

for j in[0,t - 1] : if matrix[i,j] ! = 0

k = i * t + ( j + 1)

p = variable[k] - matrix[i, j]

polynomial.append(p)

return(polynomial)

T = PolynomialRing(QQ; ['x' + str(j) for j in [1,.,r]]) M = matrix(QQ; matrix[i, j] for i,j in [1,.,t]) L = Preassigned(M)

TheGij = [G(variable[a]; variable[b]) for a, b in E]

I = T.ideal(L + TheGij) show(I • groebner -basis())

7. OTHER TYPES OF SUDOKU PUZZLES

Shidoku: It is a 4 x 4 grid divided into four 2 x 2 boxes where there are numbers between 1 and 4 in some of the squares in the grid (for example the bolded black below). To solve the puzzle we have to find the remaining numbers in such a way that every row and every column only contain the digits 1 to 4 and also every 2 x 2 box only contains those digits, without repetitions (for example the black below).

3 2 4 1

4 1 2 3

2 3 1 4

1 4 3 2

Let x1,..., x16 the 16 squares which form the shidoku S1, arranged from left to right and from top to bottom. Its solution will be (a1,..., a16), where ai is the number in the square associated to the variable xi, with preassigned data {Ci}ieL1, for L1 c {1,..., 16}.

Remark 35. Let S be a shidoku with preassigned data {Ci}ieL, for L c {1,...,16}. Let IS = I+ < {xi - Ci }ieL > be the ideal associated to the shidoku S, where I is the ideal generated by the polynomials Fi, 1 < i < 16, and Gij, (i, j) e E, defined in a similar way to the ones in Definition 19 and Notation 21. E = {(i, j): for 1 < i < j < 16 the ith andjth cells belong to the same row, column or 2 x 2 bloCk}.

Proposition 36. The following statements are equivalent:

• (1) a = (a1,..., a16) is the unique solution of the shidoku S.

• (2) Is =< {x1 - a1,...,x16 - a^}. >

• (3) {x1 - a1,...,x16 - a16} is a reduced Groebner basis of IS.

Proof. Similar to the one of Prop. 25. □

Several sudokus sharing 3 x 3 blocks

Let us denote the sudokus by Sj, 1 < j < m, arranged in such a way that some of them share a 3 x 3 block. Let S denote such a configuration with associated ideal IS. Let IS] be the ideal associated to the sudoku Sj, 1 < j < m, 1 < aij < 9. IS] c k[{xij}811]. For each j, 1 < j < m,

let [xij - aij}8—p be a reduced Groebner basis of the ideal IS]. Then uj=1({xij - aij}8—1), is a

reduced Groebner basis of the ideal 1IS, c k [{{xi/^1by Lemma 15. Notice that some

j—1 j J i—1 j—1

of the Xij are equal to some Xki and that some of the a{j are equal to some aki since some 3 x 3 blocks may be shared. Let us assume that, in S, we have g distinct Xij that we shall denote by y1,..., yg and where bi is the digit in {1,..., 9} associated to the variable yi.

Proposition 37. The following statements are equivalent:

• (1) b — (b1,..., bg) is the unique solution of S.

• (2) Is —< {y1 - b1,...,yg - bg} > .

iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.

• (3) {y1 - b1,..., yg - b16} is a reduced Groebner basis of IS.

Proof. Similar to the one of Prop. 25. □

Three sudokus joined in diagonal

We have three sudokus Si, 1 < i < 3, arranged in diagonal in such a way that Si and Si+1, 1 < i < 2, share a corner 3 x 3 block.

Let X1,..., x81 the 81 squares which form the sudoku S1, arranged from left to right and from top to bottom. Its solution will be (a1;..., a81), where ai is the number in the square associated to the variable Xi, with preassigned data {c{}ieL1, for L1 c {1,..., 81}. Similarly, let y1,...,y81 the 81 squares which form the sudoku S2, arranged from left to right and from top to bottom. Its solution will be (b1,..., b81), where bi is the number in the square associated to the variable yi, with preassigned data {di }i eL2, for L2 c {1,. ..,81}. S1 and S2 share a 3x3 block; thus, x54+i — y6+i, I — 9m + s, 1 < s < 3, 0 < m < 2.

Let z1,..., z81 the 81 squares which form the sudoku S3, arranged from left to right and from top to bottom. Its solution will be (e 1,..., e81), where ei is the number in the square associated to the variable Zi, with preassigned data {gi}ieL3, for L3 c {1,.. .,81}. S3 and S2 share a 3 x 3 block; thus, y54+i — z6+i, I — 9m + s, 1 < s < 3, 0 < m < 2.

We consider the polynomial ring R — C[x1,...,x81,y1,...,y81,z1,...,z81], with x54+i — y6+i, y54+i — z6+i, i — 9m + s, 1 < s < 3, 0 < m < 2. Let R1 — C[x1,...,x81], R2 — C[y1,...,y81], R3 — C[z1,...,z81] be subrings of R.

Let ISi be the ideals associated to the sudoku Si, 1 < i < 3; ISi c Ri, 1 < i < 3.

Remark 38. The unique solution of the three sudokus is given by n3=1V(IS;) — V(£3=1 ISt)• If each one of the sudokus has unique solution, {xi - ai}811, {yi - bi}811, {zi - ei}811, are, respectively, reduced Groebner basis of the ideals ISt, 1 < i < 3, then, by Lemma 15,

u3._ 1 ({xi - ai,yi - bi,zi - ei^M, ai, bi, ei e {1,.. .,9}, 1 < i < 81, is a reduced Groebner basis of

j—1 i—1

the idealY. 3—1 Isi c k [{xi, yi, zt }8—1], where x(54+i) — y№+i), a(54+i) — b(6+i), and y№4+i) — z(6+i), b(54+i) — e(6+i), for i — 9m + s, 1 < s < 3, 0 < m < 2; so it is the unique solution for the three of them, by Proposition 37.

It can be generalized to several sudokus Si, 1 < i < k, arranged in diagonal in such a way that Si and Si+1, 1 < i < k - 1, share a corner 3 x 3 block.

The preassigned data {ci}ieL1, {di}ieL2 and {gi}ieL3 are written in black in the example below (Figure 1).

Samurai Sudoku: It is composed of five sudokus joined in the shape of X. There is a central sudoku with four sudokus at the edges. Each shares a 3 x 3 block with the central sudoku.

Let us denote the five sudokus by Sj, 1 < j < 5, arranged in such a way that S3 is the sudoku at the center sharing a 3 x 3 block with Sj, 1 < j < 4, j + 3, the other four sudokus located at the corners of the central one.

1 5 4 2 3 8 6 7 9

2 7 6 1 5 9 3 4 8

3 8 9 4 6 7 5 1 2

4 1 2 3 8 6 7 9 5

6 3 5 7 9 2 4 8 1

8 9 7 5 1 4 2 3 6

8 6 2 1 5 7 9 4 3 6 2 1 8 5 7

9 7 4 2 3 8 5 6 1 8 7 3 9 2 4

3 1 5 6 4 9 7 2 8 9 4 5 1 6 3

7 8 3 9 1 2 6 5 4

6 5 1 8 7 4 3 9 2

4 2 9 3 6 5 8 1 7

1 4 8 5 7 9 2 3 6 7 9 1 4 8 5

2 6 7 1 3 4 5 9 8 4 2 3 1 7 6

3 5 9 2 6 8 1 4 7 5 8 6 2 3 9

4 7 1 3 2 6 8 5 9

5 8 2 4 9 1 6 7 3

6 9 3 7 8 5 4 2 1

7 1 4 6 5 3 9 8 2

8 2 5 9 1 7 3 6 4

9 3 6 8 4 2 7 1 5

Figure 1

Remark 39. The unique solution of the five sudokus is given by n5_, V(IS ) = V£ IS ). If each

j-i j ;-i j

one of the sudokus has unique solution, for each j, i < j < 5, let {x;j - aij }8ii, be a reduced Groebner basis of the ideal Ij c k[{x;j}8=i], then, by Lemma 15, u5=i({x;j - aij}8=i), aij e {i,...,9},

i < i < 8i, i < j < 5, is a reduced Groebner basis of the ideal Y.5=i Ij c k[{{x;j-i], where x(54+i)2 - x(6+i)3 and x(54+i)3 - x(6+i)5 and also x(54+t)i - xi3, x(54+t)3 - xi4, for i - 9m + s, i < s < 3, t - 9m + v, 7 < v < 9, 0 < m < 2; thus it is the unique solution for the five of them, by Proposition 37.

The preassigned data are written in black in the example below (Figure 2). Sohei sudoku (Figure 3)

It is composed of four sudokus forming a cross + arranged in such a way that each sudoku shares a 3 x 3 block with each one of the two adjacent ones.

Let us denote the four sudokus by Sj, i < j < 4, arranged in such a way that Si shares a 3 x 3 block with S2 and S4, S2 sharesa 3x3 block with S3 and Si, S3 sharesa 3x3 block with S2 and S4.

Remark 40. The unique solution of the four sudokus is given by nl ,V(ISi) - V£4_, IS ). If

j-i j ;-i j

each one of the sudokus has unique solution, for each j, i < j < 4, let {x;j - aij}8ii, be a reduced Groebner basis of the ideal Ij c k [{x;j ^-J, then, by Lemma 15, u4-i({x;j - aij }8-i), aij e

{i,. ..,9}, i < i < 8i, i < j < 4, is a reduced Groebner basis of the ideal Y.4-i Ij c k [{{x;j} 8i i -i], where x(54+i)i - x№+i)4 and x(54+i)2 - x(6+i)3, for i - 9 m + s, i < s < 3, and also x(54+t)i - xt2, x(54+t)4 - xi3, for t - 9m + v, 7 < v < 9, 0 < m < 2; thus it is the unique solution for the four of them, by Proposition 37.

5 4 7 G 1 9 2 8 3 3 3 6 1 4 5 2 9 7

3 1 6 2 8 5 9 4 7 1 2 4 3 9 7 G 5 8

2 8 9 4 3 7 5 1 G 5 9 7 2 8 6 1 3 4

1 6 4 3 7 2 8 5 9 3 T 5 4 2 8 7 6 9

9 2 3 1 5 8 6 7 4 G 2 7 / 9 5 8 3

8 7 5 9 4 6 3 2 ? 9 7 8 5 G 3 4 ? 2

7 3 2 5 9 4 1 6 8 3 7 5 2 4 9 6 3 1 8 7 5

4 5 1 8 G 3 7 9 2 1 S 4 6 5 3 8 7 2 9 4 1

6 9 8 7 2 1 4 3 5 9 G 2 7 8 1 9 5 4 3 2 6

5 1 6 2 3 9 7 4

2 4 9 7 5 ? 3 6 8

8 7 3 4 9 G 5 1 2

6 2 9 1 8 7 3 5 4 6 1 9 8 2 7 3 4 9 t G 5

4 1 8 5 3 G 9 2 1 5 4 8 1 3 G 5 1 8 4 9 2

3 7 5 2 9 4 6 8 1 2 3 7 9 5 1 2 6 7 3 8

1 5 3 4 6 8 2 7 9 2 6 4 8 1 3 5 7 9

1 4 2 3 1 9 8 G 5 9 1 3 2 5 7 5 4 6

9 8 6 7 2 5 1 4 3 5 7 8 6 9 4 3 2 1

5 3 1 8 4 2 7 9 6 6 5 ? 4 3 2 9 8 7

8 6 4 9 1 i 5 3 2 7 4 2 9 8 ? G 5 3

2 9 7 6 5 3 4 1 8 3 8 9 7 6 5 2 1 4

Figure 2

Figure 3

References

1. A. Delgado Latournerie, "Bases de Groebner y aplicaciones," Masters' Thesis UAM, Madrid, Spain, 2016.

2. R. Hartshorne, Algebraic Geometry (Graduate Texts in Mathematiics), 52, Springer-Verlag, 1977; doi: 10.1007/978-1-4757-3849-0.

3. G McGuire, B. Tugemann, G. Civario, "There is no 16-clue Sudoku: Minimum Number of Clues Problem," arXiv: 1201.0749 [cs.DS], Jan. 2012.

4. "Mathematics of Sudoku" in Wikipedia, Wikimedia Foundation, [Online]. Available: https://en. wikipedia.org/wiki/Mathematics-of-Sudoku-Minimum-number-of-givens.

5. "Magic Squares in Sudoku Grids," in Puzzling, Stack Exchange Inc. [Online]. https://puzzling. stackexchange.com/questions/6195/magic-squares-in- sudoku-grids?rq=1.

Received 72.04.2078, the final version — 70.05.2078.

Компьютерные инструменты в образовании, 2018 № 3: 5-21

УДК: 512.62, 51-8, 519.813.7

http://ipo.spb.ru/journal

doi:10.32603/2071-2340-3-5-21

РЕШЕНИЕ СУДОКУ С ПОМОЩЬЮ БАЗИСОВ ГРЕБНЕРА

Гонзалез-Доррего М. Р. Автономный университет Мадрида, Мадрид, Испания

Аннотация

Мы изучаем решение судоку и обобщенного судоку, используя технику базисов Грёбнера. Пусть x\,...,x81 переменные, связанные с 81 квадратами, которые образует головоломку судоку и линейно упорядочены сначала по строкам, затем по столбцам. Решение судоку есть набор чисел (aj,..., a81), где щ число в квадрате, ассоциированном с переменной x^.

Пусть также S — судоку с предварительно заполненными данными (с,},£l для L с (1,___, 81}. Вся необходимая информация для решения такого судоку содержится в алгебраическом множестве V(I + < (x, - с^},£l >). Мы используем технику базисов Грёбнера для поиска такого решения и приводим соответсвующий код в системе компьютерной алгебры SAGE для программы, решающей эту задачу. Ключевые слова: судоку, обобщённое судоку, базис Грёбнера, алгебраическое многообразие.

Цитирование: Gonzalez-Dorrego M. R. Resolution of Sudokus Using Groebner Basis // Компьютерные ин^рументы в образовании. 2018. № 3. С. 5-21.

Поступила в редакцию 72.04.2078, окончательный вариант — 70.05.2078.

Гонзалез-Доррего Мария Розарио, профессор, доктор, отделение математики, Автономный университет Мадрида, mrosario.gonzalez@uam.es

Gonzalez-Dorrego María Rosario, Departamento de Matemáticas Universidad Autónoma de Madrid; Ciudad Universitaria de Cantoblanco, 28049 Madrid, SPAIN mrosario.gonzalez@uam.es

© Our authors, 2018. Наши авторы, 2018.

i Надоели баннеры? Вы всегда можете отключить рекламу.