Nonogram is a typical two-dimensional logical puzzle game in which each pixel involves two constraints associated to the intersecting row and column. The recently proposed approaches can efficiently solve many puzzles via logical deduction based on the 2-SAT formulas. This paper proposes a set of new logical properties for inferencing the consistent and inverse relations among pixels. We show that the pixels with consistent (or inverse) relation must be painted to a same (or an opposite) color in the solution puzzle. Consequently, the pixels can be aggregated into groups, thereby the space of search tree of the Nonogram backtracking algorithm can be reduced.
Nonogram is a typical picture logical puzzle in which the pixels in the grid must be painted to either white or black to reveal a hidden (Simpson; Wikipedia. Nonogram) picture. When solving a puzzle, the player paints each pixel by considering two constraints, called clues, associated to the row and column intersecting on the pixel. For example, Fig. 1 illustrates a Nonogram puzzle of 8 × 8 grid with row clues (and column clues) shown in the right side (or bottom side) of the grid. In a row/column of the grid, a sequence of consecutive black pixels is called a segment. A clue indicating the row/column must be painted as k segments, with length , that are separated by any number of white pixels. For example, in Fig. 1, painting pattern of 7th row satisfies the constraint of clue .
Solving Nonogram puzzles efficiently has been considered challenging (Cohen et al., 2008; Wolter, 2009, 2013). Ueda (Ueda and Nagao, 1996) showed that even determining whether a Nonogram puzzle has a solution is NP-complete. Currently, most efficient Nonogram solver programs (Lin and Wu, 2011; Lin et al., 2011; Liu et al., 2012; Wu, 2012; Sun et al., 2012) are based on work (Wu et al., 2013). The authors in Wu et al. (2013) proposed the FP1, FP2, and FP3 relations to help find more paintings on the grid. The backtracking algorithms incorporating the FP relations has been shown can efficiently solve many puzzles.
A Nonogram puzzle and the solution painting.
In this paper, we propose a new set of logical properties, called GP relations, to inference the consistent and inverse relations among pixels. These new relations are not intended to directly derive the pixel colors. We show that a pair of pixels with consistent (or inverse) relation must be painted in a same (or an opposite) color in the solution puzzle. The pixels with consistent or inverse relations can be aggregated into groups. Thus, the search tree space of the Nonogram backtracking algorithm can be reduced.
The rest of this paper is organized as follows, Section 2 describes the background of the puzzle game. Section 3 discusses the new GP properties. Section IV describes the backtracking algorithm using the GP properties. Finally, experiments are done in Section 5 and concluding remarks are made in Section 6.
Background
Nonogram puzzles
The Nonogram puzzle is a typical puzzle game that consists of a square grid with individual entries, called pixels, denoted by , . In the grid, row y refers to the collection of pixels . Similarly, column x refers to the collection of pixels . A row or a column is also called a line. A line refers to row i if , and refers to column if . The algorithm proposed in this paper can be extended to a rectangular grid with arbitrary height and width straightforwardly.
In a Nonogram puzzle grid, each line is associated with a clue, where . In this paper, we ignore the index and simply write when the context is clear. The goal of the game is to paint all the pixels in a way that satisfies the constraints on all rows and columns as described as follows.
For a puzzle grid, the colors of the pixels must be or u, representing color white, black or unknown respectively. Following (Wu et al., 2013), the patterns of the colors of the pixels of a line are expressed using the regular expression notation. For a line L, the painting of the pixels are represented as a string , , . A sequence of consecutive 1’s is called a segment. For clue , the line L is said solved in terms of D if matches the following pattern (Wu et al., 2013):
In the above pattern, an asterisk sign (or a plus sign) means repeat the preceding character zero or more times (or at least once). Thus, () represents a pattern consisting of a segment with consecutive 1’s that are padded with 0 in the left and right borders. Also, matches either 0 or 1.
A Nonogram puzzle is said solved if every line is solved for the corresponding clue . A grid with all lines solved is called a solution grid and is denoted by . Let denote the color of pixel a in the solution grid . A partially painted puzzle grid is called an incomplete grid. For two puzzle grids G and , the relation holds if is the same as G but with some u changes to on the grid. A grid G satisfying is called a feasible grid, otherwise, conflict grid. For simplicity, this paper assumes that there is only one solution grid for a Nonogram puzzle.
In a Nonogram puzzle, each pixel belongs to two orthogonal lines: row y and column x. Thus, when solving the pixel , it must be painted in a way that both row y and column x match their associated clues. For example, in the puzzle shown in Fig. 1(b), line 3 is painted to 10000111, which satisfies the constraint . In this figure, since the painting of all the lines satisfies the line clues, this painted grid is a solution grid.
Solving Nonogram puzzles
This subsection describes the concept of the Nonogram algorithm (Wu et al., 2013) briefly.
MaxPaint
A MaxPaint procedure finds the maximal painting of the pixels in a single line by given the line clue. Let denote the finite alphabet of painting colors. Let denote the set of all possible strings with length w over . A painting string is said partially painted if it contains some u. A string is said matches another string in the sense that u can match 0 or 1, while character can only match the same character. Formally, matches if and only if for all i.
A sequence of consecutive 1’s in a string is called a segment. Given a line L and its clue . A string is said satisfy the clue D if it consists of a sequence of k segments, with length , that are separated by 0’s. Given a partially painted string , a string is called an assignment of S in terms of D if r matches S and satisfies D. Also, let .
The assignments of clue on string .
Figure 2 shows the set for and . Let us consider the first assignment 0110001110. It contains segments 11 and 111 that are separated by 0’s. Thus it satisfies clue . Clearly, it also matches the partially painted string . Thus, it is a valid assignment in terms of S and D.
For the partially painted string shown in Fig. 2, if the painted positions (i.e. the positions with ) are known to be in the solution, clearly the final solution painting of this line must be one of the assignments in . As Fig. 2 illustrates, if and is a partial solution, the final solution for this line must be one of the listed 6 assignments. On the contrary, if a position is painted to 0 (or 1) for all assignments, then the solution of this position must be 0 (or 1). For example, in Fig. 2, the solution for position 2 must be 1.
Solve pixel b via relation .
In Wu et al. (2013), Batenburg and Kosters (2009), the authors presented the MaxPaint procedure which, by given a clue D and a partially painted string S, evaluates all the assignments and find the maximum painting efficiently by using the dynamic programming method.
Propagation
In a puzzle grid, each pixel is painted by taking into consideration the clues on the two lines crossing . If a pixel is updated from u to after applying MaxPaint on row y, then the orthogonal column x may have chance to solve more pixels. A Propagation procedure consists of a sequence of MaxPaint applied on rows and columns alternatively.
Let a and b a pair of unsolved pixels in an incomplete grid G. If painting pixel a to color in G leads to painting b to color via a sequence of propagation operation, and , we said that there exists a propagation relation between pixel a and b and denote it by .
According to Wu et al. (2013), the propagation relation states that if we find is a solution, and we paint pixel b to color via a sequence of propagation relations connecting a and b, then we can conclude that . That is, the Propagation procedure can solve more pixels for multiple lines for a partially solved grid. Lemma 1 summaries this property.
Let G be a feasible grid satisfying. For a pair of unsolved pixelsin G, ifandthen.
Fully-probing deductions
For many Nonogram puzzle grids, the propagation operation ends with some unsolved pixels. In this case, the authors in Batenburg and Kosters (2009), Wu et al. (2013) suggested the guessing and checking approach, called probing. For a pair of unsolved pixels and in a feasible grid G, the following FP1 property holds (Wu et al., 2013):
FP1: Let ,
Figure 3 illustrates the notion of relation FP1. Relation FP1 holds in the sense that, ’s color must be either 0 or 1, both leading to ’s color equals to 0. Then, we can conclude that color is the solution for pixel b, i.e..
Based on contra-positive logic , we can write the contra-positive FP relation as follows:
FP2: Let ,
FP2 can help solve more pixels as demonstrated in Fig. 4. In Fig. 4(a), we cannot determine the color of pixel or since FP1 does not hold. However, as shown as in Fig. 4(b), if we incorporate the FP2 relation (the dashed edge), we can obtain that , since both and lead to .
(a) A case that FP1 cannot solve pixel a. (b) Use FP1 and FP2 to solve pixel a. (c) Transitive relation of FP2.
Group based probing
This section describes the GP relations (i.e. group based probing relations). Unlike FP relation, which deduces the colors of the pixels, a GP relation deduces the relation between the colors the pixels.
Let , denotes the reverse color of (i.e. and ). For a given feasible grid G and two unsolved pixels , the GP1 relation inferences that whether a and b must be painted consistently (i.e. using a same color):
GP1
Similarly, the inverse relation inferences whether two pixels must be painted oppositely (i.e. using different colors):
GP2
Fig. 5 illustrates GP1, GP2 and their transitive extension.
(a) Relation GP1, (b) Relation GP2, and (c) Transitive GP relation from a to c.
For a feasible grid G, both the properties GP1 and GP2 hold.
Assume that is a grid satisfying , i.e. is the same as G but with some u turns to . Let denote the grid G in which the color of pixel a is equal to . According to Wu et al. (2013), if the propagation operation paints some colors in a board , the propagation operation must also paint the same colors in both of board and board . Thus, the following property holds:
Property Q1: the condition holds for all satisfying .
(Proof of GP1) From Property Q1, if then for all , . Thus, the pixels are painted to a same color for all satisfying .
(Proof of GP2) Analogously, from Property Q1, if then for all , . Thus, the pixels are painted to a different color for all satisfying .
Maintaining groups in backtracking.
Note that in the above, the solution grid also satisfies the property , which concludes that the pixel are painted consistently (GP1) or inversely (GP2) in the solution grid. □
Group based backtracking
This section discusses using relation GP1 and GP2 in Nonogram backtracking algorithm.
Pixel groups
A set of pixels S is said consistent if all the pairs of pixels in S satisfy relation GP1. Clearly, relation GP1 forms a class of pixels in a grid. The GP2 relations between pixels is represented as a pair of sets of pixels. A pixel group (or simply group) consists of two sets of pixels, denoted by , in which both S and T are consistent, and each pair of and satisfy relation GP2.
From above, for a pixel group , all the pixels in set S (and also T) must be painted using a same color, but S and T must be painted in opposite colors. Note that if is a pixel group, so does . That is, it can be color 0 for S and color 1 for T, or vice versa.
Backtracking algorithm
Our group based backtracking algorithm is described in Algorithm 1. Given a partially solved grid G, the algorithm traverses a binary search tree recursively, starting from G, in depth first order. At each tree node (i.e. a painting state of the puzzle grid), it selects an unsolved pixel a and generates two branches for and . For each branch, since there is one more pixel a gets painted, the algorithm tries to paint more pixels by using FP relations. A key element in the success of the backtracking algorithm comes from solving more pixels in each tree node to reduce the scale of the search tree.
Algorithm 1 enhances the performance by aggregating the pixels into groups instead of solving pixels individually. Assume that the backtracking algorithm is processing a current tree node G. In Algorithm 1, line (3)–(5), it selects an unpainted pixel and generates two tree nodes and . Since and , all the propagation relations in G remain exist in and . Therefore, all the GP relations in G remain exist in and . In Algorithm 1, line (4)(b), it derives new GP relations in . Then, it merges the new relations with the old relations (those from G) to construct the new groups.
Grouped-Based Backtracking
Figure 6 illustrates an example of maintaining groups in backtracking. In tree node , there are two groups 1 and 2, each consists of an S-part (white box) and a T-part (black box). In tree node , group 1 and 2 are merged into group 3. In tree node , group 3 is unchanged and an additional group 4 is created.
Experiments and discussions
We implemented the algorithm and performed tests on the TCGA 2017 Nonogram contest dataset (TCGA Computer Game Tournaments, 2017) which contains 1000 puzzles with uniform distribution on difficulties. In the experimental results, among 1000 puzzles, there are about 50 puzzles contains groups. The group number is usually small, such as range from 2 to 5. Also, in average, the group size ranges from 2–30 pixels.
The experimental results show that the GP backtracking method may not improve the execution time in an order of magnitude. However, in some special puzzle, the GP backtracking may reduce the tree search space significantly.
To summarize, we analysis the costs of using GP relations as follows:
Since both GP and FP relations are defined on the propagation relation, detecting GP1 and GP2 relations do not impose additional propagation costs when compared to the FP relations.
The cost of merging and querying groups can exceeds the cost of painting pixels individually. To overcome the overhead, we suggest only to maintain groups with size greater than 7.
Conclusions
This paper proposes the GP relations to inference the consistent and inverse relations among pixels. In the proposed backtracking algorithm, the pixels with consistent or inverse relations can be aggregated into groups. Thus, the search tree space of the backtracking algorithm can be reduced.
Cohen, D., Jeavons, P. & Gyssens, M. (2008). A unified theory of structural tractability for constraint satisfaction problems. Journal of Computer System Sciences, 74(5), 721–743.
Lin, H.-H. & Wu, I.-C. (2011). An efficient approach to solving the minimum su oku problem. ICGA Journal, 34(4), 191–208. doi:10.3233/ICG-2011-34403.
5.
Liu, T.-Y., Wu, I.-C. & Sun, D.-J. (2012). Solving the Slitherlink Problem. In Proceedings of the 2012 Conference on Technologies and Applications of Artificial Intelligence (TAAI 2012), November 16–18, 2012 (pp. 284–289).
Ueda, N. & Nagao, T. (1996). NP-completeness Results for Nonogram via Parsimonious Reductions. TR96-0008, Technical Report, Department of Computer Science, Tokyo Institute of Technology.