Generating Functions in Combinatorics
A trick for turning a counting problem into an algebra problem: encode a whole sequence of numbers as the coefficients of one polynomial, then multiply polynomials instead of reasoning about cases by hand.
Prerequisites: Permutations and Combinations
Some counting questions resist direct reasoning. "How many ways can I make change for a dollar using pennies, nickels, dimes, and quarters?" You could enumerate cases for hours and still miss one. Generating functions solve an entire family of such problems by converting "count the ways" into "multiply out some algebra and read off a coefficient" — turning combinatorics into arithmetic a computer, or a patient human, can just grind through.
The analogy: a sequence hiding inside a polynomial
Think of a vending machine slot that can hold , , or of an item. Write that as the polynomial — the exponent of is "how many you took," and every exponent that's allowed gets a coefficient of 1 (the "1" out front of each term means "one way to take that many").
Now suppose you have two such slots and want to know, for each total count, how many ways to reach it. Multiply the polynomials:
Read off the coefficients: 1 way to take 0 total, 2 ways to take 1 total (from either slot), 3 ways to take 2 total, and so on. You never listed a single case — ordinary polynomial multiplication did the counting for you, because multiplying polynomials automatically adds up every pair of exponents, which is exactly what "combine choices from two sources" means.
Writing it down
A generating function for a sequence is the (formal — we never actually plug in a number) power series
In words: is just a bookkeeping symbol whose exponent tracks "which term," and the coefficient sitting on is the answer to your counting question for size . You don't care what "equals" as a number — you care about extracting , written ("the coefficient of in ").
The single most useful building block is the geometric series:
In words: an item you can take any number of times (0, 1, 2, …), each in exactly one way, is encoded by . This closed form is the whole payoff of the method — instead of an infinite sum you carry around one small fraction, and multiplying several such fractions together (one per type of item) still just multiplies out to another closed form you can expand.
Drag the exponent in the explorer above and watch how a power series' shape changes — it's the same object living on the other side of the equals sign: is the compressed, closed-form twin of the infinite sum , and generating-function algebra works by manipulating the compressed form and only expanding back out at the very end, when you need one specific coefficient.
Worked example 1: making change, four coin types
Back to the vending-machine question, done properly. Using pennies (worth 1c, unlimited), nickels (5c), dimes (10c), and quarters (25c), how many ways to make exactly 10 cents?
Each coin type, used any number of times, contributes a geometric-series factor — but in the value it contributes, so pennies track per coin, nickels per coin, and so on:
We want . Rather than expand the whole product, restrict to terms that can reach exponent 10: pennies contribute up to ; nickels contribute ; dimes contribute ; quarters contribute only (since already overshoots). Enumerate the coin-count combinations that sum to 10 cents directly, now organized by this factorization: 0 dimes and 0 nickels → 10 pennies (1 way); 0 dimes, 1 nickel → 5 pennies (1 way); 0 dimes, 2 nickels → 0 pennies (1 way); 1 dime, 0 nickels → 0 pennies (1 way). Total: 4 ways. The generating function didn't shortcut the arithmetic here — with only 10 cents the direct count is just as fast — but the same four fractions, multiplied out by a computer algebra system, answer "how many ways to make $47.32" exactly as easily, which is where hand enumeration would never finish.
Worked example 2: Fibonacci from a generating function
The Fibonacci sequence counts, among other things, the number of ways to tile a strip with and tiles. Find a closed form for .
Let . Multiply the recurrence relation itself by and sum over :
The left side is . The right side's sums are and . So , and solving for :
In words: the entire infinite Fibonacci sequence is compressed into one fraction. To get a formula for itself, factor the denominator using the roots of , which are (the golden ratio) and . Partial-fraction decomposition and expanding each piece as a geometric series gives Binet's formula:
Check it: should be 55. , so ; , so . . Matches — and now you can compute directly, with no recursion and no 100-step loop.
What this means in practice
- Counting paths and states. Anywhere you count "number of sequences/paths satisfying a constraint" — coin change, dice-sum distributions, lattice paths — a product of simple factors like usually encodes the whole problem.
- Solving recurrences. Any linear recurrence (Fibonacci-style trading signals, cash-flow recursions, difference equations in a discretized PDE) can be solved in closed form this way instead of simulated step by step.
- Probability generating functions. The same object with replaced by probability weights (moment generating functions, characteristic functions) is how sums of independent random variables get analyzed — convolution of distributions becomes multiplication of generating functions, exactly as in the change-making example.
- Interview problems. "How many ways to roll a sum of with dice" or "how many binary strings avoid three consecutive 1s" are textbook generating-function setups once you recognize the pattern.
A generating function turns "count the ways" into "multiply some simple factors and read off a coefficient." Independent choices multiply; the coefficient of in the product is the count for total size . The geometric series (unlimited uses, one at a time) is the one building block worth memorizing cold.
When a problem says "any number of," "at most of," or "an odd number of" a given item, that phrase tells you exactly which polynomial or series factor to write for that item — "any number" is , "at most " is , "odd number only" is .
The trap is treating as a real number and asking "but what if , doesn't the series diverge?" Don't — is a formal placeholder for "which coefficient we're tracking," not a value to be evaluated. Worrying about convergence is solving the wrong problem; the only operations that matter are algebraic ones (multiply, add, take a coefficient), and a beginner who tries to numerically evaluate at has left combinatorics for calculus by mistake.
Related concepts
Practice in interviews
Further reading
- Wilf, generatingfunctionology (ch. 1)
- Graham, Knuth, Patashnik, Concrete Mathematics (ch. 7)