The Pattern Avoidance Game

A zero-one matrix A contains another zero-one matrix P if some submatrix of A can be transformed to P by changing some ones to zeroes. Otherwise A avoids P.

Consider the following pattern avoidance game, denoted by PAG(n, P). Starting with the n x n all zeroes matrix, two players take turns changing zeroes to ones. If any player's turn causes the matrix to contain the pattern P, then that player loses.

If no dimension of P exceeds n, then PAG(n, P) will always have a winner. Define W(n, P) to be the winner of PAG(n, P) if both players employ optimal strategies.

P is a 1 x k matrix of all ones
W(n, P) is Player [(k-1)n+1 mod 2]+1. This follows from ex(n, P) = n(k-1) since there are n rows, and you can only fill each row with k-1 ones. If n(k-1) is odd, Player 1 wins. Otherwise, Player 2 wins.

P is the 2 x 2 identity matrix
W(n, P) is Player 1. On the first turn, Player 1 puts a 1 in the top left corner. After this turn, both players can only play in the top row and leftmost column. There are 2n-1 entries that are in the top row or leftmost column, so Player 1 wins.

Mimicking strategies
If n is even and at least one of j and k is odd, player 2 wins. Say k is odd, then after player 1 flips (i, 2d+1), player 2 flips (i, 2d+2), or vice versa. So after each move of player 2 column 2d+1 and 2d+2 are identical for all d, and the matrix cannot start to contain P which has an odd number of identical columns after player 2's move.

So, to generalize: if n is even and P is composed of an odd number of identical rows or columns, W(n, P)=2.

W(k, P) for k x k P
If n = k and P is a k x k pattern with a nonzero entry, then W(n, P) = k^2 (mod 2) + 1.

Multi-player pattern avoidance game
For an even k, if players of the same parity agree on their optimal strategy, then the loser L_2(n,P) = L_k(n,P) mod 2, where the subscript means the number of players. This is because if L_2(n,P)=1, then player 0, 2, 4, ..., k-2 can collectively deploy player 0's winning strategy as if there are only 2 players to make sure not to lose. Similar argument goes for if L_2(n,P)=0. This argument only works if players 0, 2, 4, ..., k-2 agree to use player 0's optimal strategy.

To generalize, L_d(n,P) = L_{md}(n,P) mod d.

Chinese Remainder Theorem
By the Chinese remainder theorem, L_M(n,P) can be uniquely determined based on L_{m_1}(n,P), …, L_{m_k}(n,P) in the multi-player game, assuming that the non-losing players agree on their optimal strategy.

Multi-player 2 x 2 identity matrix
$L_d(h, w, P)=(w+h-1)\mod d$ if $P$ is $2\times 2$ identity matrix. Player index is 0-based.

To see this, first define a maximal $h\times w$ matrix as a matrix that avoids $P$ and will contain $P$ if any of its zero is flipped to one. We claim that all maximal matrices have $w+h-1$ ones. Now let $A$ be a maximal matrix.

Every column of $A$ has at least an one entry. To see this, take any entry $A_{i,j}$, there cannot be an one entry both to its upper left and lower right direction. If there's an one to its upper left, decrease $i$ until there isn't -- now there's an one to its left, and none to its lower right, so $A_{i,j}$ should be one.

For $1\leq i\leq w$, define $h_i$ as the smallest row index such that $A_{h_i,i}=1$. The process above shows that $\{h_i\}$ is a non-increasing sequence. Moreover, for each column $i>1$ there's no reason any of $A_{h_i,i}, A_{h_i+1,i},\ldots,A_{h_{i-1},i}$ is not one. Similarly $A_{h_1,1},\ldots,A_{h,1}$ should all be one. So each column of $A$ has consecutive ones overlapping with its previous column at exactly one position. The total number of ones is thus $w+h-1$.

Multi-player 2 x 2 x ... x 2 identity matrix
The following generalizes to $d$-dimensional matrix, and gives the number of one entries in any maximal matrix that 2-avoids a $2\times\ldots\times 2$ $d$-dimensional identity matrix $P$.

For notational simplicty we define {\it 2-contain} same as {\it contain}, except when $d=1$ a matrix {\it 2-contains} $P$ as long as it has multiple one entries. Likewise {\it 2-avoid} and $ex2$ follows naturally from {\it 2-contain} thus defined.

Define $ex2(w_1,\ldots,w_d,P)$ the maximum number of one entries in any $w_1\times\ldots\times w_d$ matrix that 2-avoids $P$.

Theorem: $ex2(w_1,\ldots,w_d,P)=\prod_{i=1}^Dw_i-\prod_{i=1}^d(w_i-1)$. Moreover, every maximal $w_1\times\ldots\times w_d$ matrix that 2-avoids $P$ has exactly $ex2(w_1,\ldots,w_d,P)$ one entries. A maximal matrix is one such that flipping any zero to one makes it 2-contain $P$.

The theorem is trivial when $d=1$ or when $w_d=1$.

Let $M$ be a maximal $w_1\times\ldots\times w_d$ matrix that 2-avoids $P$. We focus on the $d$-rows of $M$.

A $d$-row $x=(x_1,\ldots,x_{d-1})$ is a {\it ancestor} of $d$-row $y=(y_1,\ldots,y_{d-1})$ if $x_i<y_i$ for $1\leq i\leq d-1$. We say $y$ is a {\it descendant} of $x$. $A(x)$ and $D(x)$ are the sets of ancestors and descendants of $x$, respectively. Define $M(x,x_d)=M_{x_1,\ldots,x_d}$. Ancestor and descendant relations are transitive, and the graphs of these two relations are directed acyclic.

Lemma 1: Every $d$-row of $M$ has some one entry.

Proof: Given a $d$-row $x$, if $A(x)$ is empty, then $M(x,w_d)=1$. If $D(x)$ is empty, then $M(x,1)=1$. If both sets are not empty, $h(A(x))$, the smallest $d$-coordinate of one entries in $A(x)$ must be greater than or equal to $l(D(x))$, the largest $d$-coordinate of one entries in $D(x)$, otherwise $M$ 2-contains $P$. Then pick some $y$ in $[l(D(x)), h(A(x))]$ and having $M(x,y)=1$ doesn't make $M$ 2-contain $P$.

Lemma 2: Given any $d$-row $x$, the set $\{y:M(x,y)=1\}$ forms an integer arithmetic progressive sequence with common difference 1.

Proof: If $M(x,y_1)=M(x,y_2)=1$ for $y_1\leq y_2$, then setting any $y$ in $[y_1,y_2]$ doesn't make $M$ 2-contain $P$.

For a $d$-row $x$ define $h(x)$ and $l(x)$ as the minimum and maximum $y$ such that $M(x,y)=1$, respectively. $w(M)$, the number of one entries in $M$, is then $\sum_{x}l(x)-h(x)+1$.

Lemma 3: $l(x)=\min(w_d, \min_{z\in A(x)}h(z))$

Proof: If $l(x)>h(z)$ for some $z\in A(x)$, then $M$ 2-contains $P$. On the other hand, if $l(x)<\min_{z\in A(x)}h(z)$, setting $M(x,\min_{z\in A(x)}h(z))=1$ doesn't make $M$ 2-contain $P$.

Lemma 4: $h(x)=\max(1, \max_{z\in D(x)}l(z))$

Proof: If $h(x)\max_{z\in D(x)}l(z)$, setting $M(x,\max_{z\in D(x)}l(z))=1$ doesn't make $M$ 2-contain $P$.

We're now going to prove the theorem by mathematical reduction on $d$ and $w_d$ through the following lemmas. Assume the theorem holds with $1,2,\ldots,d-1$ dimensions, and for $(w1,\ldots,w_{d-1},1),(w1,\ldots,w_{d-1},2),\ldots,(w1,\ldots,w_{d-1},w_d-1)$.

Lemma 5: A matrix $M$ 2-avoiding $P$ is maximal if $l(x)$ and $h(x)$ satifies equations in Lemma 3 and Lemma 4 for every $d$-row $x$.

We say that a $d$-row $x=(x_1,\ldots,x_{d-1})$ is a {\it semi-ancestor} of $d$-row $y=(y_1,\ldots,y_{d-1})$ if $x_i\leq y_i$ for all $i$ in $[1,d-1]$ and $x_ih(x)$, and $x'$ has no other descendant $z$ with $l(z)=w_d$.

Proof: In the first stage we find a $d$-row $x$ with $l(x)=w_d>h(x)$ and $A(x)\neq\emptyset$. Starting from any $d$-row $x$ with $l(x)=w_d$, if $h(x)=w_d$, then there is descendant $z$ of $x$ with $l(z)=w_d$. We move from $x$ to $z$ and see if $h(z)h(x)$, which is guaranteed to have non-empty ancestor set $A(x)$. Such $x$ exists because there are only finite number of $d$-rows, and whenever we hit one without descendants, $h(x)=1$. We set $x=x''$ and $x'=(x_1-1,\ldots,x_{d-1}-1)$.

In the second stage, if $x'$ has no other descendant $z$ with $l(z)=w_d$ then we're done. If it does, $D(z)\subset D(x)$ so $h(z)\leq h(x)<w_d$, i.e. $z$ qualifies the requirement for $x$ too. Moreover $x$ is a semi-ancestor of $z$, so we reset $x$ to $z$. We repeat this process again and again until $x'$ has no other descendant $z$ with $l(z)=w_d$. This process terminates because in the sequence of $x$ we found, each element is a semi-ancestor of it subsequent element, and this sequence couldn't be infinitely long.

Lemma 7: If the set $\{x:l(x)=w_d, A(x)\neq\emptyset\}\neq\emptyset$, we can convert $M$ to another maximal matrix $M'$ that 2-avoids $P$ with the set smaller and yet $w(M)=w(M')$.

Proof: We find $d$-rows $x$ and $x'$, such that $x=(x'_1+1,\ldots,x'_{d-1}+1)$, $l(x)=w_d>h(x)$, and $x'$ has no other descendant $z$ with $l(z)=w_d$.

Construct $M'$ identical to $M$ except $l'(x)=w_d-1$ and $h'(x')=w_d-1$. $M'$ has fewer $d$-rows in the set $\{x:l'(x)=w_d, A(x)\neq\emptyset\}$, plus $w(M')=w(M)$.

We now show that $M'$ is still a maximal matrix that 2-avoids $P$. The only changes we make are to decrease $l(x)$ and $h(x')$ by 1. Clearly equation in Lemma 3 is not violated by $M'$, because $x'$ is an ancester of $x$. The only possibility that $M'$ violates equation in Lemma 4 is that $x'$ has another descendant $z$ with $l(z)=w_d$. But this is impossible because of the choice we make with $x'$, so we're done.

Lemma 8: If $\{x:l(x)=w_d,A(x)\neq\emptyset\}=\emptyset$, then $w(M)=\prod_{i=1}^Dw_i-\prod_{i=1}^d(w_i-1)$.

Proof: Transform $M$ to $M'$ by removing the $w_d$-th $d$-cross section. Then since $l(x)=w_d$ if $A(x)=\emptyset$, $w(M')=w(M)-|\{x:A(x)=\emptyset\}|$.

By assumption $$w(M')=w_1\times\ldots\times w_{d-1}(w_d-1)-(w_1-1)\times\ldots\times (w_{d-1}-1)(w_d-2)$$

$A(x)=\emptyset$ if and only if at least one of the coordinates $x_1,\ldots,x_{d-1}$ is 1. So $|\{x:A(x)=\emptyset\}|=w_1\times\ldots\times w_{d-1}-(w_1-1)\times\ldots\times (w_{d-1}-1)$. Adding them together, $w(M)=\prod_{i=1}^Dw_i-\prod_{i=1}^d(w_i-1)$.

Finally, given any matrix $M$ that maximally 2-avoids $P$, if necessary by applying Lemma 7 a finite number of times we can convert it to another maximal $M'$ with the same number of one entries, still 2-avoids $P$, and with empty $\{x:l(x)=w_d,A(x)\neq\emptyset\}$. And then by Lemma 8 we have $w(M)=\prod_{i=1}^Dw_i-\prod_{i=1}^d(w_i-1)$. This concludes our proof.

Related Open Problems
What is W(n, P) for each 0-1 matrix P ?

If there are k players, which player loses for each n and P?

For each n, 0-1 matrix P, and k > 1, what is ex(n, P) mod k?

Thread: http://www.artofproblemsolving.com/polymath/mitprimes2016/f/c195578h1228277_pattern_avoidance_game