Generating Functions
Generating Functions
Karen Ge
Abstract
Generating functions gives us a global perspective when we need
to study a local property. We define generating functions and present
its applications in algebra and combinatorics. We also introduce the
Snake Oil Method, a powerful tool to evaluate combinatorial sums
using generating functions.
Keywords: generating functions, generalized binomial theorem, com-
binatorics, snake oil method
1 Motivation
Why generating functions? Generating functions gives us a global perspec-
tive when we need to study a local property. It also gives us a simple way
to encode and manipulate probabilities. It provides us a different angle
and often helps us uncover the secrets that we were oblivious to previously.
Quoting [3], “A generating function is a clothesline on which we hang up a
sequence of numbers for display.” Our first example illustrates the idea of
using the coefficients of a polynomial to describe probabilities.
Example 1. Compute the probability that you get exactly three 1’s after
rolling a standard 6-sided die five times. Then compute the probability that
you get three 1’s, one 2, and one 3 after rolling five times.
Solution. For the first part, we see that rolling a 1 has a 16 chance of
occurring and any other number has a 65 chance of coming up. There are
5
3 ways to choose which three of the rolls in the sequence are 1’s. So the
5 1 3 5 2
total probability is 3 ( 6 ) ( 6 ) .
For the second part, there are 53 ways to choose which three of the rolls are
1’s, 2 ways to choose which of the remaining two rolls is a 2. The probability
of any specific number coming up is again 16 . So the probability is 53 ·2·( 16 )5 .
1
Now we illustrate how expanding a polynomial can help us solve this and
similar problems. For the first part, we realize that there are two results we
care about: rolling 1 or rolling anything else. To separate these probabilities
in our polynomial, we make the coefficient of x be 16 and the coefficient of
y be 65 . Since the terms of the expansion should have total degree 5, we
consider raising ( x6 + 5y 3 2
6 ) to the 5th power. Then the coefficient of x y
corresponds tothe probability that rolling a die 5 times results in three 1’s.
Indeed, it is 53 ( 61 )3 ( 65 )2 , as computed earlier.
For the second part, there are three results we care about: rolling 1, rolling
2, or rolling 3. Then we consider the trinomial
a b c
( + + )5 .
6 6 6
5
The coefficient of a3 bc is · 2 · ( 61 )5 , as predicted.
3
Similarly, as a polynomial with infinite degree, a generating function can en-
code information about a combinatorial sequence in its coefficients. Given
aPsequence A = {a0 , a1 , a2 , ...}, we define a formal power series G(x) =
∞ n
n=0 an x . Note that G(x) is like a polynomial, except that it may have
infinitely many terms. Note also that ai , the ith member of A, is the coef-
ficient of the term xi of G(x). We call G(x) the generating function for
the sequence A = {a0 , a1 , a2 , ...}.
Before we explore further, let’s establish some useful identities.
2
When r = −1 in Theorem 1, we get
1
= 1 − x + x2 − x3 + · · · .
1+x
We can also differentiate the geometric series repeatedly, and get
∞
1 X
= xn ,
1−x
n=0
∞
1 X
= nxn−1 ,
(1 − x)2
n=1
∞
2 X
= n(n − 1)xn−2 ,
(1 − x)3
n=2
···
Therefore,
∞
1 X n+2 n
= x .
(1 − x)3 2
n=0
3 Counting
You might say that we do not have to use 5th degree polynomials to solve
Example 1. That may be true, but generating functions can be used to
solve complicated counting problems that are out of the reach of traditional
methods. Let’s look at an example.
Example 2. In how many ways can we fill a bag with n fruits chosen from
apples, bananas, oranges, and pears, subject to the following constraints?
3
Solution. Let’s use the method of divide and conquer and construct a
generating function for selecting each type of fruit. First we select apples.
We can select a set of 0 apples in 1 way, a set of 1 apple in 0 ways, a set of
2 apples in 1 way, and so on. Thus
1
A(x) = 1 + x2 + x4 + · · · = .
1 − x2
We can select a set of 0 bananas in one way, a set of 1 bananas in one way,
.... Since there are at most six bananas, we have
1 − x7
B(x) = 1 + x + x2 + x3 + x4 + x5 + x6 = .
1−x
The generating function for selecting oranges is similar to that for selecting
apples, namely,
1
O(x) = 1 + x7 + x14 + · · · = .
1 − x7
Finally, we can only select zero or one pear. So P (x) = 1 + x.
The generating function G(x) for selecting from among all kinds of fruit is
1 1 − x7 1 1
G(x) = A(x)B(x)O(x)P (x) = (1 + x) = .
1 − x2 1 − x 1 − x7 (1 − x)2
Since
1 d 1
2
=
(1 − x) dx 1 − x
d
= (1 + x + x2 + x3 + x4 + · · · )
dx
= 1 + 2x + 3x2 + 4x3 + · · · ,
4
Solution. Let G(x) = ∞ n
P
n=0 an x , where an is the number of groups of
cards with sum of points equal to n. We see that
(1 + x)2 (1 − x2048 )3
G(x) = .
(1 − x2 )3
From the last section, we know that
∞
1 X n + 2 2n
= x .
(1 − x2 )3 2
n=0
Since we are asked to find the coefficient of x2017 , we do not need to care
about (1 − x2048 )3 . That is, we want the coefficient of x2017 in
(1 + x)2
2
3 2 4 4
= (1 + 2x + x ) 1 + x + x + · · · .
(1 − x2 )3 2 2
The only way we can 2017 term is to pick the 2x term from (1 + x)2
make a x
1010 2016
and pick the 2 x term from (1 − x2 )−3 . Thus its coefficient is
1010
2 = 1019090 .
2
4 Recurrences
Generating functions are also very useful in solving recurrence relations, as
we will see in the next example.
Solution. Computing the first few terms, we get A = {0, 1, 1, 3, 5, 11, ...}.
Let G(x) = ∞ n be the generating function for the sequence.
P
a
n=0 n x
5
Now let’s see where the given recurrence relation might lead us to. We
rewrite G(x) with an = an−1 + 2an−2 in mind.
1 1 2n − (−1)n
an = 2n − (−1)n = .
3 3 3
1
Example 5. Note that 89 = 0.011235955056.... Prove that
∞
1 X Fn
= ,
89 10n+1
n=1
6
1
Plugging x = 10 into (∗), we get
10 F1 F2 F3
= + + + ··· ,
89 10 100 1000
and the result follows.
sum to work with and swapping the order of summation usually helps. Her-
bert Wilf [3] coined the name Snake Oil Method, a cure for everything.
In his own words, “don’t try to evaluate the sum that you’re looking at.
Instead, find the generating function for the whole parameterized family of
them, then read off the coefficients.” Let’s look at an example.
Let r = n − k, we get
X X k X
k
G(x) = x xr = xk (1 + x)k
r
r
k≥0 k≥0
X 1
= (x + x2 )k = .
1 − x − x2
k≥0
7
x
From Example 5, we know that 1−x−x 2 is the generating function for the
Fibonacci sequence. Thus
X k
= Fn+1 (n = 0, 1, 2, ...).
n−k
k≥0
6 Exercises
1. Let a0 = 1, a1 = 1, and an = 4an−1 − 4an for n ≥ 2. Use generating
functions to find a formula for an in terms of n.
2. Find the number an of ways n dollars can be changed into 1-dollar or
2-dollar coins regardless of the order. For example, when n = 3, there
are 2 ways, namely three 1-dollar coins or one 1-dollar coin and one
2-dollar coin.
3. Let n be a positive integer. Find the number an of polynomials P (x)
with coefficients in {0, 1, 2, 3} such that P (2) = n.
4. Let a0 , a1 , a2 , ... be an increasing sequence of nonnegative integers
such that every nonnegative integer can be expressed uniquely in the
form ai + 2aj + 4ak , where i, j and k are not necessarily distinct.
Determine a1998 .
5. Assume that for some positive integer n there are sequences of positive
numbers a1 , a2 , ..., an and b1 , b2 , ..., bn such that the sums
a1 + a2 , a1 + a3 , ..., an−1 + an and
b1 + b2 , b1 + b3 , ..., bn−1 + bn
8
References
[1] Evan Chen, Summation, web.evanchen.cc/handouts/Summation/
Summation.pdf