[Math] The number of length-n ternary sequences with even ones and even zeroes

recurrence-relations

Just starting to appreciate recurrence relations

Let $T_n = $ number of length-n ternary sequences with an even number of ones and an even number of zeroes.

$T_0 = 1$, because $0$ is an even number, and there are zero $1$s and zero $0$s.

$T_1 = 1$, because the sequence $\{2\}$ has zero $1$s and zero $0$s

How to find $T_n$?

Possibility: split into cases.

Case 1: We have an $n$ digit sequence with an even number of $0$s and an even number of $1$s.

To get to length $n$, we have all of the length $n-1$ sequences with an even number of $0$s and $1$s, which is $T_{n-1}$

Case 2: We have an $n$ digit sequence with an odd number of $0$s and an odd number of $1$s.

Here, we basically need the total amount of length-$n$ sequences with an odd number of $0$s and an odd number of $1$. This should be $3^n – T_{n-1}$.

Case 3: We have an even number of 0s, but an odd number of 1s.

Case 4: We have an odd number of 0s, but an even number of 1s.

How to account for cases 3 and 4?

Best Answer

Let $T_n$ be as in the post. Let $U_n$ be the number of $n$-sequences with an odd number of $0$ and an even number of $1$. Let $V_n$ be the number with an even number of $0$ and an odd number of $1$. (Note that by symmetry $U_n=V_n$.) Let $W_n$ be the number of $n$-sequences with an odd number of $0$ and an odd number of $1$. We have $$T_{n+1}=T_n +U_n+V_n.$$ This is because we can get a sequence of length $n+1$ with an even number of $0$ and of $1$ by appending a $2$ to such a sequence of length $n$, or by appending a $0$ to a sequence of length $n$ with an odd number of $0$ and an even number of $1$, or doing something similar to an $n$-sequence with an even number of $0$ and an odd number of $1$. Similarly, $$U_{n+1}=T_{n}+U_n+W_n,$$ $$V_{n+1}=T_n+V_n+W_n,$$ $$W_{n+1}=U_n+V_n+W_n.$$ A general way to attack the problem is then to express the above $4$ relationships in matrix terms, and using some linear algebra. That is, I believe, the best approach.

But we can bypass that by some manipulation. Use the fact that $V_n=U_n$ and $T_n+2u_n+W_n=3^n$. Then the first two recurrences can be rewritten as $$T_{n+1}=T_n+2U_n,$$ $$U_{n+1}=3^n-U_n.$$ Multiply each side of the first recurrence by $2$, and add. We get $$T_{n+1}+2U_{n+1}=T_n+2\cdot 3^n.\tag{1}$$ By bumping up the indices of the first recurrence by $1$, we get $$T_{n+2}=T_{n+1}+2U_{n+1}=T_{n+1}+ T_n +2\cdot 3^n-T_{n+1}=T_n+2\cdot 3^n.$$ We have reached the recurrence $$T_{n+2}=T_n+ 2\cdot 3^n.$$ This can be solved by standard methods.