# Lecture 12: Bivariate Generating Functions

A bivariate generating (BGF) is a formal power series in two variables. BGFs can be used
to analyze the behaviour of a parameter of a family of combinatorial structures. For
example, important parameters for binary trees are: height, number of leaves, path length.
These parameters are important for algorithm analysis as they correspond to the
performance of algorithms that compute with or are modeled by binary trees. One variable
in the BGF is used to track the size of the structure (e.g. number of nodes in a binary
tree) and the other is used to track the parameter of interest (e.g. height, number of
leaves, path length).
Operations on BGFs can be used to compute the average value for a given parameter in
all structures of a given size.

### Background Material

- Lecture 9 on generating functions.
- Lecture 12 on PGFs.
- Lecture 10 on quicksort.
- Chapter 3 (sec. on BGFs) from Flajolet and Sedgewick.
- Attribute grammars.

### Reading

Make sure you carefully study the Maple worksheet for this lecture. Additional
handwritten notes are provided.
### Topics

- Definition and examples of BGFs
- A(u,z) = sum_k sum_n a_kn*u^k*z^n = sum_n a_n(u)*z^n
- [u^k*z^n] A(u,z) = a_kn. [z^n]A(u,z) = a_n(u).
- Let P be a family of combinatorial structures. For p in P, let cost(p) be
be the parameter of interest and |p| the size of p. The BGF associated
with P and the specified cost is P(u,z) = sum_{p in P} u^cost(p) * z^|p| =
sum_k sum_n p_kn*u^k*z^n = sum_n p_n(u)*z^n, where p_kn is the number
of structures of size n with cost k. p_n(u) is an ordinary generating
function in the variable u, with p_n(u) = sum_k p_nk*u^k. Note that, p_n(1) is equal to the
number of structures of size n, and P(1,z) = sum_n p_n(1)*z^n is the ordinary
generating function associated with P.
- E.G. with binary trees. a_kn = number of binary trees with path length k
and n nodes. P(u,z) = 1 + z + 2*u*z^2 + (u^2 + 4*u^3)*z^3 + ...

- Computing mean value of parameter for a given size.
- Let P(u,z) be a BGF for a given structure and cost parameter.
P_u(1,z), i.e. the partial derivative with respect to u of P(u,z)
evaluated at u=1 is the cumulative generating function for the given
structure and cost parameter: P_u(1,z) = sum_n c_n * z^n, where c_n
is the sum of the costs of all structures of size n.
- The average cost of all structures of size n is equal to [z^n] P_u(1,z)
divided by [z^n] P(1,z).

- Attribute grammars in the combstruct package. Attributes are used to
define a cost paramater. BGFs are automatically derived using the symbolic
method and are used to analyze the cost parameter.
- Defining attributes.
- Computing functional equations for the BGF associated with the structure
and attribute. series expansion of associated BGFs.
- Computing moments.

- Additional examples
- Other costs, e.g. height, number of leaves.
- Binary search trees, general trees, etc.

### Maple worksheets and programs

- bgf.mw - Maple worksheet illustrating BGFs and attribute
grammars in the combstruct package.

### Lecture Notes

- bgf.pdf - Handwritten notes from class.

### Assignment

Created: Dec. 1, 2006 by jjohnson@cs.drexel.edu