Introduction
Catalan numbers frequently occur in various counting problems. The Belgian mathematician Eugène Charles Catalan discovered this sequence in 1958 while studying the counting of parenthesis sequences, and it was named after him. The Qing mathematician Ming Antu had already discovered this sequence in the 1730s.
Catalan numbers satisfy the following recurrence:
The first few terms of the sequence are (OEIS: A000108, with the index starting from ):
Applications
The recurrence for Catalan numbers has a natural recursive structure: a counting problem of size , counted by , can be split by enumerating a dividing point into two subproblems of sizes and . This recurrence causes Catalan numbers to occur widely in problems with similar recursive structures.
-
Path-counting problem: Consider an grid with lower-left corner and upper-right corner . Starting from the lower-left corner, at each step one may move one unit right or one unit up. The number of paths that reach the upper-right corner without going above the diagonal (touching it is allowed) is .
??? note "Proof" Let the number of solutions be . Consider the case . Let the first point where the path reaches the diagonal be . Consider paths from to whose intermediate points, excluding the start and end points, do not pass through the diagonal (and cannot touch it).
<!-- To make bot happy. Do NOT delete this line. --> As shown in the figure, the first step of such a path must be to the right, from $(0,0)$ to $(1,0)$, and the last step must be upward, from $(k,k-1)$ to $(k,k)$. Therefore, these paths are exactly paths from $(1,0)$ to $(k,k-1)$ that do not cross the line $y=x-1$, so their number is $T_{k-1}$. At the same time, the number of legal paths from $(k,k)$ to $(n,n)$ is $T_{n-k}$. By the multiplication principle, the number of paths that first touch the diagonal at $(k,k)$ is $T_{k-1} T_{n-k}$. Enumerating all possible values of $k$, the total number of legal paths is $$ T_n = \sum_{k=1}^n T_{k-1}T_{n-k}. $$ Substituting $k=i+1$ reveals that this is exactly the recurrence for Catalan numbers. Since $T_0=1$, we have $T_n = C_n$. -
Counting nonintersecting chords in a circle: On a circle, connect points in pairs so that the resulting line segments are pairwise disjoint. The number of possible configurations is .
??? note "Proof" Let the number of configurations for points be . Label the points clockwise as . Since the chords are pairwise disjoint, point can only be connected to an even-numbered point; otherwise, the odd number of points between the two endpoints could not be paired without crossing the chord connecting them. If is connected to , there are points on the left and points on the right. By the multiplication principle, the number of configurations is . Thus, enumerating gives . Setting yields the recurrence for Catalan numbers. Since , we have .
<!-- To make bot happy. Do NOT delete this line. --> -
Counting triangulations: The number of ways to divide a convex -gon into triangular regions with nonintersecting diagonals is .
??? note "Proof" Let the number of triangulations of an -gon be . First choose an edge as the base edge. It must belong to a triangle; let the third vertex of that triangle be . The original convex polygon is then divided into three parts:
<!-- To make bot happy. Do NOT delete this line. -->- Triangle $(1,k,n+2)$. - A $k$-gon with vertices $1\sim k$. - An $(n+3-k)$-gon with vertices $k\sim (n+2)$. The latter two parts are subproblems, so the recurrence is $$ T_n = \sum_{k=2}^{n+1} T_{k-2}T_{n+1-k}. $$ Setting $k=i+2$ yields the Catalan recurrence. Since $T_0=T_1=1$, we have $T_n=C_n$. -
Counting binary trees: The number of structurally distinct binary trees with nodes is . Equivalently, the number of structurally distinct full binary trees with non-leaf nodes is .
??? note "Proof" Let the number of binary trees with nodes be . Choose an arbitrary root node and enumerate the sizes of the left and right subtrees. If the left subtree has size , the right subtree has size . Both subtrees are subproblems, so the recurrence is
<!-- To make bot happy. Do NOT delete this line. -->$$ T_n = \sum_{i=0}^{n-1}T_iT_{n-1-i}. $$ This is the recurrence for Catalan numbers. Since $T_0=T_1=1$, we have $T_n=C_n$. -
Counting parenthesis sequences: The number of valid parenthesis sequences formed by pairs of parentheses is .
??? note "Proof" Relate this to the path-counting problem. Regard a left parenthesis as a move upward and a right parenthesis as a move to the right. A valid parenthesis sequence is one in which, at every position, the number of left parentheses is no less than the number of right parentheses. This is equivalent to the path-counting problem in which, at every moment, the number of upward moves is no less than the number of rightward moves. Therefore, valid parenthesis sequences and valid paths are in bijection. The number of valid parenthesis sequences is also .
<!-- To make bot happy. Do NOT delete this line. --> -
Counting pop sequences: Suppose a stack of unbounded size receives the push sequence . The number of valid pop sequences is .
??? note "Proof" Relate this to the parenthesis-sequence counting problem. Regard a push as a left parenthesis and a pop as a right parenthesis. At every moment, the number of pushes is no less than the number of pops. Therefore, valid pop sequences and valid parenthesis sequences are in bijection. The number of valid pop sequences is also .
<!-- To make bot happy. Do NOT delete this line. --> -
Counting sequences: Among sequences formed from copies of and copies of , the sequences , the number of sequences whose partial sums satisfy is .
??? note "Proof" Relate this to the parenthesis-sequence counting problem. Regard as a left parenthesis and as a right parenthesis. At every moment, the number of terms is no less than the number of terms. Therefore, valid sequences and valid parenthesis sequences are in bijection. The number of valid sequences is also .
Although this recurrence has many applications, its direct computation is relatively expensive, so a simpler formula is needed.
Common Forms
Catalan numbers have the following common expressions:
All of these forms of Catalan numbers can be computed efficiently: the first two turn the problem into computing factorials and binomial coefficients, while the third provides a recurrence for sequential computation.
This article provides two proofs for these three common forms.
Algebraic Derivation
There are two steps to deriving the above expressions for Catalan numbers algebraically. First, verify that the three forms are equivalent.
Proof that expressions are equivalent
It suffices to show that expression can be transformed into the factorial form in expression :
Likewise, expression can be transformed into the factorial form in expression :
Therefore, the three expressions are equivalent.
Next, verify that these forms are indeed solutions to the Catalan recurrence. To do so, consider using generating functions to directly solve recurrence .
Solving recurrence using generating functions
Consider the ordinary generating function of the Catalan numbers. Since the Catalan recurrence is very similar to a convolution, consider constructing an equation for using a convolution:
In the penultimate equality, the order of summation is exchanged and is substituted. Thus:
From the initial condition , we know that . Substitution shows that the only feasible solution is
Next, this must be expanded as a power series. Using the power-series expansion of , we obtain
where is a falling factorial power:
Substituting this into the expression for gives
This gives in expression .
Combinatorial Meaning
Since Catalan numbers have clear combinatorial meanings, the forms can also be proved using only combinatorial counting. This section gives one combinatorial proof for each of the three expressions.
Proof of expression
Consider the sequence-counting problem. For any sequence consisting of , , define its partial sum as , and define its exceedance as the number of indices satisfying and . An exceedance of is equivalent to a valid sequence. The exceedance can take values in , giving possible values in total. It remains to prove that the numbers of sequences for different exceedances are actually equal.
To this end, construct a mapping from sequences with exceedance to sequences with exceedance , denoted by . For a sequence with exceedance , represented as , choose the smallest index such that and . Exchange the sequences on the two sides of to obtain the following sequence :
Since the partial sums corresponding to the part to the right of in the original sequence are unchanged by the exchange, its contribution to the exceedance is also unchanged. For the part to the left of in the original sequence, all corresponding partial sums increase by after the exchange. Therefore, its contribution to the exceedance decreases, and the amount of the decrease is exactly the number of indices in the part to the left of in the original sequence satisfying and . Because of the choice of , there is exactly one such index, so the exceedance of is . In other words, the mapping decreases the exceedance of a sequence by exactly .
The mapping is invertible. Notice that in the sequence , the position corresponding to is exactly the largest index satisfying and . This is because, after the exchange, these partial sums are exactly greater than the corresponding partial sums before the exchange, so the current partial sum corresponds to a partial sum of before the exchange. However, by the choice of , the part before this position after the exchange (that is, the part to the left of in the original sequence) contains no index satisfying and .
Thus, is a bijection between sequences with exceedance and sequences with exceedance . This shows that the numbers of sequences for different exceedances are equal. Since the total number of sequences is , the number of valid sequences (that is, sequences with exceedance ) is
This proves expression for Catalan numbers.
Proof of expression
Consider the path-counting problem. This is a typical lattice-path counting problem and can be solved using the reflection principle. For this problem, subtract the number of invalid paths from the total number of paths. A total path takes steps, of them to the right, so the number of configurations is . A path is invalid if and only if it touches the line . For any invalid path, find its first intersection with the line and reflect the portion after that point across the line . It can then be seen that an invalid path from to becomes a path from to .
Since every path from to must cross the line , every such path corresponds to an invalid path from to . As in the calculation of the total number of paths, the total number of invalid paths is . Therefore, the total number of valid paths is
This is expression for Catalan numbers.
Proof of expression
Consider the triangulation-counting problem. Let be a convex -gon, and fix one of its edges as the base edge. For every triangulation of polygon , choose one of its non-base edges (including edges newly added during triangulation), mark it, and orient it. There are ways to triangulate and mark it. Now let be a convex -gon, again with one edge fixed as the base edge. For polygon , choose one of its non-base edges, mark it, and then triangulate it. There are ways to mark and triangulate it.
As shown in the figure, the results of these two sets of operations are clearly in bijection. Given a triangulation and marked edge of , extend its marked edge to a triangle, extend the endpoint indicated by the orientation into a new edge, and mark this new edge. This gives a marked triangulation of . Conversely, given a marked triangulation of , contract its marked edge into a point, mark the diagonal created by the contraction, and orient it toward the resulting vertex. This gives a triangulation and marked edge of . Therefore,
Rearranging and using gives expression for Catalan numbers.
Example
Luogu P1044 Stack
Given the push order , find the total number of possible pop orders.
Reference code
C++
#include <iostream>
using namespace std;
int n;
long long f[25];
int main() {
f[0] = 1;
cin >> n;
for (int i = 1; i <= n; i++) f[i] = f[i - 1] * (4 * i - 2) / (i + 1);
// This uses common form 4
cout << f[n] << endl;
return 0;
}
Python
f = [0] * 25
f[0] = 1
n = int(input())
for i in range(1, n + 1):
f[i] = f[i - 1] * (4 * i - 2) // (i + 1)
# This uses common form 4
print(f[n])
Exercises
- Luogu P2532 [AHOI2012] Treehouse Stairs
- Luogu P1641 [SCOI2010] Generating String
- Luogu P3200 [HNOI2009] Interesting Sequence
- AtCoder Beginner Contest 205 E - White and Black Balls
- AtCoder Regular Contest 145 C - Split and Maximize
- Luogu P5014 Triangle of Water (revised version)
- Luogu P3978 [TJOI2015] Probability Theory