Generating Functions
A generating function packs an infinite sequence into a single power series:
The variable 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 . We write for the coefficient of .
Basic generating functions
| Sequence | Generating function |
|---|---|
Operations: adding sequences adds functions; multiplying by shifts right; differentiating multiplies by (and shifts left).
Counting by multiplying
The key fact: if counts ways to pick from one source by size and from another, then counts ways to pick from both, because
(the convolution: from the first, from the second).
Example: making change. In how many ways can you pay cents with 1-, 5- and 10-cent coins? Each coin type contributes a factor :
and the answer is . Multiplying truncated polynomials is a few lines of code, and it is exactly the dynamic-programming algorithm for the coin problem:
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:
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 with , , . Multiply the recurrence by and sum over :
Factor with and , and split into partial fractions:
using the table entry for . This is Binet's formula.
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 ? What is ?
Answer
; .
Solution
, a geometric series.
Exercise 2: Snack bags
A snack bag holds 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
; 6 bags of 5 items.
Solution
Cookies: ; crackers: ; apple: . Since , the product simplifies to . So there are bags of items, and 6 bags of 5. A direct count agrees (choose the crackers and the apple ; cookies fill the rest):
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 outcomes of three dice sum to 10.
Answer
Solution
One die is ; three dice are its cube, and we want .
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 ?
Multiplying two generating functions corresponds to which operation on their sequences?
- Adding them term by term
- Convolution
- Shifting
- Differentiation
What is the generating function for the number of ways to pay cents with only 2-cent coins?