Generating Functions
Generating functions turn sequences into algebraic objects. Instead of studying 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 is
In many discrete mathematics applications this is treated as a formal power series, so convergence is not the main issue. The coefficient of records . We often write for the coefficient of in .
Basic generating functions:
Multiplying generating functions performs convolution:
This is why products of generating functions model independent contributions whose sizes add.
Key results
Generating functions solve linear recurrences. For the Fibonacci sequence , , and , let
Multiply the recurrence by and sum for :
Using and :
Therefore
This rational function contains the whole Fibonacci sequence in its coefficients.
Generating functions also prove identities. The coefficient of in is
while has coefficient . Hence Vandermonde's identity follows:
For restricted integer solutions, each variable contributes a factor whose terms represent its allowed values. If can be , its factor is . If it can be any nonnegative integer, its factor is .
Visual
| Constraint on one variable | Generating factor | Meaning |
|---|---|---|
| any nonnegative contribution | ||
| bounded contribution | ||
| choose or do not choose | ||
| even and nonnegative | even contribution | |
| one coin of value or none | at most one coin | |
| unlimited coins of value | any number of that coin |
Worked example 1: Count restricted solutions
Problem. Count nonnegative integer solutions to
with for each .
Method.
- Each variable can contribute , , or , so each variable has generating factor
- For three variables, use
- We need the coefficient of .
- Expand in a controlled way:
- Multiply by and track only terms contributing to :
- Contributions to are:
Checked answer. There are solutions: , , and .
Worked example 2: Solve a recurrence with a generating function
Problem. Solve and for .
Method.
- Let
- Multiply the recurrence by and sum for :
- Rewrite each sum:
- Since ,
- Solve for :
So
- Use partial fractions:
- Extract coefficients:
Checked answer. . Check: , and .
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 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 , not . 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 with each variable at most , then the coefficients should be symmetric from degree through degree : . The symmetry comes from replacing each value by . 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 , then is usually , not just . Similarly, . 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 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 , finish the task: extract , 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, immediately gives coefficient , while gives coefficient for and for smaller . 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 , use rather than just . 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 , explain that the three contributions correspond to placing one variable at and the other two at . 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 or 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 , 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
- Recurrence relations gives the recurrences generating functions often solve.
- Permutations and combinations supplies binomial coefficients and stars-and-bars counts.
- Counting principles supplies product-rule reasoning for multiplying factors.
- Discrete probability uses generating functions for distributions in more advanced work.