2

I am trying to compute the number of $n x m$ binary matrices with at most $k$ consecutive values of $1$ in each column.

I've figured out that I it will be enough to find the vectors with $1$ column and $n$ lines. Let's say that we shall find $p$ such vectors. The required matrices will be combinations of them, so their number will be $m^p$.

For $k = 2$ I've figured out that the answer resides in calculating a term of the Fibonacci sequence.

$fib(n) = fib(n-1) + fib(n-2)$
$fib(n-1) = fib(n-1)$

I can get from these 2 relations the matrix of the system, $A$, and I can find $fib(n)$ and $fib(n-1)$ by using the powers of $A$ and the first two terms from the Fib. seq.

I am trying to find a recurrence for $k$ > 2 in order to find the $A$ matrix for the general case $k > 2$, but I am stuck.

Note: I am trying to use the matrix form instead of calculating from the reccurence, because I am trying to design an efficient implementation for the algorithm.

1 Answers1

1

As you already noticed, it suffices to consider the number of possible patterns in one column of an $n\times m$ matrix. The result will be the $m$-th power according to the number of columns.

You could model the situation with generating functions. We start with some preliminary considerations.

Let's consider strings containing $0s$ and $1s$. We are looking for the number of strings with length $n$ and not more than $k$ consecutive $1s$. We build them starting with simpler patterns. Let $SEQ(1)=\{\varepsilon,1,11,111,\ldots\}$ denote all strings containing only $1s$ with length $\geq0$. The empty string is denoted with $\varepsilon$. The corresponding generating function is $z^0+z^1+z^2+\ldots=1/(1-z)$, with the exponent of $z^n$ marking the length $n$ of a string of $1s$ and the coefficient of $z^n$ marking the number of strings of length $n$. Let $SEQ^{\leq k}(1)$ be the set of all strings of $1s$ with length $\leq k$. The generating function for $SEQ^{\leq k}(1)$ is $1+z+z^2+\ldots+z^{k}=(1-z^{k+1})/(1-z)$.

How could we describe all strings containing $1s$ and $0s$ of length $\geq0$? First we may have leading $1s$. So we start with zero or more $1s$. Formally: $SEQ(1)$. Then we may have zero or more groups, each group starting with a single $0$ followed by zero or more $1s$. A single $0$ followed by zero or more $1s$ is $0 SEQ(1)$, with generating function $z(1+z+z^2\ldots)=z/(1-z)$. Zero or more groups of this pattern can be described as $SEQ(0 SEQ(1))$ with generating function $$\frac{1}{1-z\frac{1}{1-z}}$$ So, all strings containing $1s$ and $0s$ of length $\geq0$ can be described as $SEQ(1)SEQ(0 SEQ(1))$ with generating function $$\frac{1}{1-z}\frac{1}{1-z\frac{1}{1-z}}=\frac{1}{1-2z}=\sum_{n\geq0}(2z)^n$$ simply encoding, that there are $2^n$ different strings of length $n$ containing $1s$ and $0s$. We need a more complex variation of this theme, namely additionally restricting the length of consecutive $1s$ by $k$. This can be described as

$$SEQ^{\leq k}(1) SEQ(0 SEQ^{\leq k}(1))$$

The corresponding generating function $W^{\leq k}(z)$ is

$$W^{\leq k}(z)=\frac{1-z^{k+1}}{1-z}\frac{1}{1-z\frac{1-z^{k+1}}{1-z}}=\frac{1-z^{k+1}}{1-2z+z^{k+2}}$$

The number we want to find is the coefficient of $z^n$ in $W^{\leq k}(z)$. To extract it we start with some transformations. \begin{eqnarray} W^{\leq k}(z)&=&(1-z^{k+1})\sum_{t\geq 0}\left(2z-z^{k+2}\right)^t\\ &=&(1-z^{k+1})\sum_{t\geq 0}(-z)^t\left(z^{k+1}-2\right)^t\\ &=&(1-z^{k+1})\sum_{t\geq 0}(-z)^t\sum_{u=0}^{t}\binom{t}{u}z^{(k+1)u}(-2)^{t-u}\\ &=&(1-z^{k+1})\sum_{t\geq 0}(2z)^t\sum_{u=0}^{t}\binom{t}{u}\frac{1}{(-2)^u}z^{(k+1)u}\\ &=&(1-z^{k+1})\sum_{u\geq 0}\frac{1}{(-2)^u}z^{(k+1)u}\sum_{t\geq u}\binom{t}{u}{(2z)}^t\\ \end{eqnarray} Let the symbol $[z^n]f(z)$ denote the coefficient of $z^n$ in $f(z)$. So,

\begin{eqnarray} [z^n]W^{\leq k}(z)&=&[z^n](1-z^{k+1})\sum_{u\geq 0}\frac{1}{(-2)^u}z^{(k+1)u}\sum_{t\geq u}\binom{t}{u}{(2z)}^t\\ &=&\sum_{{u\geq 0}\atop{n\geq(k+1)u}}\frac{1}{(-2)^u}[z^{n-(k+1)u}]\sum_{t\geq u}\binom{t}{u}{(2z)}^t\\ &-&\sum_{{u\geq 0}\atop{n\geq(k+1)(u+1)}}\frac{1}{(-2)^u}[z^{n-(k+1)(u+1)}]\sum_{t\geq u}\binom{t}{u}{(2z)}^t\\ &=&\sum_{{u\geq 0}\atop{n\geq(k+1)u}}\frac{1}{(-2)^u}\binom{n-(k+1)u}{u}{2}^{n-(k+1)u}\\ &-&\sum_{{u\geq 0}\atop{n\geq(k+1)(u+1)}}\frac{1}{(-2)^u}\binom{n-(k+1)(u+1)}{u}{2}^{n-(k+1)(u+1)}\\ %&=&2^n\sum_{{u\geq0}\atop{n\geq(k+1)u}}\binom{n-(k+1)u}{u}\frac{(-1)^u}{2^{(k+2)u}}\\ %&+&2^n\sum_{{u\geq1}\atop{n\geq(k+1)u}}\binom{n-(k+1)u}{u-1}\frac{(-1)^u}{2^{(k+2)u-1}} \\ %&=&2^n\left(1+\sum_{{u\geq1}\atop{n\geq(k+1)u}}\left\{\binom{n-(k+1)u}{u}+2\binom{n-(k+1)u}{u-1}\right\}\frac{(-1)^u}{2^{(k+2)u}}\right) \\ &=&2^n\left(1+\sum_{{u\geq1}\atop{n\geq(k+1)u}}\frac{(-1)^u}{2^{(k+2)u}}\binom{n-(k+1)u}{u-1}\frac{n-ku+1}{u}\right) \end{eqnarray}

Finally, the number we were searching for is the $m$-th power of $[z^n]W^{\leq k}$

$$2^{nm}\left(1+\sum_{{u\geq1}\atop{n\geq(k+1)u}}\frac{(-1)^u}{2^{(k+2)u}}\binom{n-(k+1)u}{u-1}\frac{n-ku+1}{u}\right)^m$$

And the connection with Fibonacci Numbers? The Fibonacci numbers $0,1,1,2,3,5,8,13,\dots$ have generating function $F(z)=z/(1-z-z^2)$ and $W^{\leq1}(z)=(1-z^2)/(1-2z+z^3)$ starts with $1+2z+3z^2+5^3+\cdots$.

So, the connection given by their generating functions is

$$F(z)=z+z^2W^{\leq1}(z)$$

Note: I've found these techniques in Flajolets and Sedgewicks Analytic Combinatorics, section I.4.

Markus Scheuer
  • 108,315