01 / 21

Generating
Functions

Encode entire sequences into a single algebraic expression, then use algebra to solve counting and recurrence problems.

Discrete Math

What Is a Generating Function?

A generating function is a formal power series that encodes a sequence \( a_0, a_1, a_2, \ldots \) as coefficients of powers of \( x \):

Think of \( x \) as a placeholder: we don't plug in numbers. We just use the variable to "tag" each term with its position in the sequence.

The Big Idea: Sequences as Algebra

Sequence
\( 1, 1, 1, 1, \ldots \)
encode Generating Function
\(\frac{1}{1-x}\)
solve ✓

Instead of working with terms one-by-one, we pack the whole sequence into a closed-form function, manipulate it with algebra, then read off answers from the coefficients.

Why Do Generating Functions Matter?

Counting
Solve combinatorial problems: how many ways to make change, distribute objects, or partition integers.
Recurrences
Find closed-form solutions to recurrences like the Fibonacci sequence without guessing.
Proofs
Prove identities involving binomial coefficients and sums that are hard to show directly.

The Fundamental Closed Form

This is the building block for nearly every generating function. By substituting, scaling, and differentiating this identity, we can derive generating functions for a huge variety of sequences.

\( 1, r, r^2, r^3, \ldots \)
\( \displaystyle\frac{1}{1 - rx} \)
\( 1, -1, 1, -1, \ldots \)
\( \displaystyle\frac{1}{1 + x} \)
\( 1, 2, 3, 4, \ldots \)
\( \displaystyle\frac{1}{(1 - x)^2} \)

Key Generating Function Pairs

Sequence \( \{a_n\} \) OGF \( A(x) \)
\( 1, 1, 1, 1, \ldots \) \( \dfrac{1}{1 - x} \)
\( 1, c, c^2, c^3, \ldots \) \( \dfrac{1}{1 - cx} \)
\( 1, 2, 3, 4, \ldots \) \( \dfrac{1}{(1 - x)^2} \)
\( a_n = \binom{n+k-1}{k-1} \) \( \dfrac{1}{(1 - x)^k} \)

OGF vs. EGF: Key Distinction

Feature Ordinary (OGF) Exponential (EGF)
General term \( a_n x^n \) \( a_n \dfrac{x^n}{n!} \)
Product means Convolution (unordered) Labeled merge (ordered)
Best for Combinations, selections Permutations, labelings
Example Coin change, partitions Derangements, arrangements

Operations on Generating Functions

1
Scaling: Multiplying by a constant \( c \) scales every coefficient: \( c \cdot A(x) \leftrightarrow \{c \cdot a_n\} \).
2
Addition: Adding series adds coefficients term-by-term: \( A(x) + B(x) \leftrightarrow \{a_n + b_n\} \).
3
Right-shift: Multiplying by \( x^k \) shifts the sequence right by \( k \) positions (prepends \( k \) zeros).
4
Differentiation: \( A'(x) \leftrightarrow \{(n+1)\,a_{n+1}\} \): multiplies each term by its index and shifts left.
5
Product (Convolution): \( A(x)\cdot B(x) \) yields the sequence whose \( n \)-th term is \( \sum_{k=0}^n a_k b_{n-k} \).

How to Apply Generating Functions

1
Identify the sequence. Write out the first few terms of the sequence or recurrence you want to analyze.
2
Set up the series. Define \( A(x) = \sum a_n x^n \) and express any recurrence or constraint as an equation in \( A(x) \).
3
Solve for \( A(x) \). Use algebra to isolate \( A(x) \) as a closed-form rational expression.
4
Expand back. Use partial fractions or known series to read off the coefficient \( [x^n]\,A(x) = a_n \).

Example 1: Constant Sequence

Problem: Find the generating function for the sequence \( 3, 3, 3, 3, \ldots \).

1
Identify: Every term is \( a_n = 3 \).
2
Set up: \( A(x) = \sum_{n=0}^{\infty} 3x^n = 3 \sum_{n=0}^{\infty} x^n \).
3
Solve: Apply the geometric series identity.

Example 2: Powers of 2

Problem: Find the generating function for \( 1, 2, 4, 8, 16, \ldots \) (i.e., \( a_n = 2^n \)).

1
Set up: \( A(x) = \sum_{n=0}^{\infty} 2^n x^n = \sum_{n=0}^{\infty} (2x)^n \).
2
Recognize: This is geometric with ratio \( r = 2x \).
3
Solve: \( A(x) = \dfrac{1}{1 - 2x} \).

Example 3: Fibonacci Numbers

Problem: Derive the GF for the Fibonacci sequence \( F_0 = 0,\; F_1 = 1,\; F_n = F_{n-1} + F_{n-2} \).

1
Define: Let \( F(x) = \sum_{n=0}^{\infty} F_n x^n \).
2
Translate recurrence: \( F(x) = x F(x) + x^2 F(x) + x \) (from shifting both sides).
3
Solve: \( F(x)(1 - x - x^2) = x \), so \( F(x) = \dfrac{x}{1 - x - x^2} \).

Visualizing Convolution

A(x): a₀, a₁, a₂ a₀ a₁ a₂ × B(x): b₀, b₁, b₂ b₀ b₁ b₂ = C(x) = A·B
\( c_n = \sum_{k=0}^{n} a_k \, b_{n-k} \)
e.g., \( c_2 = a_0 b_2 + a_1 b_1 + a_2 b_0 \)

When you multiply two generating functions, the coefficient of \( x^n \) in the product is the convolution of the original sequences. This is the algebraic heart of counting arguments.

Counting with Products

Problem: How many ways can you select \( n \) items from two types, where you pick between \( 0 \) and \( 3 \) of type A and any number of type B?

Type A
\( 0 \) to \( 3 \) items: GF is \( 1 + x + x^2 + x^3 \)
Type B
\( 0 \) or more items: GF is \( \frac{1}{1-x} \)

Common Mistakes

✕ Plugging in values for \( x \)
\( x \) is a formal variable: a bookkeeping device. We never substitute \( x = 2 \) to get a number. We extract coefficients.
✕ Forgetting initial conditions
When translating a recurrence, you must account for base cases (e.g., \( F_0 = 0, F_1 = 1 \)). Omitting them gives the wrong closed form.
✕ Confusing OGF and EGF
Using an OGF product when the problem involves labeled arrangements gives wrong answers. Check: unordered → OGF, ordered/labeled → EGF.

Summary: Operation Cheat Sheet

Sequence Operation GF Operation
Scale by \( c \) \( c \cdot A(x) \)
Add sequences \( A(x) + B(x) \)
Right-shift by \( k \) \( x^k \cdot A(x) \)
Multiply terms by \( n \) \( x \cdot A'(x) \)
Convolution \( A(x) \cdot B(x) \)
Partial sums \( \sum_{k=0}^n a_k \) \( \dfrac{A(x)}{1-x} \)

Question 1 of 5

What is the ordinary generating function for the sequence \( 1, 1, 1, 1, \ldots \)?

Question 2 of 5

The generating function \( \dfrac{1}{1-3x} \) encodes which sequence?

Question 3 of 5

If \( A(x) \) is the GF for \( \{a_n\} \), what does multiplying \( A(x) \) by \( x^2 \) do to the sequence?

Question 4 of 5

What is the generating function for the Fibonacci recurrence \( F_n = F_{n-1} + F_{n-2} \) with \( F_0=0, F_1=1 \)?

Question 5 of 5

When we multiply two ordinary generating functions \( A(x) \cdot B(x) \), the coefficient of \( x^n \) in the product equals: