The question
Take an $n \times n$ grid of unit cells and glue opposite sides. There are $n^2$ cells and $n^2$ vertices. In each cell one may draw one of its two diagonals, or nothing. Two diagonals touch when they share an endpoint. A configuration is a set of diagonals, at most one per cell, no two of which touch. Write $m(n)$ for the largest size of a configuration.
What is the greatest number of diagonals that can be drawn on an $n \times n$ toroidal grid?
Kagey records $m(2n) = 2n^2$ and $2n^2 + n \le m(2n+1) \le 2n^2 + 2n$. The upper bound is the trivial one. There are $n^2$ vertices and every diagonal uses two of them, so nothing can beat $\lfloor n^2/2 \rfloor$. His figure for $n = 5$ draws $12$ diagonals and leaves the vertex $(2,2)$ free, which is already as many as $\lfloor 25/2 \rfloor$ allows. The gap is in the general odd case, and it is a factor of two wide in the correction term.
The planar version of the same problem is a different matter. Boyland, Pintér, Laukó, Roth, Schoenfield and Wasielewski solved it when one side is even. Two odd sides is open, and A264041 is known only as far as integer programming reaches. Gluing the sides together makes the problem easier rather than harder.
For all $n, m \ge 2$ the maximum size of a configuration on the $n \times m$ torus is $\lfloor nm/2 \rfloor$. In particular $m(n) = \lfloor n^2/2 \rfloor$, so $m(2n+1) = 2n^2 + 2n$.
Eight related questions came with the problem. Three of them are answered below: rectangular grids, the other surfaces, and the largest number of diagonals of a single direction. For the number of configurations up to symmetry there is data and no formula. Four are not addressed at all.
One direction at a time
Fix notation. The cells are $(x,y)$ with $0 \le x \le n-1$ and $0 \le y \le m-1$. The $\mathbf{a}$-diagonal of a cell joins $(x,y)$ to $(x+1,y+1)$, running south-west to north-east, and its $\mathbf{b}$-diagonal joins $(x+1,y)$ to $(x,y+1)$. On the torus the vertices form the group $\mathbb{Z}_n \times \mathbb{Z}_m$, and the two directions are the elements $\mathbf{a} = (1,1)$ and $\mathbf{b} = (1,-1)$. Put $g = \gcd(n,m)$ and $L = \operatorname{lcm}(n,m)$.
Let $H_{\mathbf{a}}$ be the multigraph on the vertices whose edges are all the $\mathbf{a}$-diagonals. Distinct $\mathbf{a}$-diagonals lie in distinct cells, so a matching in $H_{\mathbf{a}}$ is already a configuration. The cylinder and the Möbius strip aside, everything below comes out of one description of that graph.
On the $n \times m$ torus, $H_{\mathbf{a}}$ is a disjoint union of $g$ cycles of length $L$, whose vertex sets are $C_k = \{(x,y) : x - y \equiv k \bmod g\}$ for $0 \le k \le g-1$.
Every vertex $v$ lies on exactly two $\mathbf{a}$-diagonals, $\{v - \mathbf{a}, v\}$ and $\{v, v + \mathbf{a}\}$, so $H_{\mathbf{a}}$ is $2$-regular and its components are the cosets of $\langle \mathbf{a} \rangle$, a subgroup of order $L$. The map $(x,y) \mapsto x - y \bmod g$ is a surjective homomorphism onto $\mathbb{Z}_g$ that kills $\mathbf{a}$, and its kernel has $nm/g = L$ elements. So the kernel is $\langle \mathbf{a} \rangle$ and its cosets are the sets $C_k$. In the one degenerate case $L = 2$ a cycle means a pair of parallel edges.
A matching in a disjoint union of $g$ cycles of length $L$ has at most $g \lfloor L/2 \rfloor$ edges. Reflecting in a horizontal line exchanges the two directions, so the same bound holds for $\mathbf{b}$-diagonals. That settles Kagey's fourth related question.
On the $n \times m$ torus the largest number of $\mathbf{a}$-diagonals in a configuration is $nm/2$ when $nm$ is even and $(nm-g)/2$ when $n$ and $m$ are odd. Both values are attained inside a configuration of maximum size. When $n$ and $m$ are odd, every maximum configuration contains at least $(g-1)/2$ and at most $(nm-g)/2$ diagonals of each direction.
For $n = m = 2k+1$ the one-direction maximum is $(n^2-n)/2 = 2k^2 + k$. That is exactly Kagey's lower bound. His bound is therefore the best that one direction can do, and it cannot be improved without mixing the two. His $5 \times 5$ figure splits $10$ and $2$, which is the most lopsided split the theorem allows.
When $nm$ is even the cycles have even length $L$, each has a perfect matching, and the union of those matchings is a configuration with $nm/2$ diagonals, all of one direction. For even $n$ it is simply the $\mathbf{a}$-diagonals of every cell with $x$ even. That case needs nothing else.
The odd case
Now let $n$ and $m$ both be odd, so $g$ and $L$ are odd too. A matching inside an odd cycle misses a vertex. The best that $\mathbf{a}$-diagonals alone can do leaves $g$ vertices uncovered, one per cycle, which is $(g-1)/2$ short of the trivial bound. A handful of $\mathbf{b}$-diagonals makes up the difference.
Write $v_k = (0,-k)$ for the starting vertex of $C_k$, and set $\lambda_k = 0$ when $k \equiv 0, 1$ and $\lambda_k = 1$ when $k \equiv 2, 3 \pmod 4$. Choose the matching on $C_k$ so that the vertex it misses is $u_k = v_k + \lambda_k \mathbf{a}$. The $\mathbf{b}$-diagonal of the cell $(0,-k-1)$ joins $(0,-k)$ to $(1,-k-1)$, and when $\lambda_k = 0$ and $\lambda_{k+2} = 1$ those two vertices are exactly $u_k$ and $u_{k+2}$. Take one such diagonal for every $k \le g-3$ with $k \equiv 0, 1 \pmod 4$. The pairs $\{k, k+2\}$ that occur are disjoint and cover every index but one, namely $g-1$ when $g \equiv 1$ and $g-2$ when $g \equiv 3 \pmod 4$.
No cell is used twice. The $\mathbf{b}$-diagonals sit in the cells $(0,-k-1)$ of the single cell column $x = 0$, and those cells are distinct because $k < g \le m$. The $\mathbf{a}$-diagonal of the cell $(0,-k-1)$ is the edge of $C_{k+1}$ running from $v_{k+1}$ to $v_{k+1} + \mathbf{a}$, and the matchings above only ever use edges further along the cycle than that one.
Counting gives $g \cdot (L-1)/2$ diagonals of the first kind and $(g-1)/2$ of the second, which is $(nm-1)/2$ in all. With the even case that proves the theorem.
When $nm$ is even the bound $\lfloor nm/2 \rfloor$ is attained by diagonals of a single direction. When $n$ and $m$ are odd it is attained by a configuration with exactly $(g-1)/2$ diagonals of the second direction, all of them in one column of cells.
For $n = m = N$ odd there is a change of coordinates that explains why the trivial bound ought to be reachable. Since $2$ is invertible modulo $N$, the map $(x,y) \mapsto ((x+y)/2, (x-y)/2)$ is a bijection of $\mathbb{Z}_N^2$ turning the steps $\mathbf{a}$ and $\mathbf{b}$ into the unit steps $(1,0)$ and $(0,1)$. The graph of all diagonals becomes the torus grid graph $C_N \times C_N$, which has plenty of matchings missing one vertex. The two diagonals of a cell become a horizontal and a vertical edge sharing a midpoint, and the construction never uses both.
Four more surfaces
Kagey's third related question asks for the cylinder, the Klein bottle and the projective plane. The Möbius strip comes along for free. All four are the same grid of $n \times m$ cells with the vertices $(x,y)$, $0 \le x \le n$ and $0 \le y \le m$, identified in different ways.
| Surface | Identifications | Maximum |
|---|---|---|
| cylinder | $(n,y) \sim (0,y)$ | $n \lceil m/2 \rceil$ |
| Möbius strip | $(n,y) \sim (0,m-y)$ | $n \lceil m/2 \rceil - [\,m \equiv 1 \bmod 4\,]$ |
| torus | $(n,y) \sim (0,y)$, $(x,m) \sim (x,0)$ | $\lfloor nm/2 \rfloor$ |
| Klein bottle | $(n,y) \sim (0,m-y)$, $(x,m) \sim (x,0)$ | $\lfloor nm/2 \rfloor$ |
| projective plane | $(n,y) \sim (0,m-y)$, $(x,m) \sim (n-x,0)$ | $\lfloor (nm+1)/2 \rfloor - [\,n, m \text{ odd},\ n \equiv m \bmod 4\,]$ |
The cylinder and the Möbius strip are counted by levels, the argument Boyland and his coauthors use for planar grids with an even side. A diagonal in cell row $y$ joins a vertex at level $y$ to one at level $y+1$, so if $s_y$ counts the diagonals in row $y$ then $s_0 \le n$ and $s_{y-1} + s_y \le n$. Grouping the rows in consecutive pairs gives $n \lceil m/2 \rceil$, and the $\mathbf{a}$-diagonals of the even rows attain it.
On the Möbius strip the levels fold together in pairs and the same grouping gives the same answer, except when $m \equiv 1 \pmod 4$. There the bound would force every cell of the middle row to carry a diagonal. Two horizontally adjacent cells cannot carry diagonals of different directions, so all $n$ would have to run the same way, and then the diagonal in the last cell of the row meets the diagonal in the first cell across the twist. One diagonal has to go, and losing exactly one is achievable.
The Klein bottle and the projective plane go back to $H_{\mathbf{a}}$. On the Klein bottle the cycles have length $2n$ or $n$, and an odd cycle needs $n$ odd together with $2y \equiv -n \pmod m$, which has one solution for odd $m$ and none for even $m$. So $H_{\mathbf{a}}$ has no odd cycle when $nm$ is even and exactly one when $nm$ is odd, and a maximum matching has $\lfloor nm/2 \rfloor$ edges. The Klein bottle maximum is reached with diagonals of one direction only.
On the projective plane the corner $Q = (n,0) \sim (0,m)$ is isolated in $H_{\mathbf{a}}$ and the rest is again $2$-regular with at most one odd cycle. Matchings therefore give $\lfloor (nm+1)/2 \rfloor$ unless $n$ and $m$ are both odd, when they stop at $(nm-1)/2$. When $n$ and $m$ are both odd with $n \not\equiv m \pmod 4$ one can beat the matchings. Place a short list of $\mathbf{b}$-diagonals first, chosen so that it deletes one vertex from the odd cycle and two adjacent vertices from certain even cycles. Every component left over is then a path or a cycle with an even number of vertices, so the rest of the graph matches perfectly, and it can do so while avoiding the cells the $\mathbf{b}$-diagonals already occupy. For $n \equiv m \pmod 4$ no such configuration exists. The parity of $x+y$ survives both identifications, and the two ends of a diagonal always have the same parity. Both classes contain $(nm+1)/2$ vertices, which is odd because $nm \equiv 1 \pmod 4$, so each class has to leave a vertex uncovered.
Counting the maxima
Kagey's first related question asks for the number of configurations up to the action of the symmetry group. Here there is data and no formula. Let $T(n,m)$ be the number of configurations of maximum size $\lfloor nm/2 \rfloor$ on the $n \times m$ torus, with every cell labelled.
| $T(n,m)$ | $m=2$ | $3$ | $4$ | $5$ | $6$ | $7$ | $8$ |
|---|---|---|---|---|---|---|---|
| $n=2$ | 12 | 16 | 40 | 64 | 144 | 256 | 544 |
| $3$ | 54 | 44 | 330 | 148 | 1,806 | 548 | |
| $4$ | 188 | 308 | 1,132 | 2,444 | 8,132 | ||
| $5$ | 4,350 | 2,116 | 52,920 | 17,452 | |||
| $6$ | 13,356 | 33,028 | 185,092 | ||||
| $7$ | 1,346,226 | 554,852 | |||||
| $8$ | 5,240,076 |
When $nm$ is odd the translations move the uncovered vertex, so they act freely on the maximum configurations and $nm$ divides $T(n,m)$. The symmetry group of the square torus is larger, consisting of the $8n^2$ maps $v \mapsto Av + s$ with $A$ in the dihedral group of the square, four of whose eight choices of $A$ exchange the two directions of diagonals.
| $n$ | $2$ | $3$ | $4$ | $5$ | $6$ | $7$ |
|---|---|---|---|---|---|---|
| $T(n,n)$ | 12 | 54 | 188 | 4,350 | 13,356 | 1,346,226 |
| up to translation | 5 | 6 | 23 | 174 | 413 | 27,474 |
| up to the full group | 2 | 2 | 9 | 27 | 74 | 3,483 |
The first two rows of the first table follow a pattern that we have not proved.
For $m \ge 2$,
$$T(2,m) = \begin{cases} 2^{m+1}, & m \text{ odd},\\ 2^{m+1} + 2^{m/2+1}, & m \text{ even},\end{cases} \qquad T(3,m) = \begin{cases} 2m\,(2^m+1), & m \text{ odd},\\ 2^{m+1} + 2^{m/2+1} + 4, & m \text{ even}.\end{cases}$$The formulas were fitted to $m \le 8$. Their values for $9 \le m \le 14$ were written into the ledger before those cases were computed, and all twelve came out right. The four surface formulas were arrived at the same way, read off integer programming data for $n, m \le 9$ and tested against pre-registered larger cases, and those four were then proved.
One of these rows is already in the OEIS under another description. The values of $T(2,m)$ agree with A253122, an unrelated count of $0$-$1$ arrays, for every $m$ from $2$ to $14$, and A253122 carries the same closed form as an empirical conjecture of its own. The other rows of the table, the diagonal $T(n,n)$ and both orbit sequences return nothing from the OEIS.
A closed formula for $T(n,n)$, or for either orbit count, would answer Kagey's first related question. We have no guess. For odd $n$ the change of coordinates above turns $T(n,n)$ into a count of near-perfect matchings of $C_n \times C_n$ subject to a local exclusion rule.
What was checked
Every number on this page was produced by two methods.
- Maxima: an integer program with one binary variable per diagonal and one constraint per vertex and per cell, and an exhaustive search that decides each vertex in turn to be uncovered or covered by one of the at most four diagonals that end there. The integer program covers $2 \le n, m \le 9$ on all five surfaces. The search reaches the torus for $n \le 7$ and $m \le 8$ apart from $7 \times 8$, and the other four surfaces for $nm \le 30$. The two never disagree.
- The model itself was tested against the planar problem, where the answers are known. It reproduces A264041 for $n \le 8$ and the solution counts A264667 for $n \le 6$.
- The constructions were built in coordinates and checked directly for repeated cells and repeated endpoints, for all $2 \le n, m \le 80$ on the torus, for $n = m$ up to $301$, and for $2 \le n, m \le 40$ on the other four surfaces.
- Counts: the exhaustive search and a transfer matrix over cell rows, run in both orientations, which makes the symmetry $T(n,m) = T(m,n)$ a check as well.
- Orbit counts: orbit enumeration with marking, and Burnside's lemma.
Kagey's remaining related questions are open as far as we know. They ask for higher dimensions, for hexagonal and triangular grids, for configurations in which diagonals may touch but may not close a cycle, and for a version that colours edges instead of drawing diagonals. Boyland and his coauthors treat three-dimensional arrays without identifications, which is the nearest thing to an answer for the first of those.