9.7: Solving a nonlinear recurrence
In this section, we will use generating functions to enumerate the a certain type of trees. In doing this, we will see how generating functions can be used in solving a nonlinear recurrence equation. We will also make a connection to a counting sequence we encountered back in Chapter 2. To do all of this, we must introduce a bit of terminology. A tree is rooted if we have designated a special vertex called its root . We will always draw our trees with the root at the top and all other vertices below it. An unlabeled tree is one in which we do not make distinctions based upon names given to the vertices. For our purposes, a binary tree is one in which each vertex has 0 or 2 children, and an ordered tree is one in which the children of a vertex have some ordering (first, second, third, etc.). Since we will be focusing on rooted, unlabeled, binary, ordered trees (RUBOTs for short), we will call the two children of vertices that have children the left and right children.
In Figure 9.26 , we show the rooted, unlabeled, binary, ordered trees with \(n\) leaves for \(n \leq 4\).
Let \(C(x) = \sum_{n=0}^{\infty} c_nx^n\) be the generating function for the sequence \(\{c_n:n \geq 0\}\) where \(c_n\) is the number of RUBOTs with \(n\) leaves. (We take \(c_0=0\) for convenience.) Then we can see from Figure 9.26 that \(C(x)=x+x^2+2x^3+5x^4+ \cdot \cdot \cdot\). But what are the remaining coefficients? Let's see how we can break a RUBOT with \(n\) leaves down into a combination of two smaller RUBOTs to see if we can express \(c_n\) in terms of some \(c_k\) for \(k<n\). When we look at a RUBOT with \(n \geq 2\) leaves, we notice that the root vertex must have two children. Those children can be viewed as root nodes of smaller RUBOTs, say the left child roots a RUBOT with \(k\) leaves, meaning that the right child roots a RUBOT with \(n−k\) leaves. Since there are \(c_k\) possible sub-RUBOTs for the left child and \(c_{n−k}\) sub-RUBOTs for the right child, there are a total of \(c_kc_{n−k}\) RUBOTs in which the root's left child has \(k\) leaves on its sub-RUBOT. We can do this for any \(k=1,2,…,n−1\), giving us that
\(c_n = \displaystyle \sum_{k=1}^{n-1} c_k c_{n-k}\).
(This is valid since \(n \geq 2\).) Since \(c_0=0\), we can actually write this as
\(c_n = \displaystyle \sum_{k=0}^n c_k c_{n-k}\).
Let's look at the square of the generating function \(C(x)\). By Proposition 8.3 , we have
\(C^2(x) = c_0^2 + (c_0c_1 + c_1c_0)x + (c_0c_2 + c_1c_1 + c_2c_0)x^2 + \cdot \cdot \cdot\)
\(= 0 + 0 + (c_0c_2 + c_1c_1 + c_2c_0)x^2 + (c_0c_3 + c_1c_2 + c_2c_1 + c_3c_0)x^3 + \cdot \cdot \cdot\)
But now we see from our recursion above that the coefficient on \(x^n\) in \(C^2(x)\) is nothing but \(c_n\) for \(n \geq 2\). All we're missing is the \(x\) term, so adding it in gives us that
\(C(x) = x + C^2(x)\).
Now this is a quadratic equation in \(C(x)\), so we can solve for \(C(x)\) and have
\(C(x) = \dfrac{1 \pm \sqrt{1-4x}}{2} = \dfrac{1 \pm (1-4x)^{1/2}}{2}\).
Hence, we can use Newton's Binomial Theorem to expand \(C(x)\). To do so, we use the following lemma. Its proof is nearly identical to that of Lemma 8.12 , and is thus omitted.
For each \(k \geq 1\) ,
\(\dbinom{1/2}{k} = \dfrac{(-1)^{k-1}}{k} \dfrac{\binom{2k-2}{k-1}}{2^{2k-1}}\).
Now we see that
\(C(x) = \dfrac{1}{2} \pm \dfrac{1}{2} \displaystyle \sum_{n=0}^{\infty} \dbinom{1/2}{n} (-4)^nx^n = \dfrac{1}{2} \pm \dfrac{1}{2} (1 + \sum_{n=1}^{\infty} \dfrac{(-1)^{n-1}}{n} \dfrac{\binom{2n-2}{n-1}}{2^{2n-1}} (-4)^nx^n)\)
\( = \dfrac{1}{2} \pm \dfrac{1}{2} \pm \displaystyle \sum_{n=1}^{\infty} \dfrac{\binom{2n-2}{n-1}}{n} x^n\).
Since we need \(c_n \geq 0\), we take the “minus” option from the “plus-or-minus” in the quadratic formula and thus have the following theorem.
The generating function for the number \(c_n\) of rooted, unlabeled, binary, ordered trees with \(n\) leaves is
\(C(x) = \dfrac{1- \sqrt{1-4x}}{2} = \displaystyle \sum_{n=1}^{\infty} \dfrac{1}{n} \dbinom{2n-2}{n-1}x^n\).
Notice that \(c_n\) is a Catalan number, which we first encountered in Chapter 2, where we were counting lattice paths that did not cross the diagonal line \(y=x\). (The coefficient \(c_n\) is the Catalan number we called \(C(n−1)\) in Chapter 2.)