Processing math: 100%

Counting With Generating Functions

Posted by Beetle B. on Mon 22 September 2014

Generating functions can be used for counting.

Consider the problem of finding the number of trees with internal nodes.

Let T be the set of all binary trees. Let |t| be the number of internal nodes in tree t. Let TN be the number of trees with |t|=N.

Then T(z)=tTz|t|=N0TNzN.

Note the two different ways of writing the sum.

General Approach

  • Let P be the set of all given objects.
  • Let pP be an element of that object.
  • Let PN be the number of elements with |p|=N.
  • Let cost(p) be the “cost” of p.
  • Let PNk be the number of p with |p|=N and cost(p)=k.
  • Define the cumulative cost to be CN=k0kPNk.
  • Define the cumulative function to be C(z)=N0CNzN=pPcost(p)z|p|.

Now the expected cost of size N is:

CNPN

Or, using generating functions:

[zN]C(z)[zN]P(z)

Notes

Note that this equality:

C(z)=N0CNzN=pPcost(p)z|p|.

allows you to generate identities by computing something in two different ways.