Skip to main content

Generating Functions

Generating functions turn sequences into algebraic objects. Instead of studying a0,a1,a2,a_0,a_1,a_2,\dots directly, package the sequence as coefficients of a power series. Algebraic operations on the series then correspond to counting constructions, recurrence solving, and convolution.

The main idea is that the exponent tracks size while the coefficient tracks how many objects have that size. Once a counting problem is translated into a generating function, multiplication, coefficient extraction, and algebraic simplification do the bookkeeping that would otherwise require many cases.

Definitions

The ordinary generating function for a sequence {an}n0\{a_n\}_{n\ge0} is

G(x)=n=0anxn.G(x)=\sum_{n=0}^{\infty}a_nx^n.

In many discrete mathematics applications this is treated as a formal power series, so convergence is not the main issue. The coefficient of xnx^n records ana_n. We often write [xn]G(x)[x^n]G(x) for the coefficient of xnx^n in G(x)G(x).

Basic generating functions:

11x=1+x+x2+x3+,1(1x)2=n=0(n+1)xn,11ax=n=0anxn,(1+x)n=r=0n(nr)xr.\begin{aligned} \frac{1}{1-x} &= 1+x+x^2+x^3+\cdots,\\ \frac{1}{(1-x)^2} &= \sum_{n=0}^{\infty}(n+1)x^n,\\ \frac{1}{1-ax} &= \sum_{n=0}^{\infty}a^nx^n,\\ (1+x)^n &= \sum_{r=0}^{n}\binom nr x^r. \end{aligned}

Multiplying generating functions performs convolution:

(n0anxn)(n0bnxn)=n0(k=0nakbnk)xn.\left(\sum_{n\ge0}a_nx^n\right) \left(\sum_{n\ge0}b_nx^n\right) =\sum_{n\ge0}\left(\sum_{k=0}^{n}a_kb_{n-k}\right)x^n.

This is why products of generating functions model independent contributions whose sizes add.

Key results

Generating functions solve linear recurrences. For the Fibonacci sequence F0=0F_0=0, F1=1F_1=1, and Fn=Fn1+Fn2F_n=F_{n-1}+F_{n-2}, let

F(x)=n=0Fnxn.F(x)=\sum_{n=0}^{\infty}F_nx^n.

Multiply the recurrence by xnx^n and sum for n2n\ge2:

n2Fnxn=n2Fn1xn+n2Fn2xn.\sum_{n\ge2}F_nx^n =\sum_{n\ge2}F_{n-1}x^n+\sum_{n\ge2}F_{n-2}x^n.

Using F0=0F_0=0 and F1=1F_1=1:

F(x)x=xF(x)+x2F(x).F(x)-x=xF(x)+x^2F(x).

Therefore

F(x)=x1xx2.F(x)=\frac{x}{1-x-x^2}.

This rational function contains the whole Fibonacci sequence in its coefficients.

Generating functions also prove identities. The coefficient of xrx^r in (1+x)m(1+x)n(1+x)^m(1+x)^n is

k=0r(mk)(nrk),\sum_{k=0}^{r}\binom{m}{k}\binom{n}{r-k},

while (1+x)m(1+x)n=(1+x)m+n(1+x)^m(1+x)^n=(1+x)^{m+n} has coefficient (m+nr)\binom{m+n}{r}. Hence Vandermonde's identity follows:

k=0r(mk)(nrk)=(m+nr).\sum_{k=0}^{r}\binom{m}{k}\binom{n}{r-k}=\binom{m+n}{r}.

For restricted integer solutions, each variable contributes a factor whose terms represent its allowed values. If xix_i can be 0,1,20,1,2, its factor is 1+x+x21+x+x^2. If it can be any nonnegative integer, its factor is 1/(1x)1/(1-x).

Visual

Constraint on one variableGenerating factorMeaning
xi0x_i\ge01+x+x2+=11x1+x+x^2+\cdots=\frac1{1-x}any nonnegative contribution
0xim0\le x_i\le m1+x++xm1+x+\cdots+x^mbounded contribution
xi{0,1}x_i\in\{0,1\}1+x1+xchoose or do not choose
xix_i even and nonnegative1+x2+x4+1+x^2+x^4+\cdotseven contribution
one coin of value dd or none1+xd1+x^dat most one coin
unlimited coins of value dd11xd\frac1{1-x^d}any number of that coin

Worked example 1: Count restricted solutions

Problem. Count nonnegative integer solutions to

x1+x2+x3=5x_1+x_2+x_3=5

with 0xi20\le x_i\le2 for each ii.

Method.

  1. Each variable can contribute 00, 11, or 22, so each variable has generating factor
1+x+x2.1+x+x^2.
  1. For three variables, use
(1+x+x2)3.(1+x+x^2)^3.
  1. We need the coefficient of x5x^5.
  2. Expand in a controlled way:
(1+x+x2)2=1+2x+3x2+2x3+x4.(1+x+x^2)^2=1+2x+3x^2+2x^3+x^4.
  1. Multiply by 1+x+x21+x+x^2 and track only terms contributing to x5x^5:
[x5](1+2x+3x2+2x3+x4)(1+x+x2).[x^5](1+2x+3x^2+2x^3+x^4)(1+x+x^2).
  1. Contributions to x5x^5 are:
[x3][x2]+[x4][x1]=21+11=3.[x^3]\cdot[x^2] + [x^4]\cdot[x^1] = 2\cdot1+1\cdot1=3.

Checked answer. There are 33 solutions: (1,2,2)(1,2,2), (2,1,2)(2,1,2), and (2,2,1)(2,2,1).

Worked example 2: Solve a recurrence with a generating function

Problem. Solve a0=1a_0=1 and an=2an1+1a_n=2a_{n-1}+1 for n1n\ge1.

Method.

  1. Let
A(x)=n0anxn.A(x)=\sum_{n\ge0}a_nx^n.
  1. Multiply the recurrence by xnx^n and sum for n1n\ge1:
n1anxn=2n1an1xn+n1xn.\sum_{n\ge1}a_nx^n=2\sum_{n\ge1}a_{n-1}x^n+\sum_{n\ge1}x^n.
  1. Rewrite each sum:
A(x)a0=2xA(x)+x1x.A(x)-a_0=2xA(x)+\frac{x}{1-x}.
  1. Since a0=1a_0=1,
A(x)1=2xA(x)+x1x.A(x)-1=2xA(x)+\frac{x}{1-x}.
  1. Solve for A(x)A(x):
A(x)(12x)=1+x1x=11x.\begin{aligned} A(x)(1-2x) &=1+\frac{x}{1-x}\\ &=\frac{1}{1-x}. \end{aligned}

So

A(x)=1(1x)(12x).A(x)=\frac{1}{(1-x)(1-2x)}.
  1. Use partial fractions:
1(1x)(12x)=11x+212x.\frac{1}{(1-x)(1-2x)}=\frac{-1}{1-x}+\frac{2}{1-2x}.
  1. Extract coefficients:
an=1+22n=2n+11.a_n=-1+2\cdot2^n=2^{n+1}-1.

Checked answer. an=2n+11a_n=2^{n+1}-1. Check: a0=1a_0=1, and 2an1+1=2(2n1)+1=2n+112a_{n-1}+1=2(2^n-1)+1=2^{n+1}-1.

Code

from collections import Counter

def multiply(p, q):
out = Counter()
for i, ai in p.items():
for j, bj in q.items():
out[i + j] += ai * bj
return out

def power(poly, k):
result = Counter({0: 1})
for _ in range(k):
result = multiply(result, poly)
return result

restricted = power(Counter({0: 1, 1: 1, 2: 1}), 3)
print(dict(sorted(restricted.items())))
print("coefficient of x^5:", restricted[5])

The dictionary key is the exponent, and the value is the coefficient. Multiplication implements convolution.

Common pitfalls

  • Forgetting that ordinary generating functions usually track size by exponent and count by coefficient.
  • Multiplying factors when the modeled choices are not independent components.
  • Extracting the wrong coefficient after a shift in indices.
  • Treating formal power series as analytic functions without checking whether convergence matters.
  • Using an unrestricted factor 1/(1x)1/(1-x) when the problem has upper bounds.
  • Solving the algebra but not checking the resulting sequence against the initial conditions.

The most common modeling error is choosing the right-looking algebra before deciding what one unit of size means. In a coin problem, the exponent usually tracks total value, so a dime contributes x10x^{10}, not xx. In a word problem, the exponent may track length. In a distribution problem, the exponent may track total score. State this meaning explicitly before multiplying factors; otherwise the coefficient you extract may answer a different question.

Another useful check is to compute the first few coefficients directly. If a generating function claims to count restricted solutions to x1+x2+x3=nx_1+x_2+x_3=n with each variable at most 22, then the coefficients should be symmetric from degree 00 through degree 66: 1,3,6,7,6,3,11,3,6,7,6,3,1. The symmetry comes from replacing each value aia_i by 2ai2-a_i. If the algebra gives a coefficient sequence that violates an obvious structural check, the factor or coefficient target is probably wrong.

When solving recurrences, index shifts deserve special attention. If the recurrence starts at n2n\ge2, then n2anxn\sum_{n\ge2}a_nx^n is usually A(x)a0a1xA(x)-a_0-a_1x, not just A(x)A(x). Similarly, n2an1xn=x(A(x)a0)\sum_{n\ge2}a_{n-1}x^n=x(A(x)-a_0). Writing two or three terms under the summation often prevents an off-by-one error that would change the numerator of the final rational function.

Generating functions are formal objects in this setting, but algebraic manipulation still needs valid formal rules. Multiplication is safe when each coefficient of the product receives only finitely many contributions. Ordinary products used for finite constraints and standard geometric series satisfy this. Infinite products require more care and should be used only when the coefficient of each xnx^n is determined by finitely many choices.

Finally, remember that a generating function is not the answer by itself unless the problem asks for one. Most counting questions ask for a coefficient, a closed form, or a recurrence. After obtaining G(x)G(x), finish the task: extract [xn]G(x)[x^n]G(x), compute the requested coefficient, or explain how the expression encodes the sequence.

For coefficient extraction, keep a small library of standard forms and learn to reshape expressions into them. For instance, 113x\frac{1}{1-3x} immediately gives coefficient 3n3^n, while x413x\frac{x^4}{1-3x} gives coefficient 3n43^{n-4} for n4n\ge4 and 00 for smaller nn. The numerator shift is not cosmetic; it changes which terms exist. When a rational function contains several factors, partial fractions often turn one difficult coefficient extraction into several standard geometric extractions.

Generating functions can also encode choices with signs or weights. If an object contributes a weight ww, use wxkw x^k rather than just xkx^k. This produces weighted counts, such as total value or probability mass, not just the number of objects. The same algebra works, but the meaning of coefficients changes. State whether coefficients are counts, weighted sums, or probabilities.

As a final verification, translate a coefficient back into a combinatorial statement. If [x5](1+x+x2)3=3[x^5](1+x+x^2)^3=3, explain that the three contributions correspond to placing one variable at 11 and the other two at 22. This reverse translation is the best evidence that the algebra and the original counting problem still match.

A practical workflow is: define the coefficient meaning, build one factor per independent component, multiply or simplify, extract the coefficient, and then interpret the coefficient back in the original language. Skipping the final interpretation is risky because the algebra may have counted a related but different object, such as ordered selections instead of unordered selections.

For difficult coefficient extractions, compute the first few terms before seeking a closed form. Expanding through degree 55 or 66 often reveals whether the generating function has the right initial behavior. These low-degree coefficients also provide test data for recurrence solutions derived from rational generating functions.

If a generating function has a product of factors, annotate each factor in the margin. For example, in 1(1x)(1x5)(1x10)\frac{1}{(1-x)(1-x^5)(1-x^{10})}, the factors represent pennies, nickels, and dimes. This annotation keeps the algebra tied to the counting model and makes it easier to modify the expression when supplies become limited.

When the same problem can be solved by stars and bars, a recurrence, and a generating function, compare the answers. Agreement across methods is a strong validation, and disagreement usually reveals whether order, repetition, or a boundary constraint was modeled differently.

This comparison also helps decide which method gives the clearest explanation for the audience.

Prefer the method that exposes the constraint most directly.

Connections