ConceptComplete

Generating Functions - Examples and Constructions

Generating functions excel at solving counting problems involving combinations with restrictions. These examples demonstrate systematic approaches to constructing and manipulating generating functions for complex enumeration scenarios.

ExampleInteger Partitions

A partition of a positive integer nn is a way of writing nn as a sum of positive integers, where order doesn't matter. For example, partitions of 4 are: 4,3+1,2+2,2+1+1,1+1+1+14, 3+1, 2+2, 2+1+1, 1+1+1+1.

The generating function for partitions is: P(x)=k=111xk=1(1x)(1x2)(1x3)P(x) = \prod_{k=1}^{\infty} \frac{1}{1-x^k} = \frac{1}{(1-x)(1-x^2)(1-x^3)\cdots}

The coefficient of xnx^n in P(x)P(x) is p(n)p(n), the number of partitions of nn.

Explanation: The factor 11xk=1+xk+x2k+x3k+\frac{1}{1-x^k} = 1 + x^k + x^{2k} + x^{3k} + \cdots represents using 0, 1, 2, ... parts of size kk. The product over all kk gives all partitions.

ExampleDistributing Identical Objects

How many ways can we distribute nn identical balls into 3 distinct boxes, where the first box gets an even number, the second gets at most 4, and the third gets at least 2?

Generating function for each box:

  • Box 1 (even): (1+x2+x4+)=11x2(1 + x^2 + x^4 + \cdots) = \frac{1}{1-x^2}
  • Box 2 (at most 4): (1+x+x2+x3+x4)=1x51x(1 + x + x^2 + x^3 + x^4) = \frac{1-x^5}{1-x}
  • Box 3 (at least 2): (x2+x3+x4+)=x21x(x^2 + x^3 + x^4 + \cdots) = \frac{x^2}{1-x}

Combined generating function: G(x)=11x21x51xx21x=x2(1x5)(1x)2(1x2)G(x) = \frac{1}{1-x^2} \cdot \frac{1-x^5}{1-x} \cdot \frac{x^2}{1-x} = \frac{x^2(1-x^5)}{(1-x)^2(1-x^2)}

The coefficient of xnx^n gives the answer for nn balls.

ExampleSolving Recurrences with Generating Functions

Solve an=3an12an2a_n = 3a_{n-1} - 2a_{n-2} with a0=1a_0 = 1 and a1=2a_1 = 2 using generating functions.

Let G(x)=n=0anxnG(x) = \sum_{n=0}^{\infty} a_n x^n. Multiply the recurrence by xnx^n and sum: n=2anxn=3n=2an1xn2n=2an2xn\sum_{n=2}^{\infty} a_n x^n = 3\sum_{n=2}^{\infty} a_{n-1} x^n - 2\sum_{n=2}^{\infty} a_{n-2} x^n

G(x)a0a1x=3x(G(x)a0)2x2G(x)G(x) - a_0 - a_1 x = 3x(G(x) - a_0) - 2x^2 G(x)

G(x)12x=3xG(x)3x2x2G(x)G(x) - 1 - 2x = 3xG(x) - 3x - 2x^2G(x)

G(x)(13x+2x2)=1xG(x)(1 - 3x + 2x^2) = 1 - x

G(x)=1x13x+2x2=1x(1x)(12x)=112xG(x) = \frac{1-x}{1-3x+2x^2} = \frac{1-x}{(1-x)(1-2x)} = \frac{1}{1-2x}

Therefore: an=2na_n = 2^n.

ExampleDerangements via EGF

The exponential generating function for derangements is: D(x)=ex1xD(x) = \frac{e^{-x}}{1-x}

This follows from the formula Dn=n!k=0n(1)kk!D_n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}: n=0Dnxnn!=n=0(k=0n(1)kk!)xn\sum_{n=0}^{\infty} D_n \frac{x^n}{n!} = \sum_{n=0}^{\infty} \left(\sum_{k=0}^{n} \frac{(-1)^k}{k!}\right) x^n

Using series manipulation and the fact that ex=k=0(1)kxkk!e^{-x} = \sum_{k=0}^{\infty} \frac{(-1)^k x^k}{k!} and 11x=n=0xn\frac{1}{1-x} = \sum_{n=0}^{\infty} x^n: D(x)=ex11xD(x) = e^{-x} \cdot \frac{1}{1-x}

Remark

Generating functions are particularly powerful for partition problems. Besides ordinary partitions, we can count partitions with distinct parts (using k=1(1+xk)\prod_{k=1}^{\infty}(1+x^k)), partitions into odd parts, partitions with bounded part size, and many other variants. Euler's partition theorem states that the number of partitions into odd parts equals the number of partitions into distinct parts, proven elegantly by showing their generating functions are equal: k=111x2k1=k=1(1+xk)\prod_{k=1}^{\infty}\frac{1}{1-x^{2k-1}} = \prod_{k=1}^{\infty}(1+x^k). The pentagonal number theorem and Rogers-Ramanujan identities represent deep results in this area, connecting partition theory to number theory and modular forms.

These examples illustrate how generating functions transform counting problems into algebraic manipulations, often revealing surprising patterns and connections.