Concerning Nonograms

One reason why there hasn’t been too many posts recently is because of my vacation. Being away from a working broadband connection makes blogging kind of hard. The other is that I’ve been somewhat addicted to nonograms. You may know them as “paint by numbers puzzles” or “picross”, short for “picture crossword”. What’s a nonogram? The following picture from Wikipedia gives you a pretty decent idea:

Nonogram Animation

The previous paragraph was written two hours before I wrote this one. Why? Because I spent two hours solving hard nonograms.

So. Nonograms. Basically, you start with a blank rectangular grid. The goal is to fill in the right blocks so that the correct picture is revealed. Each column and row contains a sequence of numbers that tells you how many sets of consequetive blocks are colored in the column/row and how big each set is. For example, seeing “3 2 2″ on a row tells you that, from left to right, there is a group of 3 adjecant colored boxes, then a group of 2, and then another group of 2. Furthermore, these groups are not touching each other. There’s a much better explanation (as well as a list of many basic ways of solving one of these things) on Wikipedia. You can also play with some nonograms (more commonly called picross in the interactive gaming form) at Armor Games.

It turns out that solving a nonogram (assuming that it is solvable) is actually an NP-complete problem. There’s a reference to this paper which I haven’t had time to read yet. But beyond NP-completeness there are still plenty of questions you can ask about nonograms. Here’s a list of obvious ones:

  1. Given any arbiturary set of nonogram clues is there an actual solution?
  2. Given any arbiturary set of nonogram clues is there a way to determine if a solution exists?
  3. Suppose that a set of nonogram clues has a solution. Is the solution unique?
  4. Can every set of nonogram clue (that has a solution) be solved without guessing?

The answer to question 1 is, of course, false. Just take a 1 by 1 grid and label the row “0″ and the column “1″. Here I am assuming that “nonogram clues” means a set of clues that are at least somewhat reasonable; that we don’t use numbers larger than the grid sizes and that, in fact, if we looked at one row or column alone the clue should make sense. For example, “2 3 4″ on a 5 by 5 grid is silly. However, it makes sense to have something like that on a 15 by 15 grid. We’ll come back to this question later.

Question 2 is also very simple. There are finitely many, say, m by n pictures. It’s kind of ugly and scary writing it out as an exponent so let’s call this finite number N. To see if a set of (somewhat reasonable) nonogram clues has a solution we can just generate all N pictures and see if any of them match! Not the most effecient solution, but it works in theory. And I’m not a computer scientist so effeciency is not really a concern for me.

According to this nonogram solver web site, the answer to Question 3 is false. But it doesn’t give too many details (as in, hardly any). It does give a program that counts the number of solutions a set of clues has, though. Since it has documentation on how to use the counter function and the documentation says that it returns a 1 only if there’s a unique solution, that probably means that there are clues with no unique solutions.

Of course, while typing that last sentence I thought of the smallest possible set of nonogram clues that has no unique solution. Observe!

Nonogram Example 1Nonogram Example 2Nonogram Example 3
The same page the solver was from also implied that the answer to Question 4 is false. Of course, if there is no unique solution guessing should be involved when that happens.

Another question that I have been thinking about is sort of a Question 1′: what is the probability that an arbiturarily chosen set of nonogram clues (for an m by n grid) has a solution? The size of the sample space can be calculated by first finding the number of somewhat reasonable clues for a row of size n and then multiplying the result by m. Then do the same thing with the columns and add the two products together. If each valid set of clues has a unique solution then the answer would just be N (the number of m by n pictures) divided by the size of the sample space. Since the mapping from pictures to sets of clues is actually many to one, that quotient would just be an upper bound.

You are of course invited to calculate that upper bound. As for me, I’m going to try to figure out if I can install LaTeX onto this server so I can type real math next time I decide to post about math. Or I could go solve more 25 by 25 nonograms.


Posted on : Dec 30 2007
Posted under Random Math Things |