Benchmark Instances and Branch-and-Cut Algorithm for the Hashiwokakero Puzzle

05/02/2019 ∙ by Leandro C. Coelho, et al. ∙ 0

Hashiwokakero, or simply Hashi, is a Japanese single-player puzzle played on a rectangular grid with no standard size. Some cells of the grid contain a circle, called island, with a number inside it ranging from one to eight. The remaining positions of the grid are empty. The player must connect all of the islands by drawing a series of horizontal or vertical bridges between them, respecting a series of rules: the number of bridges incident to an island equals the number indicated in the circle, at most two bridges are incident to any side of an island, bridges cannot cross each other or pass through islands, and each island must eventually be reachable from any other island. In this paper, we present some complexity results and relationships between Hashi and well-known graph theory problems. We give a formulation of the problem by means of an integer linear mathematical programming model, and apply a branch-and-cut algorithm to solve the model in which connectivity constraints are dynamically generated. We also develop a puzzle generator. Our experiments on 1440 Hashi puzzles show that the algorithm can consistently solve hard puzzles with up to 400 islands.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Hashiwokakero, or simply Hashi, is a Japanese single-player puzzle played on a rectangular grid with no standard size. Some cells of the grid contain a circle, called island, with a number inside it ranging from one to eight, and the number of islands is denoted by . The remaining positions of the grid are empty. The player must connect all the islands by drawing bridges between them. For this reason, the game is often referred to as building bridges. The solution to the puzzle must respect the following rules:

  1. the bridges must begin and end at distinct islands;

  2. they must not cross any other bridges or islands;

  3. they may only run horizontally or vertically;

  4. at most two bridges may connect any pair of islands;

  5. the number of bridges connected to each island must be equal to the number inscribed in the circle;

  6. each island must be reachable from any other island.

Figure 1 depicts a 77 puzzle with islands along with a feasible solution.

Figure 1: A Hashi puzzle (left) and a feasible solution (right)

Unlike the Sudoku (see, e.g., Coelho and Laporte 2014) the literature on Hashi is rather scarce. Perhaps the most important contribution to the Hashi literature is that of Andersson (2009) who proved the problem to be NP-complete by reduction from a Hamiltonian circuit in unit-distance graphs. Therefore, like all other NP-complete problems, such as the Hamiltonian cycle problem (Lawler et al. 1985), the Hamiltonian path problem (Garey et al. 1976), the maximum cut problem (Gavril 1977), the map coloring problem (Dahl 1987), finding cliques of given sizes (Karp 1972), the Hashi problem can be reduced to a Boolean satisfiability problem.

We note that the complexity proof of Andersson (2009) is fundamentally based on rule 6 (connectivity). Nevertheless, even without rule 6, we show that the problem remains NP-complete via a different reduction. Indeed, the problem of reconstructing disjoint sets of orthogonal segments (RDOS) can be reduced to a Hashi instance in which all islands have value one, and therefore where no double bridges are needed. RDOS has been proven to be NP-complete using an elegant reduction from 3-SAT in Rendl and Woeginger (1993). Finally the problem without the connectivity and no-crossing rules (2 and 6) containing only islands of value one is solvable by an polynomial algorithm.

Beyond complexity results, some algorithms have been presented. Malik et al. (2012)

combined heuristics operators and a backtracking algorithm to find feasible solutions. The operators are based on the intuitive decisions that a player would make when solving the game. When no such operators can be feasibly applied, a backtracking procedure takes place to explore other solutions. Some other papers have considered Hashi as part of a larger framework.

Golan (2011) shows that any Hashi puzzle can be reduced to a minesweeper puzzle. While studying trees and graphs, Prosser and Unsworth (2006) proposed two new constraints for modeling trees, and used the Hashi puzzle as an illustration of their technique. Finally, Brain et al. (2009) used Hashi to illustrate how to efficiently implement algorithms based on answer set programming, a declarative programming paradigm used to model difficult search problems.

In this paper, we introduce new benchmark instances as well as a mathematical programming model and branch-and-cut algorithm for the Hashi puzzle. The remainder of this paper is organized as follows: Section 2 describes the model and algorithm; Section 3 develops a puzzle generator and Section 4 analyzes the performance of our algorithm on a large set of instances; conclusions follow in Section 5.

2 Mathematical model and branch-and-cut algorithm

The Hashi puzzle can be defined on an undirected graph , where is the set of vertices representing islands. Let be the number of bridges to be constructed from island , and . Let be the set of vertices adjacent to vertex either horizontally or vertically. Let be the set of all edges connecting two adjacent vertices of . By convention, if , then . Let be the set of intersecting edge pairs . We model Hashi as an integer linear program which admits a solution if and only if the Hashi puzzle is feasible. For

, our model uses binary variables

indicating whether two adjacent vertices and  are connected by at least one bridge in the solution, and integer variables indicating the number of bridges between and . The formulation is then:

(1)
(2)
(3)
(4)
(5)
(6)

Constraints (1) force the presence of bridges for each vertex . According to constraints (2), at most two bridges can exist between any two connected vertices. These constraints also ensure consistency between the and variables. Constraints (3) prohibit intersecting bridges, and constraints (4) are strong connectivity constraints, enforcing the solution to be connected, as in the traveling salesman problem (Dantzig et al. 1954). Constraints (5) and (6) define the domains of the variables.

This formulation can be strengthened by adding a valid inequality which exploits the fact that the graph induced by the positive variables must contain a spanning tree. It is called “weak connectivity constraint” and was found to be helpful in an algorithm in which the strong connectivity constraints (4) are initially relaxed:

(7)

To solve the problem, we use a branch-and-cut algorithm which initially relaxes constraints (4), and then detects (separates) and reintroduces the offending constraints each time an integer solution of the resulting branch-and-bound tree is found to be infeasible.

On the example of Figure 1 it was possible to find a feasible solution without need for additional strong connectivity constraint. In contrast, Figure 2 shows a new puzzle which required four added strong connectivity constraints, along with their partial solutions obtained with our algorithm. It is important to note that without the weak connectivity constraint (7), 87 strong connectivity constraints would have been needed.

(a) Hashi puzzle
(b) Intermediate steps: dynamic introduction of the strong connectivity constraints
(c) The completed Hashi
Figure 2: Example of a difficult Hashi requiring four strong connectivity constraints

3 Puzzle generator

This section describes an algorithm designed to generate large scale Hashi puzzles which will be used for our experimental analyzes. The generator receives as input the desired number of islands , the dimensions of the grid , a parameter which influences the number of cycles and the connectivity of the solutions, and a parameter which influences the number of double-bridges of the solutions. The generator is made up of four steps:

  • Step 1 – Placement of the islands. A first island is placed in a random grid location. Then, the algorithm iteratively selects a random existing island, a random direction (top, bottom, left or right), and a random position in this direction (without crossing an existing edge) if possible to add a new island and edge. This process is repeated times.

  • Step 2 – Creating cycles. At the end of Step 1, there are islands connected by edges. To avoid studying particular cases of Hashi puzzles that admit trees as solutions, the tree is augmented with additional edges, therefore creating cycles. Iteratively, the algorithm randomly selects two islands which can be connected by a horizontal or vertical edge without crossing existing ones, and connects them with one additional edge. This process is repeated times, leading to islands connected with edges.

  • Step 3 – Creating double edges.

    To favor the possible use of double edges in solutions, the algorithm iteratively considers each edge, and transforms this edge into a double-edge with probability

    .

  • Step 4 – Adjacency count and final puzzle. Finally, the number of edges adjacent to each island is counted and marked. All edges are erased, and the puzzle is returned.

By design, all puzzles are known to be feasible since a feasible solution exists at the end of Step 3 before erasing the edge information. Also note that this solution may not be unique.

4 Computational experiments

We have implemented our algorithms in Visual Basic and used Gurobi 8.0 to build the branch-and-cut algorithm. To generate a representative set of instances, we considered four possible instance size values, considering a number of islands on boards of dimensions respectively. We varied the level of connectivity by selecting as well as the level of double-edges by considering . For each parameters combination (instance group), we generated 30 random instances to increase the statistical strength of our analyzes, leading to a total of Hashi instances divided into 48 groups. These instances can be accessed at https://w1.cirrelt.ca/~vidalt/en/research-data.html. We ran the branch-and-cut algorithm on each instance, and repeated the same experiment without the weak connectivity constraint (7) to investigate its influence on the search performance.

Tables 1 and 2 present the results of our experiments with and without the weak connectivity constraint. The first set of columns reports the average CPU time needed to solve each instance of the group, and the second set of columns reports the average number of strong connectivity constraints that were dynamically generated during the solution process.

Time (s) Number of strong connectivity constraints
n Avg. Avg.
100 0.25 0.05 0.04 0.04 0.03 0.04 1.10 0.70 0.57 0.40 0.69
0.5 0.11 0.10 0.11 0.09 0.10 3.33 3.00 3.13 2.40 2.97
0.75 0.22 0.39 0.39 0.36 0.34 5.93 11.77 12.03 11.07 10.20
Avg. 0.13 0.18 0.18 0.16 0.16 3.46 5.16 5.24 4.62 4.62
200 0.25 0.16 0.15 0.13 0.13 0.14 2.10 1.63 1.13 0.90 1.44
0.5 0.52 0.53 0.49 0.44 0.50 7.93 7.53 6.13 4.87 6.62
0.75 2.13 2.87 3.15 2.91 2.76 31.13 41.40 45.50 38.80 39.21
Avg. 0.94 1.18 1.26 1.16 1.13 13.72 16.86 17.59 14.86 15.76
300 0.25 0.43 0.38 0.40 0.35 0.39 3.47 2.23 2.43 1.33 2.37
0.5 2.34 1.86 1.89 1.69 1.95 18.33 11.73 10.43 7.60 12.03
0.75 15.38 19.35 18.14 13.66 16.63 90.30 107.93 182.17 66.83 111.81
Avg. 6.05 7.20 6.81 5.23 6.32 37.37 40.63 65.01 25.26 42.07
400 0.25 0.92 0.85 0.73 0.80 0.83 4.40 3.50 2.00 2.10 3.00
0.5 6.89 5.71 6.55 7.05 6.55 28.90 16.47 19.70 14.17 19.81
0.75 76.65 152.18 100.68 84.23 103.43 161.80 238.77 159.90 130.20 172.67
Avg. 28.15 52.91 35.99 30.69 36.94 65.03 86.24 60.53 48.82 65.16
Avg. All 8.82 15.37 11.06 9.31 11.14 29.89 37.22 37.09 23.39 31.90
Table 1: Performance of the branch-and-cut algorithm with the weak connectivity constraint
Time (s) Number of strong connectivity constraints
n Avg. Avg.
100 0.25 0.05 0.04 0.03 0.03 0.04 1.30 0.80 0.53 0.43 0.77
0.5 0.14 0.13 0.12 0.11 0.13 4.33 3.80 3.67 3.40 3.80
0.75 0.80 0.64 0.47 0.45 0.59 24.57 19.20 13.77 12.73 17.57
Avg. 0.33 0.27 0.21 0.20 0.25 10.07 7.93 5.99 5.52 7.38
200 0.25 0.15 0.12 0.10 0.11 0.12 2.23 1.57 0.93 1.00 1.43
0.5 0.52 0.42 0.54 0.48 0.49 9.23 6.57 7.07 6.13 7.25
0.75 4.33 3.82 3.53 3.24 3.73 73.13 60.67 52.47 50.47 59.18
Avg. 1.67 1.46 1.39 1.28 1.45 28.20 22.93 20.16 19.20 22.62
300 0.25 0.40 0.32 0.35 0.29 0.34 4.27 2.53 2.60 1.43 2.71
0.5 2.16 1.80 1.67 1.86 1.87 18.77 12.43 9.07 8.93 12.30
0.75 24.38 24.41 19.69 15.31 20.95 145.93 162.57 105.07 75.37 122.23
Avg. 8.98 8.85 7.24 5.82 7.72 56.32 59.18 38.91 28.58 45.75
400 0.25 0.76 0.72 0.60 0.58 0.66 4.87 3.43 1.90 2.00 3.05
0.5 6.13 5.92 6.53 5.96 6.14 23.77 18.17 17.53 12.43 17.98
0.75 136.33 151.34 138.03 99.21 131.23 320.63 531.90 460.63 301.40 403.64
Avg. 47.74 52.66 48.39 35.25 46.01 116.42 184.50 160.02 105.28 141.56
Avg. All 14.68 15.81 14.31 10.64 13.86 52.75 68.64 56.27 39.64 54.33
Table 2: Performance of the branch-and-cut algorithm without the weak connectivity constraint

As observed in Table 1, our complete branch-and-cut algorithm appears to be very efficient, with an average CPU time of 11.14 seconds and an average of 31.90 strong connectivity constraints per instance. The smallest instances with 100 islands are solved within a fraction of a second, whereas the largest instances with 400 islands are significantly more difficult and require on average 36.94 seconds and 65.16 cuts. The proportion of double bridges has a significant impact on the puzzle difficulty, as reflected by a significant increase of CPU time and number of cuts when increases.

The results of our branch-and-cut algorithm without the weak connectivity constraint, reported in Table 2, lead to similar conclusions regarding problem difficulty as a function of and . These results also clearly demonstrate the usefulness of the weak connectivity constraint (7). Without it, the average CPU time rises up by 24% and the number of strong connectivity cuts generated through the search rises up by 70%.

Finally, the effect of the parameter  (impacting the number of cycles in the solutions) is less marked. This is likely due to a combination of two effects: on the one hand, increasing leads to solutions with a larger number of edges and makes the puzzle more complicated; on the other hand, higher values help respecting the connectivity constraints, an effect which is especially visible in the results of Table 2, when the weak connectivity constraint is deactivated.

5 Conclusions

We have designed a set of benchmark instances for the Hashi puzzle and proposed a branch-and-cut algorithm. We have conducted sensitivity analyses on the impact of the main instance parameters and features of the solution method. Our experiments demonstrate the good performance of the algorithm, which solves all puzzles with up to 400 islands, and the contribution of the weak connectivity constraint in the search.

Acknowledgments

We thank Vanessa Dias for suggesting this research topic to us. This work was partly supported by the Canadian Natural Sciences and Engineering Research Council (NSERC) under grants 2014-05764 and 2015-01689, as well as CAPES, FAPERJ and CNPq in Brazil. This support is gratefully acknowledged.

References

References

  • Andersson (2009) Andersson, D. 2009. Hashiwokakero is NP-complete. Information Processing Letters 109(19) 1145–1146.
  • Brain et al. (2009) Brain, M., O. Cliffe, M. De Vos. 2009. A pragmatic programmer’s guide to answer set programming. Software Engineering for Answer Set Programming (SEA09). 49–63.
  • Coelho and Laporte (2014) Coelho, L. C., G. Laporte. 2014. A comparison of several enumerative algorithms for Sudoku. Journal of the Operational Research Society 65 1602–1610.
  • Dahl (1987) Dahl, E. D. 1987. Neural network algorithm for an NP-complete problem: map and graph coloring. Proceedings of the IEEE First International Conference on Neural Networks, vol. 3. 113–120.
  • Dantzig et al. (1954) Dantzig, G. B., D. R. Fulkerson, S. M. Johnson. 1954. Solution of a large-scale traveling-salesman problem. Journal of the Operations Research Society of America 2(4) 393–410.
  • Garey et al. (1976) Garey, M. R., D. S. Johnson, R. E. Tarjan. 1976. The planar Hamiltonian circuit problem is NP-complete. SIAM Journal on Computing 5(4) 704–714.
  • Gavril (1977) Gavril, F. 1977. Some NP-complete problems on graphs. Proceedings of the 11th Conference on Information Sciences and Systems. 91–95.
  • Golan (2011) Golan, S. 2011. Minesweeper on graphs. Applied Mathematics and Computation 217(14) 6616–6623.
  • Karp (1972) Karp, R. M. 1972. Reducibility Among Combinatorial Problems. Springer, New York.
  • Lawler et al. (1985) Lawler, E. L., J. K. Lenstra, A. H. G. Rinnooy Kan, D. B. Shmoys. 1985.

    The Traveling Salesman Problem: A Guided Tour of Combinatorial Optimization

    .
    Wiley, Chichester.
  • Malik et al. (2012) Malik, R. F., R. Efendi, A. P. Eriska. 2012. Solving hashiwokakero puzzle game with hashi solving techniques and depth first search. Bulletin of Electrical Engineering and Informatics 1(1) 61–68.
  • Prosser and Unsworth (2006) Prosser, P., C. Unsworth. 2006. Rooted tree and spanning tree constraints. Available online (accessed on June 8, 2013).
  • Rendl and Woeginger (1993) Rendl, F., G. Woeginger. 1993. Reconstructing sets of orthogonal line segments in the plane. Discrete Mathematics 119(1–3) 167–174.