6

Background information

Bit flips

Given a bit string, we say that bit flip happens when $0$ changes to $1$ or $1$ changes to $0$.

To find bit flips, we can shift the string by $1$ and xor that new string with the original one. The resulting string will have $1$ in the positions where bit flips happened in the original bit string.

Having this new representation, we can repeat this operation to get the number of bit flips on the bit string of bit flips. It somewhat resembles the second derivative (because we track the change of the change). Let's call it the second-order bit flip.

Let's consider the following example: $$ 01010001 $$

It's bit flip representation is: $$ 1111001 $$

Then the second order bit flip representation is: $$ 000101 $$

Counting the number of $1$s in every bit string above gives us the number of ones in the original bit string $k = 3$, the number of bit flips $m_1 = 5$, and the number of second-order bit flips $m_2 = 2$.

Counting bit strings with a certain number of bit flips

Now let's say we have a bit string of length $n$ with $k$ ones and $m$ bit flips. Can we count the number of such strings? Let's call this $A(n,k,m)$. Bit flips split the whole sequence into $m+1$ blocks, each filled with only $1$s or $0$s. Also a block of $1$s has to be followed by a block of $0$s. Depending on whether $m$ is even or odd, the number of blocks for $1$s and $0$s is either equal or differs by exactly $1$. Now, let's say that $m = 2m' + 1$, then we need to split our $1$s and $0$s into exactly $m' + 1$ blocks each. This can be done via $\binom{k - 1}{m'}$ for $1$s and $\binom{n-k-1}{m'}$ for $0$s. We have 2 ways of choosing the first block. Putting it all together: $$ A(n,k,2m'+1) = 2\binom{k - 1}{m'}\binom{n - k - 1}{m'} $$ For $m = 2m'$, we have $m' + 1$ blocks of one kind and $m'$ blocks of another kind. Choosing whether $1$ or $0$s get more blocks determines the very first block. Putting it all together: $$ A(n,k,2m') = \binom{k - 1}{m' + 1}\binom{n - k - 1}{m'} + \binom{k - 1}{m'}\binom{n - k - 1}{m' + 1} $$

Because of the nature of these counts, we can also say that: $$ \sum\limits_{m=0}^{n} A(n,k,m) = \binom{n}{k} $$

The actual problem

Counting bit strings with a certain number of second-order bit flips

Extending the previous problem to include second-order bit flips, we can now say that we have $m_1$ bit flips and $m_2$ second-order bit flips and we want to find $B(n,k,m_1,m_2)$, i.e. the number of bit strings of length $n$ with $k$ ones, $m_1$ bit flips and $m_2$ second-order bit flips.

Again, we can expect that: $$ \sum\limits_{i=0}^{n} B(n,k,m,i) = A(n,k,m) $$

I tried writing a recurrence for this problem and solve it via generating functions: Finding coefficient in a complex multivariable generating function. However, the complexity of the intermediate steps is quite hard and I abandoned that route (there are might be tricks that I'm not aware of that can make it easier).

I'd really appreciate any insights into how to solve this family of problems. Of course, it would be amazing to find a closed form solution for the N-order bit flips: $F(n,k,m_1,...,m_N)$, but any pointers are welcome. Many thanks in advance!

PS I might have reinvented some terms, so please correct me if you spotted familiar concepts.

  • 1
    One question for clarity's sake: are you interested in the number of strings with a prescribed number of bit, bit flips, and second-order flips, or just tallying the total number of strings with a given number of second-order flips? – Steven Stadnicki May 05 '24 at 00:02
  • @StevenStadnicki If I understood the question correctly, I'm looking for the first count for all 3 specified numbers. – Valeriy Savchenko May 05 '24 at 09:00
  • Assuming this has was obtained from the same recurrence as $B(n,k,m,l)$, it should be that $$ B(n,k,m,l) \approx (-1)^{l} \sum\limits_{a=0}^{n}(-1)^a \binom{n-a}{m}\binom{m}{a-k}\sum\limits_{b=0}^a (-1)^b\binom{a-b}{l}\binom{b}{m} \binom{m}{k-b} $$ We should just guess an appropriate interpretation for $a$ and $b$. – Oleksandr Kulkov May 06 '24 at 12:20
  • Isn't it true that the second order bit flip string = (original string) XOR (original string shifted by 2)? Not sure this is helpful to your problem though, since you want to control both $m_1$ and $m_2$. – antkam May 08 '24 at 02:13
  • @antkam this is exactly true, but as you mentioned it doesn't change much since I constraint for $m_1$ as well. – Valeriy Savchenko May 13 '24 at 13:14

2 Answers2

1

This is (for now) more of a comment, but doesn't fit into one.

The total number of bit flips is equal to the total number of $01$ and $10$ substrings. A similar argument can be made for second-order bit flips.

A second-order bit flip $01$ corresponds to $001$ or $110$ in the original string, and second-order $10$ corresponds to $100$ or $011$ in the original string. These appear exactly around the ends of each substring of length at least $2$ consisting of a single repeated characters, except for the very beginning and ends of the string.

For example, $010100011$ has only two such repeated-character substrings, the $000$ and $11$ at the end, characters, so it has three second-order bit flips: one at each end of each of the substrings, except for the end of $11$ because that is the end of the entire string.

Another way to count these is to add each of $0$ and $1$ to the beginnings and ends of each of the length-three substrings in turn (hence excluding any beginning and end occurrences), and count the total number of each of the resulting substrings. This means the number of second-order bit flips is the total number of each of the length-four substrings below (double-counting duplicate occurrences): $$ \begin{array}{c|cccc} 001 & {\color{grey}0}001 & {\color{grey}1}001 & 001{\color{grey}0} & 001{\color{grey}1} \\ 110 & {\color{grey}0}110 & {\color{grey}1}110 & 110{\color{grey}0} & 110{\color{grey}1} \\ 011 & {\color{grey}0}011 & {\color{grey}1}011 & 011{\color{grey}0} & 011{\color{grey}1} \\ 100 & {\color{grey}0}100 & {\color{grey}1}100 & 100{\color{grey}0} & 100{\color{grey}1} \end{array} $$

0

I pondered a bit on the problem using @Invariance's observation and here's what I got so far.

Building block

We need to count the number of ways to split $k$ elements into $m$ blocks with $p$ blocks having at least $2$ elements. We can address that by first choosing the blocks that will contain only one element $\binom{m}{m-p}$. Then we need to split the rest $k-(m-p)$ elements into $p$ blocks each having at least two elements. We can do it $\binom{k-(m-p)-p-1}{p-1} = \binom{k-m-1}{p-1}$ ways. This gives us the most basic formula to work with:

$$ \binom{m}{m-p}\binom{k-m-1}{p-1} $$

One problem that we have is with edge cases for $m$ and $p$, when $p = 0$ the second term becomes $0$ when actually it's actually possible when $k = m$. When $m = p$, the first term becomes $0$ when we actually don't need to choose a $1$-element block at all. This term should be $1$ instead for that particular case.

Now the observation was that the blocks with at least $2$ elements contribute $1$ to the number of second-order bit flips when they are on the edge and $2$ when they are inside. So, we need to be able to include/exclude edges from the count. That actually be done solely via the first term.

(a) If we need to ensure that one of the edges contains a non-trivial (with at least $2$ elements) block, we can't choose it as a block for only $1$ element (and we still need to choose $m-p$ blocks): $$ \binom{m-1}{m-p} $$

(b) If we need to ensure that one of the edge blocks is definitely trivial, we have to choose it, so we have only $m-1$ blocks to split into $m-p-1$ places: $$ \binom{m-1}{m-p-1} $$

(c) Both edges are non-trivial: $$ \binom{m-2}{m-p} $$

(d) Both edges are trivial: $$ \binom{m-2}{m-p-2} $$

(e) One edge is trivial, the other is non-trivial: $$ \binom{m-2}{m-p-1} $$

Our reasoning about $m = p$ case applies only when we are not excluding any of the edges, it should be $0$ in those cases.

Let's say that our original formula is $f(k, m, p)$, then we can denote cases (a-e) via $f_1(f,m,p)$, $f_0(f,m,p)$, $f_{11}(f,m,p)$, $f_{00}(f,m,p)$, and $f_{01}(f,m,p)$ correspndingly.

Solution

In order to build the solution, we have 4 different cases based on whether $m_1$ and $m_2$ are even or odd.

Let's start when they are both odd. It has the simplest logic. $m_1 = 2m_1' + 1$ means that $1$s and $0$s have exactly $m_1' + 1$ blocks each. $m_2 = 2m_2' + 1$ means that there are $m_2$ inner blocks and exactly one edge block. We don't know whether its $1$s or $0$s should have non-trivial blocks, so we need to consider all the possibilities: $$ 2\sum\limits_{i=0}^{m_2'} (f_1(k, m_1'+1, i+1)f_0(n-k, m_1'+1, m_2'-i) + f_0(k, m_1'+1, i)f_1(n-k, m_1'+1, m_2'-i+1)) $$ We multiply by $2$ because there are two ways of choosing which block comes first, $1$s or $0$s. We use $f_1$ and $f_0$ because $1$s and $0$s have exactly one edge block each.

For the case when $m_1 = 2m_1' + 1$ and $m_2 = 2m_2'$, we have a difference that we can have $m_2'$ inner non-trivial blocks or $m_2'-1$ inner blocks and $2$ edge blocks. Both edge blocks have to belong to different element ($1$ or $0$). Putting it together we have: $$ 2\sum\limits_{i=0}^{m_2'} f_0(k, m_1'+1, i)f_0(n-k, m_1'+1, m_2'-i) + 2\sum\limits_{i=0}^{m_2'-1}f_1(k, m_1'+1, i+1)f_1(n-k, m_1'+1, m_2'-i) $$

Moving onto the case with $m_1 = 2m_1'$, we have $m_1' + 1$ blocks of one kind and $m_1'$ blocks of the other kind. It's important to notice that only the element with more blocks gets to have edge blocks. Applying this we get the following result for $m_2 = 2m_2' + 1$: $$ 2\sum\limits_{i=0}^{m_2'} (f_{10}(k, m_1'+1, i+1)f(n-k, m_1', m_2'-i) + f(k, m_1', i)f_{10}(n-k, m_1'+1, m_2'-i+1)) $$

And finally for $m_2 = 2m_2'$: $$ \sum\limits_{i=0}^{m_2'} (f_{00}(k, m_1'+1, i)f(n-k, m_1', m_2'-i) + f(k, m_1', i)f_{00}(n-k, m_1'+1, m_2'-i)) + \sum\limits_{i=0}^{m_2'-1}(f_{11}(k, m_1'+1, i+2)f(n-k, m_1', m_2'-i-1) + f(k, m_1', i)f_{11}(n-k, m_1'+1, m_2'-i+1)) $$

These formulas do the job and explain combinatorically what's going on, but I can't conciously call it a nice closed form solution. Maybe there is a simplification procedure or a different way of counting that can yield a nicer form.

  • As I understood, when we count the number of ways to split $k$ elements into $m$ blocks with $p$ blocks having at least $2$ elements and address that by first choosing the blocks that will contain only one element $\binom{m}{m-p}$, we assume that there are exactly $m-p$ one-element blocks. But it is not required that the blocks distinct from those $p$ blocks are one-element. – Alex Ravsky May 12 '24 at 12:37
  • @AlexRavsky if there are $m$ blocks with exactly $p$ having more than one element then $m-p$ blocks must have only one element. Otherwise, we would’ve had more than $p$ blocks with more than one element. – Valeriy Savchenko May 12 '24 at 12:56