1

I have been stuck in this problem for a couple of days now. Suppose we have $n_1$ people from group $A_1$, and $n_2$ people from group $A_2$. We want to count the number of ways to place them in a row such that a maximum of $k_1$ from group $A_1$ stand consecutively and $k_2$ from group $A_2$ stand consecutively.

The way I am trying to think about it is that if we call some one from group $A_1$ as 1, and from group $A_2$ as 2, Then we are counting the number of sequences in the form:

$1^{x_1}2^{x_2}1^{x_3}2^{x_4}...$ with $x_{2k-1} \leq k1$ and $x_{2k} \leq k2$ and also with $x_1+x_3+...=n_1$, $x_2+x_4+...=n_2$. However, I am not sure how I should go around approaching this since I don't know how many of these $x_{2k-1}$ and $x_{2k}$ exist. In other words, I don't know how many variables to solve this equation for, it could have $1$, $2$, or up to $k_1/k_2$ variables.

N. F. Taussig
  • 76,571
AspiringMat
  • 2,483
  • 1
  • 17
  • 32
  • This is related to forbidden word problems. There is an alphabet {$1$,$2$}. The sequences $(k_1+1)$ $1$s and $(k_2+1)$ $2$s are forbidden. –  Oct 03 '16 at 17:26
  • I tried searching for forbidden word problem but couldn't find anything. Care to share a link? – AspiringMat Oct 03 '16 at 17:31
  • http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.236.9870&rep=rep1&type=pdf –  Oct 03 '16 at 17:44
  • http://math.stackexchange.com/search?q=forbidden+word –  Oct 03 '16 at 17:53
  • I am kind of lost from the documents to be honest. The methods used seem to be much more advanced than what I know. Is there a relatively easier way to do this? – AspiringMat Oct 04 '16 at 03:45
  • AspiringMat - I had the same experience. I spent two weeks studying the topic just to find out that my problem (different from yours) was intractable. I don't know of a simple method for forbidden word/sequence problems. The math.stackexchange link might turn up some trick? –  Oct 04 '16 at 04:31
  • AspiringMat - simple attempts tend to count the same instance multiple times giving the wrong answer. –  Oct 04 '16 at 04:32
  • AspiringMat - A simpler paper with examples on the same method https://arxiv.org/pdf/0810.5113v1.pdf –  Oct 04 '16 at 08:46
  • I think I came up with a simple solution using recursion. I'll type it as an answer so if someone has a similar problem after that. – AspiringMat Oct 04 '16 at 13:50
  • Done writing the answer. Thanks for the help, I will make sure to read the papers you put. I am really interested about these types of questions. Thank you. – AspiringMat Oct 04 '16 at 14:31
  • You previously asked a question about the number of bit strings without blocks of consecutive zeros or ones. Your question here seems to be the same: zeros can represent the first group with at most $k_1$ in a row and ones can represent the second group with at most $k_2$ in a row. Then you want to find the number of such strings of length $n_1+n_2$. The Goulden-Jackson method that works for your previous questions works here, as well. – Rus May Oct 04 '16 at 18:56

1 Answers1

0

So after suffering with this problem for a while, I came up with this solution:

We call a bit string of length $n$ valid if it follows the rules defined in the question (i.e. less than $k_1$ $1$s in a row and less than $k_2$ $2$s in a row)

Let $A_{(i,j,b)}$ represent the number of valid bit strings with length $i+j$ where we used $i$ from the $1$s and $j$ from the $2$s in total and with the right most bit (i.e. the $i+j$ bit) being the value $b$ ($b$ is either $1$ or $2$).

Then it follows that we have this recursion relation:

$A_{i,j,1}= \sum_{k=1}^{min(i,k_1)}{(A_{(i-k,j,2)})}$ and $A_{i,j,2}= \sum_{k=1}^{min(j,k_2)}{(A_{(i,j-k,1)})}$.

With the base condition being $A_{(0,0,1)}=A_{(0,0,2)}=1$.

Now all we need to do is to compute $A_{(n1,n2,1)}+A_{(n1,n2,2)}$. I could do the recursion manually, but I thought a code would be more efficient. Here is a C++ code that I wrote to do that. I tested it on multiple test data: for example $n_1=2,n_2=3,k_1=1,k_2=2$ And I got 5 which is the correct number of bitstrings that satisfy these conditions (They are 12122, 12212, 21212, 21221, 22121). I hope this helps someone!

#include <iostream>

using namespace std;

int A[110][110][3]={0}; //Initialize 3D array to all 0s
int main(){
    int n1,n2,k1,k2;
    scanf("%d %d %d %d",&n1,&n2,&k1,&k2); //Inputs n1,n2,k1,k2

    A[0][0][1]=A[0][0][2]=1; //Base cases
    for (int i=0; i<=n1;i++){
        for (int j=0; j<=n2; j++){
            if (i!=0 || j!=0)
            for (int k=1; k<=2; k++){
                if (k==1){
                    for (int z=1; z<=k1; z++){
                        if (i>=z)A[i][j][1]+=A[i-z][j][2];
                    }
                }
                else{
                    for (int z=1; z<=k2; z++){
                        if (j>=z)A[i][j][2]+=A[i][j-z][1];
                    }
                }
            }
        }
    }
    printf(A[n1][n2][1]+A[n1][n2][2]));
}
AspiringMat
  • 2,483
  • 1
  • 17
  • 32