Why are generating functions useful?

I was under the mistaken impression that if one could find the generating function for a sequence of numbers, you could just plug in a natural number $n$ to find the nth term of the sequence. I realize now that I was confusing this with a closed form formula.

So if that is not the case, then what is the point of generating functions? How do they make understanding counting sequences easier? For example, suppose I had a problem where I wanted to count how many ways I could buy $n$ pieces of apples, oranges, and pears given that I want an even number of apples, an odd number of oranges, and at most 3 pears. This would be the number of nonnegative integer solutions to $a+b+c=n$ with $a$ even, $b$ odd, and $0\leq c\leq 3$. This is the same as the coefficient of $x^n$ in the product $$ (1+x^2+x^4+\cdots)(x+x^3+x^5+\cdots)(1+x+x^2+x^3) = \frac{1}{1-x^2}\cdot\frac{x}{1-x^2}\cdot\frac{1-x^4}{1-x}$$

But what good is that? I don't see how this is much better. Also, with use of exponential generating functions, it seems the choice of monomials we use as place holders for the terms of the sequence can be arbitrary. Then then $n$th term of the sequence is just the coefficient of the $n$th monomial that you've chosen to build the generating function with. What is the real advantage of doing things like this? Many problems I see tend to ask me find the generating function, but then I'm rarely asked to do anything with it.


Closed form formulas are overrated. When they exist, generating function techniques can often help you find them; when they don't, the generating function is the next best thing, and it turns out to be much more powerful than it looks at first glance. For example, most generating functions are actually meromorphic functions, and this means that one can deduce asymptotic information about a sequence from the locations of the poles of its generating function. This is, for example, how one deduces the asymptotic of the partition numbers.

In your particular example, the generating function is rational, so it has a finite number of poles. That means you can use partial fraction decomposition on it to immediately get a closed form.

You might be interested in reading my notes on generating functions, which have several examples and which I hope will be enlightening. The first basic thing to grasp is that manipulating generating functions is much easier than manipulating sequences, but the power of generating functions goes much deeper than this. For a really thorough discussion I highly recommend Flajolet and Sedgewick's Analytic Combinatorics, which is available for free online.


Have you read Generatingfunctionology by Herbert Wilf? The book is loaded with methods and techniques based on generating functions for solving a number of different problems. I guess the book can answer your question better than I could.

I feel that generating functions are powerful because they allow you to use tools from calculus and analysis to solve problems in areas such as discrete mathematics and combinatorics, where such tools don't seem readily applicable.

Edit: I just wanted to add one more point. Suppose you have translated the problem from the sequence to the generating function and you don't see any simple closed form solutions, you could use Wolfram Alpha to make absolutely sure. Wolfram alpha can easily expand out expressions involving large polynomial terms and will often give you a series expansion if it exists in closed form.


To answer your question for the example you state:

$$ \frac{1}{1-x^2} \cdot \frac{x}{1-x^2} \cdot \frac{1-x^4}{1-x} = (x+x^2 +x^3 + x^4)(1-x^2)^{-2} $$

By binomial theorem (yes, it is valid for negative exponents too) we get that this is equal to

$$ (x + x^2 + x^3 + x^4) \left(\sum_{j=0}^{\infty} (-1)^j {-2 \choose j} x^{2j}\right)$$

Can you now figure out a formula for the coefficient of $\displaystyle x^n$, thus solving your problem?


I like the book "Analytic Combinatorics" by Philippe Flajolet and Robert Sedgewick Part A. It doesn't only show a way how you translate combinatorical problems in terms functional relations of generating functions (the show that unlabelled structures relate to ordinary generating functions, and labelled structures relate to exponential generating functions), but it also provides ways (asymptotic expansion, ...) such that you can actually calculate something.


When trying to understand a counting situation, the best thing to have is a closed form formula, a function that is easily calculable by well known methods ('closed-form' is a bit slippery in meaning, but usually means, without any recursion, but there's room for disagreement over whether summations or products are allowed (though factorial is often allowed). From such functions, it is usually straightforward to determine approximations and asymptotics and to combine with other other functions.

The next best thing is a recurrence. It allows calculation often in a very straightforward manner with no approximation needed. But recurrences are a bit inscrutable; it's very difficult to simplify or read off properties directly from the recurrence (whereas closed form is very easy to understand).

Somewhere between these two, generating functions allow ease in combinatorial manipulation (multiplying two ogfs means that one is getting an ordered pair of the objects the gfs describe). Also there are analytical techniques that allow extraction of asymptotics from gfs. And ostensibly one can get values for $f(n)$ from the gf by differentiating it $n$ times and evaluating at 0.

Wilf's generatingfunctionology, as mentioned in the comments, is the place to go for getting these techniques for gfs.