MathLabs

Combinatorics and discrete mathematics

Generating functions

Power series that encode a sequence of numbers as coefficients, turning combinatorics into algebra.

IntuitionWhat is a generating function?

Imagine a clothesline with infinitely many pegs, numbered 0,1,2,3,…0, 1, 2, 3, \dots. To describe a sequence of numbers a0,a1,a2,…a_0, a_1, a_2, \dots, you pin the number ana_n at peg nn. A generating function is this clothesline written as a single algebraic object: the number ana_n becomes the coefficient of xxn^n in a power series. The variable xx is never plugged in with a number — it just keeps every term in its own slot, so that adding, multiplying or shifting sequences becomes ordinary algebra on polynomials.

Plot of the cubic $1+x+x^2+x^3$ approximating the generating function $\frac{1}{1-x}$.
The curve is the cubic 1+x+x2+x31+x+x^2+x^3, the first four terms of 11−x=∑n=0∞xn\frac{1}{1-x}=\sum_{n=0}^{\infty}x^n, the generating function of the constant sequence an=1a_n=1. Each coefficient (drag a,b,c,da,b,c,d) is one peg of the sequence packed into the curve.

SchoolPacking a sequence into a power series

Definition: Ordinary generating function

The ordinary generating function (OGF) of a sequence a0,a1,a2,…a_0, a_1, a_2, \dots is the formal power series G(x)=∑n=0∞anxnG(x)=\sum_{n=0}^{\infty}a_nx^n. "Formal" means we treat this sum as an algebraic expression, not a function to be evaluated numerically; questions of convergence for particular values of xx are irrelevant to combinatorics.

G(x)=∑n=0∞anxnG(x)=\sum_{n=0}^{\infty}a_nx^n

The simplest building block is the sequence an=1a_n=1 for every n≥0n \ge 0, whose OGF is the geometric series 11−x=∑n=0∞xn\frac{1}{1-x}=\sum_{n=0}^{\infty}x^n. This identity is the engine behind most generating-function arguments: it says that 11−x\frac{1}{1-x} encodes "choose nn items from an unlimited supply, order irrelevant, one way each".

1(1−x)2=∑n=0∞(n+1)xn\frac{1}{(1-x)^2}=\sum_{n=0}^{\infty}(n+1)x^n
Common sequences and their ordinary generating functions
SequenceGenerating function
Constant, an=1a_n=111−x\frac{1}{1-x}
Linear, an=n+1a_n=n+11(1−x)2\frac{1}{(1-x)^2}
Fibonacci, an=Fna_n=F_nx1−x−x2\frac{x}{1-x-x^2}
Binomial row, an=(kn)a_n=\binom{k}{n}(1+x)k(1+x)^k

UndergraduateFrom recurrences to closed forms

Let F0=0, F1=1F_0=0,\ F_1=1 and Fn=Fn−1+Fn−2F_n=F_{n-1}+F_{n-2} for n≥2n\ge2. Then the ordinary generating function F(x)=∑n=0∞FnxnF(x)=\sum_{n=0}^{\infty}F_nx^n equals F(x)=x1−x−x2F(x)=\frac{x}{1-x-x^2}.

Why is it true?

A linear recurrence with constant coefficients always turns into a simple algebraic (in fact, rational) equation once you multiply through by xxn^n and sum over all nn — the recurrence becomes multiplication by a polynomial in xx.

Proof

Multiply the recurrence Fn=Fn−1+Fn−2F_n=F_{n-1}+F_{n-2} (valid for n≥2n\ge2) by xnx^n and sum over n≥2n\ge2: ∑n≥2Fnxn=∑n≥2Fn−1xn+∑n≥2Fn−2xn\sum_{n\ge2}F_nx^n=\sum_{n\ge2}F_{n-1}x^n+\sum_{n\ge2}F_{n-2}x^n.

The left side is F(x)−F0−F1x=F(x)−xF(x)-F_0-F_1x=F(x)-x. The first sum on the right is x∑n≥2Fn−1xn−1=x∑m≥1Fmxm=x(F(x)−F0)=xF(x)x\sum_{n\ge2}F_{n-1}x^{n-1}=x\sum_{m\ge1}F_mx^m=x(F(x)-F_0)=xF(x). The second sum is x2∑n≥2Fn−2xn−2=x2∑k≥0Fkxk=x2F(x)x^2\sum_{n\ge2}F_{n-2}x^{n-2}=x^2\sum_{k\ge0}F_kx^k=x^2F(x).

So F(x)−x=xF(x)+x2F(x)F(x)-x=xF(x)+x^2F(x), i.e. (1−x−x2)F(x)=x(1-x-x^2)F(x)=x.

Solving for F(x)F(x) gives F(x)(1−x−x2)=xF(x)(1-x-x^2)=x, hence F(x)=x1−x−x2F(x)=\frac{x}{1-x-x^2}, a single rational function that packs the entire infinite Fibonacci sequence.

With φ=1+52\varphi=\frac{1+\sqrt{5}}{2} and ψ=1−52\psi=\frac{1-\sqrt{5}}{2}, the roots of 1−x−x21-x-x^2 give the closed form Fn=15(φn−ψn)F_n=\frac{1}{\sqrt{5}}\left(\varphi^n-\psi^n\right) for every n≥0n\ge0.

Why is it true?

Every rational generating function with distinct linear factors in its denominator splits into a sum of simple geometric series, and each geometric series is read off directly as an explicit formula for the coefficient.

Proof

Factor the denominator: since the roots of 1−x−x2=01-x-x^2=0 are x=1/φx=1/\varphi and x=1/ψx=1/\psi, we have 1−x−x2=(1−φx)(1−ψx)1-x-x^2=(1-\varphi x)(1-\psi x).

Write x1−x−x2=A1−φx+B1−ψx\frac{x}{1-x-x^2}=\frac{A}{1-\varphi x}+\frac{B}{1-\psi x} for constants A,BA,B. Clearing denominators and matching the constant term and the coefficient of xx gives a linear system whose solution is A=15A=\frac{1}{\sqrt5}, B=−15B=-\frac{1}{\sqrt5}, so x1−x−x2=15(11−φx−11−ψx)\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\left(\frac{1}{1-\varphi x}-\frac{1}{1-\psi x}\right).

Expand each term as a geometric series: 11−φx=∑n≥0φnxn\frac{1}{1-\varphi x}=\sum_{n\ge0}\varphi^nx^n and 11−ψx=∑n≥0ψnxn\frac{1}{1-\psi x}=\sum_{n\ge0}\psi^nx^n.

Reading off the coefficient of xnx^n on both sides gives Fn=15(φn−ψn)F_n=\frac{1}{\sqrt{5}}\left(\varphi^n-\psi^n\right), known as Binet's formula.

UndergraduateReal-world applications and worked examples

Generating functions are a working tool, not just a theoretical curiosity: computer scientists use them to find exact and asymptotic running times of recursive algorithms, physicists use closely related partition functions in statistical mechanics, and probabilists encode entire probability distributions as generating functions to compute moments by differentiating instead of summing.

Example: Change-making with two coin types

In how many ways can an amount of 44 be made using an unlimited supply of 11-unit and 22-unit coins, if the order of the coins does not matter?

Solution

Each way to make an amount is a choice of how many 11-coins and how many 22-coins to use, so the generating function is the product of the OGF for 11-coins, 11−x\frac{1}{1-x}, and the OGF for 22-coins, 11−x2\frac{1}{1-x^2}: the combined generating function is 1(1−x)(1−x2)\frac{1}{(1-x)(1-x^2)}.

Expand the two factors as geometric series and multiply: (∑i≥0xi)(∑j≥0x2j)\left(\sum_{i\ge0}x^i\right)\left(\sum_{j\ge0}x^{2j}\right). The coefficient of x4x^4 counts pairs (i,j)(i,j) with i+2j=4i+2j=4 and i,j≥0i,j\ge0.

The valid pairs are (4,0)(4,0), (2,1)(2,1), (0,2)(0,2), so there are 33 ways: four 11-coins; two 11-coins and one 22-coin; two 22-coins.

Example: Binet's formula from the generating function

Use the closed form F(x)=x1−x−x2F(x)=\frac{x}{1-x-x^2} and Binet's formula Fn=15(φn−ψn)F_n=\frac{1}{\sqrt{5}}\left(\varphi^n-\psi^n\right) to compute F5F_5 directly, without listing F0,…,F4F_0,\dots,F_4 one by one.

Solution

By the theorem above, φ=1+52\varphi=\frac{1+\sqrt{5}}{2} and ψ=1−52\psi=\frac{1-\sqrt{5}}{2} are the roots feeding Binet's formula Fn=15(φn−ψn)F_n=\frac{1}{\sqrt{5}}\left(\varphi^n-\psi^n\right).

Numerically, φ≈1.618\varphi\approx1.618 and ψ≈−0.618\psi\approx-0.618, so φ5≈11.09\varphi^5\approx11.09 and ψ5≈−0.09\psi^5\approx-0.09.

Then F5=15(φ5−ψ5)≈15(11.09−(−0.09))=11.185≈5F_5=\frac{1}{\sqrt5}(\varphi^5-\psi^5)\approx\frac{1}{\sqrt5}(11.09-(-0.09))=\frac{11.18}{\sqrt5}\approx5.

Indeed F5=5F_5=5, matching the value obtained by iterating the recurrence 0,1,1,2,3,50,1,1,2,3,5 — but Binet's formula lets us jump directly to any single FnF_n without computing the ones before it.

What is the ordinary generating function for the constant sequence an=1a_n=1 for every n≥0n\ge0?

What is the coefficient of x3x^3 in 1(1−x)2\frac{1}{(1-x)^2}?

Which of the following problems is most naturally solved using generating functions?

The generating function of the Fibonacci sequence F(x)=∑FnxnF(x)=\sum F_nx^n satisfies which closed form?

References

  1. Herbert S. Wilf (1994). generatingfunctionology
  2. Philippe Flajolet, Robert Sedgewick (2009). Analytic Combinatorics