The rule
Colour the cells of the $n \times m$ grid. Every pair of cells that share a side produces the unordered pair of their two colours, and the two colours are allowed to be equal. The colouring is good when no pair is produced twice.
An $r$-colouring of a graph $G$ is a map $f \colon V(G) \to \{1, \dots, r\}$. Each edge $uv$ receives the unordered pair $\{f(u), f(v)\}$, where $f(u) = f(v)$ is allowed. The colouring is valid if distinct edges receive distinct pairs. The $n \times m$ grid graph has the $nm$ cells as vertices and $E = 2nm - n - m$ edges. Write $r_{n \times m}$ for the least $r$ such that it has a valid $r$-colouring.
Kagey's own figure fixes the reading of the rule. His $8$-colouring of the $4 \times 5$ grid is
$$\begin{array}{ccccc} A&A&B&B&C\\ C&D&D&E&C\\ F&F&G&E&H\\ B&H&G&A&H\end{array}$$and in it all eight colours occupy two adjacent cells. So a colour may be its own partner, and the pair $\{c,c\}$ is treated like any other: at most once. That reading also gives $r_{1 \times 2} = 1$, as his table says.
Let $K_r^{\circ}$ be the complete graph on $r$ vertices with a loop added at every vertex. It has $\binom{r+1}{2}$ edges, and a valid $r$-colouring is exactly a homomorphism $G \to K_r^{\circ}$ that is injective on edges. Counting the pairs gives the counting bound
$$\binom{r_{n \times m} + 1}{2} \;\ge\; E \;=\; 2nm - n - m.$$Write $\mathrm{cb}(n,m)$ for the least $r$ that satisfies it. Fourteen of the fifteen values in Kagey's table equal $\mathrm{cb}$. The exception is $2 \times 4$, where $E = 10 = \binom52$ and $\mathrm{cb} = 4$, and yet five colours are needed. So the counting bound is not the answer, and the question is what replaces it.
The parameter has a name. A valid colouring is a line-distinguishing colouring, and $r_{n \times m}$ is the line-distinguishing chromatic number of the grid graph, introduced by Frank, Harary and Plantholt. If in addition no colour touches itself, the colouring is harmonious, so Kagey's first related question asks for the harmonious chromatic number of the grid. Paths and cycles were settled long ago. Fickes and Wong, who add several families of graphs with few cycles, report that those families are essentially all that is known, and the grid is not among them.
One row
A valid colouring of the path on $m$ cells is a walk in $K_r^{\circ}$ that uses no edge twice, that is, a trail with $m-1$ edges. Every sequence of colours is a walk, because $K_r^{\circ}$ has every edge and every loop, and every vertex has degree $r+1$ with a loop counting two.
Let $L(r)$ be the largest number of edges of a trail in $K_r^{\circ}$. Then $L(r) = \binom{r+1}{2}$ for odd $r$, and $L(r) = \binom{r+1}{2} - \left(\tfrac r2 - 1\right) = \tfrac{r^2+2}{2}$ for even $r$. Hence $r_{1 \times m}$ is the least $r$ with $L(r) \ge m-1$.
For odd $r$ every degree $r+1$ is even and $K_r^{\circ}$ is connected, so Euler's theorem gives a closed trail through all $\binom{r+1}{2}$ edges. For even $r$ the degree $r+1$ is odd. Let $H$ be the subgraph used by a trail. All but two vertices have even degree in $H$, and at such a vertex an odd number of edge ends is missing. A missing loop accounts for two of them, so at least one missing edge at that vertex is not a loop. There are at least $r-2$ such vertices and one non-loop edge serves at most two of them, so at least $\tfrac r2 - 1$ edges are missing. For the construction, delete the edges $\{1,2\}, \{3,4\}, \dots, \{r-3,r-2\}$. What is left is connected, has exactly two vertices of odd degree, and Euler's theorem gives a trail through all of it. An initial segment of a trail is a trail, so every shorter length occurs too.
This is a known result and we prove it because of what it says about the counting bound. The values of $L$ are $1, 3, 6, 9, 15, 19, 28, 33$, while $\binom{r+1}{2}$ runs $1, 3, 6, 10, 15, 21, 28, 36$. The two agree at odd $r$ and part at even $r$. So on one row the counting bound is wrong for infinitely many $m$, first at $m = 11$, then at $m = 21, 22$, then at $m = 35, 36, 37$, and in general at $\tfrac r2 - 1$ consecutive values of $m$ for each even $r \ge 4$.
Two of Kagey's variants fall to the same argument on one row. If no colour may touch itself the trails live in $K_r$, and the answer is the least $r$ with $L'(r) \ge m-1$, where $L'(r) = \binom r2$ for odd $r$ and $\binom r2 - \left(\tfrac r2 - 1\right)$ for even $r$. If the pair is read in order, the trails are directed trails in the complete digraph with a loop at every vertex, which is Eulerian with $r^2$ arcs, so the answer is the least $r$ with $r^2 \ge m-1$.
A bound from the cell degrees
The obstruction on one row is a parity condition at each colour. For grids the corresponding statement is this.
Let $f$ be a valid $r$-colouring of a graph. For a colour $c$ let $D_c$ be the sum of the degrees of the vertices coloured $c$, and $\ell_c$ the number of edges with both ends coloured $c$. Then $\ell_c \le 1$ and $D_c - 2\ell_c \le r-1$. In particular $D_c \le r+1$, and a colour with $D_c \ge r$ occupies two adjacent cells.
The pair $\{c,c\}$ occurs at most once, so $\ell_c \le 1$. The sum $D_c$ counts edge ends at cells coloured $c$. An edge inside the class contributes two and an edge with one end in the class contributes one, so exactly $D_c - 2\ell_c$ edges have exactly one end coloured $c$. Each of those receives a distinct pair $\{c, c'\}$ with $c' \ne c$, and only $r-1$ such pairs exist.
Summing $D_c \le r+1$ over the colours gives $2E \le r(r+1)$, which is the counting bound again. The gain comes from the fact that $D_c$ is a sum of cell degrees, and for $n, m \ge 2$ the grid has only four cells of degree 2 and $2(n+m-4)$ of degree 3. Everything else has degree 4, and a class cannot hold many of those.
Call $r$ packable for the $n \times m$ grid if the cells can be split into at most $r$ classes, each of degree sum at most $r-1$, or of degree sum at most $r+1$ with at least two cells. Let $\mathrm{pb}(n,m)$ be the least packable $r$. Whether $r$ is packable depends only on how many cells of each degree there are, so a small dynamic programme decides it.
$r_{n \times m} \ge \mathrm{pb}(n,m) \ge \mathrm{cb}(n,m)$ for all $n$ and $m$.
On $2 \times m$ grids the bound has a closed form. The ladder has four cells of degree 2 and $2m-4$ of degree 3, so $D_c$ is fixed modulo 3 by the number of corners in the class, and adding up the waste that this forces gives
$$6m - 4 \;\le\; r(r+1) - W(r), \qquad W(r) = \begin{cases} r-2, & r \equiv 0 \pmod 3,\\ \max(0,\,2r-8), & r \equiv 1 \pmod 3,\\ 1, & r \equiv 2 \pmod 3.\end{cases}$$This beats the counting bound for infinitely many $m$, first at $m = 10$. There $E = 28 = \binom 82$ exactly, and seven colours still do not suffice. We checked that the closed form reproduces $\mathrm{pb}(2,m)$ for every $m \le 80$.
The same kind of count reaches a grid the counting bound cannot. On $7 \times 11$ we have $E = 136 = \binom{17}{2}$, so a valid $16$-colouring would need $D_c = 17$ for all sixteen colours. The grid has 4 cells of degree 2, 28 of degree 3 and 45 of degree 4. Since 17 is odd, every class holds an odd number of cells of degree 3. If a class holds exactly one, the other 14 units of degree come from cells of degree 2 and 4, and 14 is not a multiple of 4, so the class holds a corner. At most four classes hold a corner. Hence at least twelve classes hold three cells of degree 3 and the other four hold one, which needs $36 + 4 = 40$ such cells where only 28 exist. So $r_{7 \times 11} \ge 17$, and a $17$-colouring exists, so $r_{7 \times 11} = 17$.
Three grids the bound misses
The packing bound is not the whole truth. The smallest place it fails is Kagey's own exception.
$r_{2 \times 4} = 5$, while $\mathrm{pb}(2,4) = \mathrm{cb}(2,4) = 4$.
Suppose a valid $4$-colouring exists. Here $E = 10 = \binom52$, so no colour has any waste: $D_c = 5$ for all four colours, and then $\ell_c = 1$ for all four. The cells have degree 2 at the corners and 3 elsewhere, and $5 = 2+3$ is the only way to write 5 from those. So every class is one corner together with an adjacent cell of degree 3. The top left corner has two neighbours, the corner below it and the cell to its right, and a class cannot hold two corners, so its class is the first two cells of the top row. The same argument gives the first two cells of the bottom row. The rungs in columns 1 and 2 then receive the same pair. For the upper bound, the colouring on the right below is valid, so $r_{2 \times 4} = 5$.
Two more grids behave the same way. There is no valid $6$-colouring of $2 \times 7$ and no valid $9$-colouring of $4 \times 7$, and in both cases the only proof we have is an exhaustive search. What the three share is that the packing at $r = \mathrm{cb}$ is rigid, with no freedom left in any class. That is not the explanation, because rigidity on its own is not an obstruction. The $4 \times 10$ grid has $E = 66 = \binom{12}{2}$, and an $11$-colouring exists that uses every one of the 66 pairs, the eleven pairs $\{c,c\}$ included, exactly once.
The table, and a conjecture
We computed $r_{n \times m}$ exactly for 79 grids with $n \ge 2$: every $2 \times m$ up to $m = 36$, every $3 \times m$ up to $m = 21$, $4 \times m$ up to $m = 14$, $5 \times m$ up to $m = 10$, $6 \times m$ up to $m = 9$, then $7 \times 7$, $7 \times 8$, $7 \times 11$ and $8 \times 8$. Every upper bound is an explicit colouring, stored and rechecked from the definition. Every lower bound is the packing bound, except at the three grids above.
| $r_{n \times m}$ | $m{=}1$ | $2$ | $3$ | $4$ | $5$ | $6$ | $7$ | $8$ | $9$ | $10$ |
|---|---|---|---|---|---|---|---|---|---|---|
| $n=1$ | 1 | 1 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 |
| $n=2$ | 3 | 4 | 5† | 5 | 6 | 7† | 7 | 7 | 8* | |
| $n=3$ | 5 | 6 | 7 | 7 | 8 | 9 | 9 | 10 | ||
| $n=4$ | 7 | 8 | 9 | 10† | 10 | 11 | 11 | |||
| $n=5$ | 9 | 10 | 11 | 12 | 12 | 13 | ||||
| $n=6$ | 11 | 12 | 13 | 14 | ||||||
| $n=7$ | 13 | 14 | ||||||||
| $n=8$ | 15 |
Unmarked entries equal the counting bound. A star marks a value one above it that the packing bound proves, and a dagger marks a value one above the packing bound. Beyond this window the only further starred grids are $2 \times 15$, $2 \times 18$, $2 \times 19$, $2 \times 26$, $2 \times 29$, $2 \times 30$, $2 \times 31$ and $7 \times 11$, and there are no further daggers.
For all $n, m \ge 2$ other than $(2,4)$, $(2,7)$ and $(4,7)$, $r_{n \times m} = \mathrm{pb}(n,m)$. In particular $r_{n \times m}$ is always $\mathrm{cb}(n,m)$ or $\mathrm{cb}(n,m)+1$.
It holds at all 79 grids. We have no construction that would prove it even for $n = 2$, where it asks only that the ladder inequality above be sufficient for $m \notin \{4,7\}$.
Three earlier guesses were written into a ledger, together with their predictions for grids that had not been run, and then refuted. The first said the counting bound is attained whenever it is not tight, and $2 \times 7$ refuted it. The second said the packing bound is attained except where the counting bound is exactly tight, and $2 \times 7$ and $4 \times 10$ refuted it. The third fitted an imaginary extra cell of degree 2 to the 25 grids then known, and $2 \times 25$ and $4 \times 10$ refuted it. The conjecture above is what survived, and the fourteen predictions it made for the next batch of grids were all confirmed.
The square grids are the clean column. Here $E = 2n^2-2n$, and $\binom{2n-1}{2} < E \le \binom{2n}{2}$ for $n \ge 2$, so $\mathrm{cb}(n,n) = 2n-1$ and $r_{n \times n} \ge 2n-1$. Equality holds for $2 \le n \le 8$. We also checked $\mathrm{pb}(n,n) = 2n-1$ for $n \le 16$, so the conjecture would give equality there as well.
The leading term is not open. A harmonious colouring is in particular valid, and Edwards proved $h(G) \le (1+o(1))\sqrt{2e}$ for graphs of bounded degree with $e$ edges. The grid has maximum degree 4, so
$$r_{n \times m} = (1+o(1))\sqrt{2E} = (1+o(1))\sqrt{4nm-2n-2m},$$which is $2\sqrt{nm}$ when $n$ and $m$ both grow, and $\sqrt{(4n-2)m}$ when the number of rows is held fixed. What is open is the exact value, and on the evidence it is $\mathrm{cb}$ or $\mathrm{cb}+1$ every time.
Kagey's other questions
Six come with the problem. The second asks how many optimal colourings there are up to permutation of the colours. Two colourings are counted as one when they differ only by renaming, and rotations and reflections of the grid are counted separately.
| Optimal colourings | $m{=}1$ | $2$ | $3$ | $4$ | $5$ | $6$ | $7$ | $8$ | $9$ | $10$ |
|---|---|---|---|---|---|---|---|---|---|---|
| $n=1$ | 1 | 1 | 2 | 1 | 7 | 6 | 2 | 61 | 42 | 12 |
| $n=2$ | 4 | 7 | 36 | 6 | 146 | 6522 | 4798 | 512 | ||
| $n=3$ | 2 | 27 | 922 | 52 | 3464 | |||||
| $n=4$ | 480 | 7842 | 420270 | |||||||
| $n=5$ | 75488 |
The counts are large when many pairs go unused and small when few do. The $3 \times 3$ grid has $\binom62 = 15$ pairs available for 12 edges and two optimal colourings. The $3 \times 5$ grid has 28 pairs for 22 edges and 922 of them.
Three more of his questions are small enough to compute. Variant (1) forbids a colour from touching itself, which is the harmonious chromatic number. Variant (5) reads a pair in order, left to right on a horizontal edge and top to bottom on a vertical one. Variant (4) counts horizontal and vertical adjacencies separately, so a pair may occur once each way.
| Grid | $r_{n \times m}$ | (1) no self-adjacency | (5) ordered pairs | (4) oriented |
|---|---|---|---|---|
| $2 \times 2$ | 3 | 4 | 3+ | 2 |
| $2 \times 3$ | 4 | 5 | 3 | 3 |
| $2 \times 4$ | 5 | 6+ | 4 | 3 |
| $2 \times 5$ | 5 | 7+ | 4 | 4 |
| $2 \times 6$ | 6 | 7 | 5+ | 4 |
| $3 \times 3$ | 5 | 7+ | 4 | 4+ |
| $3 \times 4$ | 6 | 7 | 5 | 4 |
| $3 \times 5$ | 7 | 8 | 5 | 5 |
| $3 \times 6$ | 7 | 9+ | 6 | 5 |
| $4 \times 4$ | 7 | 8 | 5 | 5 |
| $4 \times 5$ | 8 | 9 | 6 | 6 |
| $4 \times 6$ | 9 | 10 | 7 | 6 |
| $5 \times 5$ | 9 | 10 | 7 | 6 |
Each variant has its own counting bound, namely $\binom r2 \ge E$, then $r^2 \ge E$, then $\binom{r+1}{2} \ge \max(n(m-1), m(n-1))$. We had guessed that every variant would meet its own counting bound in this range. A plus marks the cells where it does not, seven of the 39, so the guess is refuted and we propose no formula.
Variant (1) is the only one of the three we found in the literature. Miller and Pritikin state that the harmonious chromatic number of the $n \times n$ grid is $2n$ for even $n$, and $2n$ or $2n+1$ for odd $n$. Our values agree and they settle the two odd cases in range: the answer is $2n+1$ at $n = 3$ and $2n$ at $n = 5$. For $6 \times 6$ we found a $12$-colouring in which no colour touches itself, and 12 is the counting bound there, so $h(6,6) = 12$. Set beside the square grids above, allowing a colour to touch itself saves exactly one colour on every square grid we can reach, except at $3 \times 3$, where it saves two.
Questions (3), on triangular and hexagonal grids, and (6), on diagonal adjacencies, we did not study.
What is open, and what was checked
The main question is open. Four smaller ones are worth stating.
- Prove or refute the conjecture. For $n = 2$ it asks for a construction showing the ladder inequality is sufficient away from $m = 4$ and $m = 7$. Building the top row, the bottom row and the rungs from three trails in edge-disjoint circulant subgraphs of $K_r^{\circ}$ is the natural attempt, and we could not make the rungs work.
- Is there a proof for $2 \times 7$ and $4 \times 7$ as short as the one for $2 \times 4$? The packing is rigid in all three, but it is rigid at $4 \times 10$ too, where a colouring exists.
- Is $r_{n \times m} \le \mathrm{cb}(n,m)+1$ for all $n$ and $m$? One row satisfies it.
- The harmonious chromatic number exceeds its own counting bound more often than $r_{n \times m}$ does. What is the pattern, and is $h(n,n) = 2n$ for every $n \ge 4$?
Three independently written programs stand behind the numbers above. A depth-first search that fills the cells row by row, names the colours in order of first use, and prunes with the lemma above, so it proves nonexistence as well as existence. A 0/1 integer programme solved by HiGHS through SciPy, which uses no degree argument at all. A plain enumeration in a different cell order with no pruning beyond validity. The search recomputes 78 of the 79 values from scratch, the enumeration covers the small grids together with the count and variant tables, and the integer programme covers Kagey's table and 33 of the 79 grids. A verification script runs 23 checks in about three minutes and all pass, and a ledger records every prediction and every run in the order they happened.
The integer programme is the useful cross-check on the lower bounds, because it knows nothing about degrees. It proved infeasibility in four cases: no $4$-colouring of $2 \times 4$, no $6$-colouring of $2 \times 7$, no $7$-colouring of $2 \times 10$ and no $9$-colouring of $2 \times 15$. The last two are exactly where the packing bound beats counting. On $4 \times 7$ with nine colours it did not finish inside half an hour, so that grid rests on the search alone.