第 04 章
计数:二项式系数、双射与卡特兰数
从「从 n 个里选 k 个」出发,用除以 k! 修正顺序,用双重计数证明帕斯卡恒等式,推广到多项式系数;再用双射与星线法给球染色计数,最后用反射双射导出卡特兰数 c_n=C(2n,n)/(n+1),并建立它与 Dyck 路径、二叉树之间的对应。
Counting: Binomial Coefficients, Bijections and Catalan Numbers
Starting from choosing k objects out of n, we divide out the order by k!, prove Pascal's identity by double counting, and extend to multinomial coefficients. Bijections and stars-and-bars count colourings of balls, and a reflection bijection yields the Catalan formula c_n = C(2n,n)/(n+1), together with its link to Dyck paths and binary trees.
篇内目录
- Two routes through counting
- Timeline
- Binomial coefficients
- Choose in order, then divide by
- Double counting: Pascal's identity
- Pascal's triangle and symmetry
- Multinomial coefficients
- Bijections: replacing one count by another
- Definitions
- Why bijections count
- Stars and bars: colouring balls
- The question
- Example: ,
- The bijection: stars and bars
- Dyck paths and Catalan numbers
- Definition
- Two more Catalan families
- The Catalan formula
- Proof: count the bad paths
- The reflection bijection
- Simplifying
- Binary trees ↔ Dyck paths
- Tree to path
- Path to tree
- Stirling's formula and asymptotics
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
计数的两条路线
这一讲只做一件事:数清楚有多少种可能。工具只有两个。
第一个是直接算:把选择过程拆成有序的步骤,每一步乘上选择数,最后修正重复。二项式系数就是这样来的。
第二个是双射:如果能构造一个一一对应,把陌生的对象换成熟悉的对象,计数就自动相等。卡特兰数的公式正是靠一个巧妙的反射对应证明的——那个证明很难凭空想到,但理解之后极为漂亮。
时间轴
| 时间 | 内容 |
|---|---|
| 0:00 | 二项式系数:逐步选取,再除以 |
| 3:24 | 双重计数证明帕斯卡恒等式 |
| 6:26 | 帕斯卡三角与对称性 |
| 9:25 | 多项式系数 |
| 14:13 | 双射:单射、满射与逆映射 |
| 20:03 | 给 个球染 种颜色(只看每种颜色几个) |
| 24:15 | 星线法与 |
| 31:01 | Dyck 路径与卡特兰数 |
| 38:17 | 平面树、二叉树也都是卡特兰数 |
| 45:37 | 卡特兰公式 |
| 54:13 | 反射双射:坏路径 ↔ 到 的路径 |
| 1:00:34 | 二叉树与 Dyck 路径的双射 |
| 1:14:33 | 斯特林公式与渐近 |
二项式系数
先按顺序选,再除以重复
从 个对象中选 个,最自然的做法是一个一个选:
第一个有 种选法,第二个剩 种,依此类推,共 个因子。
但这个乘积数多了:选 与选 被当成了两种。同一组 个对象可以用 种顺序被选出,所以除以 :
分子分母同乘 就得到最常用的阶乘形式。
双重计数:帕斯卡恒等式
命题。 。
证明(双重计数)。 数「从 个对象中选 个」的方法数,换一种分类方式:固定其中一个对象(比如第 个),按它是否被选中分成两类。
- 选中它:还要从其余 个中选 个,有 种。
- 不选它:要从其余 个中选 个,有 种。
两类互斥且穷尽,所以总数是两者之和。而这批对象的总数本来记作 ,两边相等。
这个证明没有展开任何阶乘,只做了一件事:把同一批对象按两种方式分类并计数。双重计数之所以强大,就在于它绕开了代数运算,直接把组合意义翻译成等式。
帕斯卡三角与对称性
把 按 排成行,就得到帕斯卡三角:
每个数等于它上方两个数之和,这正是帕斯卡恒等式。三角形左右对称,给出
代数上也一眼可见: 在把 换成 时形式不变。组合意义是:选 个等价于决定「不选哪 个」。
多项式系数
把 个对象分成三组,大小分别为 ,且 ,组是有标签的(第一组、第二组、第三组),组内无序。方法数是
证明。 先选第一组: 种;再从剩下的 个里选第二组: 种;剩下的自动是第三组。相乘:
因为 。
这个「先选一组、再选下一组」的乘法结构可以推广到任意多组:
也可以按「最后一个对象落在哪一组」做双重计数,得到与帕斯卡恒等式同型的递推。
双射:把陌生的计数换成熟悉的计数
定义
设 。
- 单射:,不同对象不会撞到同一个像。
- 满射:对每个 ,存在 使 , 中每个元素都被取到。
- 双射:既是单射又是满射。
等价地, 是双射当且仅当存在逆映射 ,满足
为什么有逆映射就一定是双射。 先证单射:若 ,两边作用 得
再证满射:对任意 ,取 ,则
所以 中每个元素都有原像。
双射为什么能用来计数
若存在双射 ,则 。于是要数清 ,只需构造一个到已知大小的 的双射。难点从来不是验证双射,而是想到该往哪里映射。
星线法:给球染色
问题
有 个球、 种颜色。只关心每种颜色用了几个,不关心具体哪个球是什么颜色。问有多少种染色方案?
例子:、
把方案按「各颜色的球数」分类(红、蓝、绿):
- :3 种(选一种颜色涂 4 个球)。
- : 种(选 3 个的那色有 3 种,选 1 个的那色剩 2 种)。
- :3 种(选不用的颜色有 3 种)。
- :3 种(选涂 2 个的颜色有 3 种)。
合计
注意 ,而不是 ——课程在这里特意纠正了一次口误。
双射:球与隔板
把每个球画成一颗星,把颜色之间用隔板分开: 种颜色需要 块隔板。
以 为例:
读法:第一块隔板前是红色,两块隔板之间是蓝色,最后一块之后是绿色。某种颜色一个球都没有时,就让对应隔板相邻或贴在端点,例如
表示红色 个、蓝色 个、绿色 个。
这是双射。 正向:给定染色方案,按颜色顺序排好球并插入隔板。反向:给定星与隔板的排列,按隔板位置读出各颜色的球数。两个方向都唯一,所以方案数等于
取 、:,与枚举一致。
等价地,这也是非负整数解 的个数:每个 是第 种颜色的球数。
Dyck 路径与卡特兰数
定义
Dyck 路径是从 走到 的路径,每一步是 或 ,并且全程不低于横轴。
由起点与终点可知,路径恰有 步向上、 步向下。第一步必向上,最后一步必向下。
长度 的 Dyck 路径的条数记作 ,称为第 个卡特兰数。
小值:
- :只有「上、下」。
- :「上下上下」与「上上下下」。
- 。
序列开头是
(第一位 对应空路径。)课程提到,Richard Stanley 曾整理出约两百种被卡特兰数计数的对象——它出现在数学的许多角落。
另外两种被计数的对象
- 平面树(plane tree,带根、左右子树有区别): 条边的平面树有 棵。 时有 棵,左右镜像算作不同的两棵。
- 满二叉树(每个内部节点恰有两个孩子): 个内部节点时有 棵。
对树有一个基本事实:满二叉树的叶子数总比内部节点数多 。证明用归纳:只有一个内部节点(两个叶子)时成立;把某个叶子替换成「内部节点 + 两个叶子」,叶子数加 、内部节点数加 ,差值不变。等价地,任何树都满足「顶点数 = 边数 + 1」。
卡特兰公式
定理。 。
先验证 :
与枚举一致。
证明:数坏路径
所有从 到 的路径(允许低于横轴)共有
条:在 步中选 步向上。Dyck 路径就是其中不碰横轴下方的那些,所以
反射双射:坏路径 ↔ 到 的路径
一条坏路径必然在某个时刻首次到达高度 。把从该点起的整段路径关于直线 反射,得到一条新路径。
反射后:
- 起点仍是 ,终点从 变成 ;
- 每一步仍是 ;
- 这条新路径必然低于横轴,因此一定不是 Dyck 路径。
反向操作:对任意一条从 到 的路径,它必然在某个时刻首次到达 ;把该点之后的部分再关于 反射回去,就唯一还原出一条坏路径。
两个方向互逆,所以这是一个双射。于是
后者需要终点高度为 ,即向下比向上多 步:设向上 步、向下 步,
所以
化简
中间用到了 。
课程坦言:这个反射想法很难凭空想到。几周后用生成函数可以机械地推出同一个公式,只是计算更长、美感稍逊。
二叉树 ↔ Dyck 路径
从树到路径
对满二叉树做一次「绕树一周」的遍历(先序遍历),每个节点第一次被访问时记一次。把内部节点记为 ,叶子记为 ,按访问顺序读出序列,再把它画成上下步。
- 满二叉树有 个内部节点、 个叶子,共 步;
- 第一步与最后一步都是叶子();
- 因为叶子总比内部节点多 ,全程高度不低于 。
去掉最后一步,就得到一条长度 的 Dyck 路径。
从路径到树
反向:从左到右读路径,遇到向上步就新建一个内部节点,并把新的两个叶子挂上;遇到向下步就结束当前这条分支。路径结束且内部节点数为 时,恰好得到一棵满二叉树。
两个方向互逆,所以满二叉树与 Dyck 路径一一对应,二者都由卡特兰数计数。
这个双射的价值不只是「又一种卡特兰对象」,它把递归结构(树)与线性序列(路径)连接起来——后面用生成函数时,这种连接正是把递归方程变成代数方程的关键。
斯特林公式与渐近
斯特林公式。
用它估计中心二项式系数:把 与 代入并相除,
于是卡特兰数的增长阶为
也就是说, 以 的速度指数增长,再被 压低。课程没有证明斯特林公式,只指出:除了常数 之外的部分都能用初等微积分或初等概率推出,而那个常数才是真正棘手的部分。
本节结论
- :先有序选取,再除以 修正顺序。
- 帕斯卡恒等式 由双重计数得到:按某个固定元素选不选分类。
- :对称性。
- 多项式系数 ,由「逐组选取」相乘得到。
- 双射 给出 ;等价条件是存在逆映射。
- 星线法: 个球染 色的方案数是 。
- 卡特兰数 计数 Dyck 路径、平面树与满二叉树。
- 卡特兰公式 ,由反射双射把坏路径对应到终点为 的路径。
- 渐近:,。
理解检验
1. 从 个里选 个,为什么要除以 ?如果只选 个,这个修正还需要吗?
参考答案:逐步选取的乘积 把「同一组对象的 种选取顺序」都数成了不同的结果。例如从 中选 个,选 可以按 六种顺序发生,但它们是同一个集合。每组集合被数了 次,所以除以 。当 时 ,修正项不起作用——只选一个元素时不存在顺序问题。
2. 用双重计数证明帕斯卡恒等式,并说明两种分类为什么互斥且穷尽。
参考答案:要数「从 个对象中选 个」的方案数 。固定第 个对象,按它是否被选中分成两类:
- 选中:还需从其余 个中选 个,共 种;
- 不选:需从其余 个中选 个,共 种。 任何一个 元子集要么包含第 个对象、要么不包含,两者不可能同时成立(互斥),且没有第三种情形(穷尽)。所以总数等于两类之和:
3. 用星线法计算: 个球染 种颜色,只区分每种颜色的球数,有多少种方案?
参考答案:需要 块隔板,总位置数为 ,从中选 个放隔板:
也可以看成非负整数解 的个数,由星线法同样是 。枚举核对:把 分成三部分(含 )的个数是 。
4. 计算 ,并说明反射双射把什么样的路径映射成什么样的路径。
参考答案:,坏路径数为 ,故 。反射双射把「从 到 且曾经低于横轴的路径」映射为「从 到 的任意路径」:找到路径首次到达高度 的位置,把该点之后的部分关于直线 反射。反向操作是对到 的路径找首次到达 的位置,再把之后的部分反射回去。两个方向互逆,所以坏路径的条数等于到 的路径条数,而后者需要 步向上、 步向下,共 条。
5. 为什么满二叉树的叶子数总比内部节点数多 ?这个事实在树到 Dyck 路径的双射中起什么作用?
参考答案:对内部节点数归纳。 时树根加两个叶子,叶子 、内部节点 ,差为 。归纳步:把任意一个叶子换成「一个内部节点 + 两个叶子」,内部节点数 、叶子数从 变成 即 ,差值不变。所以恒有「叶子数 − 内部节点数 = 1」。在双射中它保证: 个内部节点、 个叶子的遍历序列共 项,且部分和始终非负(每个叶子的 之前必有对应的内部节点 支撑),于是去掉最后一步后恰得到一条长度 的 Dyck 路径。
6. 用斯特林公式估计 ,并与真值比较。
参考答案:由 ,取 :
真值是 ,相对误差约 。这说明斯特林近似在中等 时已经相当准,而且它揭示了中心二项式系数的指数增长底数为 、多项式因子为 。
与前后课的关系
这一讲建立了计数的两套语言:代数式的直接计算与双射的意义翻译。二项式系数与多项式系数会在概率、编码理论中反复出现;星线法直接对应「非负整数解」的计数;卡特兰数则在下一讲以生成函数的形式再次登场——届时 会从一个关于 的二次方程里自然解出。
一句话概括:数不清的时候,先找一个能一一对应的东西;找得到,计数就变成了同一件事的两种说法。
Two routes through counting
This lecture does one thing: count how many possibilities exist. There are only two tools.
The first is direct computation: break a choice into ordered steps, multiply the numbers of options, then correct for over-counting. Binomial coefficients arise this way.
The second is bijection: if a one-to-one correspondence can be built between unfamiliar objects and familiar ones, the counts are equal. The Catalan formula is proved by exactly such a trick — a reflection that is hard to invent from scratch but beautiful once understood.
Timeline
| Time | Topic |
|---|---|
| 0:00 | Binomial coefficients: choose in order, then divide by |
| 3:24 | Pascal's identity by double counting |
| 6:26 | Pascal's triangle and the symmetry |
| 9:25 | Multinomial coefficients |
| 14:13 | Bijections: injective, surjective, inverse |
| 20:03 | Colouring balls with colours (only the counts matter) |
| 24:15 | Stars and bars, giving |
| 31:01 | Dyck paths and Catalan numbers |
| 38:17 | Plane trees and binary trees are Catalan objects too |
| 45:37 | The Catalan formula |
| 54:13 | Reflection bijection: bad paths ↔ paths ending at |
| 1:00:34 | The bijection between binary trees and Dyck paths |
| 1:14:33 | Stirling's formula and asymptotics |
Binomial coefficients
Choose in order, then divide by
To choose objects out of , do it one at a time:
The first choice has options, the second has , and so on, for factors.
But this product over-counts: choosing and choosing are recorded as different. Each -element set can be produced in orders, so divide by :
Multiplying numerator and denominator by gives the factorial form on the right.
Double counting: Pascal's identity
Claim. .
Proof (double counting). Count the -element subsets of objects again, but classify differently: fix one object (say the -th) and split according to whether it is chosen.
- Chosen: choose the remaining from the other , giving subsets.
- Not chosen: choose all from the other , giving subsets.
The two classes are disjoint and exhaustive, so their sum counts the same collection whose size is . Hence the identity.
No factorials were expanded; the proof only classifies the same objects two ways and counts each. That is why double counting is powerful: it replaces algebra with meaning.
Pascal's triangle and symmetry
Arranging by rows gives Pascal's triangle:
Each entry is the sum of the two above it — exactly Pascal's identity. The symmetry of each row gives
which is also immediate from , unchanged when becomes . Combinatorially: choosing is the same as deciding which to leave out.
Multinomial coefficients
Split objects into three labelled groups of sizes with ; order inside a group is irrelevant. The number of ways is
Proof. Choose the first group: . Then choose the second from the remaining : . Whatever remains forms the third. Multiplying,
since .
The "choose one group, then the next" structure extends to any number of groups:
One can also double-count by asking which group the last object fell into, recovering the same type of recurrence as Pascal's identity.
Bijections: replacing one count by another
Definitions
Let .
- Injective: ; distinct objects never collide.
- Surjective: for every there is with ; every element of is hit.
- Bijective: both injective and surjective.
Equivalently, is a bijection exactly when there is an inverse with
Having an inverse forces bijectivity. Injectivity: if , apply :
Surjectivity: for any , take ; then
So every has a preimage.
Why bijections count
If a bijection exists, then . To count , it suffices to build a bijection onto a set whose size is known. The difficulty is never verifying the bijection; it is thinking of where to map.
Stars and bars: colouring balls
The question
There are balls and colours. Only the number of balls of each colour matters, not which ball got which colour. How many colourings are there?
Example: ,
Classify by the count vector (red, blue, green):
- : ways (choose the colour used for all four).
- : ways (choose the colour used three times, then the colour used once).
- : ways (choose the unused colour).
- : ways (choose the colour used twice).
Total:
Note , not — the lecture corrects this slip mid-stream.
The bijection: stars and bars
Draw each ball as a star and separate colours with bars: colours need bars.
For :
Read off: everything before the first bar is red, between the bars is blue, after the last bar is green. A colour with zero balls corresponds to adjacent bars or a bar at an end, e.g.
means red , blue , green .
This is a bijection. Forward: given a colouring, order the balls by colour and insert the bars. Backward: given an arrangement of stars and bars, read the colour counts off the bar positions. Both directions are unique, so the number of colourings is
For , : , matching the enumeration.
Equivalently, this counts non-negative integer solutions of , where is the number of balls of colour .
Dyck paths and Catalan numbers
Definition
A Dyck path runs from to using steps and , and never goes below the horizontal axis.
Start and end force exactly up-steps and down-steps. The first step must go up and the last must go down.
The number of Dyck paths of length is , the -th Catalan number.
Small values:
- : only "up, down".
- : "up, down, up, down" and "up, up, down, down".
- .
The sequence begins
(with for the empty path). The lecture notes that Richard Stanley compiled roughly two hundred objects counted by Catalan numbers — they appear all over mathematics.
Two more Catalan families
- Plane trees (rooted, left and right children distinguished): edges give trees. For there are , and mirror images count as different trees.
- Full binary trees (every internal node has exactly two children): internal nodes give trees.
A basic fact about trees: a full binary tree always has one more leaf than internal node. Induction: a single internal node with two leaves satisfies it; replacing a leaf by "one internal node plus two leaves" increases both counts by one, so the difference is preserved. Equivalently, every tree has one more vertex than edge.
The Catalan formula
Theorem. .
Check :
matching the enumeration.
Proof: count the bad paths
All paths from to using up/down steps number
since one chooses which of the steps go up. Dyck paths are those that never dip below the axis, so
The reflection bijection
A bad path must first reach height at some point. Reflect the entire portion of the path after that point across the line . The new path:
- starts at and ends at instead of ;
- still uses steps ;
- necessarily lies below the axis, hence is not a Dyck path.
Conversely, given any path from to , it must first hit somewhere; reflecting the part after that point back across uniquely recovers a bad path.
The two directions are mutually inverse, so this is a bijection. Hence
The latter has two more down-steps than up-steps. With up and down,
so
Simplifying
The middle step uses .
The lecture admits that the reflection idea is hard to invent from nothing. Generating functions, a few weeks later, produce the same formula mechanically — longer, but less mysterious.
Binary trees ↔ Dyck paths
Tree to path
Traverse a full binary tree around its outside (preorder), recording each vertex the first time it is visited. Mark internal nodes as and leaves as , then read the sequence as up/down steps.
- A full binary tree with internal nodes has leaves, so the traversal has entries;
- the first and last are leaves ();
- because leaves outnumber internal nodes by one, the running height never drops below .
Dropping the final step yields a Dyck path of length .
Path to tree
Conversely, read the path left to right: an up-step creates an internal node with two fresh leaves; a down-step closes the current branch. When the path ends with internal nodes, a full binary tree has been built.
The operations are mutually inverse, so full binary trees and Dyck paths correspond one-to-one and are both counted by Catalan numbers.
The value of this bijection is not just "another Catalan family": it links a recursive structure (tree) to a linear sequence (path). That link is exactly what turns recursion into algebra when generating functions arrive.
Stirling's formula and asymptotics
Stirling's formula.
Apply it to the central binomial coefficient by substituting and :
Therefore
So grows exponentially at rate , damped by . The lecture does not prove Stirling's formula, only notes that everything except the constant follows from elementary calculus or probability; that constant is the genuinely hard part.
What this lecture establishes
- : choose in order, then divide by .
- Pascal's identity follows by classifying subsets according to a fixed element.
- by symmetry.
- Multinomial coefficients come from choosing groups successively.
- A bijection gives ; equivalently it has an inverse.
- Stars and bars: colouring balls with colours gives schemes.
- Catalan numbers count Dyck paths, plane trees and full binary trees.
- Catalan formula , proved by reflecting bad paths to paths ending at .
- Asymptotics: , .
Comprehension check
1. Why divide by when choosing of , and is the correction needed when ?
Reference answer: The product treats the orders in which the same set can be picked as different outcomes. For instance, choosing from counts the set six times, once for each ordering . Dividing by collapses them. When , and there is nothing to correct, since a single element has no ordering.
2. Prove Pascal's identity by double counting, and explain why the two classes are disjoint and exhaustive.
Reference answer: Count the -element subsets of objects, . Fix the -th object and split:
- containing it: choose the other from , giving ;
- not containing it: choose all from , giving . Every -subset either contains the fixed object or does not; the cases cannot both hold (disjoint) and there is no third option (exhaustive). Hence
3. Using stars and bars, how many ways are there to colour balls with colours, if only the counts per colour matter?
Reference answer: There are bars and positions in total, so the number is
Equivalently, it counts non-negative integer solutions of , which is . Enumerating the partitions of into three non-negative parts confirms .
4. Compute and describe what the reflection bijection maps to what.
Reference answer: total paths and bad paths, so . The bijection maps "paths from to that dip below the axis" to "all paths from to ": find the first point where the path reaches height and reflect everything after it across . The inverse takes a path ending at , finds its first hit of , and reflects back. Since the maps are mutually inverse, the number of bad paths equals the number of paths to , which require up-steps and down-steps, namely .
5. Why does a full binary tree always have one more leaf than internal node, and where is that used in the tree-to-path bijection?
Reference answer: Induct on internal nodes. For the tree is a root with two leaves: leaves, internal node, difference . In the induction step, replace any leaf by one internal node with two leaves: internal nodes increase by and leaves go from to , also increasing by , so the difference is preserved. Hence "leaves − internal nodes = 1" always. In the bijection this guarantees that the traversal sequence has entries and that every prefix has at least as many s as s (each leaf's is backed by an earlier internal node's ), so dropping the final step gives a genuine Dyck path.
6. Use Stirling's formula to estimate and compare with the exact value.
Reference answer: From with ,
The exact value is , a relative error of about . The approximation is already accurate at moderate and reveals the exponential base together with the polynomial factor .
Relation to neighbouring lectures
This lecture builds two languages of counting: direct algebraic computation and bijective translation of meaning. Binomial and multinomial coefficients recur in probability and coding theory; stars and bars is exactly the count of non-negative integer solutions; Catalan numbers return with generating functions, where falls out of a quadratic equation for .
One sentence to keep: when the count is out of reach, look for something to correspond it with one-to-one; once found, counting becomes two ways of saying the same thing.