cgt-53-70 (3)
cgt-53-70 (3)
Generating Functions
As we have seen, a typical counting problem includes one or more parameters, which of
( )
course show up in the solutions, such as nk , P (n, k), or the number of derangements of
[n]. Also recall that
∑n ( )
n n k
(x + 1) = x .
k
k=0
(n)
This provides the values k as coefficients of the Maclaurin expansion of a function. This
turns out to be a useful idea.
Sometimes a generating function can be used to find a formula for its coefficients, but
if not, it gives a way to generate them. Generating functions can also be useful in proving
facts about the coefficients.
53
54 Chapter 3 Generating Functions
The expression on the right makes sense even if n is not a non-negative integer, so long as
k is a non-negative integer, and we therefore define
( )
r r(r − 1)(r − 2) · · · (r − k + 1)
=
k k!
when r is a real number. For example,
( ) ( )
1/2 (1/2)(−1/2)(−3/2)(−5/2) −5 −2 (−2)(−3)(−4)
= = and = = −4.
4 4! 128 3 3!
These generalized binomial coefficients share some important properties of the usual
binomial coefficients, most notably that
( ) ( ) ( )
r r−1 r−1
= + . (3.1.1)
k k−1 k
Then remarkably:
THEOREM 3.1.1 Newton’s Binomial Theorem For any real number r that is
not a non-negative integer,
∑∞ ( )
r r i
(x + 1) = x
i=0
i
when −1 < x < 1.
Proof. It is not hard to see that the series is the Maclaurin series for (x + 1)r , and that
the series converges when −1 < x < 1. It is rather more difficult to prove that the series is
equal to (x + 1)r ; the proof may be found in many introductory real analysis books.
In many cases it is possible to directly construct the generating function whose coeffi-
cients solve a counting problem.
We can multiply this out by choosing one term from each factor in all possible ways. If
we then collect like terms, the coefficient of xk will be the number of ways to choose one
term from each factor so that the exponents of the terms add up to k. This is precisely the
number of solutions to x1 + x2 + x3 + x4 = k, where 0 ≤ x1 ≤ 2, 0 ≤ x2 ≤ 5, 0 ≤ x3 ≤ 5,
2 ≤ x4 ≤ 6. Thus, the answer to the problem is the coefficient of x17 . With the help of a
computer algebra system we get
(1 + x + x2 )(1 + x + x2 + x3 + x4 + x5 )2 (x2 + x3 + x4 + x5 + x6 )
= x18 + 4x17 + 10x16 + 19x15 + 31x14 + 45x13 + 58x12 + 67x11 + 70x10
+ 67x9 + 58x8 + 45x7 + 31x6 + 19x5 + 10x4 + 4x3 + x2 ,
so the answer is 4.
EXAMPLE 3.1.4 Find the generating function for the number of solutions to x1 +
x2 + x3 + x4 = k, where 0 ≤ x1 ≤ ∞, 0 ≤ x2 ≤ 5, 0 ≤ x3 ≤ 5, 2 ≤ x4 ≤ 6.
This is just like the previous example except that x1 is not bounded above. The
generating function is thus
(x + x3 + x5 + · · ·)(1 + x2 + x4 + · · ·)(1 + x + x2 + x3 + · · ·)
1
= x(1 + (x2 ) + (x2 )2 + (x2 )3 + · · ·)(1 + (x2 ) + (x2 )2 + (x2 )3 + · · ·)
1−x
x
= .
(1 − x2 )2 (1 − x)
Exercises 3.1.
For some of these exercises, you may want to use the sage applet above, in example 3.1.4, or your
favorite computer algebra system.
( ) (r−1) (r−1)
1. Prove that kr = k−1 + k .
∑ (r) i
2. Show that the Maclaurin series for (x + 1)r is ∞ i=0 i x .
3. Concerning example 3.1.4, show that all coefficients beginning with x16 are 180.
4. Use a generating function to find the number of solutions to x1 + x2 + x3 + x4 = 14, where
0 ≤ x1 ≤ 3, 2 ≤ x2 ≤ 5, 0 ≤ x3 ≤ 5, 4 ≤ x4 ≤ 6.
5. Find the generating function for the number of solutions to x1 + x2 + x3 + x4 = k, where
0 ≤ x1 ≤ ∞, 3 ≤ x2 ≤ ∞, 2 ≤ x3 ≤ 5, 1 ≤ x4 ≤ 5.
6. Find a generating function for the number of non-negative integer solutions to 3x + 2y + 7z =
n.
7. Suppose we have a large supply of red, white, and blue balloons. How many different bunches
of 10 balloons are there, if each bunch must have at least one balloon of each color and the
number of white balloons must be even?
8. Use generating functions to show that every positive integer can be written in exactly one
way as a sum of distinct powers of 2.
9. Suppose we have a large supply of blue and green candles, and one gold candle. How many
collections of n candles are there in which the number of blue candles is even, the number of
green candles is any number, and the number of gold candles is at most one?
∑∞
x 1 n
e = x
n=0
n!
3.2 Exponential Generating Functions 57
considering the n! to be part of the expression xn /n!, we might think of this same function
as generating the sequence 1, 1, 1, . . ., interpreting 1 as the coefficient of xn /n!. This is not
a very interesting sequence, of course, but this idea can often prove fruitful. If
∞
∑ xn
f (x) = an ,
n=0
n!
EXAMPLE 3.2.1 Find an exponential generating function for the number of permu-
tations with repetition of length n of the set {a, b, c}, in which there are an odd number
of a s, an even number of b s, and any number of c s.
For a fixed n and fixed numbers of the letters, we already know how to do this. For
( )
example, if we have 3 a s, 4 b s, and 2 c s, there are 3 49 2 such permutations. Now consider
the following function:
∑ ∞ ∞ ∞
x2i+1 ∑ x2i ∑ xi
.
i=0
(2i + 1)! i=0
(2i)! i=0
i!
What is the coefficient of x9 /9! in this product? One way to get an x9 term is
( ) 9
x3 x4 x2 9! x9 9 x
= = .
3! 4! 2! 3! 4! 2! 9! 3 4 2 9!
That is, this one term counts the number of permutations in which there are 3 a s, 4 b s,
and 2 c s. The ultimate coefficient of x9 /9! will be the sum of many such terms, counting
the contributions of all possible choices of an odd number of a s, an even number of b s,
and any number of c s.
∑∞
xi
Now we notice that = ex , and that the other two sums are closely related to
i=0
i!
this. A little thought leads to
∞
∑ ∞
∑ ∞
∑
xi (−x)i xi + (−x)i
ex + e−x = + = .
i=0
i! i=0
i! i=0
i!
∑∞
x −x 2x2i
e +e = ,
i=0
(2i)!
58 Chapter 3 Generating Functions
so that
∑∞
x2i ex + e−x
= .
i=0
(2i)! 2
∑∞
x2i+1 ex − e−x
= .
i=0
(2i + 1)! 2
ex − e−x ex + e−x x 1 1
e = (ex − e−x )(ex + e−x )ex = (e3x − e−x ).
2 2 4 4
Notice the similarity to example 3.1.5.
Exercises 3.2.
1. Find the coefficient of x9 /9! in the function of example 3.2.1. You may use Sage or a similar
program.
2. Find an exponential generating function for the number of permutations with repetition of
length n of the set {a, b, c}, in which there are an odd number of a s, an even number of b s,
and an even number of c s.
3. Find an exponential generating function for the number of permutations with repetition of
length n of the set {a, b, c}, in which the number of a s is even and at least 2, the number of
b s is even and at most 6, and the number of c s is at least 3.
4. In how many ways can we paint the 10 rooms of a hotel if at most three can be painted red,
at most 2 painted green, at most 1 painted white, and any number can be painted blue or
orange? (The rooms are different, so order matters.)
5. Recall from section 1.4 that the Bell numbers Bn count all of the partitions of {1, 2, . . . , n}.
∑∞
xn
Let f (x) = Bn · , and note that
n=0
n!
( n ( ) )
′
∑∞
xn−1 ∑∞
xn ∑∞ ∑ n xn
f (x) = Bn = Bn+1 = Bn−k ,
n=1
(n − 1)! n=0
n! n=0
k n!
k=0
using the recurrence relation 1.4.1 for Bn+1 from section 1.4. Now it is possible to write this
as a product of two infinite series:
(∞ )( ∞ )
′
∑ xn ∑
f (x) = Bn · an xn
= f (x)g(x).
n=0
n! n=0
Find an expression for an that makes this true, which will tell you what g(x) is, then solve
the differential equation for f (x), the exponential generating function for the Bell numbers.
From section 1.4, the first few Bell numbers are 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147,
115975, 678570, 4213597, 27644437. You can use Sage to check your answer.
3.3 Partitions of Integers 59
When this product is expanded, we pick one term from each factor in all possible ways, with
the further condition that we only pick a finite number of “non-1” terms. For example,
if we pick x3 from the first factor, x3 from the third factor, x15 from the fifth factor,
and 1s from all other factors, we get x21 . In the context of the product, this represents
3 · 1 + 1 · 3 + 3 · 5, corresponding to the partition 1 + 1 + 1 + 3 + 5 + 5 + 5, that is, three
1s, one 3, and three 5s. Each factor is a geometric series; the kth factor is
1
1 + xk + (xk )2 + (xk )3 + · · · = ,
1 − xk
so the generating function can be written
∞
∏ 1
.
1 − xk
k=1
60 Chapter 3 Generating Functions
Note that if we are interested in some particular pn , we do not need the entire infinite
product, or even any complete factor, since no partition of n can use any integer greater
than n, and also cannot use more than n/k copies of k.
(1 + x + x2 + x3 + x4 + x5 + x6 + x7 + x8 )(1 + x2 + x4 + x6 + x8 )(1 + x3 + x6 )
(1 + x4 + x8 )(1 + x5 )(1 + x6 )(1 + x7 )(1 + x8 )
= 1 + x + 2x2 + 3x3 + 5x4 + 7x5 + 11x6 + 15x7 + 22x8 + · · · + x56 ,
so p8 = 22. Note that all of the coefficients prior to this are also correct, but the following
coefficients are not necessarily the corresponding partition numbers.
Partitions of integers have some interesting properties. Let pd (n) be the number of
partitions of n into distinct parts; let po (n) be the number of partitions into odd parts.
6, 5 + 1, 4 + 2, 3 + 2 + 1,
5 + 1, 3 + 3, 3 + 1 + 1 + 1, 1 + 1 + 1 + 1 + 1 + 1,
so po (6) = 4.
In fact, for every n, pd (n) = po (n), and we can see this by manipulating generating
functions. The generating function for pd (n) is
∞
∏
fd (x) = (1 + x)(1 + x )(1 + x ) · · · =
2 3
(1 + xi ).
i=1
We can write
1 − x2 1 − x4 1 − x6
fd (x) = · · ···
1 − x 1 − x2 1 − x3
and notice that every numerator is eventually canceled by a denominator, leaving only the
denominators containing odd powers of x, so fd (x) = fo (x).
3.3 Partitions of Integers 61
We can also use a recurrence relation to find the partition numbers, though in a
somewhat less direct way than the binomial coefficients or the Bell numbers. Let pk (n) be
the number of partitions of n into exactly k parts. We will find a recurrence relation to
compute the pk (n), and then
∑n
pn = pk (n).
k=1
Now consider the partitions of n into k parts. Some of these partitions contain no 1s, like
3 + 3 + 4 + 6, a partition of 16 into 4 parts. Subtracting 1 from each part, we get a partition
of n − k into k parts; for the example, this is 2 + 2 + 3 + 5. The remaining partitions of
n into k parts contain a 1. If we remove the 1, we are left with a partition of n − 1 into
k − 1 parts. This gives us a 1–1 correspondence between the partitions of n into k parts,
and the partitions of n − k into k parts together with the partitions of n − 1 into k − 1
parts, so pk (n) = pk (n − k) + pk−1 (n − 1).
Using this recurrence we can build a triangle containing the pk (n), and the row sums of
this triangle give the partition numbers. For all n, p1 (n) = 1, which gives the first column
of the triangle, after which the recurrence applies. Also, note that pk (n) = 0 when k > n
and we let pk (0) = 0; these are needed in some cases to compute the pk (n − k) term of
the recurrence. Here are the first few rows of the triangle; at the left are the row numbers,
and at the right are the row sums, that is, the partition numbers. For the last row, each
entry is the sum of the like-colored numbers in the previous rows. Note that beginning
with p4 (7) = 3 in the last row, pk (7) = pk−1 (6), as pk (7 − k) = 0.
1 1 0
1
2 1 1 0 2
3 1 1 1 0 3
4 1 2 1 1 5
5 1 2 2 1 1 7
6 1 3 3 2 1 1 11
7 1 3 4 3 2 1 1 15
Yet another sometimes useful way to think of a partition is with a Ferrers diagram.
Each integer in the partition is represented by a row of dots, and the rows are ordered
from longest on the top to shortest at the bottom. For example, the partition 3 + 3 + 4 + 5
would be represented by
• • • • •
• • • •
• • •
• • •
The conjugate of a partition is the one corresponding to the Ferrers diagram produced
by flipping the diagram for the original partition across the main diagonal, thus turning
62 Chapter 3 Generating Functions
rows into columns and vice versa. For the diagram above, the conjugate is
• • • •
• • • •
• • • •
• •
•
Exercises 3.3.
1. Use generating functions to find p15 .
2. Find the generating function for the number of partitions of an integer into distinct odd
parts. Find the number of such partitions of 20.
3. Find the generating function for the number of partitions of an integer into distinct even
parts. Find the number of such partitions of 30.
4. Find the number of partitions of 25 into odd parts.
5. Find the generating function for the number of partitions of an integer into k parts; that is,
the coefficient of xn is the number of partitions of n into k parts.
6. Complete row 8 of the table for the pk (n), and verify that the row sum is 22, as we saw in
example 3.3.3.
7. A partition of n is self-conjugate if its Ferrers diagram is symmetric around the main diagonal,
so that its conjugate is itself. Show that the number of self-conjugate partitions of n is equal
to the number of partitions of n into distinct odd parts.
Note that some initial values must be specified for the recurrence relation to define a unique
sequence.
The starting index for the sequence need not be zero if it doesn’t make sense or some
other starting index is more convenient. We saw two recurrence relations for the number
3.4 Recurrence Relations 63
of derangements of [n]:
D1 = 0, Dn = nDn−1 + (−1)n .
and
D1 = 0, D2 = 1, Dn = (n − 1)(Dn−1 + Dn−2 ).
To “solve” a recurrence relation means to find a formula for an . There are a variety
of methods for solving recurrence relations, with various advantages and disadvantages in
particular cases. One method that works for some recurrence relations involves generating
functions. The idea is simple, if the execution is not always: Let
∞
∑
f (x) = ai xi ,
i=0
To get the second sum we have simply “re-indexed” so that the index value gives the
exponent on x, just as in the series for f (x). Likewise,
∞
∑ ∞
∑
x2 f (x) = Fi xi+2 = Fi−2 xi .
i=0 i=2
f (x) = x + F2 x2 + F3 x3 + F4 x4 + · · ·
xf (x) = x2 + F2 x3 + F3 x4 + · · ·
x2 f (x) = x3 + F2 x4 + · · ·
x −x
f (x) = = 2 .
1−x−x 2 x +x−1
If we can find an explicit representation for the series for this function, we will have solved
the recurrence relation. Here is where things could go wrong, but in this case it works out.
Let a and b be the roots of x2 + x − 1; using the quadratic formula, we get
√ √
−1 + 5 −1 − 5
a= ,b = .
2 2
Borrowing a technique from calculus, we write
−x A B
= + .
x2 +x−1 x−a x−b
∑ ∞ ∑ ∞
1 1
= (1/a)i xi and = (1/b)i xi .
1 − x/a i=0
1 − x/b i=0
A B
Fi = − (1/a)i − (1/b)i .
a b
3.4 Recurrence Relations 65
Simplifying gives √ √
1 ( 1 + 5 )i 1 ( 1 − 5 )i
Fi = √ −√ .
5 2 5 2
√
Here’s an interesting feature of this expression: since |(1 − 5)/2| < 1, the limit of
√
((1 − 5)/2)i as i goes to infinity is 0. So when i is large enough,
( √ ))
(
1 1+ 5 i
Fi = round √ ,
5 2
that is, the first term rounded to the nearest integer. As it turns out, this is true starting
with i = 0.
You can see how to do the entire solution in Sage.
We can also use this expression for Fn to compute limn→∞ Fn /Fn−1 .
(
√ )n (√ )n
√1 1+ 5
− √1 1− 5 √
Fn 5 2 5 2 1+ 5
lim
n→∞ Fn−1
= lim ( √ )n−1 ( √ )n−1 = .
n→∞ 1 2
√
5
1+ 5
2 − √1
5
1− 5
2
Exercises 3.4.
1. Find the generating function for the solutions to hn = 4hn−1 − 3hn−2 , h0 = 2, h1 = 5, and
use it to find a formula for hn .
2. Find the generating function for the solutions to hn = 3hn−1 + 4hn−2 , h0 = h1 = 1, and use
it to find a formula for hn .
3. Find the generating function for the solutions to hn = 2hn−1 + 3n , h0 = 0, and use it to find
a formula for hn .
4. Find the generating function for the solutions to hn = 4hn−2 , h0 = 0, h1 = 1, and use it to
find a formula for hn . (It is easy to discover this formula directly; the point here is to see
that the generating function approach gives the correct answer.)
5. Find the generating function for the solutions to hn = hn−1 + hn−2 , h0 = 1, h1 = 3, and use
it to find a formula for hn .
6. Find the generating function for the solutions to hn = 9hn−1 − 26hn−2 + 24hn−3 , h0 = 0,
h1 = 1, h2 = −1, and use it to find a formula for hn .
7. Find the generating function for the solutions to hn = 3hn−1 + 4hn−2 , h0 = 0, h1 = 1, and
use it to find a formula for hn .
8. Find a recursion for the number of ways to place flags on an n foot pole, where we have red
flags that are 2 feet high, blue flags that are 1 foot high, and yellow flags that are 1 foot
high; the heights of the flags must add up to n. Solve the recursion.
9. In Fibonacci’s original problem, a farmer started with one (newborn) pair of rabbits at month
0. After each pair of rabbits was one month old, they produced another pair each month
in perpetuity. Thus, after 1 month, he had the original pair, after two months 2 pairs,
66 Chapter 3 Generating Functions
three months, 3 pairs, four months, 5 pairs, etc. The number of pairs of rabbits satisfies
hn = hn−1 + hn−2 , h0 = h1 = 1. (Note that this is slightly different than our definition, in
which h0 = 0.)
Suppose instead that each mature pair gives birth to two pairs of rabbits. The sequence
for the number of pairs of rabbits now starts out h0 = 1, h1 = 1, h2 = 3, h3 = 5, h4 = 11.
Set up and solve a recurrence relation for the number of pairs of rabbits. Show also that the
sequence statisfies hn = 2hn−1 + (−1)n .
10. Explain why
( √
)n ( √
)n
√1 1+ 5
2
− √1 1− 5
2
√
Fn 5 5 1+ 5
lim = lim ( √
)n−1 ( √
)n−1 = .
n→∞ Fn−1 n→∞ 2
√1
5
1+ 5
2
− √1
5
1− 5
2
• •
• • •
•. •
Let us denote this number by Cn ; these are the Catalan numbers. For convenience,
we allow a rooted binary tree to be empty, and let C0 = 1. Then it is easy to see that
C1 = 1 and C2 = 2, and not hard to see that C3 = 5. Notice that any rooted binary tree
on at least one vertex can be viewed as two (possibly empty) binary trees joined into a
new tree by introducing a new root vertex and making the children of this root the two
roots of the original trees; see figure 3.5.2. (To make the empty tree a child of the new
vertex, simply do nothing, that is, omit the corresponding child.)
Thus, to make all possible binary trees with n vertices, we start with a root vertex, and
then for its two children insert rooted binary trees on k and l vertices, with k + l = n − 1,
3.5 Catalan Numbers 67
• + • = • •
• • • • • •
•. • • •
for all possible choices of the smaller trees. Now we can write
∑
n−1
Cn = Ci Cn−i−1 .
i=0
C3 = C0 C2 + C1 C1 + C2 C0 = 1 · 2 + 1 · 1 + 2 · 1 = 5,
as mentioned above. Once we know the trees on 0, 1, and 2 vertices, we can combine them
in all possible ways to list the trees on 3 vertices, as shown in figure 3.5.3. Note that the
first two trees have no left child, since the only tree on 0 vertices is empty, and likewise
the last two have no right child.
• • • • •
• • • • • •
.• • • •
∑∞ i
Now we use a generating function to find a formula for Cn . Let f = i=0 Ci x .
∑ n
Now consider f 2 : the coefficient of the term xn in the expansion of f 2 is i=0 Ci Cn−i ,
corresponding to all possible ways to multiply terms of f to get an xn term:
quadratic formula, √
1±
1 − 4x
f= ,
2x
as long as x ̸= 0. It is not hard to see that as x approaches 0,
√
1+ 1 − 4x
2x
goes to infinity while √
1− 1 − 4x
2x
goes to 1. Since we know f (0) = C0 = 1, this is the f we want.
Now by Newton’s Binomial Theorem 3.1.1, we can expand
∑∞ ( )
√ 1/2
1 − 4x = (1 + (−4x)) 1/2
= (−4x)n .
n=0
n
Then
√ ∞ ( ) ∞ ( )
1− 1 − 4x ∑ 1 1/2 ∑ 1 1/2
= − n n−1
(−4) x = − (−4)n+1 xn .
2x n=1
2 n n=0
2 n + 1
( 1/2 )
Expanding the binomial coefficient n+1 and reorganizing the expression, we discover that
( ) ( )
1 1/2 1 2n
Cn = − (−4) n+1
= .
2 n+1 n+1 n
In exercise 7 in section 1.2, we saw that the number of properly matched sequences of
( ) ( 2n )
parentheses of length 2n is 2n n − n+1 , and called this Cn . It is not difficult to see that
( ) ( ) ( )
2n 2n 1 2n
− = ,
n n+1 n+1 n
Exercises 3.5.
1. Show that ( ) ( ) ( )
2n 2n 1 2n
− = .
n n+1 n+1 n
2. Find a simple expression f (n) so that Cn+1 = f (n)Cn . Use this to compute C1 , . . . , C6 from
C0 .
3. Show that if An is the number of properly matched sequences of parentheses of length 2n,
then
∑
n−1
An = Ai An−i−1 .
i=0
Do this in the same style that we used for the number of rooted binary trees: Given all the
sequences of shorter length, explain how to combine them to produce the sequences of length
2n, in such a way that the sum clearly counts the number of sequences. Hint: Prove the
following lemma: If s is a properly matched sequence of parentheses of length 2n, s may
be written uniquely in the form (s1 )s2 , where s1 and s2 are properly matched sequences of
parentheses whose lengths add to 2n−2. For example, (())() = ([()])[()] and ()(()) = ([ ])[(())],
with the sequences s1 and s2 indicated by [ ]. Note that s1 and s2 are allowed to be empty
sequences, with length 0.
4. Consider a “staircase” as shown below. A path from A to B consists of a sequence of edges
starting at A, ending at B, and proceeding only up or right; all paths are of length 6. One
such path is indicated by arrows. The staircase shown is a “3 × 3” staircase. How many
paths are there in an n × n staircase?
.....
B
..........
..
...
.
..............................
... .. ......
... . ...
... ..
... ...
. . ..
.....................................................................
.
. .
...... ..
. ...
..... ... ..
... ... ...
... .
. .
..
......................... ..................................................
. ...
.. . .
A
5. A convex polygon with n ≥ 3 sides can be divided into triangles by inserting n − 3 non-
intersecting diagonals. In how many different ways can this be done? The possibilities for
n = 5 are shown.
. . . . .