Open problems / 112

Rectangles on a grid, edges counted mod 2

Place rectangles along the lines of an $n \times m$ grid of unit cells. An edge counts as covered if it lies on the boundary of an odd number of them, so a coincident pair cancels. Kagey asks how many of the $2nm + n + m$ edges can be covered at once. The answer is $2nm + 2$ when $nm$ is odd or the grid is a single strip, and $2nm$ otherwise.

The setup

An $n \times m$ grid of unit cells has $(n+1)(m+1)$ vertices and $|E| = 2nm + n + m$ unit edges. A rectangle is a set $R = [a,b] \times [c,d]$ with integer corners inside the grid, and $\partial R$ is the set of unit edges on its perimeter. Work in the vector space $\mathbb{F}_2^E$ of edge sets, with symmetric difference as addition.

An arrangement is a finite family of rectangles, and its cover is $\sum_R \partial R$, the set of edges lying on an odd number of the perimeters. Let $\mathcal{C}$ be the set of all covers, and let

$$M(n,m) = \max_{H \in \mathcal{C}} |H|, \qquad N(n,m) = \#\{H \in \mathcal{C} : |H| = M(n,m)\}.$$

What is the greatest number of edges that can be covered on an $n \times m$ grid?

The figure attached to the problem puts three rectangles on the $4 \times 3$ grid and misses seven edges. That grid has $4$ columns and $3$ rows of cells and $31$ edges, of which $24$ are covered, which fixes the conventions used here.

++=
Kagey's three rectangles on the $4 \times 3$ grid and the cover they add up to. The rectangles are the columns $1$ to $3$, the middle row, and column $2$. Edges on two of the perimeters cancel, so the cover keeps $24$ of the $31$ edges. The seven missed edges are dashed. The dots are the ten boundary vertices of odd degree, and every missed edge has an end there. That arrangement is optimal, and three rectangles is also the fewest that reach the maximum on this grid.

Ten related questions came with the problem. Four of them are answered in full below, three in part, one only by computation on small grids, and two are not treated at all.

Covers are the even subgraphs

For a set $S$ of cells let $\delta(S)$ be the set of edges with a cell of $S$ on exactly one side, an edge on the boundary of the grid having only one cell next to it. An edge lies on at most two cells, so $\delta(S) = \sum_{c \in S} \partial c$.

For every rectangle $R$, $\partial R = \delta(\text{cells of } R)$. The map $S \mapsto \delta(S)$ is an isomorphism from $\mathbb{F}_2^{\,\mathrm{cells}}$ onto $\mathcal{C}$, so $\mathcal{C} \cong (\mathbb{Z}/2)^{nm}$ and there are exactly $2^{nm}$ covers. Moreover $\mathcal{C}$ is the set of all $H \subseteq E$ in which every vertex has even degree.

An edge inside $R$ has a cell of $R$ on both sides, an edge of the perimeter on one side, any other edge on neither. So every cover is some $\delta(S)$, and every $\delta(S)$ is the cover of the arrangement of the unit cells in $S$. The map is linear, and its kernel is trivial: walk from a cell of $S$ in a straight line out of the grid, and the first edge at which the walk leaves $S$ belongs to $\delta(S)$. Each $\partial R$ is a cycle, so all degrees in a cover are even. For the converse, colour cell $(i,j)$ by the parity of the number of edges of $H$ crossed on the way from that cell out through the left side. Counting degrees along a row shows that the resulting $\delta(S)$ is $H$.

This settles two of the related questions. The group generated by placing rectangles is elementary abelian of rank $nm$, the unit cells are a basis, and its order $2^{nm}$ is the number of possible covers. And an edge set with all degrees even is an edge-disjoint union of cycles, each component of which has an Eulerian circuit, so the main question asks for the largest subgraph of the grid graph all of whose components are Eulerian.

It is convenient to read a cover as a $0/1$ colouring of the cells. The cover is then the set of edges separating cells of unlike colour, with the outside counted as $0$, and placing a rectangle flips the colours of the cells inside it.

The largest cover

Let $T$ be the set of vertices of odd degree in the grid graph. A vertex has degree $4$ minus the number of sides of the grid it lies on, so $T$ is the set of $2(n-1) + 2(m-1)$ boundary vertices other than the four corners. Since $\deg_J = \deg_G - \deg_H$, the proposition says that $H$ is a cover exactly when $J = E \setminus H$ has odd degree at every vertex of $T$ and even degree everywhere else. Such a $J$ is a $T$-join. Writing $\tau(n,m)$ for the least size of one,

$$M(n,m) = |E| - \tau(n,m),$$

and $N(n,m)$ is the number of $T$-joins of size $\tau$. Finding $\tau$ is the Chinese postman problem on the grid graph, and $\tau$ is the number of edges a postman has to walk twice.

For all $n, m \ge 1$,

$$M(n,m) = \begin{cases} 2nm + 2 & \text{if } nm \text{ is odd or } \min(n,m) = 1,\\ 2nm & \text{otherwise.}\end{cases}$$

Equivalently, the least number of missed edges is $\tau = n + m - 2$ in the first case and $\tau = n + m$ in the second.

For the lower bound, colour cell $(i,j)$ with $[i \text{ odd}] + [j \text{ even}] \bmod 2$. Adjacent cells get unlike colours, so all $2nm - n - m$ interior edges are covered, and counting the boundary edges next to a cell of colour $1$ gives $2nm + 2$ when $n$ and $m$ are both odd and $2nm$ otherwise. If $m = 1$ the perimeter of the whole grid is a cover with $2n + 2$ edges. For the upper bound, $2|J| = \sum_v \deg_J(v) \ge |T|$, so $\tau \ge |T|/2 = n + m - 2$. Suppose $n, m \ge 2$ and one of them is even. Two vertices of $T$ are adjacent only when they are consecutive on one side, so the graph induced on $T$ is four paths, on $n-1$, $n-1$, $m-1$ and $m-1$ vertices. One of them has odd order, so there is no perfect matching and $\tau \ge n + m - 1$. Any two $T$-joins differ by a cover, which is a sum of four-edge cell boundaries, so all $T$-joins have the same parity. The colouring above misses $n + m$ edges, so $\tau \ge n + m$.

The two cases differ by exactly two edges, and the difference is a parity obstruction. On the $4 \times 3$ grid the colouring in the proof is Kagey's own arrangement, so his figure is optimal and $M(4,3) = 24$.

Counting the largest covers

Kagey also asks how many maximal covers there are. Counting minimum $T$-joins answers it for every $n$ and $m$.

$N(n,m)$ is symmetric in $n$ and $m$, and

  • $N(n,1) = F_n$, the Fibonacci numbers.
  • $N(n,m) = 1$ if $n, m \ge 3$ are both odd.
  • $N(2,2) = 7$.
  • $N(n,m) = 8 + e/2$ if one of $n, m$ is $3$ and the other is an even number $e$.
  • $N(n,m) = 8$ in all remaining cases with $n, m \ge 2$.

When $n, m \ge 3$ are both odd, $\tau = |T|/2$, so a minimum $T$-join is a perfect matching of the four paths. Each path has even order and so has exactly one matching. The largest cover is then unique, and it is the checkerboard colouring with the corner cells coloured $1$. When $m = 1$ the graph on $T$ is a ladder with $n-1$ rungs. Its number of perfect matchings satisfies $p_k = p_{k-1} + p_{k-2}$ with $p_0 = p_1 = 1$, since the last rung is either used or covered along with the rung before it, so $p_{n-1} = F_n$, the Fibonacci numbers of A000045.

The remaining case is where the $8$ comes from. A minimum $T$-join now has $|T|/2 + 2$ edges, so the degrees carry a total excess of $4$. Away from the exceptions below, that excess sits on two vertices outside $T$, each of which carries a link, a path of two edges joining two vertices of $T$. A link can only sit at a corner of the grid, passing either through the corner itself or through its diagonal neighbour, and it puts one of its two ends on each of the two sides meeting there. A side holds an odd number of the four ends exactly when it holds an odd number of $T$-vertices, which happens when its length is even. So if $n$ and $m$ are both even, every side holds exactly one end and the two links sit at opposite corners. If one of them is even and the other odd, only the two sides of even length hold an odd number, which forces the links to share a side of odd length and sit at its two ends. Either way there are two choices of corner pair and two choices of route at each end.

links at the top left and bottom right cornerslinks at the bottom left and top right corners
The eight largest covers of the $6 \times 4$ grid, each drawn by the ten edges it misses rather than the forty-eight it covers. Both side lengths here are even, so the two links sit at opposite corners. The top row uses one diagonal of the grid and the bottom row the other, and within a row each link runs either through the corner or through the corner's diagonal neighbour. The two dots in each panel mark where the links turn. These are the configurations enumerated by the theorem, computed here from the definition.

Two cases break the pattern. On the $2 \times 2$ grid the diagonal neighbour of every corner is the centre, so a pair of links can merge there into one vertex of degree $4$. Both diagonals give the same $T$-join that way, the four edges at the centre, so the count is $7$ and not $8$. When one of the two sides has length $3$ there are further $T$-joins in which the two excess vertices are adjacent, so that in place of two links there is a single straight path of three edges spanning the grid across its side of length $3$. There are $e/2$ of those, one for each odd position along the even side.

Here are the values of $N(n,m)$, with $n$ running across the top and $m$ down the side.

N12345678910
$m=1$11235813213455
$m=2$1798888888
$m=3$29110111112113
$m=4$38108888888
$m=5$5818181818
$m=6$88118888888

The fewest rectangles

Write $\rho(n,m)$ for the least number of rectangles in an arrangement whose cover is a largest one, and $A(n,m)$ for the number of sets of $\rho$ rectangles that do it. The count comes from one invariant. For an edge set $H$ let $K(H)$ be the set of vertices meeting an odd number of horizontal edges of $H$. Then $K$ is additive, and $K(\partial R)$ is the set of the four corners of $R$. If $H$ is a cover, a vertex of $K(H)$ has degree $2$ in $H$ with the two edges perpendicular, so $K(H)$ is the set of vertices at which $H$ turns.

If $k$ rectangles have cover $H$, then $k \ge |K(H)|/4$, with equality only when the corner sets of the $k$ rectangles are pairwise disjoint and their union is $K(H)$. Conversely, rectangles whose corner sets partition $K(H)$ have cover $H$.

$\rho(n,m) = 1$ if $\min(n,m) = 1$ or $n = m = 2$. Otherwise $\rho(n,m) = \lfloor (n+m)/2 \rfloor$.

The construction is a pile of nested strips. Let $n, m \ge 2$, and since the answer is symmetric in the two, let $n$ be the even one when exactly one of them is even. Take $\lceil n/2 \rceil$ vertical strips $[s-1,\,n'-s] \times [0,m]$ and $\lfloor m/2 \rfloor$ horizontal strips $[0,n] \times [s,\,m'+1-s]$, both indexed by $s = 1, 2, \dots$, where $n'$ and $m'$ round $n$ up and $m$ down to even numbers. Column $i$ lies in a number of vertical strips with the parity of $i$, and row $j$ in a number of horizontal strips with the parity of $j-1$, so the arrangement produces exactly the colouring $[i \text{ odd}] + [j \text{ even}]$ used in the proof above. That choice of $n$ is what makes $\lceil n/2 \rceil + \lfloor m/2 \rfloor$ equal to $\lfloor (n+m)/2 \rfloor$ rather than one more.

five rectangles60 of 71 edges covered
The construction on the $6 \times 5$ grid, where $\lfloor (n+m)/2 \rfloor = 5$. Three nested vertical strips and two nested horizontal strips are drawn slightly inset from each other on the left so that they can be told apart. On the right, the shaded cells are those flipped by an odd number of the five rectangles, and the cover is the set of edges between cells of unlike shading together with the boundary edges next to a shaded cell. It covers $60$ of the $71$ edges, which is the maximum, and the eleven missed edges are dashed. Five is the fewest possible, and no set of four rectangles reaches any of the eight largest covers of this grid.

The lower bound is the lemma. Away from the cases the theorem excepts, every largest cover turns at $2(n+m)$ vertices when $n$ and $m$ are both odd, and at $2(n+m) - 2$ vertices otherwise, which can be read off the $T$-joins classified above. So $\rho \ge \lceil (n+m-1)/2 \rceil = \lfloor (n+m)/2 \rfloor$ in both cases.

When $n$ and $m$ are both odd the largest cover is unique and $|K(H)| = 4\rho$ exactly, so the lemma turns $A(n,m)$ into a counting problem about partitions of the boundary vertices.

If $n, m \ge 3$ are both odd, the number of sets of $(n+m)/2$ rectangles whose cover is the largest one is

$$A(n,m) = m!!\,(n-2)!! + n!!\,(m-2)!! - (n-2)!!\,(m-2)!!,$$

where $k!! = k(k-2)(k-4)\cdots$ for odd $k$ and $(-1)!! = 1$.

A rectangle in such a set has all four corners on the boundary of the grid, which forces it to be a full-width or a full-height strip. A strip is named by the pair of grid lines it runs between. A vertex $(0,y)$ with $0 < y < m$ is a corner of no vertical strip, so every interior horizontal line has to be paired off by the horizontal strips, and the same holds the other way round. What is being counted is therefore a perfect matching of $\{1, \dots, n-1\}$ together with one of $\{1, \dots, m-1\}$, adjusted for whichever rectangle takes the corner $(0,0)$. A set of $2k$ elements has $(2k-1)!!$ perfect matchings, the double factorials of A001147, and splitting by the three possibilities for that one rectangle, the whole grid or a shorter horizontal strip or a shorter vertical strip, gives three counts that add up to the formula. The first values are $A(3,3) = 5$, $A(5,3) = 21$, $A(5,5) = 81$, $A(7,3) = 135$ and $A(9,3) = 1155$.

When $n$ and $m$ are not both odd there is no formula. The values below were computed twice, by breadth-first search on the group $(\mathbb{Z}/2)^{nm}$ with all rectangles as generators, and by meet in the middle over subsets of rectangles. The columns headed fewest and sets are $\rho$ and $A$, and the last two are $\rho_\square$ and $A_\square$, the same pair when only squares may be placed.

GridMNFewestSetsFewest, squaresSets, squares
$2 \times 2$871111
$3 \times 2$12921121
$4 \times 2$16837248
$5 \times 2$20832458
$6 \times 2$248436068
$7 \times 2$288412078
$8 \times 2$3285252088
$3 \times 3$2013555
$4 \times 3$241032642
$5 \times 3$32142161
$6 \times 3$36114135710
$4 \times 4$328413642
$5 \times 4$40844062

The other questions

Squares only. A unit cell is a square, so squares generate the same group $\mathcal{C}$, and $M$ and $N$ are unchanged. What changes is the number of pieces. The table shows that more squares than rectangles are usually needed, though not always, since $\rho_\square(4,4) = \rho(4,4) = 4$. There is no formula for $\rho_\square$.

Every rectangle at once. The arrangement consisting of all $\binom{n+1}{2}\binom{m+1}{2}$ rectangles covers $(n+1)(m+1)$ edges when $n$ and $m$ are both odd, and no edge otherwise. Cell $(i,j)$ lies in $i(n+1-i)\,j(m+1-j)$ of the rectangles. If $n$ is even then one of $i$ and $n+1-i$ is even, so every cell gets colour $0$. If both are odd, the cells of colour $1$ are those with $i$ and $j$ odd, and they share no edges, so the cover is the disjoint union of their boundaries.

Counting mod $c$. Suppose it takes $c$ rectangles to cancel, so an arrangement gives each edge the number of perimeters through it as an element of $\mathbb{Z}/c$, and the edge is covered when that is non-zero. For $c \ge 3$ the arrangement of the $nm$ unit cells covers every one of the $2nm+n+m$ edges, since each edge lies on one or two cells and neither $1$ nor $2$ is zero mod $c$. So for $c \ge 3$ the answer to the main question is every edge. For odd $c$ and $n, m \ge 2$, the subgroup of $(\mathbb{Z}/c)^E$ generated by the perimeters is the set of vectors taking equal values on the two edges at each corner of the grid, which is isomorphic to $(\mathbb{Z}/c)^{|E|-4}$.

What is open

Five things are not settled.

  • There is no formula for $A(n,m)$ when $n$ and $m$ are not both odd. When $n + m$ is odd every largest cover still has exactly $4\rho$ turns, so the lemma again reduces $A$ to counting partitions of a known vertex set into corner sets of rectangles, and that count has not been carried out.
  • The largest cover obtainable with at most $k < \rho$ rectangles is known only by computation. On the $8 \times 2$ grid, one, two, three and four rectangles reach $20$, $22$, $26$ and $30$ edges, against $32$ with five.
  • The fewest squares $\rho_\square(n,m)$ is known only for the grids in the table.
  • The group generated mod $c$ is settled for odd $c$ only, and the number of distinct sets of covered edges mod $c$ is not counted for any $c \ge 3$.
  • Higher dimensions, the triangular grid, and covering vertices or facets instead of edges are not treated. The planar duality between covers and $T$-joins is what drives everything above, and it is lost in three dimensions.

What was checked

The verification script builds each $\partial R$ edge by edge and uses the closed forms only as the right-hand side of a comparison. Every check passes.

  • $M$ and $N$ by three methods. Enumeration of all $2^{nm}$ covers as edge sets, for $40$ grids. A transfer matrix over the rows of a cell colouring, for $m \le 8$ and $n \le 16$. And, for $M$ alone, shortest paths between the vertices of $T$ followed by a minimum-weight perfect matching, for the $25$ grids with $n + m \le 10$.
  • $\rho$, $A$ and the best cover with at most $k$ rectangles by two methods, for the $13$ grids of the table and for rectangles and squares alike. The methods are breadth-first search on $(\mathbb{Z}/2)^{nm}$ with all rectangles as generators, and enumeration of subsets by meet in the middle.
  • The formulas for $N$, $\rho$ and $A$ were read off data with $nm \le 16$. Their predictions on larger grids were written down before those cases were run. Seventeen predictions, all confirmed, are recorded in the ledger with the data they came from.

The numbers on this page were then recomputed from scratch for the write-up, with methods chosen to differ from the script: brute force over all $2^{nm}$ cell colourings for every grid with $nm \le 16$, a transfer matrix over columns rather than rows for $m \le 8$ and $n \le 12$, subset counting over the group for $\rho$ and $A$, and meet in the middle on the $6 \times 5$ grid. Every entry agrees with the paper, and the configurations in the three figures are the computed ones rather than drawings.