Solving logical puzzles using mathematical models 1

0 downloads 0 Views 481KB Size Report
Jl. MH Thamrin Boulevard, Lippo Karawaci Tangerang 15811 Indonesia ... this puzzle in mathematical way and implement those into a system. As a result,.
Solving logical puzzles using mathematical models Susanti, Kie Van Ivanky Saputra, Samuel Lukas Faculty of Science and Mathematics, Universitas Pelita Harapan Jl. MH Thamrin Boulevard, Lippo Karawaci Tangerang 15811 Indonesia email: [email protected] Abstract In this paper, we are going to use mathematical models to solve logical puzzles. Logical puzzles discussed in this talk is Slitherlink, which has various shapes. We derive mathematical models for this puzzle and represent some of those rules for this puzzle in mathematical way and implement those into a system. As a result, we can solve some puzzles whose difficulty are normal. However, for more difficult puzzles we need the help of another method and algorithm.

Keywords : Numberlink, Slitherlink, Logical Puzzles, Mathematical Model.

1

INTRODUCTION

Logical puzzles such as Sudoku, Kakuro, are interesting in its own. The puzzle only need pure logic and one does not require any mathematics to solve and to find the solution. As these puzzles offer endless fun and are highly addictive they have taken the whole world like a storm. In this paper, we are going to use mathematical models to solve logical puzzles. Logical puzzles discussed in this paper is Slitherlink, which has various shapes such as square, kites, triangle and many other shapes. We choose the triangular shape in this paper. In the first step, we derive mathematical models for each puzzle. After that, we are going to build a procedure using the mathematical models derived to solve such puzzles. Sudoku and Kakuro are examples of two logical puzzles whose basic shapes are rectangular. The models for these puzzles are much easier to make than that of Slitherlink. Every puzzle has its unique representation, which is different one to another. Also, every puzzle has its logical rules which one uses to solve the puzzle. We are going to represent some of those rules for each puzzle in mathematical way and implement those into a procedure to solve such puzzles. The slitherlink puzzle first appears in the web1 . This games has many other names such as Loopy, Fences, Number Line and et cet. This game consists of cells and inside some of those cells there are numbers. As far as the authors know, there are not much discussions how to solve this Slitherlink puzzles especially wheh whose shape is triangular. The current research is conducted by Yoshinaka et al. [1]. They conducted a research to build an algorithms using a zero-suppressed binary decision diagrams (ZDDs). Another research is conducted by Herting [4] who has also used mathematical model and implemented his model to solve the slitherlink puzzle whose shape is rectangular. This research is also conducted by Hurlimann [5]. In his unpublished thesis Rank [3] has also conducted his research about Slitherlink. He reviews many algorithms 1

http://www.nikoli.co.jp

1

Figure 1: This is a screenshot of the slitherlink puzzle whose cells are triangle. The dimension of this puzzle is 10 × 10. and shows characteristics of the Slitherlink puzzle. Most of the research is not talking about how to solve the puzzle, instead it is talking about NP-complete as shown in [6]. This paper is organised as follows. In section 2 we shall build a mathematical model of the puzzle. While in section 3 the basic rules one usually apply are represented in a mathematical way that is in similar manner with the model derived in section 2. This paper concludes in section 4 where we discuss the implementation of our model and algorithms to solve the slitherlink puzzles.

2

MATHEMATICAL MODEL

The Slitherlink puzzle is shown in Figure 1. The goal of this puzzle is to find a (unique) loop using some of the edges of triangles such that the number of the edges of the solution must be equal to the number shown in some triangles. Thus, if a number zero is shown in one of the triangles, this means the solutions will not pass such a triangle. To begin with, we shall try to find a mathematical representation of this puzzle. This includes an indexing for (triangular) cells and an indexing for vertices. A slitherlink puzzle has dimension m × n if it has m rows and for each row there are 2n triangles, consisting of n triangles facing down and n triangles facing up. For every triangle, we denote Cij where i = 1, . . . , m and j = 1, . . . , 2n. If i + j ≡ 0 mod 2 then the triangle is facing down but if i + j ≡ 1 mod 2 then the triangle is facing up.

2

Figure 2: Labeling of edges connected to vertex Pxy . For every Cij there is always three edges and one of them is horizontally located, which we denote by hqr where  i, if i + j ≡ 0 mod 2, q= (1) i + 1, if i + j ≡ 1 mod 2, and r = (j + 1) ÷ 2.

(2)

For every Cij , there are also edges which are 60/120 degrees positioned, denoted by vij and vi,j+1 . The value of each Cij is usually given by the puzzle, however there are some triangles whose values are blank, thus we give them 3 as its value. This mean Cij ∈ {0, 1, 2, 3}. The value of vij or hqr is either 0 or 1. It is 1 if the solution loop passes through this edge and it is 0 if the solution loop does not pass through this edge. Initially (before the game is played), the value of each vij or hqr is 0. For vertices, we recall that every triangle has only three vertices. Suppose the triangle is facing up, that there are two vertices on the base and one vertex on the top of it. The vertices on the base are denoted by Pqr and Pq,r+1 where q and r are from equations (1) and (2). While the vertex on the top is denoted by Pq−1,t where  r, if i ≡ 0 mod 2, t= (3) r + 1, if i ≡ 1 mod 2. There is some slight modification if the triangle is facing down. Two vertices on the top are denoted by Pqr and Pq,r+1 while the vertex on the base is denoted by Pq+1,s where  r, if i ≡ 1 mod 2, s= r + 1, if i ≡ 0 mod 2. After that, we shall define two sets with respect to the vertex Pxy . We denote the sets by Exy and Hxy where x and y are the position of the vertex. Generally, every vertex Pxy has 6 edges connected to that vertex. We shall label of these edges according to Figure 2. In the beginning of the game, we still do not know which hqr and vij have value 0 or 1. Thus we collect all the label in a set Exy = {0, 1, 2, 3, 4, 5}. Once we know the solution loop, we shall know if the value of edges connected to this vertex is 0 or 1. Thus we shall remove these labels and put the label whose value is 1 in a set Hxy . The degree of a vertex (denoted by deg(Pxy )) is determined by the number of edges whose value is one, connected to this vertex. Therefore the degree of a vertex is equal to the number of element in set Hxy once Exy is empty. 3

Figure 3: Example of the slitherlink puzzle. The black line denotes part of the loop we are looking.

Figure 4: These are the conditions such that the cell rule can be applied. Let us discuss an example given in Figure 3. The dimension of this puzzle is 5 × 5. Let us focus on the top left of the puzzle where the triangle C13 is located. This triangle is facing down with edges h12 , v12 and v14 . The triangle has three vertices; two of the top are P12 and P13 and one on the base is P22 . All of these can be computed using the indexing in the above section. In Figure 3 we already have a little bit of the solution, which marked the black solid line. Therefore we know that h12 = 1, v13 = 1 but v14 = 0. We also know that for vertex P22 the degree is yet to be determined but E22 = {3, 4, 5} while H22 = {2}.

4

3

RULE-BASED MODELING

3.1

BASIC RULES

We have represented the slitherlink puzzle in a mathematical way using only indexes and sets. Now we are going to represent some of rules that people mostly use when solving this puzzles. Let us take an arbitrary triangle, Cij then 1. if Cij 6= 3, we have vij + vi,j+1 + hqr = Cij ,

(4)

2. if Cij = 3, we have vij + vi,j+1 + hqr ≤ Cij , where q and r are from equations (1) and (2). Thus, among vij , vi,j+1 and hqr , if we know the value of some of them, using the equation above, we can find some of the solution. The ultimate goal of this puzzle is to find a unique loop using (some of) the edges of triangles such that the number of the edges of the loop passing through a triangle must be equal to the number shown in that triangle. In mathematical way, this means we need to find a loop φ, where φ = {Pxy | deg(Pxy ) = 2} such that for every Px1 y1 , Px2 y2 ∈ φ there is a path from Px1 y1 to Px2 y2 .

3.2

CELL-BASED RULE

This rule is used whenever we have a cell, Cij , whose value is 2 and there is another cell nearby whose value is also 2. We have total 12 possibilities such that this rule is working. We depict all of these in Figure 4. If we find one of these 12 conditions then there is a definite move we can apply to get some of the solution. Remark, the conditions (a)-(f) are similar due to symmetry. The cases (g)-(i) and the cases (j)-(l) are respectively similar for the same reason. Rules for condition (a), (g) and (j) are summarised in the following theorems. Theorem 3.1 Suppose for some i and j with i + j ≡ 1 mod 2, we have Cij = 2 and Ci,j+2 = 2, then vij = vi,j+3 = 1, vi+1,j+1 = vi+1,j+2 = 0 and hq−1,t = 0, where q is from (1) and t is from (3). Theorem 3.2 Suppose for some i and j with i + j ≡ 0 mod 2, we have Cij = 2 and Ci+1,j = 2, then hqr = hq+2,r = 1 and hq+1,r−1 = hq+1,r = 0, where q and r are from (1) and (2) respectively. Theorem 3.3 Suppose for some i and j with i + j ≡ 1 mod 2, we have Cij = 2 and Ci+1,j = 2, then hqr = 1 and hq,r−1 = hq,r+1 = 0, vi,j−1 = vi,j+2 = 0, and vi+1,j−1 = vi+1,j+2 = 0 where q and r are from (1) and (2) respectively. Illustrations of the above theorems are depicted in Figure 5. We shall give a proof of Theorem 3.1 while the rest can be proved in a similar manner. Proof of Theorem 3.1. We are going to prove that if Cij = 2 and Ci,j+2 = 2 with i + j ≡ 1 mod 2 then vij = vi,j+3 = 1, vi+1,j+1 = vi+1,j+2 = 0 and hq−1,t = 0. However, we shall only prove that vij = 1 since the rest can be proven in a similar way. 5

Figure 5: The above figures are cell-based rules one can apply to get a part of the solution loop. There are three different cases and each of the cases has symmetry variants.

Figure 6: Illustration of the proof of Theorem 3.1. A contradiction is found when assuming one of the edges of the top left triangle is not part of the loop. Suppose vij 6= 1 then vij = 0, then since Cij = 2 we have hqr = vi,j+1 = 1. This implies that vi,j+2 and hq,r+1 must be 0. This means Ci,j+2 ≤ 1. This is a contradiction as it is given that Ci,j+2 = 2. The proof above is best explained using a figure that one can see in Fig. 6. Besides the values of the edges around Cij , the vertices involved will also changed. Thus, the elements of sets Exy and Hxy with respect to Cij will change. Let us take the case (a) for an example. Since the triangulars Cij and Ci,j+2 are facing up then we can label all the vertices involved according to Figure 7. The changes of each Exy and

Figure 7: Labeling of each vertices involved in case (a).

6

Hxy are the following: • Eq−1,t = {0, 1, 2, 3, 4, 5}, Hq−1,t = {} are now Eq−1,t = {1, 2, 3, 5} and Hq−1,t = {4}, • Eq−1,t+1 = {0, 1, 2, 3, 4, 5} and Hq−1,t+1 = {} are now Eq−1,t+1 = {0, 1, 2, 4} and Hq−1,t+1 = {5}, • Eq,r = {0, 1, 2, 3, 4, 5} and Hq,r = {} are now Eq,r = {0, 2, 3, 4, 5} and Hq,r = {1}, • Eq,r+1 = {0, 1, 2, 3, 4, 5} and Hq,r+1 = {} are now Eq,r+1 = {0, 1, 2, 3} and Hq,r+1 = {}, • Eq,r+2 = {0, 1, 2, 3, 4, 5} and Hq,r+2 = {} are now Eq,r+2 = {0, 1, 3, 4, 5} and Hq,r+2 = {2}, • Eq+1,t = {0, 1, 2, 3, 4, 5} and Hq+1,t = {} are now Eq+1,t = {0, 2, 3, 4, 5} and Hq+1,t = {}, • Eq+1,t+1 = {0, 1, 2, 3, 4, 5} and Hq+1,t+1 = {} are now Eq+1,t+1 = {0, 1, 3, 4, 5} and Hq+1,t+1 = {}.

3.3

VERTEX BASED RULES

Another basic rules that one can apply whenever the following situations are satisfied. Theorem 3.4 (Single path rule) Suppose for some x and y, we have n(Exy ) = 1 and n(Hxy ) = 1 then Hxy = Hxy + {w}, where w ∈ Exy . Theorem 3.5 (Dead-end rule) Suppose for some x and y, we have n(Exy ) = 1 and n(Hxy ) = 0 then Exy = {}. Theorem 3.6 (No branches rule) Suppose for some x and y, we have n(Exy ) ≥ 1 and n(Hxy ) = 2 then Exy = {}. We will not prove the above theorems as the prove is a good exercise for undergraduate students. The illustrations of the above theorem are depicted in Figure 8. Even though the above theorems look simple but in the search for a solution, these happen to be very useful.

3.4

VERTEX-CELL BASED RULES

In this section, we shall derive rules using the information from vertices and cells. There are many rules that one can find and derive using these information. Some of the rules are given and depicted in Figure 9. Remark, more rules and theorems will follow due to symmetry of the puzzle.In this section, we shall only give one theorem and its proof as the other theorems and their proofs will be good exercises for undergraduate students. Consider part (a) of Figure 9. Theorem 3.7 Suppose for some i and j with i + j ≡ 1 mod 2, we have Cij = 1, Eq−1,t = {4, 5} and n(Hq−1,t ) = 1 then hqr = 0.

7

Figure 8: These are illustrations of theorems 3.4 and 3.5, whenever a vertex has either a single path or no path. While theorem 3.6 is useful to remove unnecessary edges as some edges are already determined.

a

b

c

d

Figure 9: Illustrations of rules that are derived using the information from vertices and cells. There are 4 rules in this figure but actually one can derive much more than those are given in this paper. Proof.Suppose hqr 6= 0 then hqr = 1. Since Cij = 1 then vij = vi,j+1 = 0. This implies that 4 and 5 will be removed from Eq−1,t . This is impossible as we have a dead-end situation while we know that n(Hq−1,t ) = 1. We also need to update Exy and Hxy for each vertex involved in this case. From Figure 9 there are only two vertices that need to be updated which are Pq,r and Pq,r+1 . These vertices are vertices of the base of the facing up triangle. We summarised the

8

changes of these vertices as follows. • Eq,r = {0, 1, 2, 3, 4, 5}, Hq,r = {} are now Eq,r = {1, 2, 3, 4, 5} and Hq,r = {}, • Eq,r+1 = {0, 1, 2, 3, 4, 5} and Hq,r+1 = {} are now Eq,r+1 = {0, 1, 2, 3, 4} and Hq,r+1 = {}.

3.5

SOLUTION CHECKING RULE

In this section, we shall derive a rule to check whether or not we are finished. Different from the above sections, we will not derive a theorem, instead we shall build a procedure to check a solution. 1. Suppose φ = {Pxy | n(Pxy ) = 2} 2. Define REP EAT = T RU E 3. Take arbitrary Px1 ,y1 ∈ φ 4. Define x = x1 and y = y1 5. Define COU N T ER = 0 6. While REP EAT 7.

COU N T ER = COU N T ER + 1

8.

take w ∈ Hx,y

9.

find Px2 ,y2 that is connected to Px,y through w.

10.

Hx,y = Hx,y − {w}

11.

Hx2 ,y2 = Hx,y − {5 − w}

12.

if x2 = x1 and y2 = y1 then

13.

REP EAT = F ALSE

14.

else

15. 16.

x = x2 and y = y2 end if

17. end while 18. if COU N T ER = n(φ) 19.

solutions is achieved

20. else 21.

solutions is not achieved

22. end if 9

Figure 10: Illustrations of a situation when a mini loop can be created. We need to derive a rule such that this situation can be avoided.

3.6

RULE TO AVOID MINI LOOP

In this section, we shall derive a rule to avoid a mini loop. The ultimate goal of this puzzle is to find a loop, however, along the way to get the solution, we sometimes find a possibility to build a mini loop. This means that a loop could be created without violating the rules but the game is not finished yet as we have still triangles untouched. This situation is illustrated in Figure 10. The procedure of this rule is summarised in the following. Suppose γ = {Pxy , n(Pxy ) = 1} and n(γ) = 2. Suppose Px1 ,y1 , Px2 ,y2 ∈ γ. If |x1 − x2 | = 1 or |y1 − y2 | = 1 then let us closed the loop by Ex1 ,y1 = Ex1 ,y1 − {w} and Hx1 ,y1 = Hx1 ,y1 + {w} and also Ex2 ,y2 = Ex2 ,y2 − {5 − w} and Hx2 ,y2 = Hx2 ,y2 + {5 − w}, where w ∈ Ex1 ,y1 is a label that connects Px1 ,y1 and Px2 ,y2 Then we can use the basic equation (4) to check whether there is a contradiction. Let us collect all of Cij s whose value is either 0, 1 or 2 and check whether the equation (4) is satisfied for all Cij s. If there is a contradiction then remove w and 5 − w from Hx1 ,y1 and Hx2 ,y2 respectively.

3.7

GENERAL ALGORITHM

The general algorithm to seek the solution is summarised in the following. 1. Do all four rules discussed in section 3.1, 3.2, 3.3 and 3.4 until there is no move available. 2. Check whether or not the solution is achieved using rule in section 3.5. If it is then it is finished, if the solution is not achieved then do rule in section 3.6 and go back to step 1. 3. If there is no move and the solution is still not achieved, then we need another algorithm to solve the puzzle.

4

DISCUSSION

We finalised our paper in this section. We have derived a mathematical model for the Slitherink puzzle. We have shown that using a mathematical model, we can solve one of the logic puzzles available in the markets.

10

No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Dimension 7×7 7×7 7×7 7×7 7×7 10 × 10 10 × 10 10 × 10 10 × 10 10 × 10 15 × 15 15 × 15 15 × 15 15 × 15 15 × 15 20 × 20 20 × 20 20 × 20 20 × 20 20 × 20

Easy Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved

Normal Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved Solved

Hard Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved

Tricky Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved Unsolved

Table 1: This table summarised what we have implemented and tested. This shows that our algorthm can only solve easy and normal puzzles while we need another algorithm to solve puzzles whose difficulty is harder. We realise that our model and algorithm have to be implemented and tested whether or not it can solve the puzzle thus we have implemented and tested it using a system consisting of: 1. Software: the operating system is Windows 7 and the programming language is Java JDK 1.6.0. 2. Hardware: the computer is 2.4 GHz and 2 GB of RAM. Using the above system, we try to solve some slitherlink puzzles, that we obtained from the web2 . The result is summarised in Table 1. As the table shows, that our mathematical model and algorithm can only solve puzzles whose difficulty is either easy or normal. We need another advanced algorithm to solve puzzles whose difficulty is hard or tricky. We note that the indexing we use for this puzzle is not unique nor the best indexing as well. There are another indexing we need to search and derive, especially we need another indexing that could cope with the symmetry this puzzle owns, and this might not an easy task. As an implications, when we have another indexing, the mathematical way to write those rules presented in this paper will also be changed. The rules that are derived in this paper are all basic but useful rules. One can derive 2

Loopy from Simon Tathams Portable Puzzle Collection, http://www.chiark.greenend.org.uk/ sgtatham/puzzles/java/loopy.html

11

another complicated and untrivial rules and write it mathematically. The search for such a rule as well as the search of a good indexing would be good candidates for future research. Also, there is also a need for advanced algorithms so that we can solve puzzles whose difficulty is hard or tricky. Other than the above suggestions for future research, one can investigate and build a model for another slitherlink puzzles whose shape are not square nor triangle. This could be interesting as there are various shapes available.

Acknowledgment The authors would like to thank Arnold Aribowo for fruitfulness discussion during presentation of this research. This research is partially supported by Riset UPH 20112012 (no. R003/KPPMF-FSM/IV/2012).

Reference [1] R. Yoshinaka, T. Saitoh, J. Kawahara, K. Tsuruma, Finding All Solutions and Instances of Numberlink and Slitherlink by ZDDs, Algorithms 5(2), 176–213, 2012. [2] S. Hiroki, I. Chikara, T. Yasuhiro, K. Yoshiyuki, Solving and Making Problems of Slither Link, Joho Shori Gakkai Shinpojiumu Ronbunshu 32, 2006. [3] J. Rank, Slitherlink, Unpublished thesis. [4] S. Herting, A rule based approach to the puzzle of Slither Link, preprints. [5] T. Hurlimann, The Slitherlink Puzzle, preprints. [6] T. Yato, On the NP-completeness of the Slither Link puzzle, preprints.

12

Suggest Documents