Catalan Numbers¶
Catalan numbers is a number sequence, which is found useful in a number of combinatorial problems, often involving recursively-defined objects.
This sequence was named after the Belgian mathematician Catalan, who lived in the 19th century. (In fact it was known before to Euler, who lived a century before Catalan).
The first few Catalan numbers
Application in some combinatorial problems¶
The Catalan number
- Number of correct bracket sequence consisting of
- The number of rooted full binary trees with
- The number of ways to completely parenthesize
- The number of triangulations of a convex polygon with
- The number of ways to connect the
- The number of non-isomorphic full binary trees with
- The number of monotonic lattice paths from point
- Number of permutations of length
- The number of non-crossing partitions of a set of
- The number of ways to cover the ladder
Calculations¶
There are two formulas for the Catalan numbers: Recursive and Analytical. Since, we believe that all the mentioned above problems are equivalent (have the same solution), for the proof of the formulas below we will choose the task which it is easiest to do.
Recursive formula¶
The recurrence formula can be easily deduced from the problem of the correct bracket sequence.
The leftmost opening parenthesis
You can also think it in this manner. By definition,
C++ implementation¶
const int MOD = ....
const int MAX = ....
int catalan[MAX];
void init() {
catalan[0] = catalan[1] = 1;
for (int i=2; i<=n; i++) {
catalan[i] = 0;
for (int j=0; j < i; j++) {
catalan[i] += (catalan[j] * catalan[i-j-1]) % MOD;
if (catalan[i] >= MOD) {
catalan[i] -= MOD;
}
}
}
}
Analytical formula¶
(here
The above formula can be easily concluded from the problem of the monotonic paths in square grid. The total number of monotonic paths in the lattice size of
Now we count the number of monotonic paths which cross the main diagonal. Consider such paths crossing the main diagonal and find the first edge in it which is above the diagonal. Reflect the path about the diagonal all the way, going after this edge. The result is always a monotonic path in the grid
The number of monotonic paths in the lattice