In combinatorial mathematics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects.
The Catalan numbers on nonnegative integers n are a sequence Cn which begins:
C0=1, C1=1, C2=2, C3=5, C4=14, C5=42, C6=132, C7=429, C8=1430, C9=4862, C10=16796, ...
The nth Catalan number can be expressed directly in terms of binomial coefficients:
C(n) = (2n)! / ((n + 1)! * n!)
Or using the recursive formula:
C(0) = 1
C(n) = sum of C(i) * C(n-1-i) for i = 0 to n-1
Catalan numbers have many applications in combinatorics:
- Binary Search Trees:
Cnis the number of different binary search trees withnkeys - Parentheses:
Cnis the number of expressions containingnpairs of correctly matched parentheses - Polygon Triangulation:
Cnis the number of ways to triangulate a convex polygon withn+2sides - Path Counting:
Cnis the number of paths in a grid from(0,0)to(n,n)that don't cross the diagonal - Dyck Words:
Cnis the number of Dyck words of length2n - Mountain Ranges:
Cnis the number of "mountain ranges" you can draw usingnupstrokes andndownstrokes
With 3 keys (1, 2, 3), there are C3 = 5 different BSTs:
1 1 2 3 3
\ \ / \ / /
2 3 1 3 1 2
\ / \ /
3 2 2 1
With 3 pairs of parentheses, there are C3 = 5 valid combinations:
((()))
(()())
(())()
()(())
()()()
A hexagon (6 sides = n+2, so n=4) can be triangulated in C4 = 14 different ways.
- Time Complexity:
O(n²)- Using dynamic programming approach - Space Complexity:
O(n)- To store intermediate results