Catalan Numbers and the Many Things They Count
Meet the sequence 1, 1, 2, 5, 14, 42, see why it counts brackets, paths and triangulations, and derive its formula.
Key takeaways
- The first Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430 and 4862.
- A compact formula is Cₙ = C(2n, n)/(n + 1), where C(2n, n) is the central binomial coefficient.
- A reflection argument shows why the count of paths that stay on one side equals C(2n, n) − C(2n, n + 1).
- The recurrence Cₙ₊₁ = C₀Cₙ + C₁Cₙ₋₁ + ... + CₙC₀ appears whenever an object splits into two smaller ones of the same kind.
- To show a new problem has a Catalan answer, match it one-for-one with a known case or verify the recurrence.
The Catalan numbers are the sequence 1, 1, 2, 5, 14, 42, 132, 429 and so on. They count a startling variety of things: ways to match parentheses, paths that never dip below a line, ways to cut a polygon into triangles, shapes of binary trees, and many more.
The n-th Catalan number, starting from n = 0, is given by Cₙ = (2n)! / (n! (n + 1)!). Here n! means the product of the whole numbers from 1 to n, with 0! = 1. The rest of this article shows where that formula comes from and why such different problems share one answer.
Matched parentheses
How many ways can you write three pairs of parentheses so that every closing mark has an earlier opening partner? There are exactly five:
((())) (()()) (())() ()(()) ()()() With two pairs there are two ways, (()) and ()(). With one pair there is one, and with none there is one: the empty string. That gives 1, 1, 2, 5.
The rule that makes a string valid is simple. Reading left to right, the number of closing marks never exceeds the number of opening marks, and at the end the two totals are equal. Compilers and spreadsheet programs check exactly this kind of condition.
Paths that stay above ground
Replace each opening mark with an upward diagonal step and each closing mark with a downward one. A valid string becomes a path that starts and ends at height zero and never goes below it. These are called Dyck paths.
An equivalent picture uses a square grid. Walk from one corner to the opposite corner of an n by n grid, moving only right or up, without ever crossing above the diagonal. The number of such walks is Cₙ.
Translating between pictures in this way is called a bijection, a pairing that matches each object of one kind with exactly one of the other. Once you have it, the two counts must be equal.
The reflection argument
Count all strings with n opening and n closing marks, valid or not. You are choosing which n of the 2n positions get an opening mark, so there are C(2n, n) strings.
Now count the bad ones. In a bad path, find the first moment the height reaches −1. Reflect everything after that moment, swapping ups and downs. The path originally had to finish at height 0, so after reflection it finishes at height −2. It therefore has n − 1 ups and n + 1 downs.
The step can be reversed: any path ending at −2 must pass through −1, and reflecting after the first visit restores a bad path. So bad paths are as numerous as all paths with n + 1 downs, which is C(2n, n + 1).
Cₙ = C(2n, n) − C(2n, n + 1)
= C(2n, n) / (n + 1)
For n = 3: 20 − 15 = 5, and 20 / 4 = 5 The same reflection idea solves an election-counting puzzle described in lattice paths and the ballot problem.
Cutting polygons into triangles
Take a convex polygon and draw non-crossing diagonals until only triangles remain. A triangle has 1 way (do nothing). A square has 2, one for each diagonal. A pentagon has 5 and a hexagon has 14.
In general a polygon with n + 2 sides has Cₙ triangulations, with its corners treated as labeled, so rotated copies count separately. Euler studied this problem in the eighteenth century, and Johann Segner found a recurrence for it.
The sequence is named for Eugène Catalan, a Belgian mathematician who wrote about it in the nineteenth century. The Mongolian scholar Ming Antu, working in Qing China, had used these numbers earlier in his study of infinite series.
The splitting recurrence
Look at a valid bracket string with n + 1 pairs. Its first opening mark has a partner somewhere. Inside that pair sits a valid string with k pairs, and after it sits a valid string with n − k pairs.
Every string splits this way in exactly one manner. Summing over k gives
Cₙ₊₁ = C₀Cₙ + C₁Cₙ₋₁ + ... + CₙC₀
C₄ = C₀C₃ + C₁C₂ + C₂C₁ + C₃C₀
= 1×5 + 1×2 + 2×1 + 5×1
= 14 Binary trees show the same split: a root, a left subtree with k nodes and a right subtree with n − k. Polygons do too, since the triangle resting on one chosen side divides the rest into two smaller polygons.
Whenever you see an object that breaks into two independent smaller copies in all possible size splits, suspect Catalan numbers.
Turning a bracket string into a tree
It helps to carry out one bijection fully. Take a valid string and read it as (A)B, where the first pair of marks encloses the string A and B is whatever follows. Make a node. Its left subtree is the tree built from A, and its right subtree is the tree built from B. The empty string becomes no tree at all.
For the string (())() you get A = () and B = (). The root therefore has one child on the left and one on the right.
To go backwards, take a tree, write an opening mark, then the string for the left subtree, then a closing mark, then the string for the right subtree. The two rules undo each other, so n pairs of brackets and binary trees with n nodes are equally numerous.
Notice what "binary" means here: a single child on the left and a single child on the right count as different trees. Without that distinction the totals would be smaller.
A use in computing
Multiplication of matrices is associative, so a chain ABC gives the same result however you group it. The work required can differ a great deal.
Suppose A is 10 by 100, B is 100 by 5 and C is 5 by 50. Multiplying a p by q matrix with a q by r matrix in the standard way takes p × q × r scalar multiplications.
| Grouping | Cost of the first product | Cost of the second | Total |
|---|---|---|---|
| (AB)C | 5,000 | 2,500 | 7,500 |
| A(BC) | 25,000 | 50,000 | 75,000 |
With three matrices there are C₂ = 2 groupings. With 11 matrices there are C₁₀ = 16,796, and the count keeps growing nearly fourfold at each step. Checking them all soon becomes wasteful.
The standard remedy is dynamic programming: find the cheapest way to multiply every short stretch of the chain, store those answers and combine them. The structure of that algorithm is the splitting recurrence above, with "choose the cheapest" in place of "add up".
Stacks and sorting
A stack is a list where you add and remove items only at the top, like a pile of plates. Feed the numbers 1, 2, 3 into a stack in order, popping whenever you like. Each push is an opening mark and each pop a closing one, so there are exactly 5 possible output orders for three items.
The six arrangements of 1, 2, 3 therefore include one that a stack cannot produce. It is 3, 1, 2: to output 3 first you must have pushed 1 and 2 beneath it, and then 2 has to come out before 1.
A generating function
Pack the whole sequence into a power series C(x) = 1 + x + 2x² + 5x³ + 14x⁴ + .... The recurrence says that multiplying this series by itself shifts it by one place:
C(x) = 1 + x · C(x)²
C(x) = (1 − √(1 − 4x)) / (2x) (the root that equals 1 as x → 0) Expanding the square root with the general binomial series recovers the formula for Cₙ. The approach is explained in generating functions.
Fast computation and growth
To list values by hand, use the ratio of neighbors:
Cₙ₊₁ = Cₙ × 2(2n + 1) / (n + 2)
C₅ = 14 × 18 / 6 = 42
C₆ = 42 × 22 / 7 = 132 | n | Cₙ |
|---|---|
| 5 | 42 |
| 6 | 132 |
| 7 | 429 |
| 8 | 1430 |
| 9 | 4862 |
| 10 | 16796 |
The ratio approaches 4, so the numbers grow nearly as fast as 4ⁿ. A sharper estimate, from Stirling's approximation, is Cₙ ≈ 4ⁿ / (n^1.5 × √π). At n = 10 this gives about 18,700, roughly 11% above the true value, and the relative error shrinks as n grows.
Spotting a false match
A sequence that begins 1, 2, 5 is not necessarily Catalan. The Bell numbers, which count the ways to split a set into groups, begin 1, 1, 2, 5, 15. They part company at the fifth term: 15 against 14. The difference is that for four labeled points on a circle, exactly one grouping, {1, 3} with {2, 4}, has blocks that cross. Non-crossing groupings are counted by Catalan numbers.
This is a general warning. Several matching terms suggest a conjecture, and nothing more, as other deceptive sequences show. Prove it with a bijection or a recurrence.
Exercises
- Draw the five binary trees with three nodes and pair each with a bracket string.
- Count the ways to fully parenthesize a product of four factors, abcd. You should find 5, because n + 1 factors give Cₙ.
- Six people sit around a table and shake hands in pairs with no arms crossing. Check that there are 5 ways.
- Which Catalan numbers are odd? Compute a few and look for the pattern.
For the last, the odd ones occur at n = 0, 1, 3, 7, 15, exactly when n is one less than a power of 2.