1

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.

Kyuubi
  • 273
  • 3
  • 9

2 Answers2

1

Not an answer, but related.

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.

In some cases there are formulas for the chromatic polynomial, but in the case of grids it's an open question to find such a formula, see this question on math.se.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
1

I would suggest you try using MINION, as suggested in the other question. It should be very simple to try and you might quickly find out whether it works or not.

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.

D.W.
  • 167,959
  • 22
  • 232
  • 500