Welcome back. In our last lesson, we mastered the characteristic equation method, a powerful algebraic technique for finding closed-form solutions to linear homogeneous recurrence relations. While effective, this method has its limits—it can't handle non-homogeneous relations or more complex combinatorial structures.
Today, we will introduce a concept that transcends these limitations: ordinary generating functions (OGFs). This is a paradigm shift. Instead of solving for a single term , we will learn to encode an entire infinite sequence into a single, compact function. The main goal of this lesson is to learn how to construct these functions by translating the rules and constraints of a counting problem into the algebraic language of generating functions. This will serve as the foundation for a method that can solve a much broader class of problems, including those that were inaccessible to our previous tools.
The Core Idea: From Sequences to Functions
At its heart, a generating function is a simple but brilliant idea: use a polynomial or power series as a "container" for a sequence of numbers. The coefficients of the series are the terms of the sequence, and the powers of a variable (say, ) act as placeholders to keep them in order.
For a sequence , its ordinary generating function is the formal power series:
The term "formal" means we're not primarily concerned with whether this series converges to a number. We treat it as an algebraic object where the coefficients hold our combinatorial information.
This concept is explained beautifully in the following notes. The author uses an analogy of building words to show how multiplying polynomials naturally corresponds to combining objects and adding their properties (like length).
[PDF] Notes on generating functions
These notes by James Aspnes provide a wonderfully intuitive entry point into the "why" of generating functions.
Please read the first two sections, "A simple example" and "Why this works". Focus on how multiplying expressions like (2z + z^2) and (5z^2) automatically keeps track of the number of combinations and their total "weight" (represented by the exponent).
The Fundamental Building Block
The most important generating function, from which many others are derived, is the one for the constant sequence . This corresponds to the geometric series:
This equality is the cornerstone of generating functions. We will call it our fundamental identity. By manipulating this single identity, we can construct generating functions for a vast number of other sequences.
The following video provides a clear, step-by-step guide to this fundamental identity and several essential manipulations.
Discrete Math II - 8.4.2 Readiness for Generating Functions - Power Series and Fundamental Identity
This video by Kimberly Brehm systematically introduces the key techniques for building basic generating functions from the ground up.
First, watch the introduction to understand the formal definition of a generating function. Next, see how the fundamental identity is derived using long division. You don't need to do this yourself, but it's good to see where it comes from. Then, pay close attention to the following manipulations: Multiplying by x^k to create sequences that start with zeros. Substituting -x to create alternating sequences. Substituting x^k to create sequences with periodic zeros. Substituting cx to generate geometric progressions.
(Note: The segment from 14:40-17:39 about derivatives is also a valid manipulation, but we will set it aside for now to focus on the more common algebraic constructions.)
Here is a summary of the key generating functions. Having these at your fingertips is incredibly useful.
[PDF] Lecture 18 — Generating functions - Holden Lee
This table from Holden Lee's lecture notes lists several common sequences and their corresponding closed-form generating functions. The first, third, and sixth entries are particularly fundamental.
Constructing Generating Functions for Combinatorial Problems
The true power of generating functions comes from translating combinatorial rules directly into algebra. The two most important rules are:
- Disjoint Union (OR): If you can choose an object from set or from a disjoint set , the generating function for the combined choices is .
- Cartesian Product (AND): If you construct an object by picking a part from set and a part from set , the generating function for the composite object is .
This second rule is the magic key. When you multiply the polynomials, the exponents add (), which corresponds to combining the "weights" of the components. The coefficients multiply, which corresponds to the product rule for counting.
Let's see this in action. The following video demonstrates how to take a counting problem with constraints and build its generating function piece by piece.
GENERATING FUNCTIONS - Discrete Mathematics
This video by TrevTutor excels at showing the direct application of generating functions to solve problems you might have previously tackled with inclusion-exclusion or stars and bars.
Watch the first six minutes of the video, covering three core examples: Integer solutions with bounds: Pay attention to how the restriction 0 <= X <= 6 translates into the finite polynomial 1 + x + ... + x^6. Jelly bean selection: See how different constraints (even, at least 14, less than 5) for each color become a distinct polynomial factor. Making change: This is a classic. Notice how the value of each coin determines the exponents in its corresponding factor (e.g., 1, x^5, x^10, ... for nickels).
Let's consolidate this with an example. Suppose we want to find the number of solutions to in non-negative integers, where:
- must be an even number.
- must be a multiple of 3.
- must be at least 1 and at most 4.
We construct a factor for each variable, where the exponents represent the possible values that variable can take.
- For (even): The possible values are 0, 2, 4, ... This gives the series , which is a geometric series with ratio . Its generating function is .
- For (multiple of 3): The possible values are 0, 3, 6, ... This gives the series , a geometric series with ratio . Its generating function is .
- For (1 4): The possible values are 1, 2, 3, 4. This gives the finite polynomial . We can factor this as and use the formula for a finite geometric sum: .
Since a solution requires a value for AND AND , we multiply their generating functions to get the total generating function :
The coefficient of in the full expansion of this gives the number of solutions for a specific sum . For today, our goal is simply to get to this point: constructing the function.
Practice Problems
Now it's your turn to build some generating functions. For each problem, your task is to write down the generating function whose coefficients would answer the question. You do not need to expand the function or find the final numerical answer.
-
Find the generating function for the number of ways to choose fruits from a basket containing apples, bananas, and oranges, such that you pick an even number of apples, a number of bananas that is a multiple of 3, and at most 2 oranges.
-
Find the generating function for the number of ways to distribute identical cookies to three distinct children (let's call them A, B, and C) such that child A gets at least 2 cookies, child B gets at most 4 cookies, and child C gets 1 or 3 cookies.
-
What is the generating function for the number of ways to make cents in change using pennies (1 cent), nickels (5 cents), and dimes (10 cents), if you must use an odd number of pennies and at least one dime?
Conclusion
Today, you've learned a new and profoundly useful way to represent combinatorial problems. We've seen that an entire sequence of numbers, representing the answers to a counting problem for all , can be "packaged" into a single algebraic expression—an ordinary generating function. You learned how to construct these functions by translating constraints (like "even", "at most 4", "multiple of 5") into polynomial factors and combining them through multiplication.
We've focused entirely on the construction of these functions. You may be wondering how we get the final answer—the specific coefficient of —out of a complicated expression like the ones we built. That is precisely the topic of our next lesson. We will explore techniques, such as partial fraction decomposition, to expand these compact functions back into power series, allowing us to read off the coefficients and solve the recurrence relations and counting problems that motivated us in the first place.