Nonograms: Combinatorial questions and algorithms

Nonograms: Combinatorial questions and algorithms

Discrete Applied Mathematics 169 (2014) 30–42 Contents lists available at ScienceDirect Discrete Applied Mathematics journal homepage: www.elsevier...

593KB Sizes 4 Downloads 160 Views

Discrete Applied Mathematics 169 (2014) 30–42

Contents lists available at ScienceDirect

Discrete Applied Mathematics journal homepage: www.elsevier.com/locate/dam

Nonograms: Combinatorial questions and algorithms Daniel Berend a,b , Dolev Pomeranz b,∗ , Ronen Rabani b , Ben Raziel b a

Department of Mathematics, Ben-Gurion University of the Negev, Beer Sheva 84105, Israel

b

Department of Computer Science, Ben-Gurion University of the Negev, Beer Sheva 84105, Israel

article

info

Article history: Received 12 January 2012 Received in revised form 5 January 2014 Accepted 8 January 2014 Available online 28 January 2014

abstract The Nonograms puzzle, also known as Paint by Numbers, is a Japanese logic puzzle. It has been shown that the general problem of solving it is NP-hard. In this paper, we answer several theoretical questions related to the puzzle. In addition, we present a new solver and compare its performance with that of existing solvers. © 2014 Elsevier B.V. All rights reserved.

Keywords: Nonogram Paint-by-numbers Logic puzzles Solver Number of solutions

1. Introduction In this section we first introduce the puzzle, and then discuss several approaches employed in order to solve it. In addition, we briefly mention the solver developed by us, as well as several mathematical questions of interest regarding the puzzle. 1.1. Rules of the game Nonograms are usually all about discovering a concealed image. Say we have an image represented by an m × n grid, each cell of which may be either black or white. Each line (i.e., row or column) may be described by an array of numbers, each of which represents the size of a consecutive block of black cells. Notice that between each two adjacent blocks there must be at least one white cell. There may also be white cells to the left of the leftmost block and to the right of the rightmost block. The order of elements in the array is according to the blocks’ order of appearance in the line (for rows left-to-right and for columns top-to-bottom). These arrays are also known as descriptions. When given an empty grid (i.e., the cells’ colours are unknown) and its descriptions, our goal is to reconstruct the original image. One method of solving a nonogram puzzle is to process a single line at a time. The idea is to find cells that have the same colour in every colouring of the line satisfying the given description. In this manner, we can derive with absolute certainty the colour of those cells. For example, in Fig. 1(a) we see an empty grid and the descriptions of the lines within it. The first row description implies that the row contains a block of three consecutive black cells. There are three possible colourings for this row. In each of these colourings the third cell is black, and therefore the colour of this cell must be black. As we continue this process, more information is extracted from the lines. The final result, as seen in Fig. 1(b), is the puzzle’s only solution. A white cell is represented by a minus sign. Even though the vast majority of puzzles intended for humans have a unique solution, puzzles with several solutions do exist. The puzzle in Fig. 2 is the simplest puzzle with two solutions. This structure, and similar larger ones, often appear



Corresponding author. Fax: +972 8 6477650. E-mail address: [email protected] (D. Pomeranz).

0166-218X/$ – see front matter © 2014 Elsevier B.V. All rights reserved. http://dx.doi.org/10.1016/j.dam.2014.01.004

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

(a) The empty puzzle.

31

(b) The puzzle’s unique solution. Fig. 1. An example of a 3 × 5 puzzle, and its unique solution.

(a) First solution.

(b) Second solution.

Fig. 2. A 2 × 2 puzzle with two solutions.

in puzzles, and are a simple cause for multiple solutions. Batenburg and Kosters [3] refer to it as an elementary switching component. Obviously, puzzles may well have no solutions. Usually, the grid is coloured only in black and white. Nevertheless, nonograms with more than two colours exist [12]. In this paper we deal only with black and white puzzles. 1.2. Solving the nonogram problem Nonograms form a well-known type of puzzle nowadays. Even though puzzles published in newspapers are solvable by hand, nonograms can offer a greater challenge. This is implied by the fact that the nonogram problem is known to be NPcomplete [23]. Furthermore, Batenburg et al. [1] defined a difficulty measure on a subclass of nonograms. This metric indicates how difficult it is to solve a certain puzzle. The problem of generating uniquely solvable puzzles is addressed in [1,14]. A solver is a program that generates a solution when given a puzzle (or ascertains that no solution exists). Solvers may differ not only by implementation, but by other attributes. For example, some solvers can only solve puzzles with a unique solution while others can handle multiple solutions. When dealing with multiple solutions, some solvers can produce only one solution while others can find them all. A heuristic solver that operates on puzzles with a unique solution is presented in [17]. In addition, a reasoning framework for solving those puzzles is proposed in [3]. Some solvers use evolutionary algorithms [2,24]. These solvers can operate on puzzles with multiple solutions, but can find only one solution. Still other solvers convert the problem to an instance of a Constraint Satisfaction Problem (CSP) such as integer programming [7,12] or SAT [3,11]. Additional solvers can also be found in the internet [19,25]. Nonogram applications are not confined to educational/recreational use [18,22]. They can be found in fields such as pattern recognition, where they are used to recognize printed letters [21] and sentences [20]. (See also [2,3] for a connection with discrete tomography.) 1.3. Theoretical and algorithmic questions This paper started from trying to write a good solver for nonograms. For example, as hinted above, a basic task a solver may do is extract all possible information by considering only lines. Our algorithm for that step performs better than others accomplishing the same task (see Theorem 1). Employing our line solver, along with other improvements, we have developed a solver that seems to outperform most other solvers (see Tables 3 and 4 in Section 4). Altogether, our solver is able to solve (within 2 min on a 2.6 GHz AMD Phenom II X4 810 quad-core 64-bit processor with 8 GB RAM [25]) the vast majority of 30 × 30 randomly generated puzzles, a majority of 40 × 40 puzzles, but only a minority of 45 × 45 puzzles. We also develop an efficient algorithm for solving puzzles on narrow grids.

32

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

When developing a solver, one is naturally confronted with combinatorial and probabilistic questions. For example: 1. How many cells in a typical m × n grid will the line solver be able to find the colour of? 2. How many solutions does a random puzzle have? One can ask other combinatorial questions about nonograms. For example, Benton et al. [5] raised the question of the number of distinct m × n puzzles. In their paper they dealt with the related question of the numbers of possible descriptions if one records for each row and column only the number of black cells, and not how they are divided between blocks; see also Goldstein and Stong [10]. In Section 2 we deal with algorithmic and theoretical aspects of line solving. Section 3 discusses grids of constant width and the typical number of solutions of a grid. Section 4 surveys our solver and its performance. 2. Solving a single line The most obvious approach to a puzzle is by considering each row (and column) in turn, trying to get as much information on it as possible. In this section we study theoretical and algorithmic issues involved with this phase of the solution. 2.1. The main results As mentioned earlier, many solvers start by extracting as much information as possible by considering first of all each line separately. Thus, in this section we deal with this task. A line solver is an algorithm that solves a single line. Given a line and its description, the goal of the solver is to find all cells that have the same colour in each of the line’s solutions or to discover that the description admits no solution. Even though there may be an exponential number of solutions, this problem is solvable in polynomial time in the length n of the line. In fact, in [3] an algorithm with a runtime of O(kn2 ) is provided for this, where k is the number of blocks of black cells. (Usually, this means O(n3 ) time.) In this section we design an algorithm with a runtime of O(kn) for the same purpose. Line solvers are actually the basic tool for solving general m × n puzzles. In fact, it is natural, when given a puzzle, to look first for the information we can derive by considering each row and column separately. Thus, the first step would be to go over all rows and columns, and mark all entries whose colour can be deduced merely from the hints at that row or column. Note that a line solver needs to deal also with lines, some of whose cells are constrained to have a certain colour. For example, suppose we have a row i to which we have already applied the line solver and obtained some information (or not). Later we apply the line solver to some column j and find that the ith cell of this column must be of some colour. It is quite possible that this new bit of information on row i will enable us to obtain new information on row i if we apply the line solver to it again. (This is the case, for example, already in the puzzle in Fig. 1.) Thus, in the following, all our results are to be understood as applying to partially coloured lines. When we consider the results of applying a line solver to an m × n puzzle, we mean that a line solver is applied again and again to the rows and columns. We maintain a list of rows and columns to which we need to apply the solver. In the beginning, this list consists of all rows and columns. Each time, after the solver is applied to a row or a column, that line is taken off the list. However, each time we manage to find the colour of another cell in a row (column, resp.), we add to the list the column (row, resp.) that cell belongs to. Thus, after a line solver is applied to a puzzle, there remain no cells in the puzzle whose colour can be found by considering only the row or the column that cell belongs to. Formally, each of the line’s cells is coloured by one of the following: white (0), black (1) or unknown (2). The ‘‘unknown’’ relates to our state of knowledge at a certain point. In the beginning all cells are such, and our task is to change all cells that must be coloured in some way to ‘‘white’’ or ‘‘black’’. Thus, each line of size n has 3n possible colourings. We shall refer to such a colouring as a partial colouring. Our line solver is given by Algorithm 1. Algorithm 1 lineSolver() 1: 2: 3:

initialize sol initialize solvedLine return solve(n, k)

Theorem 1. Given a partially coloured line of length n and a corresponding description d, Algorithm 1: 1. finds the number of solutions (perhaps 0) of the line; 2. finds all cells having the same colour in each solution of the line (and the colour of each of these cells). The algorithm runs in O(n2 ) time. Sections 2.2–2.5 are devoted to proving the theorem. In fact, as mentioned above, our algorithm works in O(kn) time, where k is the number of blocks of black cells. Obtaining an O(kn2 ) time algorithm by dynamic programming, same as the result obtained in [3], is relatively simple. In Remark 3 below we will highlight the improvement in our algorithm that allows us to go down from O(kn2 ) to O(kn). How many cells are we going to know the colour of employing only the line solver? It may well be the case that the colour of very few cells will be determined, even if the puzzle is small and uniquely solvable.

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

(a) The empty puzzle.

33

(b) The puzzle’s unique solution.

Fig. 3. A uniquely solvable 2 × 4 puzzle that is not solvable using only a line solver.

Example 1. One readily verifies that the only solution of the puzzle in Fig. 3(a) is seen in Fig. 3(b), yet a line solver will be unable to determine the colour of even a single cell. Our interest is to answer the question for random puzzles. To this end, we first need to define what we mean by a random puzzle. Our interpretation is that we start with an uncoloured grid, and colour each cell in white or black with probabilities of 1/2 each, distinct cells being independent. (One may also consider other probabilities p of a cell being black, not necessarily p = 1/2, as done in [3]. We take only p = 1/2 as it corresponds to giving all 2mn colouring of an m × n grid the same ‘‘weight’’; see Theorems 2 and 3 below.) Note that this is very different from taking all puzzle descriptions and picking one of them uniformly at random. (The latter possibility is more in line with Mullen’s interpretation of randomness for single lines [13].) To emphasize the difference, we shall henceforth refer to our version of a random puzzle as a randomly generated puzzle. (Namely, the grid is coloured uniformly at random, but some puzzles have a much larger probability of being obtained than others.) Theorem 2. Let C be the number of cells of an n × n randomly generated puzzle whose colour is determined by a line solver. Then P (C = 0) −−−→ 1 exponentially fast. n→∞

The theorem is proven in Section 2.6. Remark 1. In view of Mullen’s result [13, Theorem 3.2], one may guess that the situation is drastically different if we take his definition of a random puzzle, detailed prior to Theorem 2. That is, with that definition, C is probably usually very close to n2 , or even exactly n2 . Remark 2. The theorem does not hold for general rectangular grids with size going to ∞. For example, if n is much larger than 2m , then with high probability some of the columns of the grid will be monochromatic, and their colour will be uniquely determined by the description vector. In view of Theorem 2 one may ask whether there is any efficient method which lets one determine uniquely the colour of some cells for a randomly generated puzzle. In fact, in view of Theorem 4, which asserts that such a puzzle typically has numerous solutions, one might suspect that usually the colour of no cell is uniquely determined. The following result shows that this is not the case. Theorem 3. For sufficiently large m and n, a randomly generated m × n puzzle has, with probability at least 1/6, at least one cell whose colour is uniquely determined. Moreover, one can find such a cell (with probability at least 1/6) in constant time. The theorem is proven in Section 2.7. We note in passing that the theorem is proved by showing that the colour of corner cells can often be found easily, but the same holds also for cells close to a corner. 2.2. A recursive formula for counting the solutions In this subsection we detail the methods in Algorithm 1. As mentioned before, each description is represented by an array of numbers. We will denote by k the length of the array. Given a description d of size k for a single line, d[j] will represent the jth (1 ≤ j ≤ k) entry in d. For a given entry j in d (indicating a block of size d[j] of black cells), the function 1>1 (j) returns 1 if to the left of block j there must be at least one white cell. Otherwise, the function returns 0. For an index i of a cell (1 ≤ i ≤ n) and an index j of the description array (1 ≤ j ≤ k), denote by S (i, j) the number of colourings of the first i cells of the line, consistent with a description vector consisting of the first j entries of d. We claim that S (i, j) = S (i − 1, j) + S (i − d[j] − 1>1 (j), j − 1), where the initial values are: S (i, j) =



1, 0,

i = j = 0, i < 0 or j < 0.

(1)

34

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

In fact, the first term on the right-hand side of (1) is the number of colourings in which the ith cell is coloured white, while the second is the number of those in which it is black. Clearly, the first part of Theorem 1 requires to compute only S (n, k). However, we need other values of S to use (1). 2.3. Trivial contradictions to the partial colouring Suppose we try to place the jth block so that it will end at the ith cell. Algorithm 2 checks whether this does not cause an obvious conflict with the colouring of those cells coloured already. Algorithm 2 canPlaceBlock(i, j) 1: 2: 3: 4: 5: 6: 7: 8: 9:

for (m = i; m > i − d[j]; m − −) do if cell m is coloured white in the partial colouring then return false end if end for if 1>1 (j) = 1 and cell i − d[j] is coloured black in the partial colouring then return false end if return true

2.4. Finding the cells’ colours During the calculation of the functions, we can conclude which cells have the same colour in all solutions. For this purpose an array solvedLine of size n is used. The array is initialized with the value −1 in all cells except for the known cells (i.e., black or white cells in the partial colouring). The solvedLine array is updated by Algorithm updateCellColour(i, colour) (Algorithm 3). Algorithm 3 updateCellColour(i, colour) 1: 2: 3: 4: 5: 6: 7:

if solvedLine[i] = −1 then solvedLine[i] ← colour // First time a colour is set to cell i else if solvedLine[i] ̸= colour then solvedLine[i] ← 2 // A conflict — both colours are possible else return // No update, as it has been known already that cell i can be coloured by colour end if

Updating an entire block is the responsibility of the updateBlockColour(i, j) algorithm (not shown here). It uses the updateCellColour algorithm to update all d[j] cells of block j in black. In addition, it uses the 1>1 (j) function to determine whether the colour of the cell before block j should be updated to white. 2.5. The line solver algorithm We obtain the solve(i, j) algorithm by combining the algorithms in the previous subsections. It uses an n × k matrix sol to store each of the algorithm’s results for a given pair (i, j). At the end of the process sol[i, j] will be the number of colourings of the first i cells containing (exactly) the first j blocks of black cells. In fact, as we really need only sol[n, k], the value of sol[i, j] will be calculated only for pairs (i, j) that are important for our purposes. The initial value −1 of sol[i, j] indicates that the number of solutions in question has not been calculated yet. The line solver algorithm (Algorithm 1) is responsible for initializing the sol matrix and the solvedLine array. After that, it initiates solve(n, k). To conclude the proof of Theorem 1 we need to analyse the performance of Algorithm 1. The complexity of both canPlaceBlock and updateBlockColour is O(d[j]). Each of these algorithms is activated only once for a given entry in the sol matrix. k Since j=1 d[j] ≤ n, for each of the matrix lines the combined complexity is O(n). The matrix has n lines and thus the total complexity of those two algorithms is O(n2 ). The solve algorithm calculates an entry in O(1) time when disregarding the complexity of canPlaceBlock and updateBlockColour. It is bounded by the number kn of entries, and thus the line solver’s complexity is O(kn) = O(n2 ).

Remark 3. The improvement in our algorithm that allows us to go down from O(kn2 ) time, obtained in [3], to O(kn) time lies in the additional data structure, the solvedLine array. While calculating the value in each of the sol matrix entries, we actually realize which of the possible colourings could be applied to any given cell (i.e., if there exists a solution in which the current cell is coloured in a certain colour). Storing this information in the solvedLine array is done by Algorithm 3.

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

35

Algorithm 4 solve(i, j) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:

if i < 0 or j < 0 then return 0 else if i = j = 0 then return 1 end if if sol[i, j] ̸= −1 then return sol[i, j] else sol[i, j] ← 0 if solve(i − 1, j) > 0 and cell i is not coloured black in the partial colouring then updateCellColour(i, w hite) sol[i, j] ← sol[i, j] + solve(i − 1, j) end if if solve(i − d[j] − 1>1 (j), j − 1) > 0 and canPlaceBlock(i, j) then updateBlockColour(i, j) sol[i, j] ← sol[i, j] + solve(i − d[j] − 1>1 (j), j − 1) end if end if

Updating the solvedLine array with this information while calculating the sol matrix entries saves us from extracting the same information out of the calculated sol matrix. If we extracted this information from the calculated sol matrix, we would have ended up with an O(kn2 ) time algorithm. 2.6. The line solver’s effectiveness In this subsection we prove Theorem 2. Denote by X , Yb and M the number of black cells, the number of black blocks and the length of the maximal black block, respectively, in a randomly generated line of length n. Clearly, we have X ∼ B(n, 1/2), where B denotes the binomial distribution. It follows from Chernoff’s inequality that P (X > (1/2 + δ)n) < exp −2δ 2 n .





(2)

1 Now Y − ≤ Yb ≤ Y +2 1 , where Y is the total number of blocks, both black and white, in our random line. Since at the first 2 cell we certainly start a new block, and each of the other n − 1 cells starts a new block with probability 1/2, independently of all other cells, Y − 1 ∼ B(n − 1, 1/2), so that, again by Chernoff’s inequality:

P (Yb > (1/4 + ε)n) < P (Y > 2(1/4 + ε)n − 1)

= P (Y − 1 > (1/2 + 2ε)n − 2) < P (Y − 1 > (1/2 + 2ε − 2/(n − 1))(n − 1))   < exp −8ε 2 (n − 1) + 16ε .

(3)

For M to be at least some number, there must be a block of black cells of that length starting at some point. Hence for θ > 0: P (M ≥ θ n) ≤ n2−θ n .

(4)

It is easy to see that a description determines the colour of at least one cell if and only if either it is empty (in which case the whole line is white) or X + Yb − 1 + M > n (see [13]). Hence, denoting by L the number of cells whose colour is determined by a line solver for a randomly generated line of length L, we have P (L > 0) = 1/2n + P (X + Yb + M > n + 1)

≤ 1/2n + P (X > (1/2 + δ)n) + P (Yb > (1/4 + ε)n) + P (M ≥ θ n), for any δ, ε, θ > 0 with δ + ε + θ ≤ 1/4. By (2)–(4): P (L > 0) ≤ 1/2n + e−2δ

2n

+ e−8ε

2 (n−1)+16ε

+ n2−θ n .

Now C > 0 if and only if the line solver determines the colour of some cell in at least one of the rows or columns of the grid. Hence P (C > 0) ≤ 2nP (L > 0). Thus, P (C > 0) decays to 0 exponentially as n → ∞.

36

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

Remark 4. To get an explicit bound, one can take in the proof

ε=

−3 +



9 + 8/ ln 2

16

· ln 2,

δ = 2ε,

θ = 8ε 2 / ln 2

(which yields almost the best bound that can be obtained by means of our proof). 2.7. Finding the colour of corner cells In this section we prove Theorem 3, namely that for any board size it is often the case that the colour of several cells is uniquely determined by the hints. Denote by A1 the event that the description vectors satisfy the following constraints: dr1 [1] ≥ 2,

dr2 [1] ≥ 2,

dc1 [1] ≥ 2,

dc2 [1] = 1.

Suppose that A1 occurs. We claim that the cell at (1, 1) is coloured white. In fact, if it is black, then the inequalities dr1 [1] ≥ 2 and dc1 [1] ≥ 2 imply that the cells at (1, 2) and (2, 1), respectively, are also black. Now the condition dr2 [1] ≥ 2 implies that the cell (2, 2) is black, whereas the condition dc2 [1] = 1 implies that it is white. The contradiction implies that (1, 1) is indeed white. Note that the argument above shows also that at least one of the cells (1, 2) and (2, 1) is coloured white. From the discussion above, it is clear that, if A1 occurs, then we can deduce that the cell (1, 1) is coloured white in O(1) time. By finding the probability of A1 , we can prove the theorem, but with a probability less than claimed. To arrive at a larger event with the same property, we note that there are analogous events with similar properties. First, let A2 be the event analogous to A1 , obtained from it by exchanging the axes. Namely, A2 is defined by the constraints: dr1 [1] ≥ 2,

dr2 [1] = 1,

dc1 [1] ≥ 2,

dc2 [1] ≥ 2.

Now A1 and A2 relate basically to the picture we have near the top left corner of the puzzle. We have similar events associated with the three other corners. Namely, there are six events A3 , A4 , . . . , A8 , defined similarly to A1 and A2 , with the following changes: (a) Replace the first two rows by the last two rows and the first entry of the column description vectors by the last entry. (b) Same as (a), interchanging rows and columns. (c) Both changes described in (a) and (b). (Thus, A3 and A4 relate basically to the picture we have near the top right corner, A5 and A6 —near the bottom left corner, and A7 and A8 —near the bottom right corner.) Obviously, under the event ∪8i=1 Ai , we can find in O(1) time that one of the corners of the board is necessarily coloured white. To conclude the proof, we just need to estimate the probability of this event. We start by calculating the probabilities of the events Ai . Obviously, these probabilities are in principle all equal, except that four of them have indeed the same probabilities while the other four are obtained by switching the roles of m and n. Thus, we shall deal only with A1 . The event A1 may be decomposed into a union of three pairwise disjoint sub-events, defined as follows: (a) A1bw —the cell (1, 2) is coloured black and (2, 1) white. (b) A1wb —the cell (1, 2) is coloured white and (2, 1) black. (c) A1ww —both cells (1, 2) and (2, 1) are coloured white. If either one of the first two of these events occurs, then the colour of (2, 2) is uniquely determined—under the first it is white and under the second black. However, the third splits further into a union of two disjoint sub-events: (a) A1wwb —the cell (2, 2) is coloured black. (b) A1www —the cell (2, 2) is coloured white. Consider the event A1bw . We know that cells (1, 1) and (2, 1) are coloured white, while (1, 2) is coloured black. The constraint dr1 [1] ≥ 2 now implies that (1, 3) is coloured black, the constraint dc2 [1] = 1—that (2, 2) is coloured white, the constraint dc1 [1] ≥ 2—that the first run of black cells in column 1 is of length at least 2, and the constraint dr2 [1] ≥ 2—the analogous property for row 2. It follows that, out of the 22m+2n−4 possible colourings of the first two rows and columns, there are exactly 2n−3 · (2n−3 − 1) · (2m−3 − 1) · 2m−2 that are in agreement with A1bw . Hence: P (A1bw ) = 2−7 (1 − 1/2m−3 )(1 − 1/2n−3 ). Similarly: P (A1wb ) = 2−7 (1 − 1/2n−3 ), P (A1wwb ) = 2−8 (1 − 1/2m−3 )(1 − 1/2n−3 ), P (A1www ) = 2−8 (1 − 1/2m−3 )(1 − 1/2n−3 )2 . Thus, P (A1 ) −→ 3 · 2−7 . The event A2 is obviously disjoint from A1 , so that for large m and n we have P (A1 ∪ A2 ) ≈ 3 · 2−6 . m,n→∞

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

37

Fig. 4. An isolated switching component.

Now we claim that the four events A1 ∪ A2 , A3 ∪ A4 , A5 ∪ A6 , and A7 ∪ A8 are asymptotically independent. In other words, the difference between the probability of the intersection of some of these events and the product of the probabilities of the same events vanishes as m, n → ∞. It suffices to prove this claim for the sub-events Aibw , Aiwb , and so forth. We exemplify it on the intersection A1bw ∩ A3bw . For a colouring to belong to this intersection, it has to satisfy the following: (a) Cells (1, 1) and (1, m) are white, cells (1, 2), (1, 3), (1, n − 2), (1, n − 1) are black. (b) Cells (2, 1), (2, 2), (2, n − 1), (2, n) are all white. Also, both the first and the last blocks on row 2 must be of length at least 2. (Note that these blocks may coincide.) (c) The first block on column 1 must be of length at least 2. (d) The first block on column n must be of length at least 2.

Counting the admissible colourings of row 2 by splitting them into sets of colourings depending on the initial cell of the first block and the initial cell of the last block, we see that there are 2n−6 + n − 6 such colourings. The calculations regarding row 1 and columns 1, 2, n − 1, n are similar to the calculations above for A1bw , and altogether we obtain: P (A1bw ∩ A3bw ) = 2−14 (1 − 1/2m−3 )2 (1 + (n − 6)/2n−6 ). Thus, both P (A1bw ∩ A3bw ) and P (A1bw )P (A3bw ) are close to 2−14 for large m, n. Similarly we deal with the other relevant intersections. It follows that for large m, n we have: P (∪8i=1 Ai ) ≈ 1 − (1 − 3/26 )4 ≈ 0.175. This proves the theorem. 3. The number of solutions of a puzzle Puzzles appearing in newspapers usually have a unique solution. In this section we show that most randomly generated puzzles have numerous solutions. We also deal with counting the solutions of a given puzzle, and show that it can be done in polynomial time for grids of bounded width. 3.1. The number of solutions of a random puzzle We use the definition of a randomly generated puzzle from Section 2.6. Theorem 4. There exist effective positive constants δ, ε > 0 such that the probability of a randomly generated m × n puzzle (m, n ≥ 2) to have less than 2δ mn solutions is less than 2−εmn . As we shall see in the proof, the theorem is valid with the choices δ = 2−18 , ε = 2−20 . Proof. First we define an isolated switching component. This is a 4 × 4 component whose 4 central cells comprise an elementary switching component, its four corners are coloured arbitrarily and its other 8 cells are all white (see Fig. 4). The proof will be based on the observation that each isolated switching component yields locally two solutions, independently of the rest of the grid. non-overlapping 4 × 4 squares. Let X be Assume first that both m and n are divisible by 4. Divide the grid into W = mn 16 the number of these squares forming isolated switching components for a random grid, and note that the resulting puzzle will have at least 2X solutions. Clearly, X ∼ B(W , 2−11 ). Recall that, in general, if Z ∼ B(N , p) and k < E (X ), it follows from Chernoff’s inequality that



P (Z < k) < exp −

1 (Np − k)2 2p

N

 .

(5)

Take, say, k = 2−12 W . By (5) 1 (2−11 W − 2−12 W )2 P (X < 2−12 W ) < exp − −10 2 W





  = exp −2−18 mn ,

which proves the theorem in this case with δ = 2−16 , ε = 2−18 .

38

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

If the width of the grid is 2 or 3, then the theorem is proven directly with even larger δ and ε . It remains to deal with the case of arbitrary m, n ≥ 4. In this case we follow the first part of the proof with a sub-grid of size 4⌊m/4⌋ × 4⌊n/4⌋ (say, the top left sub-grid of this size). Put W = ⌊m  /4⌋ · ⌊n/4⌋. As above, the probability of having less than 2−12 W isolated switching components is bounded above by exp −2−14 W . Now W ≥ m/7 · n/7 > mn/26 , which proves the theorem with δ = 2−18 , ε = 2−20 .



The following examples show that, in some cases, the number of solutions might be enormous. n/4

   Example 2. Consider a 2 × n puzzle (with 4|n for simplicity), in which the description vector of each row is (1, 1, . . . , 1), while the column descriptions are alternating; the odd columns contain one black cell each and the even columns contain none. To define a solution, we need to colour black n/4 out of the n/2 cells at the odd columns along the first row (and the cells at all other n/4 odd columns along the second row). Hence the number of solutions is



n/2

2n/2+1



≈ √

n/4

πn

. n/ 4







Example 3. Consider an n × n puzzle with 4|n. Let the description vector of all rows and columns be the same—(1, 1, . . . , 1). Consider only solutions for which all cells (i, j) with i + j odd are white. For the purposes of this example, refer to such solutions as sub-periodic. Now consider two subgrids of our grid: E—comprising all cells both of whose coordinates are even, and O—comprising all cells both of whose coordinates are odd. A colouring of the grid is a sub-periodic solution of our puzzle if and only if in each line of E and each line of O it has exactly n/4 black cells, while each cell outside E ∪ A is white. By Canfield and McKay [9], the number of such colourings of E (as well as of O) is

 

n/2 n/4

n 

n/2 − 1

(n/2−1)/2

n/2

(n/2) (n/4)2

 2

√ (2n/2+1 / π n)n 1 2  ≈ √ ≈ 2n /4 , 2 +1 ( n / 2 ) 2 e 2 / π n /2

2 2 2 where the approximations are up to a factor of (1 + o(1))n . Hence the number of solutions of our puzzle is 2n /2−o(n ) .

There are puzzles with even more solutions.



2



Example 4. The preceding example provides n × n puzzles with over ( 2 − ε)n solutions. Here we show that the base 2 can be further increased. In fact, consider all puzzles in which all description vectors, for both rows and columns, are of the form (1, 1, . . . , 1) (where the number of 1’s may be any integer between 0 and ⌈n/2⌉ + 1, not necessarily the same for all rows and columns). Thus, the number of these puzzles is trivially at most (⌈n/2⌉ + 1)2n−1 . Now the set of solutions of all these puzzles combined is the set of all binary matrices having no two adjacent ones either horizontally or vertically. It is 2

2

known that, for every ε > 0, this number lies between (κ − ε)n and (κ + ε)n for sufficiently large n, where κ = 1.503 . . . is the so-called hard square entropy constant (see [4,8, OEIS sequence A006506]). It follows that there exists an n × n puzzle 2

with over (κ − ε)n solutions. 3.2. Finding the number of solutions of a given c × n puzzle 3.2.1. The main result In this section we introduce a method for computing the number of solutions of a given puzzle. The method is based on dynamic programming and may be roughly viewed as a generalization of the line solver’s algorithm, presented in Section 2. We deal with c × n puzzles, where c is a constant value, in which case the algorithm works polynomially. Theorem 5. There exists an O(nc ) time and space algorithm which, given a c × n puzzle, finds its number of solutions (perhaps 0). Remark 5. In fact, it is not essential here that the number of rows be fixed. For m and n we similarly obtain an O(nm ) time algorithm (which is not bad, considering that a brute force algorithm should take at least Ω (2mn ) time, but would still be impractical in terms of time and space requirements). We have formulated the theorem this way, as in the case of constant m our algorithm is polynomial. The rest of this section is devoted to proving the theorem. We note that from the proof it is clear that one can similarly find the actual solutions of the puzzle; each solution would require additional O(nc ) time. 3.2.2. The concept of a role Given a cell in some row, we will need to know what role it plays there. More precisely, if it is a black cell, we would like to know which block it belongs to and what is its location within that block (say, the 15th cell of the 23rd block); if it is a white

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

39

cell, we want to know between which two consecutive blocks of black cells it lies (including the cases where it is before the first black block or after the last such block). Thus, if the description d of row i is of length k, then the number of roles in this k row is j=1 d[j] + k + 1 (although it may be the case that there are no cells with the role of a white cell preceding the first black block or of a white cell after the last such block). Note that all white cells between a pair of consecutive black blocks have the same role. The roles will be denoted by numbers from 1 on. For example, the role of a white cell preceding the first k black block is 1, and the role of the 20th cell in that block is 21. Given a row i and one of its roles r (with 1 ≤ r ≤ j=1 d[j] + k + 1), the function Colour returns the role’s colour: Colour(i, r ) =



0, 1,

a cell with role r in row i is white, a cell with role r in row i is black.

We refer to a role r as white if Colour(i, r ) = 0 and as black if Colour(i, r ) = 1. 3.2.3. The basic method We first present a basic method that solves the problem in O(nc +1 ) time. In the next subsection we will make an improvement that will reduce the time and space needed by a factor of n. Similarly to the line solver algorithm, we use a function S ∗ , that calculates the number of solutions for a given column i, 1 ≤ i ≤ n, and each of that column’s cells’ roles r1 , r2 , . . . , rc , where each role is within the range of its row’s roles. (That is, we consider the roles of all cells in any single column, each role being taken with respect to the row containing the cell.) The function is defined recursively. In each of the recursive calls for column i, the column index is the column to the left, namely i − 1. Denote by R the set of all possible role vectors for our puzzle. For a given vector of roles r = (r1 , . . . , rc ) for the ith column, denote by Pred(r) the set of all possible role vectors for the (i − 1)st column, that is Pred(r) = R ∩ {r′ = (r1′ , . . . , rc′ ) : rh − 1 ≤ rh′ ≤ rh for all 1 ≤ h ≤ c , Colour(h, rh ) = 1 ⇒ rh′ = rh − 1}. Then: S ∗ (i, r) =



S ∗ (i − 1, r′ ),

1 ≤ i ≤ n, r ∈ R .

r′ ∈Pred(r)

We also have to define the initial values of the function. First we check if the given roles’ colours adhere to the description of column i. If not, the function’s value is set to 0. This can be checked in O(c ) time. For example, if c = 3, Colour(1, r1 ) = 1, Colour(2, r2 ) = 1, Colour(3, r3 ) = 0, and the description of column i is (1, 1), then the value of S ∗ (i, (r1 , r2 , r3 )) is 0. In addition, the following initial values are defined: S ∗ (i, (r1 , r2 , . . . , rc )) =



1, 0,

i = r1 = · · · = rc = 0, i < 0 or r1 < 0 or . . . or rc < 0. c +1

The method stores the results of the computations in a (c + 1)-dimensional array of size n × n × · · · × n at most. The calculation of each entry might take up to O(2c ) = O(1) time. To find the number of solutions a given puzzle has, we evaluate S ∗ (n + 1, (r1max , . . . , rcmax )), where rimax is the highest role of row i. Thus, this evaluation solves the problem in O(nc +1 ) time and space. We did not address the case where the puzzle is partially coloured since, unlike the line solver algorithm, this method is not intended to be iteratively invoked. Nevertheless, this case can be addressed using similar methods to those used by the line solver algorithm in Section 2.3.







3.2.4. The main method The key observation needed for the improvement of the basic method is that the role of a cell in column i is uniquely determined by the roles of the c − 1 other cells in the same column. For concreteness, we explain how the role of the cell at the cth row is computed based on the roles of the c − 1 cells above it. In fact, given their c − 1 roles, we know the total number of black cells in the top left (c − 1) × i subgrid of our grid. Using the description vectors of the first i columns of our grid, we know the total number of black cells in the first i columns of our grid. The difference between these two numbers provides the number of black cells among the i leftmost cells of row c. Similarly, using the roles of the first c − 1 cells in column i, we know the colours of these cells, and then the description vector of column i will reveal the colour of the bottom cell of column i. These two pieces of information – the number of black cells within the first i cells of row i, and the colour of the ith cell itself – determine (using the description vector of row c) the role of the ith cell in the cth row. The computation of the cth role adds an additional O(c ) = O(1) time to the evaluation of each entry. Since this improvement reduces the matrix size by a factor of n, the evaluation of S ∗ (n + 1, (r1max , . . . , rcmax )) will require O(nc ) time and space. 4. Our solver We will now present the algorithm employed by us and test its performance. Similarly to the case of line solvers, we usually start from a blank grid, but as we proceed there is even more information. Thus, our algorithm takes in general any

40

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

partially coloured grid, and strives to fill in the uncoloured cells. Our search method can find all possible solutions to a puzzle (unless we explicitly set a limit on the number of solutions at which to stop). It can also detect that no solutions exist. When referring to a line solver algorithm in the sequel, we refer to the line solver algorithm developed in Section 2. We note that the solver’s source code and GUI are available for the use of the community [6]. 4.1. The solver’s algorithm We start by initializing a priority queue with the puzzle’s lines. The queue is ordered according to our expectations as to the amount of information we are going to extract from each line. Thus, in the beginning, the lines at the head of the queue are those with many long blocks. As we continue, each time a line (say row) is processed, all lines (columns) in which we obtained new information are added to the queue; if they were already in the queue, their priority is increased. After the queue is initialized, we activate the following function. Algorithm 5 processPriorityQueueElements 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:

while priority queue is not empty do currentLine ← popFromQueue() solv eLine(currentLine) if line solver returned false then return false else for each cell c whose colour was found by solv eLine do if currentLine is a row then pushOrUpdateColumnInQueue(c) else pushOrUpdateRowInQueue(c) end if end for end if end while

During every iteration, the function operates on a single line. It uses the line solver to extract new information about the line’s cells. If the current line is a row, it means that for each cell c that was coloured by this activation of the line solver, we will insert column c to the queue (or update its position there to increase its priority). An analogous behaviour is defined for columns. If the line solver returns a false value, it means that it reached a contradiction and no solution exists. When the queue is empty, the function has exhausted all possible data that can be obtained by the line solver. Namely, for each uncoloured square there exists a colouring of the two lines containing it with this square white, and another with this square black. At this point we start guessing. Guessing usually takes exponential time. Many solvers implement a depth-first search method. Using heuristics, they try to make a good guess towards a solution. Our approach does the opposite—we use probing (see, for example, [16] and the references therein). The main idea of probing is to guess against all odds, that is make the least probable guess. This most often leads to a contradiction the fastest, and thus enables us to cut branches from the search tree, thereby reducing the search space. Probing traverses the search tree with a depth-first search approach. At each crossroad, probing uses a breadth-first search to decide which guess is most likely to reach a contradiction. If during the breadth-first search we encounter a contradiction, the search is stopped, the opposite colour is marked and we continue to process lines with the line solver. Since the breadth-first search method is expensive and could be stopped, our probing implementation defines an order in which the unknown cells are tested: 1. 2. 3. 4.

Probe the neighbours of the cell selected in the last iteration. Probe the neighbours of cells that were changed (i.e., their value was determined as black or white) in the last iteration. Probe cells with 2 or more known neighbours. Probe cells with 1 known neighbour.

If during the breadth-first search we did not encounter a contradiction, we use the following heuristic to select an unknown cell and colour as our guess. We choose the guess that, when tested by the breadth-first search, allowed us to determine the colour of as many uncoloured cells as possible. We use a tree-based data structure to keep track of our guesses. It allows us to backtrack whenever we hit a contradiction. In addition, this data structure enables our search method to find all possible solutions. One interesting direction of future work could be to use a guessing heuristic based on the work of Ortiz-García et al. [15]. In their paper, a heuristic is presented, designed to guess correctly which colour a cell should have. The heuristic is based on counting the number of possibilities of completing the row and column of that cell if we colour it white, as well as if we colour it black. These two numbers hint which of the two possible choices is more promising. Note that this information

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

41

Table 1 Our solver cache hit rates on selected puzzles. Puzzle

Cache hit rate

Petro (#436) Hot (#2040) Light (#803) Lion (#2712) Marley (#10088) Forever (#6574)

55% 65% 86% 93% 93% 98%

Table 2 Comparing the benchmark’s two sample sets. Here, ‘‘line solvable’’ means that a puzzle can be solved with the exclusive use of a line solver, without the use of guessing.

Multiple solutions Unique solution but not line solvable Unique solution and line solvable

Webpbn puzzles

Random puzzles

2.6% 15.6% 81.8%

97.5% 2.1% 0.4%

Table 3 Webpbn Puzzles. The table is sorted according to the rightmost columns. This places our solver (known as the ‘‘BGU’’ solver) in second place. Solver

Time (s) 0–0.09

0.1–0.19

0.2–0.49

0.5–0.99

1–3.99

4–9.99

10–29.99

30–59.99

1–2 min

2 min or more

Wolter Our solver (BGU) Simpson Ol˘sák Lagerkvist/Gecode Kjellerstrand/Lazyfd Kjellerstrand/Gecode Wilk

2474 632 2439 2414 2380 125 2306 2107

8 756 10 13 23 92 42 60

3 1029 4 10 20 409 31 76

4 59 5 8 12 343 15 52

1 11 10 16 12 712 20 54

1 2 2 3 12 424 14 31

0 1 3 6 4 284 5 19

0 1 0 3 5 66 5 9

0 0 2 2 5 17 3 11

0 0 16 16 18 19 50 72

may be used in two opposite ways. One may take the more probable choice with the hope of getting to a solution, or take the less probable one with the hope of obtaining a contradiction and thus know that the other choice is necessarily correct. We noticed when traversing the search tree that we often backtrack and reach similar board partial colourings, and encounter lines we have previously solved. Addressing this, each time we apply the line solver to a line, we store the result in a hash table as a key-value pair: The key for the hash is (d, p), where d is the description of the line, and p is the partial colouring before applying the line solver. The value is p′ —the partial colouring obtained by applying the line solver. Based on this, whenever we try to solve a line, we first check if it exists in our cache—and if it does, we do not have to apply the line solver at all to this line. The hash table was proven to be extremely helpful when solving hard puzzles, where a cache hit rate of over 90% was achieved. Table 1 describes the cache hit rate for some sample webpbn puzzles [25]. 4.2. Our solver’s performance Very conveniently for people developing nonograms and nonogram solvers, there is a paint-by-number community web site (webpbn) that may assist in many ways [25]. It was founded by Wolter, and lets one create, solve and validate (i.e., ensure that a given puzzle has a unique solution) puzzles. In addition, Wolter founded a survey of nonograms solvers. The following results were taken from his survey [25]. The benchmark used by Wolter to compare solvers (see Table 2) consists of a set of 2491 puzzles of various sizes (see Table 3), intended for humans, and a set of 5000 randomly generated 30 × 30 puzzles (see Table 4). The benchmark was mainly used to compare the performance of the various solvers. We present some statistics regarding the two sets of puzzles [25]. Note that our solver is actually the worst for simple puzzles, as it takes more time than most others to solve them. This is due to the fact that our solver is written in Java. Thus, it takes a relatively long time to load the program, and then it works slower since we have an interpreter and not an efficient machine code. Implementing our algorithm in C, for example, would reduce the extra costs. As our main interest is in dealing with hard instances, we have not done that. Wolter’s benchmark [25] depends primarily on the performance on hard instances. In fact, Wolter [25] sorts his tests (see Tables 3 and 4) according to the right-most column, i.e., it cares little about easy-to-solve instances, and rather ranks solvers by their performance on the hardest instances. We should mention here also a very recent solver, developed by Metodi et al. [11]. It is based on translating the puzzle to an instance of SAT, and then employing some software for solving SAT problems. It seems that their solver surpasses our solver in terms of the time required to find a single solution (or several solutions) of a problem. It would be interesting to check how well that method will work if asked to provide all solutions of a puzzle.

42

D. Berend et al. / Discrete Applied Mathematics 169 (2014) 30–42

Table 4 Random Puzzles. The table is sorted according to the rightmost columns. This places our solver (known as the ‘‘BGU’’ solver) first. Solver

Time (s) 0–0.09

Our solver (BGU) Wolter Kjellerstrand/Lazyfd Ol˘sák Kjellerstrand/Gecode Lagerkvist/Gecode Simpson Wilk

0 4362 0 2879 2227 2325 2568 644

0.1–0.19 23 221 0 333 461 381 306 310

0.2–0.49

0.5–0.99

1–3.99

4–9.99

10–29.99

30–59.99

1–2 min

2 min or more

2852 169 0 381 495 468 309 484

1790 70 0 244 311 308 213 370

284 88 616 405 491 460 348 697

22 38 2921 199 238 251 215 424

12 21 1208 146 205 235 224 452

8 6 176 84 115 104 105 237

4 7 50 61 97 79 91 231

5 18 29 268 360 389 621 1151

Acknowledgements The authors express their gratitude to M. Goldberg for many helpful discussions regarding the implementations of their algorithms and to S. Golan for his careful review and helpful comments on the first version of the paper. The second author wishes to thank the generous support of the Frankel fund. We would also like to thank the referees for their meticulous reading of the paper and their suggestions that improved the paper considerably. References [1] K.J. Batenburg, S. Henstra, W.A. Kosters, W.J. Palenstijn, Constructing simple nonograms of varying difficulty, Pure Math. Appl. 20 (2009) 1–15. [2] K.J. Batenburg, W.A. Kosters, A discrete tomography approach to Japanese puzzles, in: Proceedings of the 16th Belgium–Netherlands Conference on Artificial Intelligence, 2004, pp. 243–250. [3] K.J. Batenburg, W.A. Kosters, Solving nonograms by combining relaxations, Pattern Recognit. 42 (2009) 1672–1683. [4] R.J. Baxter, Planar lattice gases with nearest-neighbour exclusion, Ann. Comb. 3 (1999) 191–203. [5] J. Benton, R. Snow, N. Wallach, A combinatorial problem associated with nonograms, Linear Algebra Appl. 412 (2006) 30–38. [6] D. Berend, D. Pomeranz, R. Rabani, B. Raziel, The BGU nonograms project, http://www.cs.bgu.ac.il/~berend/nonograms/. [7] R.A. Bosch, Painting by numbers, Optima 65 (2001) 16–17. [8] N.J. Calkin, H.S. Wilf, The number of independent sets in a grid graph, SIAM J. Discrete Math. 11 (1998) 54–60. [9] E.R. Canfield, B.D. McKay, Asymptotic enumeration of dense 0-1 matrices with equal row sums and equal column sums, Electron. J. Combin. 12 (2005). [10] D. Goldstein, R. Stong, On the number of possible row and column sums of 0,1-matrices, Electron. J. Combin. 13 (2006). [11] A. Metodi, M. Codish, V. Lagoon, P.J. Stuckey, Boolean equi-propagation for optimized SAT encoding, in: Proceedings of the 17th International Conference on Principles and Practice of Constraint Programming, 2011, pp. 621–636. [12] L. Mingote, F. Azevedo, Colored nonograms: an integer linear programming approach, in: Proceedings of the Portuguese Conference on Artificial Intelligence, 2009, pp. 213–224. [13] R. Mullen, On determining paint by numbers puzzles with nonunique solutions, J. Integer Seq. 12 (2009). Article 09.6.5. [14] E.G. Ortiz-García, S. Salcedo-Sanz, J.M. Leiva-Murillo, Á.M. Pérez-Bellido, J.A. Portilla-Figueras, Automated generation and visualization of picture-logic puzzles, Comput. Graph. 31 (2007) 750–760. [15] E.G. Ortiz-García, S. Salcedo-Sanz, Á.M. Pérez-Bellido, L. Carro-Calvo, A. Portilla-Figueras, X. Yao, Improving the performance of evolutionary algorithms in grid-based puzzles resolution, Evol. Intell. 2 (2009) 169–181. [16] W. Ruml, Incomplete tree search using adaptive probing, in: Proceedings of the International Joint Conference on Artificial Intelligence, 2001, pp. 235–241. [17] S. Salcedo-Sanz, E.G. Ortiz-García, Á.M. Pérez-Bellido, J.A. Portilla-Figueras, X. Yao, Solving Japanese puzzles with heuristics, in: Proceedings of the IEEE Symposium on Computation Intelligence and Games, Honolulu, USA, 2007, pp. 224–231. [18] S. Salcedo-Sanz, J.A. Portilla-Figueras, E.G. Ortiz-García, Á.M. Pérez-Bellido, X. Yao, Teaching advanced features of evolutionary algorithms using Japanese puzzles, IEEE Trans. Educ. 50 (2) (2007) 151–156. [19] S. Simpson, Website nonogram solver, http://www.comp.lancs.ac.uk/~ss/nonogram/links.html. [20] Y.S. Sohn, Recognition of the printed English sentence by using Japanese puzzle, Int. J. Fuzzy Logic Intell. Syst. 8 (2008) 225–230. [21] Y.S. Sohn, K.S. Oh, B.S. Kim, A recognition method of the printed alphabet by using nonogram puzzle, in: Proceedings of the 8th International Symposium on Advanced Intelligent Systems, 2007, pp. 232–236. [22] J.T. Tsai, P.Y. Chou, J.C. Fang, Learning intelligent genetic algorithms using Japanese nonograms, IEEE Trans. Educ. 99 (2011) 164–168. [23] N. Ueda, T. Nagao, NP-Completeness Results for Nonogram via Parsimonious Reductions, Technical Report TR96-0008, Tokyo Institute of Technology, 1996. [24] W. Wiggers, A comparison of a genetic algorithm and a depth first search algorithm applied to Japanese nonograms, in: Proceedings of the First Twente Student Conference on IT, 2004, pp. 1–6. [25] J. Wolter, Survey of paint-by-numbers puzzle solvers, http://webpbn.com/survey/index.html.