Вы находитесь на странице: 1из 6

GENERATING FUNCTIONS

RAHUL ARYA
1. Introduction
Generating functions are a technique used to nd closed formulas for terms in a sequence,
where the sequence is dened recursively. In addition, generating functions can be used for
nding solutions to counting problems. Both of these cases will be studied in the examples
below.
In essence, the generating function technique involves writing each term in the given
sequence as a coecient in an innite polynomial
1
. Let S = S
0
, S
1
, S
2
, . . .. The sequence
S is associated with a function G(x), called its generating function, which is dened to be
G(x) =

i=0
S
i
x
i
= S
0
x
0
+S
1
x
1
+S
2
x
2
+ .
By manipulating G(x) and using the given information about the sequence, we try to nd
a closed formula for G(x). Next, we try to write a series expansion for G(x) and so nd the
coecient of the x
n
term, which will be equal to S
n
. Occasionally, nding the expansion
requires us to rewrite G(x) as the sum of simpler functions.
2. Fibonacci Numbers
The Fibonacci numbers are a sequence dened by the following recurrence:
F
n
= F
n1
+F
n2
F
0
= 0
F
1
= 1.
The rst few Fibonacci numbers are
F = 0, 1, 1, 2, 3, 5, 8, 13, 21, . . ..
The closed formula for the Fibonacci numbers is not obvious, but can be easily found using
generating functions. Let G(x) be the Fibonacci generating function, dened as
G(x) = 0x
0
+ 1x
1
+ 1x
2
+ 2x
3
+ 3x
4
+ 5x
5
+ 8x
6
+ 13x
7
+ 21x
8
+
Date: 28 July 2013.
1
Actually, it may not be a polynomial but for the purpose of these notes this is the only case we will
consider.
1
2 RAHUL ARYA
By multiplying G(x) with x, and then adding it to G(x), we get
(1 +x)G(x) = 1x
1
+ 2x
2
+ 3x
3
+ 5x
4
+ 8x
5
+ 13x
6
+ 21x
7
+ .
Multiplying this by x and then adding x, we obtain
x(1 +x)G(x) +x = 0x
0
+ 1x
1
+ 1x
2
+ 2x
3
+ 3x
4
+ 5x
5
+ 8x
6
+ 13x
7
+ 21x
8
+
= G(x).
Isolating G(x), we get
G(x) =
x
x
2
+x 1
.
We now have a closed formula for G(x). Recall that the nth Fibonacci number, F
n
, is the
coecient of x
n
in the expansion of G(x). Unfortunately, this quantity is still not apparent
from the closed formula for G(x). To determine this quantity, we will separate this formula
into the sum of two geometric series by writing it in the form
x
_
A
x
+
B
x

_
where A and B are constants, and and

are the roots of x


2
+x1. Using the quadratic
formula, we obtain = (1 +

5)/2 and

= (1

5)/2. Solving for A and B, we nd


that
A =
1

5
B =
1

5
.
Here we should note that these numbers are very similar to . This is to be expected, as
and the Fibonacci numbers are closely related. We can now rewrite G(x) to become
G(x) = x
_
1

5
x

5
x
_
.
Simplifying and rewriting, we get
G(x) =
x

5
_
1

1
x

1
x

_
.
We can now observe that the terms inside the bracket are similar to the geometric sum-
mation formula. Converting them into geometric sums, we get
G(x) =
x

5
_

i=0
1

_
x

_
i

i=0
1

_
x

_
i
_
.
Moving the x inside the summations, we get
G(x) =
1

5
_

i=0
_
x

_
i+1

i=0
_
x

_
i+1
_
.
GENERATING FUNCTIONS 3
We can now see the x
n
coecient, which is
F
n
=
1

5
__
1

_
n

_
1

_
n
_
.
Substituting in the values of and

, we get
F
n
=
1

5
__
1 +

5
2
_
n

_
1

5
2
_
n
_
which is the formula for the Fibonacci series. Note that this formula is entirely nontrivial,
and would have been very hard to derive without the aid of generating functions.
3. The Fruit Problem
2
The next problem is intended to show how generating series can simplify calculations
enormously. In this problem, we have to construct a bag of fruits with the following
constraints.
The number of apples must be even.
The number of bananas must be a multiple of 5.
There can be at most four oranges.
There can be at most one pear.
The problem asks us to nd a closed formula for the number of ways we can do this,
assuming we have exactly n fruits. To solve this problem, let us rst look at the generating
functions for each restriction individually.
G
1
(x) = 1 +x
2
+x
4
+x
6
+ =
1
1 x
2
G
2
(x) = 1 +x
5
+x
10
+ =
1
1 x
5
G
3
(x) = 1 +x +x
2
+x
3
+x
4
=
1 x
5
1 x
G
4
(x) = 1 +x = 1 +x.
From this, we cannot see any obvious way to nd the generating function for all of these
conditions combined. Let us look at the coecient of the x
n
term of the product. If we
consider one possible way to compose a bag of n fruits with these restrictions, we can see
that this way will be included in the product of the generating function in the x
n
term.
Thus, the product of these four functions will be the generating function with all of these
conditions in force.
Finding the product of the above four generating functions gives us
1
(1 x)
2
.
2
Problems in this section taken from MIT Mathematics for Computer Science 18.062J Fall 05
4 RAHUL ARYA
At this point, let us consider the generating function
1
1 x
= 1 +x +x
2
+x
3
+ .
Taking its derivative, we get
1
(1 x)
2
= 1 + 2x + 3x
2
+
which is the function we are using. Therefore, S
n
= n+1 and so the number of ways to ll
a bag with n fruits under these conditions is n+1. This statement is not at all obvious, and
shows how useful generating functions are. An interesting thing to note is that by taking
the product of two generating functions that represent conditions on counting problems,
we get a generating function which represents both of the conditions. This technique is
useful, as solving the problem without it would be very messy and complicated to solve
this problem.
4. Catalan Numbers
Consider a string made of left and right parenthesis. We associate each left parenthesis
with a corresponding right parenthesis (which must come after it). We have the following
recursive denition. We say that a string is balanced if it contains no symbols (i.e., no left
or right parenthesis) or if the portion of the string between any left parenthesis and the
corresponding right parenthesis is balanced. Note that this implies that every parenthesis
has a corresponding parenthesis of the opposite type. For example, (()) and ()(()())() are
balanced while ()(( is not. Let C
n
denote the number of distinct balanced strings of length
2n. C
n
is given by the recurrence relation
C
n+1
=
n

i=0
C
i
C
ni
.
The justication for this recurrence relation is that any balanced string S of length 2n +2
can be written uniquely as an opening parenthesis, followed by a balanced string S
1
of
length i, the corresponding closing parenthesis, and a balanced string S
2
of length n i.
Note here that these conditions follow from the denition of a balanced string. Since S
1
has length i and S
2
has length ni, the number of choices for S
1
and S
2
are C
i
and C
ni
,
respectively. As such, the number of ways a string of length n + 1 can be written is equal
to the product of C
i
and C
ni
, summed over i ranging from 0 to n.
To nd the closed formula for C
n
, we will create the generating function G(x) with terms
G(x) = C
0
+C
1
x
1
+C
2
x
2
+C
3
x
3
+ .
Consider the coecient of the x
n
term in G
2
x. It would be C
0
C
n
+C
1
C
n1
+ +C
n
C
0
),
which equals C
n+1
because of the aforementioned recurrence relation. Thus,
G
2
(x) = C
1
+C
2
x +C
3
x
2
+ .
Multiplying this by x, we obtain
xG
2
(x) = C
1
x
1
+C
2
x
2
+C
3
x
3
+ .
GENERATING FUNCTIONS 5
Adding one to both sides, we get
1 +xG
2
(x) = C
0
+C
1
x
1
+C
2
x
2
+C
3
x
3
+ .
Note that the right-hand side is the same as G(x). Therefore, we have the relation
1 +xG
2
(x) = G(x).
As this as a quadratic in G(x), we can solve to get
G(x) =
1

1 4x
2x
.
Now we have to determine which of these two solutions is equal to G(x). We will do this
by rationalizing the numerator, to get
G(x) =
2
1

1 4x
.
Letting x 0
+
, we see that when the sign of

1 4x is negative, the expression approaches


innity. Since G(0) = C
0
= 1, this option cannot be correct and so we can see that
G(x) =
1

1 4x
2x
.
For now, let us consider the series expansion of

1 +y. Using Newtons Generalized
Binomial Theorem, we can see that
_
1 +y =

i=0
_
1/2
i
_
y
i
=

i=0
_
1
2
_ _

1
2
_ _

3
2
_

_

2i3
2
_
i!
y
i
.
Rewriting this, we obtain
_
1 +y = 1

i=1
_

1
2
_
i
1 3 (2i 3)
i!
y
i
.
Multiplying both the numerator and denominator of the second term by 2 4 6 (2i 2),
we get
_
1 +y = 1 2

i=1
_

1
4
_
i
(2i 2)!
(i 1)!(i 1)!
y
i
i
.
We can rewrite this as
_
1 +y = 1 2

i=1
_

1
4
_
i
_
2i 2
i 1
_
y
i
i
.
Now, substituting in 4x for y we get

1 4x = 1 2

i=1
_

1
4
_
i
_
2i 2
i 1
_
(4x)
i
i
= 1 2

i=1
_
2i 2
i 1
_
x
i
i
.
6 RAHUL ARYA
Shifting the sum index down by 1, we obtain

1 4x = 1 2

i=0
_
2i
i
_
x
i+1
i + 1
.
We can now substitute this expression back into G(x), to get
G(x) =
1

1 4x
2x
=
1
_
1 2

i=0
_
2i
i
_
x
i+1
i+1
_
2x
=

i=0
_
2i
i
_
x
i
i + 1
.
It follows that
C
n
=
_
2n
n
_
1
n + 1
.
5. Conclusion
In conclusion, these notes have shown how generating functions can facilitate nding
the closed formula for sequences dened by recurrence relations. They provide a simple,
general and powerful approach for nding the nth term of a series, and can also be applied
to solve counting problems. These notes have demonstrated all of these applications, but
have barely brushed the surface of what generating functions can do. For example, with
simple manipulations to the closed formula of the generating function, we can nd the
sums of several related sequences. These operations are straightforward with generating
functions, but would be dicult without them. In summary, generating functions are a
versatile tool for solving a variety of problems.