组合数学 (Spring 2015)/Generating functions
Contents
Generating Functions
In Stanley's magnificent book Enumerative Combinatorics, he comments the generating function as "the most useful but most difficult to understand method (for counting)".
The solution to a counting problem is usually represented as some depending a parameter . Sometimes this is called a counting function as it is a function of the parameter . can also be treated as a infinite series:
The ordinary generating function (OGF) defined by is
So . An expression in this form is called a formal power series, and is the sequence of coefficients.
Furthermore, the generating function can be expanded as
 G(x)=
so it indeed "generates" all the possible instances of the objects we want to count.
Usually, we do not evaluate the generating function on any particular value. remains as a formal variable without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence
 .
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.
Combinations
Suppose we wish to enumerate all subsets of an set. To construct a subset, we specifies for every element of the set whether the element is chosen or not. Let us denote the choice to omit an element by , and the choice to include it by . Using "" to represent "OR", and using the multiplication to denote "AND", the choices of subsets of the set are expressed as
 .
For example, when , we have
 .
So it "generate" all subsets of the 3set. Writing for and for , we have . The coefficient of is the number of subsets of a 3element set.
In general, has the coefficients which are the number of subsets of fixed sizes of an element set.
Suppose that we have twelve balls: 3 red, 4 blue, and 5 green. Balls with the same color are indistinguishable.
We want to determine the number of ways to select balls from these twelve balls, for some .
The generating function of this sequence is
The coefficient of gives the number of ways to select balls.
Fibonacci numbers
Consider the following counting problems.
 Count the number of ways that the nonnegative integer can be written as a sum of ones and twos (in order).
 The problem asks for the number of compositions of with summands from . Formally, we are counting the number of tuples for some such that and .
 Let be such a number. We observe that a composition either starts with a 1, in which case the rest is a composition of ; or starts with a 2, in which case the rest is a composition of . So we have the recursion for that
 .
 Count the ways to completely cover a rectangle with dominos without any overlaps.
 Dominos are identical rectangles, so that only their orientations  vertical or horizontal matter.
 Let be the number. It also holds that . The proof is left as an exercise.
In both problems, the solution is given by which satisfies the following recursion.
Instead we consider a closely related quantity:
Clearly and the quantity is called the Fibonacci number.
Theorem  ,
 where and .
The quantity is the socalled golden ratio, a constant with some significance in mathematics and aesthetics.
We now prove this theorem by using generating functions. The ordinary generating function for the Fibonacci number is
 .
We have that for , thus
For generating functions, there are general ways to generate and , or the coefficients with any smaller indices.
So we have
 ,
hence
 .
The value of is the coefficient of in the Taylor series for this formular, which is . Although this expansion works in principle, the detailed calculus is rather painful.
It is easier to expand the generating function by breaking it into two geometric series.
Proposition  Let and . It holds that
 .
 Let and . It holds that
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.

Note that the expression has a well known geometric expansion:
 .
Therefore, can be expanded as
So the th Fibonacci number is given by
 .
Solving recurrences
The following steps describe a general methodology of solving recurrences by generating functions.
 1. Give a recursion that computes . In the case of Fibonacci sequence
 .
 2. Multiply both sides of the equation by and sum over all . This gives the generating function
 .
 And manipulate the right hand side of the equation so that it becomes some other expression involving .
 .
 3. Solve the resulting equation to derive an explicit formula for .
 .
 4. Expand into a power series and read off the coefficient of , which is a closed form for .
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.
The second and the forth steps need some nontrivial analytic techniques.
Algebraic operations on generating functions
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of , which is easy; and then manipulating the resulting formal power series to express it in terms of , which is more difficult (because it works backwards).
We can apply several natural algebraic operations on the formal power series.
Generating function manipulation  Let and .
When manipulating generating functions, these rules are applied backwards; that is, from the righthandside to the lefthandside.
Expanding generating functions
The last step of solving recurrences by generating function is expanding the closed form generating function to evaluate its th coefficient.
Taylor expansion
In principle, we can always use the Taylor series
 ,
where is the value of the th derivative of evaluated at .
Geometric sequence
In the example of Fibonacci numbers, we use the well known geometric series:
 .
It is useful when we can express the generating function in the form of . The coefficient of in such is .
Binomial theorem
The th derivative of for some real is
 .
By Taylor series, we get a generalized version of the binomial theorem known as Newton's formula:
Newton's formular (generalized binomial theorem) If , then
 ,
where is the generalized binomial coefficient defined by
 .
Example: multisets
In the last lecture we gave a combinatorial proof of the number of multisets on an set. Now we give a generating function approach to the problem.
Let be an element set. We have
 ,
where each species a possible multiset on with multiplicity function .
Let all . Then
The last equation is due to the the definition of . Our task is to evaluate .
Due to the geometric sequence and the Newton's formula
So
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of as the combinatorial proof.
Catalan Number
We now introduce a class of counting problems, all with the same solution, called Catalan number.
The th Catalan number is denoted as . In Volume 2 of Stanley's Enumerative Combinatorics, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.
 C_{n} is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's (see also Dyck language). For example, the following are the Dyck words of length 6:
 Reinterpreting the symbol X as an open parenthesis and Y as a close parenthesis, C_{n} counts the number of expressions containing n pairs of parentheses which are correctly matched:
 C_{n} is the number of different ways n + 1 factors can be completely parenthesized (or the number of ways of associating n applications of a binary operator). For n = 3, for example, we have the following five different parenthesizations of four factors:
 Successive applications of a binary operator can be represented in terms of a full binary tree. (A rooted binary tree is full if every vertex has either two children or no children.) It follows that C_{n} is the number of full binary trees with n + 1 leaves:
 C_{n} is the number of monotonic paths along the edges of a grid with n × n square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for "move right" and Y stands for "move up". The following diagrams show the case n = 4:
 C_{n} is the number of different ways a convex polygon with n + 2 sides can be cut into triangles by connecting vertices with straight lines. The following hexagons illustrate the case n = 4:
 C_{n} is the number of stacksortable permutations of {1, ..., n}. A permutation w is called stacksortable if S(w) = (1, ..., n), where S(w) is defined recursively as follows: write w = unv where n is the largest element in w and u and v are shorter sequences, and set S(w) = S(u)S(v)n, with S being the identity for oneelement sequences.
 C_{n} is the number of ways to tile a stairstep shape of height n with n rectangles. The following figure illustrates the case n = 4:
Solving the Catalan numbers
Recurrence relation for Catalan numbers  , and for ,
 .
 , and for ,
Let be the generating function. Then
Due to the recurrence,
 .
Solving , we obtain
 .
Only one of these functions can be the generating function for , and it must satisfy
 .
It is easy to check that the correct function is
 .
Expanding by Newton's formula,
Then, we have
Thus,
So we prove the following closed form for Catalan number.
Theorem  .
Analysis of Quicksort
Given as input a set of numbers, we want to sort the numbers in in increasing order. One of the most famous algorithm for this problem is the Quicksort algorithm.
Quicksort algorithm Input: a set of numbers.
 if do:
 pick an as the pivot;
 partition into and ;
 recursively sort and ;
 if do:
Usually the input set is given as an array of the elements in an arbitrary order. The pivot is picked from a fixed position in the arrary (e.g. the first number in the array).
The time complexity of this sorting algorithm is measured by the number of comparisons.
The quicksort recursion
It is easy to observe that the running time of the algorithm depends only on the relative order of the elements in the input array.
Let be the average number of comparison used by the Quicksort to sort an array of numbers, where the average is taken over all total orders of the elements in the array.
The Quicksort recursion  and .
The recursion is got from averaging over the subcases that the pivot is chosen as the th smallest element for . Partitioning the input set to and takes exactly comparisons regardless the choice of the pivot. Given that the pivot is chosen as the th smallest element, the sizes of and are and respectively, thus the costs of sorting and are given recursively by and .
Manipulating the OGF
We write the ordinary generating function (OGF) for the quicksort:
The quicksort recursion also gives us another equation for formal power series:
We express the three terms , and in closed form involving as follows:
 Evaluate the power series: .
 Apply the convolution rule of OGF: ,
 where ,
 therefore, .
 Apply the differentiation rule of OGF: .
Therefore we have the following identity for the OGF for quicksort:
Equation for the generating function  .
Solving the equation
The above equation for the generating function is a firstorder linear differential equation, which has a general solution.
 .
Expanding
Due to Taylor's expansion,
 .
 .
The generating function is a convolution product of these two series.
Thus the coefficient of in , denoted as , is:
where is the th harmonic number defined as .
Therefore, the average number of comparisons used by the quicksort to sort lists of length is
 .
Reference
 Graham, Knuth, and Patashnik, Concrete Mathematics: A Foundation for Computer Science, Chapter 7.
 van Lin and Wilson, A course in combinatorics, Chapter 14.