TExcelTAD
Home › Discrete Mathematics › Generating Functions

Generating Functions

A generating function packs an infinite sequence a0,a1,a2,…a_0, a_1, a_2, \ldots into a single power series:

G(x)=a0+a1x+a2x2+⋯=∑n≥0anxnG(x) = a_0 + a_1 x + a_2 x^2 + \cdots = \sum_{n \ge 0} a_n x^n

The variable xx is just a placeholder; what matters is the coefficients. The payoff is that operations on sequences become algebra on functions: counting problems turn into multiplying polynomials, and recurrences turn into equations you can solve for GG. We write [xn] G(x)[x^n]\,G(x) for the coefficient of xnx^n.

Basic generating functions

Sequence Generating function
1,1,1,1,…1, 1, 1, 1, \ldots 11−x\frac{1}{1 - x}
1,c,c2,c3,…1, c, c^2, c^3, \ldots 11−cx\frac{1}{1 - cx}
1,0,1,0,…1, 0, 1, 0, \ldots 11−x2\frac{1}{1 - x^2}
0,1,2,3,…0, 1, 2, 3, \ldots x(1−x)2\frac{x}{(1 - x)^2}
(n0),(n1),…,(nn)\binom{n}{0}, \binom{n}{1}, \ldots, \binom{n}{n} (1+x)n(1 + x)^n
(k−1k−1),(kk−1),(k+1k−1),…\binom{k-1}{k-1}, \binom{k}{k-1}, \binom{k+1}{k-1}, \ldots 1(1−x)k\frac{1}{(1 - x)^k}

Operations: adding sequences adds functions; multiplying by xx shifts right; differentiating multiplies ana_n by nn (and shifts left).

Counting by multiplying

The key fact: if A(x)A(x) counts ways to pick from one source by size and B(x)B(x) from another, then A(x)B(x)A(x) B(x) counts ways to pick from both, because

[xn] A(x)B(x)=∑k=0nakbn−k[x^n]\,A(x)B(x) = \sum_{k=0}^{n} a_k b_{n-k}

(the convolution: kk from the first, n−kn - k from the second).

Example: making change. In how many ways can you pay nn cents with 1-, 5- and 10-cent coins? Each coin type contributes a factor 1+xc+x2c+⋯=11−xc1 + x^c + x^{2c} + \cdots = \frac{1}{1 - x^c}:

G(x)=1(1−x)(1−x5)(1−x10)G(x) = \frac{1}{(1 - x)(1 - x^5)(1 - x^{10})}

and the answer is [xn] G(x)[x^n]\,G(x). Multiplying truncated polynomials is a few lines of code, and it is exactly the dynamic-programming algorithm for the coin problem:

python · try it yourself
def change_ways(n, coins):
    ways = [1] + [0] * n                 # coefficients of the product so far
    for c in coins:                      # multiply by 1/(1 - x^c)
        for total in range(c, n + 1):
            ways[total] += ways[total - c]
    return ways

w = change_ways(100, [1, 5, 10])
print("ways to pay 0..15:", w[:16])
print("ways to pay 100 cents:", w[100])
print("with quarters too:", change_ways(100, [1, 5, 10, 25])[100])
ways to pay 0..15: [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6]
ways to pay 100 cents: 121
with quarters too: 242

SymPy can expand the series directly, as a check:

python · try it yourself
from sympy import symbols, series

x = symbols("x")
G = 1 / ((1 - x) * (1 - x ** 5) * (1 - x ** 10))
print(series(G, x, 0, 16))
1 + x + x**2 + x**3 + x**4 + 2*x**5 + 2*x**6 + 2*x**7 + 2*x**8 + 2*x**9 + 4*x**10 + 4*x**11 + 4*x**12 + 4*x**13 + 4*x**14 + 6*x**15 + O(x**16)

Solving a recurrence: Fibonacci

Let F(x)=∑FnxnF(x) = \sum F_n x^n with F0=0F_0 = 0, F1=1F_1 = 1, Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}. Multiply the recurrence by xnx^n and sum over n≥2n \ge 2:

F(x)−x=xF(x)+x2F(x)⇒F(x)=x1−x−x2F(x) - x = x F(x) + x^2 F(x) \quad\Rightarrow\quad F(x) = \frac{x}{1 - x - x^2}

Factor 1−x−x2=(1−φx)(1−φ^x)1 - x - x^2 = (1 - \varphi x)(1 - \hat\varphi x) with φ=1+52\varphi = \frac{1 + \sqrt5}{2} and φ^=1−52\hat\varphi = \frac{1 - \sqrt5}{2}, and split into partial fractions:

F(x)=15(11−φx−11−φ^x)⇒Fn=φn−φ^n5F(x) = \frac{1}{\sqrt5}\left(\frac{1}{1 - \varphi x} - \frac{1}{1 - \hat\varphi x}\right) \quad\Rightarrow\quad F_n = \frac{\varphi^n - \hat\varphi^n}{\sqrt5}

using the table entry for 11−cx\frac{1}{1 - cx}. This is Binet's formula.

python · try it yourself
from sympy import symbols, series, apart, sqrt, simplify

x = symbols("x")
F = x / (1 - x - x ** 2)
print(series(F, x, 0, 12))
phi, psi = (1 + sqrt(5)) / 2, (1 - sqrt(5)) / 2
print([simplify((phi ** n - psi ** n) / sqrt(5)) for n in range(12)])
x + x**2 + 2*x**3 + 3*x**4 + 5*x**5 + 8*x**6 + 13*x**7 + 21*x**8 + 34*x**9 + 55*x**10 + 89*x**11 + O(x**12)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

In software

Generating functions are the theory behind counting DP tables (the coin-change and partition algorithms are polynomial multiplication), behind probability generating functions used to analyse random algorithms (Probability Generating Functions), and behind fast polynomial multiplication with the FFT, which turns convolutions into pointwise products.

Exercises

Exercise 1: Read a coefficient

What sequence has generating function 11−3x\frac{1}{1 - 3x}? What is [x4][x^4]?

Answer

an=3na_n = 3^n; [x4]=81[x^4] = 81.

Solution

11−3x=∑(3x)n=∑3nxn\frac{1}{1 - 3x} = \sum (3x)^n = \sum 3^n x^n, a geometric series.

Exercise 2: Snack bags

A snack bag holds nn items: any number of cookies, an even number of crackers (they come in pairs), and at most 1 apple. Write the generating function and find the number of bags with 5 items.

Answer

G(x)=1+x(1−x)(1−x2)=1(1−x)2G(x) = \frac{1 + x}{(1 - x)(1 - x^2)} = \frac{1}{(1 - x)^2}; 6 bags of 5 items.

Solution

Cookies: 11−x\frac{1}{1 - x}; crackers: 1+x2+x4+⋯=11−x21 + x^2 + x^4 + \cdots = \frac{1}{1 - x^2}; apple: 1+x1 + x. Since 1−x2=(1−x)(1+x)1 - x^2 = (1 - x)(1 + x), the product simplifies to 1(1−x)2=∑(n+1)xn\frac{1}{(1 - x)^2} = \sum (n + 1) x^n. So there are n+1n + 1 bags of nn items, and 6 bags of 5. A direct count agrees (choose the crackers c∈{0,2,4}c \in \{0, 2, 4\} and the apple a∈{0,1}a \in \{0, 1\}; cookies fill the rest):

python · try it yourself
count = sum(1 for b in range(0, 6, 2) for o in range(2) if 5 - b - o >= 0)
print(count)
6

Exercise 3: Dice sums (code)

Use polynomial multiplication to find how many of the 636^3 outcomes of three dice sum to 10.

Answer
Solution

One die is x+x2+⋯+x6x + x^2 + \cdots + x^6; three dice are its cube, and we want [x10][x^{10}].

python · try it yourself
from sympy import symbols, Poly

x = symbols("x")
die = sum(x ** k for k in range(1, 7))
print(Poly(die ** 3, x).coeff_monomial(x ** 10))
27

Test yourself

Which sequence has generating function 11−x\frac{1}{1 - x}?

  1. 1,2,3,…1, 2, 3, \ldots
  2. 1,1,1,…1, 1, 1, \ldots
  3. 1,0,1,0,…1, 0, 1, 0, \ldots
  4. 0,1,0,1,…0, 1, 0, 1, \ldots

Multiplying two generating functions corresponds to which operation on their sequences?

  1. Adding them term by term
  2. Convolution
  3. Shifting
  4. Differentiation

What is the generating function for the number of ways to pay nn cents with only 2-cent coins?

  1. 11−2x\frac{1}{1 - 2x}
  2. 11−x2\frac{1}{1 - x^2}
  3. (1+x)2(1 + x)^2
  4. 21−x\frac{2}{1 - x}