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:
the bridges must begin and end at distinct islands;
they must not cross any other bridges or islands;
they may only run horizontally or vertically;
at most two bridges may connect any pair of islands;
the number of bridges connected to each island must be equal to the number inscribed in the circle;
each island must be reachable from any other island.
Figure 1 depicts a 77 puzzle with islands along with a feasible solution.
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 variablesindicating 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:
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:
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.
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|
|Time (s)||Number of strong connectivity constraints|
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.
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.
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.
- 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.