# Calculate number of ways to color matrix using inclusion-exclusion principle

This was asked in a recent contest. The question asked to count the number of ways to color an $M \times N$ matrix with $K$ colours such that no two adjacent cells (sharing an edge) have the same color. In other words, I want to count the number of different graph colorings of a grid graph: the graph of a $N \times M$ grid. The limits were $1 \le N,M \le 8$ and $1 \le K \le 1000000000$.

Is it possible to count the number of colors using the inclusion-exclusion principle, and if so, how? I saw another question that solves the special case (where $M=2$) using inclusion-exclusion, so I'm wondering if that kind of approach can be generalized here.

• What makes you think that inclusion-exclusion may work? What have you tried? Which other approaches have you disregarded, and why? – Raphael Jan 19 '14 at 15:04
• Note that the problem has been solved before, so this question apparently targets inclusion-exclusion specifically. – Raphael Jan 19 '14 at 15:28
• It's not "very similar to graph colouring" -- it is graph colouring: the question is the $k$-colour a rectangular grid graph. You're asking this as a purely combinatorial question with, for example, no algorithmic content so I'm not sure it's really on-topic, here. – David Richerby Jan 19 '14 at 16:39
• @Kyuubi It doesn't make sense to say a problem is solved using a problem. Graph coloring is a problem, and it is solved by an algorithm. If you only wanted to solve the problem you desctibe using any method, it can be done in an instant with a modern SAT/CSP solver for instance. – Juho Jan 20 '14 at 12:48
• @Kyuubi Whatever algorithm you use to solve the problem, it is counting the proper $K$-colourings of an $M\times N$ grid graph. That is what it is doing, even if it is unable to count proper $K$-colourings of any other kind of graph. It doesn't matter if you call the problem "$K$-colouring a matrix", "$K$-colouring a grid graph" or "The $K$ effluvient widgetization problem" because renaming a problem doesn't change its complexity. It also doesn't matter how inefficient a general-purpose $K$-colouring algorithm might be because you're not trying to solve the general $K$-colouring problem. – David Richerby Jan 20 '14 at 14:03

The number of colorings of a graph on $N$ vertices with $c$ colors is a polynomial of degree at most $N$ in $c$ known as the chromatic polynomial. As such, it can be calculated by evaluating it at $N+1$ points, say $c=0$ to $c=N$. Your program need $\binom{8+1}{2} = 36$ of these polynomial.
Another possible approach might be to try dynamic programming. In particular, pick a column in the middle of the grid. Enumerate all possible ways to color that column. Then, count the number of ways to fill in the left half of the grid (recursively) and the number of ways to fill in the right half of the grid (again, recursing). You can use symmetry to reduce the number of ways to enumerate the middle column: for instance, if all of the colors in the middle column are distinct, then you only need to try one case (and multiply by $M!$); if there are only two distinct colors used in the middle column, you need to try $2^M$ cases; and so on. This seems like it might work for small enough grids. It'll explode as the grid size grows, but it's something you could try to see how well it scales.