PrevNext
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 nn-th Catalan can be expressed as follows using binomial coefficients:

Cn=1n+1(2nn)=(2n)!(n+1)!n!C_n=\frac{1}{n+1}\cdot \binom{2n}{n}=\frac{(2n)!}{(n+1)!\,n!}

They also have the recurrence formula

Cn+1=i=0nCiCni for n0C_{n+1}= \sum^{n}_{i=0}{C_i \cdot C_{n-i}} \,\,\, \text{ for } n \ge 0 \\

which can also be expressed as

Cn=2(2n1)n+1Cn1C_n=\frac{2(2n-1)}{n+1} \cdot C_{n-1}

The first 55 Catalan numbers are

n012345
CnC_n11251442

Applications

The Catalan numbers can be used to represent a wide variety of things.

For example, CnC_n is equal to the number of valid parenthesis expressions of length 2n2n. Take, for instance, C3=5C_3=5:

  • ()()()
  • (())()
  • ()(())
  • ((()))
  • (()())

It's also equal to the number of full binary trees with n+1n+1 leaves. The following image shows the 55 binary trees with 44 leaves:

binary-trees

CnC_n is also the number of monotonic lattice paths along the edges of a n×nn \times n 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 C4=14C_4=14 paths in a 4×44 \times 4 grid:

lattice points

The next two examples are a bit more niche, but they're still interesting to think about.

Consider a convex polygon with n+2n+2 sides divided into nn triangles by connecting vertices with non-intersecting lines. The number of different ways to divide the polygon in this way is equal to CnC_n.

Here's the particular case for n=3n=3 in which we have C3=5C_3=5:

polygons

CnC_n is also equal to the number of mountain ranges of length 2n2n consisting of nn upstrokes and nn downstrokes.

mountains

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 y=1y = -1. It turns out that for every such path ending at (2n,0)(2n, 0), we can biject it to a path ending at (2n,2)(2n, -2) 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 y=1y = -1. Since we assumed the blue path touches y=1y = -1 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 y=1y = -1. This point is guaranteed to exist because the path starts at y=0y = 0 and ends at y=2y = -2, which means it must cross y=1y = -1 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:

# of red paths=(up + downdown)=(2nn+1)\text{\# of red paths} = \binom{\text{up + down}}{\text{down}} = \binom{2n}{n + 1}

Thus, the number of mountain ranges is the total number of paths minus the number of blue paths:

(2nn)(2nn+1)=(2nn)nn+1(2nn)=1n+1(2nn)\begin{align*} \binom{2n}{n} - \binom{2n}{n + 1} &= \binom{2n}{n} - \frac{n}{n + 1}\binom{2n}{n} \\ &= \frac{1}{n + 1}\binom{2n}{n} \end{align*}

Note that we can generalize this bijection: to count the paths that cross y=ky = -k, we can instead count the number of paths that end at (2n,2k)(2n, -2k).

Exceedance

This explanation offers more insight into why 1n+1\frac{1}{n + 1} appears in the final expression.

First, let's define the exceedance of a path as the total number of upstrokes it takes above y=0y = 0.

A path with exceedance 5.

The core result is that for all ii from 00 to nn, the number of paths with exceedance ii is equal. Therefore, the number of mountain ranges, i.e. the number of paths with exceedance nn, is precisely 1n+1\frac{1}{n + 1} of the total number of paths.

To prove this equality, we will define a bijection between paths with exceedance ii and paths with exceedance i+1i + 1 for 0i<n0 \leq i \lt n. Let's consider the following mapping: for a path with exceedance ii, we take the last downstroke that goes from y=0y = 0 to y=1y = -1 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 i+1i + 1.

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 y=0y = 0 to y=1y = -1, to the first downstroke from y=1y = 1 to y=0y = 0. Therefore, we have established a bijection between paths with exceedance ii and paths with exceedance i+1i + 1, 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 NN is the N/2N/2 Catalan Number.

Implementation

Time Complexity: O(N)\mathcal{O}(N)

MAXN = 10**6
MOD = 10**9 + 7
fac = [0] * (MAXN + 1)
inv = [0] * (MAXN + 1)
Code Snippet: Combinatorics Functions (from the module) (Click to expand)
n = int(input())

Problems

StatusSourceProblem NameDifficultyTags
LCEasy
Show TagsCatalan, Combinatorics
SPOJNormal
Show TagsCatalan, Combinatorics
CSESHard
Show TagsCatalan, Combinatorics
CFHard
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!

PrevNext