I'm trying to calculate the number of sequences of length 20 which built from 8 $A$'s, 7 $B$'s and 5 $C$'s while there are niether two linked together $C$'s nor $A$ and $B$ linked together in the sequence.
Any idea?
I'm trying to calculate the number of sequences of length 20 which built from 8 $A$'s, 7 $B$'s and 5 $C$'s while there are niether two linked together $C$'s nor $A$ and $B$ linked together in the sequence.
Any idea?
Here we are given a ternary alphabet $V=\{A,B,C\}$. We are asking for the number of words of length $20$ built from $8$ A's, $7$ B's and $5$ C's which have no runs of length $2$.
Words with no consecutive equal characters are called Smirnov or Carlitz words. See example III.24 Smirnov words from Analytic Combinatorics by Philippe Flajolet and Robert Sedgewick for more information.
A generating function for the number of Smirnov words over a ternary alphabet is \begin{align*} \left(1-\frac{A}{1+A}-\frac{B}{1+B}-\frac{C}{1+C}\right)^{-1} \end{align*}
Denoting with $[X^n]$ the coefficient of $X^n$ in a series, we calculate the wanted number (with some help of Wolfram Alpha) as \begin{align*} [A^{8}B^7C^5]&\left(1-\frac{A}{1+A}-\frac{B}{1+B}-\frac{C}{1+C}\right)^{-1}\\ &=[A^{8}B^7C^5]\sum_{j=0}^\infty\left(\frac{A}{1+A}+\frac{B}{1+B}+\frac{C}{1+C}\right)^j\\ &=73416 \end{align*}
You can count all sequences without restrictions and then subtrack using inclusion–exclusion principle. For exemple, to count the number of sequences with two linked together $C$'s, you consider $8$ $A$'s, $7$ $B$'s, $3$ $C$'s and $1$ $D$(that will represent the $CC$).
My interpretation of the conditions is
- No consecutive $c$s.
- No occurrences of $ab$ or $ba$.
Imagine listing all such sequences with those conditions but of any length. For reasons that will become clear we will put "+"s in-between the "words" in our list. So, for example the list will start
$$R=\epsilon + a+b+c+aa+ac+bb+bc+ca+cb+\ldots$$
where we include the empty word $\epsilon$.
Now, we call this whole list $R$ because this is known as a "regular expression".
As you can see it looks algebraic, with the difference that the order of the letters in our regular expression matters. However we may still perform some operations on our regular expression, in this case the only ones we need to concern ourselves with are factorisation addition and subtraction
We may factorise (whilst maintaining order) e.g.
$$a+abc+ac=a(\epsilon+b+c)$$ $$bc+abc+ac=(b+ab+a)c$$
of course we cannot factorise out letters that are blocked in by others, e.g.
$$ab+ca\ne a(b +c)$$
because the $a$ in $ca$ is blocked from being left-factorised by the $c$.
We may add or subtract words to both sides just as we would in algebra and we may cancel identical words just as we might expect, e.g. For regular expression $L$
$$L= ab+ac$$
we can add a term aa to our list
$$L+aa=aa+ab+ac$$
notice the order of addition doesn't matter just as with basic algebra.
We could also subtract ab if desired to shorten our list
$$L-ab=ab-ab+ac=ac$$
Usually we are only interested in the number of $a$s, $b$s, $c$s etc in our words, not their order. In such a case we may replace letters $a$,$b$ and $c$ with their counterpart variables $x_a$,$x_b$ and $x_c$ then the regular expression $R$ becomes a generating function.
Notice that for a generating function every term of the kind $x_a^{k_a}x_b^{k_b}x_c^{k_c}$ will receive a contribution for every word in the list with $k_a$ $a$s, $k_b$ bs and $k_c$ $c$s.
So if we can find a regular expression for words obeying your conditions then turn it into a generating function and take the coefficient of $x_a^8x_b^7x_c^5$ we are done.
With this in mind let's set out our regular expression $R$, it must be the list of all words that are either empty or end with $a$,$b$ or $c$ (represented by regular expressions $R_a$, $R_b$ and $R_c$ respectively)
$$R=\epsilon + R_a+R_b+R_c \tag{*}$$
Now, any word ending in $a$ can be formed only by appending an $a$ to words ending in anything but $b$, similarly words ending in $b$ are formed by appending $b$ to any word except those ending in $a$:
$$R_a=(R-R_b)a\tag{1}$$ $$R_b=(R-R_a)b\tag{2}$$
words ending in $c$ can be formed only by appending $c$ to those words ending in anything but $c$
$$R_c=(R-R_c)c\tag{3}$$
we could jump straight to turning these into generating functions but first we can make a substituting of $(2)$ into $(1)$
$$\begin{align}&R_a\; =\; (R-(R-R_a)b)a\\ \implies &R_a\; =\;R(a - ba)+R_aba\\ \implies &R_a(\epsilon-ba)\; =\; R(a - ba) \tag{4}\end{align}$$
similarly by substituting $(1)$ into $(2)$
$$R_b(\epsilon-ab)=R(b-ab)\tag{5}$$
and by simply expanding and rearranging $(3)$ we have
$$R_c(\epsilon+c)=Rc\tag{6}$$
now we may write down the generating function equivalents of $(4)$, $(5)$ and $(6)$ then rearrange them (since we can now treat them using the commutative property of multiplication and inverse multiplication)
$$f_a=f\frac{x_a-x_bx_a}{1-x_ax_b} \tag{7i}$$ $$f_b=f\frac{x_b-x_ax_b}{1-x_bx_a}\tag{7ii}$$ $$f_c=f\frac{x_c}{1+x_c}\tag{7iii}$$
Writing the generating function version of (*) and substituting all of $(7)$ we have
$$f=1 + f\frac{x_a-x_bx_a}{1-x_ax_b}+f\frac{x_b-x_ax_b}{1-x_bx_a}+f\frac{x_c}{1+x_c}$$
rearranging
$$\bbox[#FFA,10px,border: solid black 1px]{f=\left(1-\left(\frac{x_a-2x_bx_a+x_b}{1-x_ax_b}+\frac{x_c}{1+x_c}\right)\right)^{-1}}\tag{8}$$
this is our generating function.
It is possible to use this to find the recurrence for the coefficients $f_{k_a,k_b,k_c}$ of
$$f(x_a,x_b,x_c)=\sum_{k_a,k_b,k_c\ge 0}f_{k_a,k_b,k_c}x_a^{k_a}x_b^{k_b}x_c^{k_c}$$
However, using the following input in sage (here I use $a,b,c$ as variables) yields the desired coefficient:
a,b,c=var('a,b,c')
f(a,b,c)=1/(1-((a-2*a*b+b)/(1-a*b)+c/(1+c)))
taylor(f(a,b,c),(a,0),(b,0),(c,0),20).coefficient(a^8).coefficient(b^7).coefficient(c^5)
sage returns our answer:
17416
[Edited: I don't know where I came up with "d", removed.]