Let's begin with a very famous sequence: \(0\ 1\ 1\ 2\ 3\ 5\ 8\ 13\ 21\ 34\ 55\ 89\ 144\) . They are Fibonacci numbers, which can be defined as:
(1)This is a recursive definition, which is good, but it's not straightforward enough. For example, if we want to know \(F_{100}\), we'll have to calculate \(F_{0}\), \(F_{1}\), \(\ldots\), \(F_{98}\) and \(F_{99}\). This is a lot of math to do. Is there any better solution? Or, is there a closed form for Fibonacci numbers?
Fortunately the answer is yes, and (unsurprisingly) generating function is such a tool to solve such kinda recurrence.
Generating function is smart and subtle. The only way to understand it is to see how it works by an example. Here in this tutorial, I'll show you how generating function solves Fibonacci numbers recurrence.
Generally speaking, four steps are needed to solve a recurrence via generating function:
Let's apply these steps to Fibonacci numbers.
At the beginning of this tutorial, we have seen the recursive definition of Fibonacci numbers (equation (1)), but it is not a single equation. So let's simplify it:
(2)Note that:
Let's multiply both sides of equation (2) by \(z^{n}\):
and sum over all \(n\):
Please keep in your mind that \(G(z)=\sum_{n}g_{n}z^{n}=g_0+g_1z+g_2z^2+\cdots\), that is to say, if we denote \(G(z)\) as a power series, then the coefficients of \(z^n\) will be \(F_n\).
Solve the equation \(G(z)=zG(z)+z^{2}G(z)+z\), getting a closed form for \(G(z)\):
Here comes the hardest part: to expand \(G(z)\) into a power series. How can we do this? Well, there are more than one way to achieve it, but as to this Fibonacci question, there is a simple but smart way.
You may recall that
(3)Similarly we have
(4)If we add (3) \(\times A\) and (4) \(\times B\), the left is
and the right is
Then we realize that if we find the solutions to these two equations:
(5)then
obviously \(g_n=A\alpha^n+B\beta^n\) and we'll be done.
Now all we have to do is to solve (5). Fortunately it's no hard job, first we realize that:
therefore
in which
Therefore
We can expand \(G(z)\) now:
We can read off the coefficient of \(z^n\):
In other words, the \(n\)th Fibonacci number is \(\frac{\phia^{n}-\phic^{n}}{\sqrt{5}}\).
This is amazing. We introduce \(z^n\), then we use \(z^n\) and the unknown \(g_n\) (here \(g_n\) is \(F_n\)) to construct a \(G(z)\), finally we expand \(G(z)\) to get \(g_n\). It's like music: we have a subject, then we develop it and eventually we come back to it.
Please note that \(z\) and \(G(z)\) are meaningful only because we care about the coefficients of \(z^n\). We don't care what \(z\) is, neither do we care what \(G(z)\) is. What we care is the expanded form of \(G(z)\), so once we have a \(G(z)\), we focus on how to expand it.
Generating function is beautiful and very powerful. You must have seen this in this tutorial. If you want to learn more, I suggest you read [GKP]. This tutorial is actually a note I made when I read the book. Fibonacci numbers are amazing. If you read [GKP], you'll find more discussions in Chapter 6 and Chapter 7.
[GKP] | (1, 2) Ronald L. Graham, Donald E. Knuth, and Oren Patashnik, Concrete Mathematics: A Foundation for Computer Science. Addison-Wesley, 1989; second edition, 1994. |