Skip to main content

Section 8.3 Using generating functions to solve recursively-defined sequences

At last we are ready to apply the mechanics we've introduced in this chapter, to find an explicit formula for the \(n\)th term of a recursively-defined sequence.

This method is probably most easily understood using examples.

Consider the recursively-defined sequence: \(a_0=2\text{,}\) and for every \(n \ge 1\text{,}\) \(a_n=3a_{n-1}-1\text{.}\) Find an explicit formula for \(a_n\) in terms of \(n\text{.}\)

Solution

The generating function for this sequence is \(a(x)=\sum_{i=0}^\infty a_i x^i\text{.}\)

Now, we are going to use the recursive relation. We know that \(a_n=3a_{n-1}-1\text{,}\) or, by rearranging this, \(a_n-3a_{n-1}=-1\text{.}\) Thus, if we could get the coefficient of \(x^n\) to look like \(a_n-3a_{n-1}\text{,}\) we could use the recursive relation to replace this by \(-1\text{.}\) Right now, the coefficient of \(x^n\) is \(a_n\text{,}\) and the only place \(a_{n-1}\) shows up is as the coefficient of \(x^{n-1}\text{.}\) But if we multiply \(a(x)\) by \(x\text{,}\) then \(a_{n-1}x^{n-1}\) becomes \(a_{n-1}x^n\text{,}\) so if we also multiply by \(-3\text{,}\) we get a coefficient of \(-3a_{n-1}\) for \(x^n\) in \(-3xa(x)\text{.}\) Now add this to \(a(x)\text{.}\) This may be easier to see as written below:

\begin{equation*} \begin{matrix}a(x) \amp = \amp a_0\amp +a_1x\amp +a_2x^2 \amp + \ldots \amp + a_mx^m\amp + \ldots \\ -3xa(x) \amp = \amp \amp -3a_0x\amp -3a_1x^2\amp - \ldots\amp -3a_{m-1}x^m\amp - \ldots \\ \hline (1-3x)a(x) \amp = \amp a_0\amp -x \amp -x^2\amp -\ldots \amp -x^m \amp - \ldots \end{matrix} \end{equation*}

We see that this gives

\begin{equation*} (1-3x)a(x)=3-(1+x+x^2+x^3+\ldots)\text{,} \end{equation*}

and we know that

\begin{equation*} 1+x+x^2+x^3+\ldots = 1/(1-x)\text{,} \end{equation*}

so \((1-3x)a(x)=3-1/(1-x)\text{.}\) Dividing through by \(1-3x\) gives

\begin{equation*} a(x)=\frac{3}{1-3x}-\frac{1}{(1-x)(1-3x)}\text{.} \end{equation*}

Now it's time to apply what we learned in the preceding sections of this chapter. The denominator is already factored, so we can immediately apply the method of partial fractions to the second fraction. If

\begin{equation*} \frac{-1}{(1-x)(1-3x)}=\frac{A}{1-x}+\frac{B}{1-3x}=\frac{A(1-3x)+B(1-x)}{(1-3x)(1-x)}\text{,} \end{equation*}

then \(A+B=-1\) and \(-3A-B=0\text{,}\) so \(B=-3A\text{,}\) which gives \(-2A=-1\text{,}\) so \(A=1/2\) and \(B=-3/2\text{.}\) Thus,

\begin{equation*} a(x)=\frac{3}{1-3x}+\frac{1/2}{1-x}-\frac{3/2}{1-3x}=\frac{1/2}{1-x}+\frac{3/2}{1-3x}\text{.} \end{equation*}

The coefficient of \(x^n\) in the first of these terms is \(1/2\text{,}\) while in the second term, the coefficient of \(x^n\) is \((3/2)3^n\text{.}\) Thus, \(a_n=1/2+(3/2)3^n\text{.}\) Since our generating function began with \(a_0x^0\text{,}\) this formula applies for every \(n \ge 0\text{.}\)

When going through so much algebra, it's easy to make a mistake somewhere along the way, so it's wise to do some double-checking. For a recursively-defined sequence, if the formula you work out gives the correct answer for the first three or four terms of the sequence, then it's very likely that you've done the calculations correctly. Let's check the first three terms of this one. We know from our initial condition that \(a_0=2\text{,}\) and our new formula gives

\begin{equation*} a_0=1/2+(3/2)3^0=1/2+3/2=2\text{.} \end{equation*}

Using the recursive relation, we should have \(a_1=3(2)-1=5\text{,}\) and our formula gives

\begin{equation*} a_1=1/2+(3/2)3^1=1/2=9/2=5\text{.} \end{equation*}

Finally, the recursive relation gives \(a_2=3(5)-1=14\text{,}\) while our formula gives

\begin{equation*} a_2=1/2+(3/2)3^2=1/2+27/2=14\text{.} \end{equation*}

You can see the benefit to having an explicit formula if you were asked to work out \(a_{100}\text{.}\) Clearly, it's much easier to determine \(1/2+(3/2)3^{100}\) than to apply the recursive relation one hundred times.

Let's look at one more example, where the recursive relation involves more than one previous term.

Consider the recursively-defined sequence: \(b_0=1\text{,}\) \(b_1=0\text{,}\) \(b_2=1\text{,}\) and for every \(n \ge 3\text{,}\) \(b_n=b_{n-1}-2b_{n-3}\text{.}\) Find an explicit formula for \(b_n\) in terms of \(n\text{.}\)

Solution

The generating function for this sequence is

\begin{equation*} b(x)=\sum_{i=0}^\infty b_i x^i\text{.} \end{equation*}

Again, we'll use the recursive relation, which we rearrange as

\begin{equation*} b_n-b_{n-1}+2b_{n-3}=0 \end{equation*}

for every \(n \ge 3\text{.}\) We want to end up with a polynomial in which the coefficient of \(x^m\) looks like \(b_m-b_{m-1}+2b_{m-3}\text{,}\) so that we'll be able to use the recursive relation to replace this by \(0\text{.}\) In order to do this, we'll take \(b(x)\) (to get the \(b_mx^m\) piece), minus \(xb(x)\) (to get the \(-b_{m-1}x^m\) piece), plus \(2x^3b(x)\) (to get the \(+2b_{m-3}x^m\) piece).

The result looks like:

\begin{equation*} \begin{matrix}b(x) \amp = \amp b_0\amp +b_1x\amp +b_2x^2 \amp +b_3x^3\amp + \ldots \amp + b_mx^m\amp + \ldots \\ -xb(x) \amp = \amp \amp -b_0x\amp -b_1x^2\amp -b_2x^3\amp - \ldots\amp -b_{m-1}x^m\amp - \ldots \\ +2x^3b(x) \amp = \amp \amp \amp \amp +2b_0x^3\amp + \ldots\amp +2b_{m-3}x^m\amp + \ldots \\ \hline (1-x+2x^3)b(x) \amp = \amp b_0\amp +(b_1-b_0)x \amp +(b_2-b_1)x^2\amp +0x^3\amp +\ldots \amp +0x^m \amp + \ldots \end{matrix} \end{equation*}

We see that this gives

\begin{equation*} (1-x+2x^3)b(x)=1+(-1)x+1x^2\text{.} \end{equation*}

Dividing through by \(1-x+2x^3\) gives

\begin{equation*} b(x)=\frac{1-x+x^2}{1-x+2x^3}\text{.} \end{equation*}

If we want to be able to do anything with this, we need to factor the denominator. Although we don't have a general method for factoring cubic polynomials, in this case it's not hard to see that \(-1\) is a zero of the polynomial (because \(1-(-1)+2(-1)^3=0\)), and hence \(x+1\) is a factor of the polynomial. You will not be expected to factor cubic polynomials yourself in this course, so we won't review polynomial long division, but if you recall polynomial long division (or look it up in some other source), you can use it to determine that

\begin{equation*} 1-x+2x^3=(1+x)(2x^2-2x+1)\text{.} \end{equation*}

In any case, you can multiply the right-hand side out to verify that it is true.

Now it's time to use the factoring formula, with \(a=2\text{,}\) \(b=-2\text{,}\) and \(c=1\text{,}\) to factor \(2x^2-2x+1\text{.}\) This gives

\begin{equation*} 2x^2-2x+1=1\left(1-\frac{2+\sqrt{4-8}}{2}x\right)\left(1-\frac{2-\sqrt{4-8}}{2}x\right)=\left(1-(1+i)x\right)\left(1-(1-i)x\right)\text{.} \end{equation*}

Having factored

\begin{equation*} 1-x+2x^3=(1+x)(1-(1+i)x)(1-(1-i)x)\text{,} \end{equation*}

we now apply the method of partial fractions to split this up into three separate pieces.

If

\begin{align*} \frac{1-x+x^2}{1-x+2x^3}\amp= \frac{A}{1+x}+\frac{B}{1-(1+i)x}+\frac{C}{1-(1-i)x}\\ \amp= \frac{A(2x^2-2x+1)+B(1+x)(1-(1-i)x)+C(1+x)(1-(1+i)x)}{1-x+2x^3}\text{,} \end{align*}

then this gives us three equations:

\begin{equation*} 2A-B(1-i)-C(1+i)=1 \end{equation*}

(from the coefficient of \(x^2\));

\begin{equation*} -2A+B(1-(1-i))+C(1-(1+i))=-1 \end{equation*}

(from the coefficient of \(x\)); and \(A+B+C=1\) (from the constant term). The second of these simplifies to \(-2A+iB-iC=-1\text{.}\)

The algebra can be done in different ways and gets a bit ugly, but these three equations can be solved, resulting in \(A=3/5\text{,}\) \(B=(2-i)/10\text{,}\) \(C=(2+i)/10\text{.}\)

Thus,

\begin{equation*} \frac{1-x+x^2}{1-x+2x^3}=\frac{3/5}{1+x}+\frac{(2-i)/10}{1-(1+i)x}+\frac{(2+i)/10}{1-(1-i)x}\text{.} \end{equation*}

The coefficient of \(x^n\) in the first of these terms is \((3/5)(-1)^n\text{;}\) in the second, it is \(((2-i)/10))(1+i)^n\text{,}\) and in the third, it is \(((2+i)/10)(1-i)^n\text{.}\)

We conclude that for every \(n \ge 0\text{,}\) we have

\begin{equation*} b_n=\frac{3}{5}(-1)^n+\frac{2-i}{10}\left(1+i\right)^n+\frac{2+i}{10}\left(1-i\right)^n\text{.} \end{equation*}

It is somewhat surprising that these formulas involving complex numbers will always work out (when \(n\) is an integer) to be not only real numbers, but integers! Once again, we should check this formula for several values of \(n\) to ensure we haven't made errors in our calculations along the way.

From the initial conditions, \(b_0=1\text{.}\) Our formula gives

\begin{equation*} b_0=3/5+(2-i)/10+(2+i)/10=1\text{.} \end{equation*}

From the initial conditions, \(b_1=0\text{.}\) Our formula gives

\begin{equation*} b_1=-3/5+\frac{2-i}{10}(1+i)+\frac{2+i}{10}(1-i)=-3/5+(3+i)/10+(3-i)/10=0\text{.} \end{equation*}

Finally, the initial conditions gave \(b_2=1\text{,}\) and our formula gives

\begin{equation*} b_2=\frac{3}{5}+\frac{2-i}{10}(2i)+\frac{2+i}{10}(-2i)=\frac{3}{5}+\frac{2i+1}{5}-\frac{2i-1}{5}=1\text{.} \end{equation*}

We could continue, but this is sufficient verification to inspire reasonable confidence.

We now have a general method that we can apply to solve normal linear recursive relations. Before describing this method, we remark that it is important to our method that our recursively-defined sequence begin with the \(0\)th term, as in Examples 8.3.1 and 8.3.2 above where we started out knowing \(a_0\) and \(b_0\) respectively. If instead we had started with \(a_1=5\) in Example 8.3.1 and had not been given \(a_0\) then in order to use this method we would need to use the recursive relation to work out what \(a_0\) should be. We would do this by taking what we do know: \(a_1=5\) and the recursive relation \(a_n=3a_{n-1}-1\) to solve for \(a_0\text{.}\) In this case, we see that \(a_1=5=3a_0-1\) gives \(3a_0=6\) so \(a_0=2\text{.}\) We would do this even if the recursive relation had only been defined for \(n \ge 2\text{,}\) since we would be using our calculations to force the recursive relation to hold for \(n=1\text{.}\)

Method

  1. Start with a recurrence relation of the form

    \begin{equation*} h_n=a_1h_{n-1}+a_2h_{n-2}+\ldots+a_kh_{n-k}+f(n) \text{ for every } n \ge i, \end{equation*}

    for some \(i \ge k\text{,}\) where \(f(n)\) is a function in \(n\text{,}\) and with initial terms \(h_{i-k}, \ldots, h_{i-1}.\)

    If \(i > k\) then use this information (the recurrence relation and the given initial terms) to find values for \(h_0, \ldots, h_{i-k-1}\) that make our recurrence relation true for every \(n \ge k\text{.}\)

  2. Rearrange the recurrence relation into the form

    \begin{equation*} h_n-a_1h_{n-1}-a_2h_{n-2}-\ldots-a_kh_{n-k}=f(n), \end{equation*}

    which by our work in the previous step is now true for every \(n \ge k\text{.}\) Let

    \begin{equation*} a(x)=1-a_1x-a_2x^2-\ldots -a_kx^k\text{.} \end{equation*}
  3. Define the generating function

    \begin{equation*} h(x)=h_0+h_1x+h_2x^2+\ldots\text{.} \end{equation*}
  4. Find a linear combination of the generating function so that the coefficient of \(x^m\) is \(f(m)\) for every \(m\) greater than or equal to \(k\) as follows:

    \begin{equation*} \begin{matrix}h(x) \amp = \amp h_0\amp +h_1x\amp +h_2x^2 \amp +h_3x^3\amp + \ldots \amp + h_kx^k\amp + \ldots \\ -a_1xh(x) \amp = \amp \amp -a_1h_0x\amp -a_1h_1x^2\amp -a_1h_2x^3\amp - \ldots\amp -a_1h_{k-1}x^k\amp - \ldots \\ -a_2x^2h(x) \amp = \amp \amp \amp -a_2h_0x^2\amp -a_2h_1x^3\amp -\ldots\amp -a_2h_{k-2}x^k\amp + \ldots \\ \vdots \amp \amp \amp \amp \amp \amp \amp \vdots\amp \\ -a_kx^kh(x) \amp = \amp \amp \amp \amp \amp \amp -a_kh_{0}x^k\amp + \ldots \\ \hline a(x)h(x) \amp = \amp h_0\amp +(h_1-a_1h_0)x \amp +(h_2-a_1h_1-a_2h_0)x^2\amp +\ldots \amp \ldots\amp +f(k)x^k \amp + \ldots \end{matrix} \end{equation*}

    So

    \begin{equation*} h(x)=\frac{h_0+(h_1-a_1h_0)x+\ldots + (h_{k-1}-a_1h_{k-2}+\ldots+a_{k-1}h_{0})x^{k-1}+\sum_{n=k}^\infty f(n)x^n}{a(x)}\text{.} \end{equation*}
  5. Substitute in the values of \(h_0, \ldots, h_{k-1}\) and factor \(a(x)\) (remember that you can use complex roots). Find a closed form for

    \begin{equation*} \sum_{n=k}^\infty f(n)x^n\text{.} \end{equation*}
  6. Use partial fractions to turn our expression for \(h(x)\) into a sum of expressions that we can expand using the generalised binomial theorem.

  7. Make variable substitutions if necessary to get summands that look like

    \begin{equation*} \frac{A}{(1+y)^n}\text{.} \end{equation*}
  8. Use the generalised binomial theorem to find \(h_n\text{,}\) the coefficient of \(x^n\) in \(h(x)\text{.}\)

For each of the following recursively-defined sequences, use the method of generating functions to find an explicit formula for the \(n\)th term of the sequence.

  1. \(c_0=2\text{,}\) \(c_1=0\text{,}\) \(c_n=c_{n-1}+2c_{n-2}\) for every \(n \ge 2\text{.}\)

  2. \(d_0=0\text{,}\) \(d_1=1\text{,}\) \(d_n=2d_{n-2}+1\) for every \(n \ge 2\text{.}\)

  3. \(e_0=2\text{,}\) \(e_n=3e_{n-1}-2\) for every \(n \ge 1\text{.}\)

  4. \(f_0=1\text{,}\) \(f_1=3\text{,}\) and \(f_n=4(f_{n-1}-f_{n-2})\) for every \(n \ge 2\text{.}\)

  5. \(g_0=2\text{,}\) \(g_1=0\text{,}\) and \(g_n=2g_{n-1}-2g_{n-2}\) for every \(n \ge 2\text{.}\)

  6. \(h_0=1/2\) and \(h_n=3h_{n-1}-1/2\) for every \(n \ge 1\text{.}\)

  7. \(i_0=i_1=2\text{,}\) \(i_2=0\text{,}\) and \(i_n=3i_{n-1}-3i_{n-2}+i_{n-3}\) for every \(n \ge 3\text{.}\)

  8. \(j_0=-1\text{,}\) \(j_1=0\text{,}\) and \(j_n=2j_{n-1}+3j_{n-2}\) for every \(n \ge 2\text{.}\)

  9. \(k_0=10\) and \(k_n=11k_{n-1}-10\) for every \(n \ge 1\text{.}\)

Solve the following problems.

  1. Let \(p_n\) denote the number of ways to build a pipe \(n\) units long, using segments that are either plastic or metal, and (for each material) come in lengths of 1 unit or 2 units. For example, \(p_1=2\) since we can use a 1-unit segment that is either plastic or metal, and \(p_2=6\) since we can use either type of \(2\)-unit segment, or any of the \(2^2\) possible ordered choices of \(2\) segments each having a length of \(1\) unit. Define \(p_0=1\text{.}\) Determine a recurrence relation for \(p_n\text{.}\) Give a combinatorial proof that your recurrence relation does solve this counting problem. Use your recurrence relation and the method of generating functions to find a formula for \(p_n\text{.}\)
    Hint

    Your final answer should be
    \begin{equation*} p_n=\frac{1}{2\sqrt{3}}(1+\sqrt{3})^{n+1}-\frac{1}{2\sqrt{3}}(1-\sqrt{3})^{n+1} \end{equation*}
    for every \(n \ge 0\text{.}\)

  2. Let \(s_n\) denote the number of lists of any length that have the fixed sum of \(n\text{,}\) and whose entries come from \(\{1,2,3\}\text{.}\) For example, \(s_2=2\) because \((1,1)\) and \((2)\) are the only such lists; and \(s_4=7\) because the lists are \((3,1)\text{,}\) \((1,3)\text{,}\) \((2,2)\text{,}\) \((2,1,1)\text{,}\) \((1,2,1)\text{,}\) \((1,1,2)\text{,}\) and \((1,1,1,1)\text{.}\) Define \(s_0=1\text{.}\) Determine \(s_1\text{,}\) \(s_3\text{,}\) and \(s_5\) by finding all possible lists. Give a combinatorial proof that \(s_n=s_{n-1}+s_{n-2}+s_{n-3}\) for every \(n \ge 3\text{.}\) Use this recurrence relation to show that the generating function \(S(x)\) for \(\{s_n\}\) is \(\frac{1}{1-x-x^2-x^3}\text{.}\)