A remarkable number of apparently unrelated problems have this answer β and they are all in bijection with one another.
The family
| Problem (size ) | Count |
|---|---|
| Balanced bracket sequences of length | |
| Binary trees with internal nodes | |
| Full binary trees with leaves | |
| Triangulations of a convex -gon | |
| Ways to parenthesise factors | |
| Monotone lattice paths from to staying below the diagonal | |
| Non-crossing chord diagrams on points | |
| Non-crossing partitions of | |
| Permutations avoiding any one 3-pattern (e.g. 231) | |
| Standard Young tableaux of shape | |
| Mountain ranges with up and down strokes | |
| Ways to stack coins in a triangular pile | |
| Rooted plane trees with vertices | |
| Dyck words of length | |
| Stack-sortable permutations of |
Recognising any one of these tells you the answer to all of them.
The reflection principle
Paths from to using right and up steps that never cross above the diagonal:
The subtracted term counts the bad paths. Reflect the portion of a bad path after its first violation across the line ; this is a bijection between bad paths and unrestricted paths to , which number .
The reflection argument is the technique, and it generalises far beyond Catalan numbers:
- Ballot problem: paths to with staying strictly above the diagonal number .
- Paths with a shifted boundary: subtract the reflected count.
- Paths between two boundaries: inclusion-exclusion over repeated reflections.
Computing them
// O(n) with factorials mod p
long long catalan(int n) {
return fact[2*n] % MOD * invFact[n] % MOD * invFact[n+1] % MOD;
}
// O(n) iteratively
// C[0] = 1; C[i] = C[i-1] * 2*(2i-1) / (i+1)
long long c = 1;
for (int i = 1; i <= n; i++)
c = c % MOD * (2 * (2*i - 1) % MOD) % MOD * inv[i + 1] % MOD;The recurrence is directly, or as a convolution β but the closed form makes both unnecessary unless the problem is a variant.
Generating function
The functional equation is the recurrence: a non-empty Dyck path splits uniquely as ( A ) B with Dyck paths. Solving the quadratic gives the closed form β a good demonstration of generating functions doing real work.
The relatives
| Sequence | Counts | Formula |
|---|---|---|
| Narayana | Dyck paths with exactly peaks | ; |
| Motzkin | paths with up/down/flat steps | 1,1,2,4,9,21,51 |
| SchrΓΆder | paths with diagonal steps | 1,2,6,22,90 |
| Ballot numbers | paths to staying above | |
| -Catalan | -ary trees | |
| Bell | set partitions (all, not non-crossing) | 1,1,2,5,15,52 |
Note that Catalan and Bell agree up to then diverge β the difference is non-crossing versus arbitrary partitions.
The cycle lemma β a bijective proof
For a sequence of integers summing to , exactly of its rotations have all prefix sums positive.
Applied with to sequences of ones and minus-ones, it gives directly β a proof with no algebra at all. The same lemma is why the gas station problem has a unique answer.
Why it is worth knowing
Two reasons: the sequence itself is worth recognising on sight (compute 6 terms, compare against ), and the reflection principle is a genuinely reusable counting technique for any lattice-path problem with a boundary.
See also: Catalan and Special Numbers Β· Combinatorics Β· Generating Functions