组合数学 (Fall 2011)/Generating functions: Difference between revisions
imported>Etone |
imported>Etone |
||
Line 443: | Line 443: | ||
== Reference == | == Reference == | ||
* ''Graham, Knuth, and Patashnik'', Concrete Mathematics: A Foundation for Computer Science, Chapter 7. | * ''Graham, Knuth, and Patashnik'', Concrete Mathematics: A Foundation for Computer Science, Chapter 7. | ||
* ''van Lin and Wilson'', A course in combinatorics, Chapter 14. | * ''van Lin and Wilson'', A course in combinatorics, Chapter 14. |
Latest revision as of 11:46, 6 March 2013
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
The ordinary generating function (OGF) defined by
So
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
.
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
.
For example, when
.
So it "generate" all subsets of the 3-set. Writing
In general,
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
The generating function of this sequence is
The coefficient of
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 the solution. 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 solution. It also holds that . The proof is left as an exercise.
In both problems, the solution is given by
Theorem ,
- where
and .
The quantity
We now prove this theorem by using generating functions.
The ordinary generating function for the Fibonacci number
.
We have that
For generating functions, there are general ways to generate
So we have
,
hence
.
The value of
It is easier to expand the generating function by breaking it into two geometric series.
Proposition - Let
and . It holds that .
- Let
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.
|
Note that the expression
.
Therefore,
So the
.
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 non-trivial 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
We can apply several natural algebraic operations on the formal power series.
Generating function manipulation - Let
and .
- Let
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.
Expanding generating functions
The last step of solving recurrences by generating function is expanding the closed form generating function
,
where
Some interesting special cases are very useful.
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
Binomial theorem
The
.
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
Let
,
where each
Let all
The last equation is due to the the definition of
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
Catalan Number
We now introduce a class of counting problems, all with the same solution, called Catalan number.
The
- Cn 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:
- Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which are correctly matched:
- Cn 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 Cn is the number of full binary trees with n + 1 leaves:

- Cn 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:

- Cn 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:

- Cn is the number of stack-sortable permutations of {1, ..., n}. A permutation w is called stack-sortable 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 one-element sequences.
- Cn 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 , .
Let
Due to the recurrence,
.
Solving
.
Only one of these functions can be the generating function for
.
It is easy to check that the correct function is
.
Expanding
Then, we have
Thus,
So we prove the following closed form for Catalan number.
Theorem .
Analysis of Quicksort
Given as input a set
Quicksort algorithm Input: a set
of numbers.- if
do:- pick an
as the pivot; - partition
into and ; - recursively sort
and ;
- pick an
- if
Usually the input set
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
The Quicksort recursion - and
.
The recursion is got from averaging over the
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
- Evaluate the power series:
. - Apply the convolution rule of OGF:
,- where
, - therefore,
.
- where
- 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
.
Expanding
Due to Taylor's expansion,
. .
The generating function
Thus the coefficient of
where
Therefore, the average number of comparisons used by the quicksort to sort lists of length
.
Reference
- Graham, Knuth, and Patashnik, Concrete Mathematics: A Foundation for Computer Science, Chapter 7.
- van Lin and Wilson, A course in combinatorics, Chapter 14.