Discrete Mathematics and Combinatorics

Generating Functions as a Tool for Counting

Pack a whole sequence into one power series, then let ordinary algebra count dice totals, coin change, baskets and recurrences.

Key takeaways

  • In an ordinary generating function, the coefficient of xⁿ counts the objects of size n.
  • Combining independent choices whose sizes add corresponds to multiplying their generating functions.
  • The series 1/(1 − x) = 1 + x + x² + ... is the basic building block, and many others come from it.
  • A linear recurrence turns into an algebraic equation for the series, which partial fractions then unpack into a formula.

A generating function stores a sequence a₀, a₁, a₂, ... as the coefficients of a power series, A(x) = a₀ + a₁x + a₂x² + .... The payoff is that operations on counting problems turn into operations on series: independent choices become multiplication, and recurrences become equations you can solve.

The x is a bookkeeping device. Its exponent records a size, and the coefficient in front records how many objects have that size.

Why multiplication counts

Roll two ordinary dice. Represent one die by D(x) = x + x² + x³ + x⁴ + x⁵ + x⁶, one term for each face. In the product D(x)·D(x), a term xⁱ from the first and xʲ from the second combine into xⁱ⁺ʲ. So the coefficient of xⁿ counts the ordered rolls with total n.

D(x)² = x² + 2x³ + 3x⁴ + 4x⁵ + 5x⁶ + 6x⁷
        + 5x⁸ + 4x⁹ + 3x¹⁰ + 2x¹¹ + x¹²

The coefficient of x⁷ is 6, so 6 of the 36 rolls total seven.

A curiosity falls out of factoring this polynomial. Dice marked 1, 2, 2, 3, 3, 4 and 1, 3, 4, 5, 6, 8 give exactly the same distribution of totals as a standard pair. They are known as Sicherman dice.

The basic series

The geometric series is the engine of the subject:

1/(1 − x)   = 1 + x + x² + x³ + ...
1/(1 − cx)  = 1 + cx + c²x² + c³x³ + ...
1/(1 − x²)  = 1 + x² + x⁴ + ...
1/(1 − x)ᵏ  = sum of C(n + k − 1, k − 1) xⁿ

In calculus these require |x| to be small. In counting you can often treat them as formal power series, symbolic objects with agreed rules of addition and multiplication, so that no question of convergence arises. The analytic side is covered in geometric series and power series.

The last line is stars and bars: multiplying k copies of 1/(1 − x) shares n units among k boxes.

Three dice without listing 216 rolls

In how many ordered ways can three dice total 10? We need the coefficient of x¹⁰ in D(x)³. Each die factors as x(1 − x⁶)/(1 − x), so

D(x)³ = x³ × (1 − x⁶)³ × 1/(1 − x)³

The x³ in front means we want the coefficient of x⁷ in the rest. Expand (1 − x⁶)³ as 1 − 3x⁶ + 3x¹² − x¹⁸. Only the first two terms can contribute to x⁷. The coefficient of xᵐ in 1/(1 − x)³ is C(m + 2, 2).

from 1:       C(9, 2) = 36                 (m = 7)
from −3x⁶:   −3 × C(3, 2) = −9             (m = 1)
total         27

So 27 of the 216 rolls total 10, a probability of 1/8. The subtraction is inclusion-exclusion carried out automatically by the algebra: it removes the "dice" that would have shown more than six.

A fruit basket with awkward rules

How many baskets of n pieces of fruit can you fill if apples come in even numbers, bananas in multiples of 5, there are at most 4 oranges and at most 1 pear? Write one factor for each fruit.

apples    1/(1 − x²)
bananas   1/(1 − x⁵)
oranges   1 + x + x² + x³ + x⁴ = (1 − x⁵)/(1 − x)
pears     1 + x

product = (1 + x) / ((1 − x²)(1 − x))
        = 1 / (1 − x)²
        = 1 + 2x + 3x² + 4x³ + ...

Everything cancels, and the number of baskets is n + 1. For n = 2 the three baskets are two apples, two oranges, or one orange and one pear.

From counting problem to answer
1 One factor per choice Exponents list the allowed sizes 2 Multiply Sizes add as exponents add 3 Simplify Use geometric series and fractions 4 Read a coefficient The number of xⁿ is the count One factor per choice Exponents list the allowed sizes 1 Multiply Sizes add as exponents add 2 Simplify Use geometric series and fractions 3 Read a coefficient The number of xⁿ is the count 4

Making change

In how many ways can you make 10 cents from coins of 1, 5 and 10 cents? Each coin type gets a factor listing the amounts it can contribute:

C(x) = 1/(1 − x) × 1/(1 − x⁵) × 1/(1 − x¹⁰)

The coefficient of x¹⁰ is 4: ten pennies, one nickel with five pennies, two nickels, or one dime.

For large amounts you can compute coefficients by adding one coin type at a time. Start with 1 way for each amount using pennies only. To allow nickels, update each amount n ≥ 5 in increasing order by adding the current number of ways for n − 5. Repeat for dimes. Programmers will recognize a standard dynamic programming routine, and it is exactly multiplication of these series.

Identities from two ways of expanding

If one function can be expanded in two ways, the coefficients must agree, and that proves an identity.

The series (1 + x)ⁿ has coefficients C(n, k), as in the binomial theorem. Now write (1 + x)ᵐ⁺ⁿ as (1 + x)ᵐ × (1 + x)ⁿ and compare the coefficient of xʳ:

C(m + n, r) = sum over k of C(m, k) × C(n, r − k)

This is Vandermonde's identity, obtained in one line. Another example: since 1/(1 − x) × 1/(1 + x) = 1/(1 − x²), the alternating sequence 1, −1, 1, ... combined with the constant sequence leaves 1, 0, 1, 0, ..., which you can confirm term by term.

Solving a recurrence

Take aₙ = 5aₙ₋₁ − 6aₙ₋₂ for n ≥ 2, with a₀ = 2 and a₁ = 5. Let A(x) be its series, and look at A(x) multiplied by 1 − 5x + 6x². For every n ≥ 2 the coefficient is aₙ − 5aₙ₋₁ + 6aₙ₋₂ = 0. Only the first two terms survive.

(1 − 5x + 6x²)·A(x) = a₀ + (a₁ − 5a₀)x = 2 − 5x

A(x) = (2 − 5x) / ((1 − 2x)(1 − 3x))
     = 1/(1 − 2x) + 1/(1 − 3x)

so aₙ = 2ⁿ + 3ⁿ

Splitting the fraction into simple pieces is called partial fraction decomposition. The denominator is the characteristic polynomial written backwards, which links this to the usual method for linear recurrences.

For Fibonacci numbers with F₀ = 0 and F₁ = 1, the same steps give F(x) = x/(1 − x − x²).

A first-order rule with an extra term works too. Suppose aₙ = 2aₙ₋₁ + 1 with a₀ = 0. Multiply the rule by xⁿ and add over all n ≥ 1. The left side is A(x) − a₀, and the right side is 2x·A(x) + x/(1 − x).

A(x) = x / ((1 − x)(1 − 2x))
     = 1/(1 − 2x) − 1/(1 − x)

so aₙ = 2ⁿ − 1

These are the move counts for the Tower of Hanoi.

Operations on a series

Several moves on the series have clear meanings.

Operation on A(x) Effect on the sequence
Multiply by x Shift one place later
Differentiate, then multiply by x aₙ becomes n·aₙ
Divide by 1 − x Running totals a₀ + ... + aₙ
Multiply by 1 − x Differences aₙ − aₙ₋₁

For example, starting with 1/(1 − x) and applying "differentiate, then multiply by x" gives x/(1 − x)² = 0 + x + 2x² + 3x³ + ....

Averages for free

In probability, let the coefficient of xⁿ be the chance that a random whole number equals n. For one fair die, G(x) = (x + x² + ... + x⁶)/6.

Setting x = 1 gives 1, because probabilities add to 1. Differentiating and then setting x = 1 gives the average:

G′(1) = (1 + 2 + 3 + 4 + 5 + 6)/6 = 3.5

For independent quantities, the function for their sum is the product of the individual functions. The product rule then shows that averages add. A close relative of this idea is described in moment generating functions.

Partitions of an integer

A partition of n is a way to write it as a sum of positive integers, ignoring order. Each part size k may be used any number of times, so it gets a factor 1/(1 − xᵏ). The generating function is the infinite product of these factors over all k.

If instead each size may be used at most once, the factors are 1 + xᵏ. Euler observed that this product equals the product of 1/(1 − xᵏ) over odd k only. In words, partitions into distinct parts and partitions into odd parts are equally numerous.

For 5, the distinct-part partitions are 5, 4 + 1 and 3 + 2. The odd-part ones are 5, 3 + 1 + 1 and 1 + 1 + 1 + 1 + 1. More on this subject is in integer partitions.

Exponential generating functions

When objects carry labels, it often works better to use aₙxⁿ/n!. This is the exponential generating function. Its product rule automatically accounts for choosing which labels go to which part.

For all permutations, aₙ = n! and the series is 1/(1 − x). For derangements, arrangements with nothing in its original place, it is e⁻ˣ/(1 − x). Words over a two-letter alphabet have aₙ = 2ⁿ and series e²ˣ, the product of one eˣ for each letter.

An equation for a branching structure

Some objects are built from smaller copies of themselves. A binary tree is either empty or a root with a left binary tree and a right binary tree. Let T(x) count binary trees by number of nodes.

The description translates word for word: empty contributes 1, and "a root and two trees" contributes x·T(x)·T(x).

T(x) = 1 + x·T(x)²
T(x) = (1 − √(1 − 4x)) / (2x)
     = 1 + x + 2x² + 5x³ + 14x⁴ + ...

Solving the quadratic and choosing the root that stays finite at x = 0 gives the Catalan numbers. No recurrence had to be solved by guesswork.

Limits and good habits

A generating function does not guarantee a neat formula. Sometimes the best you can extract is a fast way to compute coefficients, or an estimate of how fast they grow, found by studying where the function blows up as a function of a complex variable.

  • Check the first few coefficients against hand counts.
  • Decide whether order matters before choosing the factors.
  • Do not substitute a number for x without checking that the series converges there.

Herbert Wilf, who wrote a well-liked textbook on the subject, compared a generating function to a clothesline on which a sequence is hung for display. The image fits: once everything hangs in one place, patterns become visible.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic