| Resources | |||||
|---|---|---|---|---|---|
| cp-algo | Well documented article. | ||||
The Catalan numbers are a sequence of positive integers that can be very useful in counting problems in combinatorics. The -th Catalan can be expressed as follows using binomial coefficients:
They also have the recurrence formula
which can also be expressed as
The first Catalan numbers are
| n | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 5 | 14 | 42 |
Applications
The Catalan numbers can be used to represent a wide variety of things.
For example, is equal to the number of valid parenthesis expressions of length . Take, for instance, :
()()()(())()()(())((()))(()())
It's also equal to the number of full binary trees with leaves. The following image shows the binary trees with leaves:
![]()
is also the number of monotonic lattice paths along the edges of a grid that don't pass above the diagonal. The paths start in the lower left corner and ends in the upper right corner.
For example, there are paths in a grid:
![]()
The next two examples are a bit more niche, but they're still interesting to think about.
Consider a convex polygon with sides divided into triangles by connecting vertices with non-intersecting lines. The number of different ways to divide the polygon in this way is equal to .
Here's the particular case for in which we have :
![]()
is also equal to the number of mountain ranges of length consisting of upstrokes and downstrokes.
![]()
Derivations
Using the "mountain ranges" interpretation, we can derive two nice bijective proofs for the Catalan formulas.
Reflection
Consider complementary counting: we then need to characterize the paths that dip below ground level, i.e. the paths that touch the line . It turns out that for every such path ending at , we can biject it to a path ending at by reflecting a portion of the path, like so:
![]()
To be precise, the bijection is defined as follows:
- To go from blue to red, we reflect the blue path about the first point that touches the . Since we assumed the blue path touches at least once, this point is guaranteed to exist.
- To go from red to blue, we also reflect the red path about the first point that touches . This point is guaranteed to exist because the path starts at and ends at , which means it must cross at some point.
Therefore, the number of blue paths equals the number of red paths, and the number of red paths is easy to count:
Thus, the number of mountain ranges is the total number of paths minus the number of blue paths:
Note that we can generalize this bijection: to count the paths that cross , we can instead count the number of paths that end at .
Exceedance
This explanation offers more insight into why appears in the final expression.
First, let's define the exceedance of a path as the total number of upstrokes it takes above .
![]()
A path with exceedance 5.
The core result is that for all from to , the number of paths with exceedance is equal. Therefore, the number of mountain ranges, i.e. the number of paths with exceedance , is precisely of the total number of paths.
To prove this equality, we will define a bijection between paths with exceedance and paths with exceedance for . Let's consider the following mapping: for a path with exceedance , we take the last downstroke that goes from to and move it to the end of the path.
![]()
This transformation indeed increases exceedance by 1, but unfortunately it is not bijective. One easy way to see this is the fact that the resultant path always ends in a downstroke, which clearly does not cover every possible path of exceedance .
Thankfully, we can make an easy fix: after applying our current mapping, just swap the second half (i.e. the part originally after the red arrow) of the path with the first!
![]()
We can show that this mapping is invertible by verifying that it transforms the red arrow from the last downstroke from to , to the first downstroke from to . Therefore, we have established a bijection between paths with exceedance and paths with exceedance , as desired.
Just for fun, here's an image from Wikipedia that demonstrates how this algorithm decreases the exceedance of various paths:
![]()
Bracket Sequences I
Focus Problem – try your best to solve this problem before continuing!
Explanation
The problem is a direct application of Catalan numbers. The answer for is the Catalan Number.
Implementation
Time Complexity:
MAXN = 10**6MOD = 10**9 + 7fac = [0] * (MAXN + 1)inv = [0] * (MAXN + 1)Code Snippet: Combinatorics Functions (from the module) (Click to expand)n = int(input())
Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| LC | Easy | Show TagsCatalan, Combinatorics | ||||
| SPOJ | Normal | Show TagsCatalan, Combinatorics | ||||
| CSES | Hard | Show TagsCatalan, Combinatorics | ||||
| CF | Hard | Show TagsCatalan, Combinatorics | ||||
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!