第 07 章
Catalan 数的生成函数
从满二叉树的递推 C_n=ΣC_jC_{n−j−1} 得到 C(x)=1+xC(x)²,解二次方程并取舍根,再用二项式定理展开出 C_n=(1/(n+1))C(2n,n);最后用 Dyck 路径的首次回零分解给出同一条方程的第二种来源。
The generating function for Catalan numbers
The full-binary-tree recurrence C_n=ΣC_jC_{n−j−1} yields C(x)=1+xC(x)²; solving the quadratic and choosing the right root, then expanding by the binomial theorem, gives C_n=(1/(n+1))C(2n,n). A decomposition of Dyck paths at their first return produces the same equation by a different route.
篇内目录
- What this lecture is for
- Timeline
- Two faces of the Catalan numbers
- The recurrence from binary trees
- Solving the quadratic
- Expanding with the binomial theorem
- Converting to a binomial coefficient
- A second route: decomposing Dyck paths
- The sequence theorem
- Application: tiling a strip
- Reading off the recurrence
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
本讲要解决的问题
Catalan 数 同时数着很多看似无关的对象:满二叉树、Dyck 路径、括号匹配、多边形三角剖分。上一讲用生成函数解了 Fibonacci 的线性递推;这一讲处理一个非线性递推——它的生成函数满足二次方程。
目标有三步:写出 满足的方程;解出 的闭式;再用二项式定理展开,得到
时间轴
| 时间 | 内容 |
|---|---|
| 0:01 | Catalan 数数什么:满二叉树、Dyck 路径 |
| 3:39 | 二叉树的递推关系 |
| 7:03 | 生成函数方程 |
| 8:34 | 解二次方程,讨论两个根 |
| 11:27 | 二项式定理展开 |
| 13:43 | 逐项算出 的系数,验证 |
| 18:21 | 得到 |
| 25:43 | 第二种推导:Dyck 路径分解 |
| 37:43 | 序列定理的一般陈述 |
| 43:14 | 应用: 铺砖与递推 |
Catalan 数的两种面孔
本讲反复使用两个模型,它们给出同一个数列:
- 满二叉树:每个内部结点恰有两个孩子。含 个结点的满二叉树有 棵。 时有 棵。
- Dyck 路径:从 出发,走 步,每步 或 ,始终不低于 ,最后回到 。这样的路径也有 条。
两个模型各给出一条通往 的路,最后汇合到同一个二次方程。
从二叉树的递推出发
一棵不是单结点的满二叉树,由一个根和左右两棵子树构成。设整棵树有 个结点,左子树有 个结点,则右子树有
个结点。左右子树的形状可以独立选择,于是
验算 :
这串卷积正好是乘积的系数形式。设 。乘积 中 的系数是 ,正是 。再乘一个 把次数抬到 :
常数项 对应 (单结点树,也是空路径)。这一步是整讲的关键:递推被翻译成一个关于未知函数 的代数方程。
解二次方程
把方程整理成标准形式:
用求根公式(把 当作二次项系数):
两个根在代数上都满足方程,必须用初始条件筛掉一个。
令 。分子在 处等于 ,分母趋于 :
- 取加号:分子趋于 ,商发散,不可能等于 。
- 取减号:分子趋于 ,需要看极限。
对减号用一次洛必达法则,或直接用下一节的展开:
所以
常数项为 ,与 一致。这个检验不是形式上的:生成函数是形式幂级数, 处的值就是常数项,选错根会得到 型的奇点,级数根本无法展开。
用二项式定理展开
要读出 的系数,需要 的展开。广义二项式定理对分数指数同样成立:
其中
是双阶乘。代入 :
负号两两抵消,每一项都是负的,于是
再除以 , 变成 :
令 ,即 :
化成组合数
用双阶乘与阶乘的关系
(分子是所有 到 的乘积,除去其中的偶数因子 ,剩下奇数因子),代入:
验算 时 ✓;用双阶乘形式验算: ✓。
课程在黑板上逐项展开到 时,中间系数出现过口误;以统一公式为准,,与枚举一致。
第二条路:Dyck 路径的首次回零分解
Dyck 路径有一个天然的分解:在第一次回到 的地方切开。
每个非空 Dyck 路径唯一地写成
前半段「上步 + Dyck 路径 + 下步」称为一个抬升路径:它从 出发,第一步向上,最后一步向下,中间始终不低于 ,且只在终点回到 。去掉首尾两步,剩下的正是一条 Dyck 路径,只是整体上移了一层。
于是:
- 抬升路径的生成函数 : 记录多出的两步, 记录中间那段。
- 任意 Dyck 路径是若干抬升路径的序列(可以为空),由序列公式
两边乘以 :
即
与二叉树得到的是同一个二次方程。两条完全不同的组合分解,落到同一个代数方程上,这是生成函数方法最漂亮的地方之一。
序列定理
上面用到的定理值得单独写出。
设 中的对象大小至少为 ,。由 的对象排成的有限序列(含空序列)的生成函数是
为什么要求大小至少为 ? 若存在大小为 的对象,就能在任意位置插入任意多个而不改变总大小,于是每个大小都有无穷多个序列,系数不再是有限数,生成函数失去意义。
在 Dyck 路径里,被排成序列的对象是抬升路径,最短长度为 、大小记为 ,满足条件;空序列对应空路径,贡献常数项 。
应用: 条带的铺砖
用两种可翻转的骨牌铺 条带。先数不可再分的块:不存在一条竖线把它切成两段各自可铺。
- :最短的基本块长度为 ,沿水平线翻转得到另一种。
- :长度为 无法由基本块拼成。
- 、、,依此类推。
一般地,
直观上,要让一条竖线无处可切,必须用两个「L」形块夹住中间任意多个「Z」形块,因此基本块只能取奇数长度,且只有两种镜像。
于是
任意铺法是基本块的序列:
读出递推
比较 两端系数:
初值为
而不是 ,原因是:空铺法加上一个长度 的块并不能得到长度 的铺法——最短的基本块长度是 。分母给出递推,分子给出初值,这一点与上一讲的铺砖例子完全一致。
递推也可以直接看出来:任何非空铺法,要么在末尾接一个长度 的竖块( 种),要么接一个长度 的基本块(两种朝向, 种)。
本节结论
- Catalan 数 满足 。
- 生成函数满足 ,解为 ,取减号由 决定。
- 二项式展开给出 。
- Dyck 路径按首次回零分解,得到 ,与二叉树给出同一方程。
- 序列定理:对象大小至少为 时,序列的生成函数是 。
- 铺砖问题:,,递推 。
理解检验
1. 用二叉树的结点数解释 。
参考答案:一棵有 个结点的满二叉树由根、左子树、右子树组成。若左子树有 个结点,则右子树有 个结点。左子树有 种形状,右子树有 种,二者独立,乘起来即得。 从 取到 ,对应左子树从最小到最大。
2. 解出 时为什么要取减号?请给出严格理由。
参考答案:方程 的两根为 。生成函数的常数项必须是 ,即 。取加号时分子趋于 ,商发散;取减号时用洛必达: 。 因此只有减号给出有限的常数项,加号对应的级数含 项,不是形式幂级数。
3. 从 推出 ,写出双阶乘那一步。
参考答案:由 ,且 ,取 得 。于是 ,除以 后 。 再用 ,约去 得 。
4. 为什么抬升路径的生成函数是 ?
参考答案:一条抬升路径从 出发,第一步必为上步,最后一步必为下步,且中间始终不低于 ,只在终点回到 。去掉首尾两步,剩余部分整体下移一层后是一条长度任意、始终非负的 Dyck 路径。因此抬升路径与「Dyck 路径 + 两次额外步」一一对应。按本讲的大小约定(大小为步数的一半),额外两步贡献因子 ,所以 。
5. 铺砖问题中,为什么 而 ?为什么 ?
参考答案:基本块要求不存在一条竖线把它切成两段各自可铺。两种骨牌的形状使得只有奇数长度的条带能被「不可分」地铺满:最短是长度 ,之后每加一对「Z」形块长度加 ,两端必须用互为镜像的「L」形块封口,因此长度只能是 ,且每种长度恰有两种镜像,故 ,偶数长度无法构成基本块,。 是因为空铺法无法通过添加基本块得到长度 ——最短的基本块长度是 ;这不违反递推,递推从 起才适用。
6. 序列定理为什么要求每个对象大小至少为 ?如果允许大小为 的对象会怎样?
参考答案:序列的生成函数是 ,这个等式作为形式幂级数成立的前提是每一项对 只贡献有限多次。若存在大小 的对象,它在 中贡献常数项 ,那么 对每个 都贡献 量级的项,取所有 之和后系数发散,级数不再有定义。直观上,可以往序列里塞任意多个零大小对象而总大小不变,因此每个大小都有无穷多个序列。
与前后课的关系
上一讲用生成函数处理线性递推,得到 Binet 公式;这一讲处理二次方程,得到 Catalan 公式。两者的共同套路是:组合分解 函数方程 解方程 展开系数。
下一讲转入概率。不过生成函数不会退场:概率里的矩母函数 与这里的生成函数是同一套技术,只是把「计数」换成了「矩」。再往后讲 Catalan 数还会在二叉树、括号匹配与算法分析中反复出现。
What this lecture is for
The Catalan numbers count many objects that look unrelated: full binary trees, Dyck paths, balanced bracketings, triangulations of polygons. The previous lecture solved a linear recurrence with generating functions; this one takes on a nonlinear recurrence whose generating function satisfies a quadratic equation.
Three steps: write the equation for ; solve it in closed form; expand by the binomial theorem to obtain
Timeline
| Time | Topic |
|---|---|
| 0:01 | What Catalan numbers count: full binary trees, Dyck paths |
| 3:39 | The recurrence for binary trees |
| 7:03 | The functional equation |
| 8:34 | Solving the quadratic, comparing the two roots |
| 11:27 | Expanding with the binomial theorem |
| 13:43 | Computing the coefficient as a check |
| 18:21 | Arriving at |
| 25:43 | A second derivation: decomposing Dyck paths |
| 37:43 | The sequence theorem in general form |
| 43:14 | Application: tiling a strip and its recurrence |
Two faces of the Catalan numbers
Two models recur throughout the lecture and give the same sequence.
- Full binary trees: every internal vertex has exactly two children. Trees with vertices number ; for there are .
- Dyck paths: walks of steps from , each step or , never below , ending at . There are of these.
Each model supplies a route to , and both routes meet at the same quadratic equation.
The recurrence from binary trees
A full binary tree that is not a single vertex consists of a root and two subtrees. Suppose the whole tree has vertices and the left subtree has ; then the right subtree has
vertices. The two shapes are chosen independently, so
Check :
This convolution has exactly the shape of a product coefficient. Let . The coefficient of in is , which is ; multiplying by raises the degree to :
The constant accounts for (the single-vertex tree, and also the empty path). This is the key step: the recurrence becomes an algebraic equation for the unknown function .
Solving the quadratic
Rewrite in standard form:
With as the quadratic coefficient, the roots are
Both roots satisfy the equation algebraically; the initial condition must eliminate one.
Let . The numerator tends to while the denominator tends to :
- With the plus sign the numerator tends to and the quotient diverges, so it cannot equal .
- With the minus sign the numerator tends to and a limit must be taken.
One application of L'Hôpital (or the expansion in the next section) gives
Hence
The constant term is , matching . This check is not cosmetic: a generating function is a formal power series, its value at is its constant term, and the wrong root produces a singularity that prevents expansion altogether.
Expanding with the binomial theorem
To read off the coefficient of we need the expansion of . The generalized binomial theorem applies to fractional exponents:
where
and is the double factorial. Substituting :
The signs cancel pairwise and every term is negative, so
Dividing by shifts to :
Set , i.e. :
Converting to a binomial coefficient
Use
since the product of all integers from to divided by the even factors leaves the odd factors. Substituting:
Check: gives , and . Also , matching the enumeration.
A second route: decomposing Dyck paths
Every Dyck path admits a canonical decomposition: cut it at its first return to .
Each nonempty path is uniquely
The leading block "up step, Dyck path, down step" is called a raised Dyck path: it starts at , begins with an up step, ends with a down step, stays at least in between, and returns to only at the end. Removing the first and last steps leaves an ordinary Dyck path lifted one level.
Therefore:
- Raised paths have generating function : the factor records the two extra steps, the path in between.
- Every Dyck path is a sequence of raised paths (possibly empty), so the sequence theorem gives
Multiplying by :
that is,
This is the same quadratic equation obtained from binary trees. Two entirely different combinatorial decompositions landing on one algebraic equation is one of the most attractive features of the method.
The sequence theorem
The theorem used above deserves a standalone statement.
Let every object of have size at least , and let . Then finite sequences of -objects, including the empty sequence, have generating function
Why must sizes be at least ? If an object of size existed, any number of copies could be inserted without changing the total size, so every size would admit infinitely many sequences, the coefficients would not be finite numbers, and the generating function would be meaningless.
For Dyck paths the objects being sequenced are raised paths, whose shortest length is and whose size we take to be , so the hypothesis holds; the empty sequence corresponds to the empty path and contributes the constant term .
Application: tiling a strip
Tile a strip with two shapes that may be reflected. First count indecomposable pieces: tilings that no vertical line can split into two separately tileable strips.
- : the shortest indecomposable piece has length , and reflecting it horizontally gives the second.
- : length cannot be built indecomposably.
- , , , and so on.
In general,
Intuitively, to leave no vertical cut, an "L" piece must cap each end with any number of "Z" pieces between them, so indecomposable pieces have odd length and come in two mirror images.
Hence
Every tiling is a sequence of indecomposable pieces:
Reading off the recurrence
Comparing coefficients in :
with initial values
That rather than reflects the fact that adding a piece of length to the empty tiling is not allowed — the shortest indecomposable piece has length . As in the previous lecture's tiling example, the denominator encodes the recurrence and the numerator encodes the initial conditions.
The recurrence is also visible directly: any nonempty tiling ends either with a vertical piece of length ( ways) or with a length- indecomposable piece in one of two orientations ( ways).
What this lecture establishes
- satisfy .
- The generating function satisfies , with solution ; the minus sign is forced by .
- Binomial expansion gives .
- Decomposing Dyck paths at the first return gives , the same equation as for trees.
- Sequence theorem: with all sizes at least , sequences have generating function .
- Tiling: , , so .
Comprehension check
1. Explain using vertex counts.
Reference answer: A full binary tree with vertices consists of a root, a left subtree and a right subtree. If the left subtree has vertices, the right has . There are shapes on the left and on the right, independently, so the count multiplies. Summing from to covers all left-subtree sizes.
2. Why is the minus sign chosen when solving for ? Give a rigorous reason.
Reference answer: The two roots are . A generating function must have constant term , i.e. . The plus sign has numerator tending to and a vanishing denominator, so the quotient diverges. The minus sign gives, by L'Hôpital, . Only the minus sign yields a finite constant term; the plus sign produces a term and is not a formal power series.
3. Derive from , showing the double-factorial step.
Reference answer: Using and , with we get . Hence , and dividing by gives . Then , so the cancels and .
4. Why is the generating function of raised Dyck paths ?
Reference answer: A raised path starts at , its first step is up and its last is down, it stays at least in between, and it returns to only at the end. Removing the first and last steps and shifting down by one level leaves an arbitrary Dyck path. So raised paths correspond bijectively to "a Dyck path plus two extra steps". With the lecture's size convention (half the number of steps), the two extra steps contribute a factor , hence .
5. In the tiling problem, why is but ? Why is ?
Reference answer: An indecomposable piece admits no vertical cut into two tileable strips. The two tile shapes force odd length: the shortest case has length , and each extra pair of "Z" pieces adds while the ends must be capped by mirror-image "L" pieces. So only lengths occur, each in exactly two mirror forms, giving and . because the empty tiling cannot be extended to length by adding an indecomposable piece — the shortest has length ; the recurrence itself only starts at .
6. Why does the sequence theorem require objects of size at least ? What goes wrong otherwise?
Reference answer: The sequence generating function is , and this identity holds as a formal power series only if each receives finitely many contributions. A size-zero object adds a positive constant term to ; then contributes on the order of at every degree, and summing over all makes the coefficients diverge. Intuitively, arbitrarily many zero-size objects can be inserted without changing the total size, so every size has infinitely many sequences.
Relation to neighbouring lectures
The previous lecture used generating functions on a linear recurrence to obtain Binet's formula; this one solves a quadratic to obtain the Catalan formula. Both follow the same pattern: combinatorial decomposition functional equation solve expand.
The next lecture turns to probability, but generating functions do not disappear: the moment-generating function is the same technique with counting replaced by moments. Catalan numbers reappear later in binary trees, bracketings and the analysis of algorithms.