Generating functions transform counting problems into algebra problems. Instead of reasoning directly about how many ways to arrange or select objects, you encode the counts as coefficients of a power series, manipulate the series using standard algebra, and then read off the answer. This technique is especially useful when direct counting gets messy or when you need to solve recurrence relations.
There are two main types: ordinary generating functions (OGFs) for problems involving unlabeled or distinct objects, and exponential generating functions (EGFs) for problems involving labeled structures. Choosing the right type depends on whether the objects in your problem are distinguishable and how they combine.
Generating functions for counting problems
Understanding generating functions
A generating function encodes a sequence as a formal power series:
The coefficient of tells you the count for objects of size . You don't need the series to converge; you're treating it as a formal algebraic object.
OGF vs. EGF: Use an OGF when you're counting unordered selections or compositions of unlabeled pieces. Use an EGF (where the series is ) when objects carry labels and you care about how labels are distributed. For example, counting the number of ways to partition a set of labeled elements into groups calls for an EGF, while distributing identical coins into distinct bins calls for an OGF.
Basic building blocks and operations
A few fundamental generating functions appear over and over:
- represents unrestricted selection with repetition (every non-negative integer is allowed).
- represents choosing either zero or one copy of an item.
- is the EGF for unrestricted labeled selection.
Combining these building blocks mirrors combinatorial operations:
- Multiplication of two OGFs corresponds to forming ordered pairs (or concatenating independent choices). If counts ways to fill one slot and counts ways to fill another, then counts ways to fill both.
- Addition corresponds to taking the union of disjoint alternatives.
- Composition arises when you substitute one structure inside another (e.g., partitions of partitions).
From these pieces you can build generating functions for many familiar sequences. For instance, the OGF for the Fibonacci numbers is , and the OGF for the Catalan numbers is .
Adapting generating functions for specific problems
Real problems come with constraints, and you handle them by modifying the building blocks:
- Minimum or maximum size: If each part must have size at least 2, replace with (shifting the index).
- Alternating signs: Replacing with gives , useful for inclusion-exclusion arguments.
- Periodic sequences: Combine roots of unity filters. For example, to pick out only even-indexed coefficients of , use .
- Weighted counts: If each object of size carries a weight , just use as the coefficient instead of a pure count.
Extracting coefficients from generating functions
Once you have a generating function, the whole point is to read off , the coefficient of . Several techniques exist, ranging from elementary to advanced.
Direct expansion and algebraic techniques
Binomial Theorem. For expressions of the form :
This generalizes to negative and fractional exponents. The generalized binomial series gives:
which counts multiset selections (choosing items from types with repetition).
Partial fraction decomposition. When your generating function is a ratio of polynomials, decompose it into simpler fractions. For example, to extract coefficients from :
- Write .
- Solve for and (here , after clearing denominators... but always check by plugging in convenient values of ). Actually: setting gives , and setting gives . You get .
- Since , the coefficient of is .
Undetermined coefficients. If you suspect the coefficient sequence has a particular form (e.g., ), plug in known values of to solve for the unknowns.

Advanced extraction techniques
- Differentiation and integration shift indices. Multiplying by and differentiating produces , which is handy when you need weighted sums. Integrating does the reverse.
- Taylor series comparison. Expand known functions and match coefficients. For instance, when is even, and 0 when is odd.
- Cauchy's Integral Formula. For asymptotic estimates of coefficients:
This is the starting point for singularity analysis, where you estimate the integral by studying the dominant singularity of . For example, the th Catalan number (from ) grows asymptotically as .
Solving recurrence relations with generating functions
This is one of the most practical applications. The strategy is always the same:
- Multiply both sides of the recurrence by and sum over all valid .
- Recognize the sums as the generating function (or shifted versions of it).
- Solve the resulting algebraic equation for .
- Extract coefficients to get a closed form for .
Translating recurrence relations to functional equations
Example: Fibonacci numbers. The recurrence is with .
-
Define .
-
Multiply the recurrence by and sum for :
-
Rewrite in terms of :
-
Substitute initial conditions ():
-
Solve: .
Example: Non-homogeneous recurrence. For with :
- The same multiply-and-sum procedure gives .
- Solving: .
- Partial fractions and coefficient extraction yield .
Solving and analyzing functional equations
After isolating , you typically get a rational function. Apply partial fraction decomposition to break it into terms of the form , each contributing to the coefficient of .
For the Fibonacci example, the denominator factors using the roots of the characteristic equation , giving . Partial fractions then produce the famous Binet formula:
Techniques for non-homogeneous recurrences
When the recurrence has a non-homogeneous term (like or ), the generating function approach handles it naturally: the non-homogeneous term just adds an extra known function to the equation. You solve for algebraically, and the partial fraction decomposition automatically separates the homogeneous and particular parts of the solution.
This is a real advantage over the classical method of "guess the particular solution form," because the generating function framework doesn't require any guessing.

Advanced combinatorial problems with generating functions
Multivariate generating functions
Some problems track two or more parameters at once. A bivariate generating function encodes a doubly-indexed sequence. For instance, lattice paths on a rectangular grid that take unit steps right or up can be counted with a bivariate GF where tracks horizontal steps and tracks vertical steps.
Multivariate GFs also appear in partition theory. The generating function for partitions into distinct odd parts is:
Here the coefficient of counts the number of such partitions of .
Implicit and functional equation techniques
Not every generating function can be written in closed form. Sometimes the GF satisfies an implicit equation, and you need specialized tools:
-
Lagrange Inversion Theorem. If for some known function , then: This is the standard way to count labeled rooted trees (where gives Cayley's formula: labeled trees on vertices).
-
The kernel method. For functional equations involving, say, where one variable can be set to a specific algebraic function of the other to "kill" one term, simplifying the equation. This comes up frequently in lattice path problems with boundary constraints.
-
The quadratic method. When a GF satisfies a quadratic equation (often involving a square root), you can solve directly and choose the branch consistent with initial conditions. The Catalan number GF is the classic example.
Exponential generating functions and labeled structures
For labeled structures, the EGF is the natural choice. If there are labeled structures of size , the EGF is:
The key property: multiplying two EGFs corresponds to forming labeled products, where you split the label set into two parts in all possible ways and build one structure on each part.
The exponential formula is especially powerful: if is the EGF for connected labeled structures, then is the EGF for sets of such structures (allowing any number of connected components). For example:
- The EGF for labeled rooted trees is , so the EGF for labeled forests is .
- The EGF for cycles (cyclic permutations) is , so the EGF for sets of cycles (all permutations) is , confirming there are permutations of elements.
The symbolic method provides a dictionary: the SET construction translates to , the SEQUENCE construction to , and the CYCLE construction to in the EGF world.
Analyzing algorithms and data structures
Generating functions connect combinatorics to algorithm analysis. The average number of comparisons in quicksort, for instance, can be derived by setting up a recurrence for the expected cost, translating it into a generating function equation, and solving.
Bivariate GFs are particularly useful here: one variable tracks the size of the input, and the other tracks the cost or some structural parameter (like tree height). Singularity analysis of the resulting GF then gives asymptotic estimates for typical behavior on large inputs.