第 05 章
生成函数:从凯莱树定理到求和的机械化
先用「同一种对象数两次」证明凯莱树定理:n 个顶点的标号树有 n^(n-2) 棵;再把序列打包成幂级数,定义生成函数,说明不交并对应相加、笛卡尔积对应相乘,用骰子与找零两个例子演示,最后用求导技巧算出 Σ k·C(n,k)=n·2^(n-1)。
Generating Functions: From Cayley's Tree Theorem to Mechanical Sums
Cayley's theorem — there are n^(n-2) labelled trees on n vertices — is proved by counting the same upgraded objects two ways. We then package a sequence into a formal power series, show that disjoint union adds and Cartesian product multiplies, work through dice and coin-change examples, and use differentiation to evaluate the sum of k times C(n,k).
篇内目录
- Two questions, one method
- Timeline
- Cayley's tree theorem
- Statement and small cases
- Strategy: upgrade the objects, then count twice
- Why this is a bijection
- Generating functions: turning a sequence into a function
- Definition
- Example one: binomial coefficients
- Example two: a biased coin
- Operations on generating functions
- Disjoint union: add
- Cartesian product: multiply
- Example three: the sum of two dice
- Example four: making change
- Using generating functions to evaluate a sum
- The target
- Step one: write the known generating function
- Step two: differentiate
- Step three: substitute
- Double-counting check
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
两个问题,一种方法
这一讲处理两个看起来无关的问题:
- 个顶点的标号树有多少棵?
- 怎样把一整列计数问题变成可以机械计算的代数?
第一个问题的答案出奇简单:。证明的关键是把同一类对象用两种方式数一遍——这正是上一讲反复训练的技巧。
第二个问题是生成函数。它把序列 打包成一个幂级数,于是「组合」变成「代数运算」:不交并变成相加,笛卡尔积变成相乘,而求导可以给每一项乘上它的下标。
时间轴
| 时间 | 内容 |
|---|---|
| 0:00 | 凯莱树定理:标号树的计数 |
| 5:24 | 证明策略:数标号有根树,再数带边序的有根树 |
| 11:10 | 第二种数法:从森林出发逐个加边 |
| 25:52 | 两种数法相等,完成证明 |
| 29:03 | 生成函数的定义; 只是标记 |
| 33:32 | 例一:二项式系数与 |
| 36:24 | 例二:有偏硬币与 |
| 39:16 | 生成函数的加法与乘法 |
| 41:55 | 六面骰与八面骰的和 |
| 56:04 | 找零问题: 枚 1 分、 枚 5 分、 枚 10 分 |
| 1:02:13 | 求导技巧:计算 |
凯莱树定理
陈述与小的情形
定理(Cayley)。 个标号顶点的树共有 棵。
先看几个小值(标号是 ):
| 树的数量 | ||
|---|---|---|
可以手工核对:路径型 的标号方式有 种(左右对称导致除以 ),星型(一个中心接三个叶子)有 种(只选中心),合计 。
时,课程同样分类相加得到 ,于是规律浮现。
证明策略:把对象升级,再数两次
直接数标号树很难。策略是先数一类更容易构造的对象:标号有根树、且给边排了序。
设 为 个顶点的标号树数量。考虑
这一类对象。
第一种数法。 从一棵标号树出发:根可以任选( 种), 条边可以任意排序( 种)。所以总数是
第二种数法。 换一个构造顺序:从 个孤立顶点(即 棵单点树构成的森林)出发,每次把一个连通块挂到另一个连通块上。
第 步()开始时还剩 棵树:
- 从 个顶点中任选一个作为挂载点: 种;
- 选另一棵树(不能是挂载点所在的那棵): 种;
- 把那棵树的根接到挂载点上,边记为第 条。
每一步减少一棵树,共走 步,最后只剩一棵,得到标号有根树,且边带有添加顺序。总数为
其中 出现了 次,而 从 一路乘到 。
为什么这是双射
第二种构造是可逆的:给定一棵带边序的标号有根树,看最后添加的那条边,把它剪断,就唯一地还原出「两棵树」的森林,以及此前所有步骤留下的结构。反复执行,直到回到 个孤立顶点。
因为正反两个方向都唯一,所以第二种数法数出的正是同一类对象。于是
两边约去 :
这个证明的美感在于:没有直接数树,而是数「树 + 根 + 边序」。多加的结构让构造变得容易,两边再一起约掉。
课程顺带提到,也可以构造标号树与长度 、取值 的序列之间的双射(Prüfer 序列),但那个双射相当复杂、直觉上不友好,所以这里走双重计数的路线。
生成函数:把序列变成一个函数
定义
给定数列 ,它的生成函数是形式幂级数
这里 不是数,也不必赋予任何值。它的唯一作用是记录下标: 与 绑定在一起,从而把「第 项」与「第 项」区分开。
既然是形式幂级数,就可以像多项式一样做加法、乘法、求导、代入,而不必担心收敛。这些代数操作会对应到组合意义,这正是它的价值。
例一:二项式系数
取 (当 时记作 )。则
这正是二项式定理。逐项解释:把 看成 个 相乘,要得到 ,必须从其中 个括号里取 、其余取 ——选择方式有 种,所以 的系数就是 。
生成函数把「组合选择」编码成了「代数展开」。
例二:有偏硬币
抛一枚有偏硬币 次,每次正面概率 、反面概率 。令 为恰好出现 次正面的概率:
其生成函数为
同样由二项式定理得到。这里 的作用只是标记「正面次数」,不含概率意义。
生成函数的运算
不交并:相加
设 是不交的对象类,。若按「大小」分层,大小为 的对象在 中有
个。于是
组合上的「或者」对应代数上的「加」。
笛卡尔积:相乘
设 是两类对象, 中的对象形如 ,其大小定义为两者之和。则大小为 的对象有
个:第一分量大小为 、第二分量大小为 ,对所有 求和。这正是多项式乘法的系数公式,所以
组合上的「先选一个、再选一个,大小相加」对应代数上的「乘」。
例三:两枚骰子的和
一枚六面骰(–)的生成函数是
一枚八面骰(–)的生成函数是
两枚骰子之和的分布就是乘积
看 的系数:它数的是满足 、、 的有序对
共 种。展开乘积确实给出 。
注意这里是有序对: 与 被算作两种,因为第一枚和第二枚骰子是不同的。
例四:找零
有限枚数。 有 枚 1 分、 枚 5 分、 枚 10 分,凑出 分有多少种方法?
每种硬币的选择对应一个因子:
第一个因子表示「用 到 枚 1 分」,第二个表示「用 或 枚 5 分」,第三个表示「用 、 或 枚 10 分」。展开后 的系数就是凑出 分的方法数。
看 的系数。能凑出 的组合只有两种:
所以答案是 。( 枚 1 分加 枚 5 分加 枚 10 分;或 枚 1 分加 枚 10 分。)
无限枚数。 如果 1 分、5 分、10 分都取之不尽,则每个因子变成无穷级数:
这里用了几何级数
于是「凑零钱」这个组合问题,被转化成了有理函数的系数提取问题。
用生成函数计算求和
目标
计算
第一步:写出已知的生成函数
由二项式定理,
第二步:求导
对两边求导,左边每一项 变成 :
第三步:代入
左边变成 ,右边变成 。于是
技巧总结: 要让生成函数的第 项乘上 ,就对它求导,再乘以 (若最终代入 ,乘以 这一步可省)。这个操作在后面处理期望与递推时会反复用到。
双重计数验证
同一个和也可以用组合意义算出来:数「从 个元素中选出一个 元子集,并在其中指定一个特殊元素」。
- 先选特殊元素: 种;
- 剩下的 个元素各自决定是否加入子集: 种。
所以总数是 。
两种方法结果一致,但生成函数的路线不需要想出一个巧妙的组合解释——只需要机械地求导、代入。这正是课程强调的优势:创意可以换成代数。
本节结论
- 凯莱定理: 个标号顶点的树有 棵。
- 证明方法是数「标号有根树 + 边序」两类方式: 与 ,相等即得结论。
- 生成函数 把序列编码成形式幂级数, 只起标记作用。
- ;有偏硬币对应 。
- 不交并 ⟹ 生成函数相加;笛卡尔积(大小相加)⟹ 生成函数相乘。
- 两枚骰子之和的分布是两者生成函数之积;有限枚硬币的找零问题对应多项式,无限枚对应有理函数 。
- 求导技巧:给第 项乘 相当于求导,于是 。
理解检验
1. 用凯莱公式验证 的情形,并说明 时路径型与星型各有多少种标号。
参考答案:公式给出 。 时:路径型 的标号方式为 (左右镜像被视为同一棵树,故除以 );星型(一个中心连三个叶子)只需选定中心,共 种。合计 ,与公式一致。
2. 在凯莱定理的第二种数法中,第 步为什么有 种选择?
参考答案:第 步开始时森林里有 棵树。先选挂载点: 个标号顶点中的任意一个,共 种。再选一棵要挂上去的树:不能选挂载点所在的那一棵(否则不构成加边),所以有 种。把该树的根接到挂载点上,这一步就唯一确定了。于是每步有 种, 从 到 连乘,得到 。
3. 为什么「逐个加边」的构造是可逆的?请描述逆过程。
参考答案:给定最终的标号有根树以及边的添加顺序,逆过程是:找到最后添加的那条边并剪断,于是得到两棵树——原来被挂上去的那棵(其根就是这条边的下端)和剩下的那棵。这就唯一还原了上一步的森林,以及所有更早的边。重复执行直到剩下 个孤立顶点。因为正向的每一步都由「选挂载点 + 选被挂的树」唯一确定,逆过程也唯一确定,所以这是双射。
4. 写出六面骰与八面骰之和的生成函数,并解释 的系数为什么是 。
参考答案:
的系数统计满足 、、 的有序对 :,共 个。乘积展开时, 项由所有「第一个因子取 、第二个因子取 」的乘法贡献,每个可行对恰好贡献一次,所以系数是 。
5. 用生成函数证明 ,并说明求导为什么能达到「乘 」的效果。
参考答案:由 ,两边求导得
令 ,右边为 ,左边为 ,故等式成立。求导之所以能乘 : 的导数是 ,每一项的指数系数被提到了前面;若需要保留 的形式,再乘以 即可(代入 时两者相同)。
6. 用 枚 1 分、 枚 5 分、 枚 10 分凑 分,有几种方法?写出对应的生成函数并说明理由。
参考答案:生成函数为
三个因子分别记录 1 分、5 分、10 分的枚数选择,展开后 的系数就是凑出 分的方法数。 的系数:可行组合只有 与 ,所以答案是 。
与前后课的关系
这一讲完成了计数单元的两条主线:双重计数(凯莱定理)与生成函数(把计数机械化)。生成函数目前还只是雏形——只用了加法、乘法与求导。下一讲会用它处理递推关系,尤其是斐波那契数列;再往后,卡特兰数的公式也会从一个二次方程里解出来,而不必再依赖那个漂亮的反射双射。
一句话概括:当计数问题能被写成「结构的组合」,它就能被写成「函数的代数」;此后只需算代数,不必再想组合。
Two questions, one method
This lecture handles two seemingly unrelated problems:
- How many labelled trees are there on vertices?
- How can a whole family of counting problems be turned into mechanical algebra?
The first answer is surprisingly clean: . The proof counts the same class of objects two ways — precisely the technique drilled in the previous lecture.
The second topic is generating functions. They pack a sequence into a power series, so that "combining structures" becomes "algebra": disjoint union becomes addition, Cartesian product becomes multiplication, and differentiation multiplies the -th term by .
Timeline
| Time | Topic |
|---|---|
| 0:00 | Cayley's tree theorem: counting labelled trees |
| 5:24 | Strategy: count rooted labelled trees, then those with ordered edges |
| 11:10 | Second count: build from a forest, adding one edge at a time |
| 25:52 | Equate the two counts and finish the proof |
| 29:03 | Definition of a generating function; is only a marker |
| 33:32 | Example one: binomial coefficients and |
| 36:24 | Example two: a biased coin and |
| 39:16 | Addition and multiplication of generating functions |
| 41:55 | The sum of a six-sided and an eight-sided die |
| 56:04 | Coin change: six pennies, one nickel, two dimes |
| 1:02:13 | The differentiation trick: computing |
Cayley's tree theorem
Statement and small cases
Theorem (Cayley). The number of trees on labelled vertices is .
Small values (labels ):
| number of trees | ||
|---|---|---|
For a hand check works: the path has labellings (dividing by for left-right symmetry), and the star with one centre has labellings (choose the centre), totalling .
For , the lecture sums the cases to , and the pattern becomes visible.
Strategy: upgrade the objects, then count twice
Counting labelled trees directly is hard. The strategy is to count an easier-to-construct class first: rooted labelled trees with an ordering on their edges.
Let be the number of labelled trees on vertices. Consider objects consisting of
First count. Start from a labelled tree: choose any root ( ways), then order the edges in any way ( ways). The total is
Second count. Build the same objects by construction: start from isolated vertices (a forest of one-vertex trees), and repeatedly attach one component to another.
At step () there are trees left:
- choose one of the vertices as the attachment point: ways;
- choose a different tree (not the one containing that vertex): ways;
- attach the root of that tree to the attachment point, labelling this the -st edge.
Each step reduces the number of trees by one; after steps a single tree remains, labelled and rooted, with its edges ordered by insertion. The total is
since appears times and runs from down to .
Why this is a bijection
The construction is reversible. Given a rooted labelled tree with ordered edges, look at the edge added last, cut it, and the two resulting components uniquely recover the previous forest together with all earlier structure. Repeating returns to the isolated vertices.
Because both directions are unique, the second count enumerates the same class. Hence
and cancelling gives
The elegance lies in not counting trees directly, but counting "tree + root + edge order". The extra structure makes construction easy, and it cancels at the end.
The lecture notes that one could instead build a bijection between labelled trees and sequences of length over (Prüfer sequences), but that bijection is complicated and unintuitive, so the double-counting route is taken instead.
Generating functions: turning a sequence into a function
Definition
Given a sequence , its generating function is the formal power series
Here is not a number and need not be assigned any value. Its only job is to record the index: is tied to , which keeps the -th and -th terms distinct.
Being formal, the series can be added, multiplied, differentiated and substituted without any convergence worries. Each algebraic operation then corresponds to a combinatorial one — that is the point.
Example one: binomial coefficients
Take (with for ). Then
This is the binomial theorem. Term by term: view as copies of multiplied together. To obtain one must take from exactly factors and from the rest — there are ways — so the coefficient of is .
The generating function encodes a choice problem as an algebraic expansion.
Example two: a biased coin
Flip a biased coin times, heads with probability and tails with probability . Let be the probability of exactly heads:
Its generating function is
again by the binomial theorem. Here only marks the number of heads and carries no probabilistic meaning.
Operations on generating functions
Disjoint union: add
Let be disjoint classes and . Stratified by size, the number of size- objects in is
so
Combinatorial "or" becomes algebraic addition.
Cartesian product: multiply
Let be two classes and let consist of pairs with size defined as the sum of the sizes. Then the number of size- objects is
counting first components of size paired with second components of size , summed over . This is exactly the coefficient rule for multiplying polynomials, so
Combinatorial "choose one, then another, sizes add" becomes algebraic multiplication.
Example three: the sum of two dice
A six-sided die (–) has generating function
and an eight-sided die (–) has
The distribution of the sum is the product
Look at the coefficient of : it counts ordered pairs with , , :
five in total. Expanding the product indeed gives .
The pairs are ordered: and count separately because the two dice are distinct.
Example four: making change
Finitely many coins. With pennies, nickel and dimes, in how many ways can cents be made?
Each coin type contributes a factor:
The first factor records "use to pennies", the second "use or nickel", the third "use , or dimes". After expansion, the coefficient of is the number of ways to make cents.
Consider the coefficient of . Only two combinations work:
So the answer is (six pennies, one nickel and one dime; or one penny and two dimes).
Unlimited coins. If pennies, nickels and dimes are unlimited, each factor becomes an infinite series:
This uses the geometric series
So "making change" becomes the problem of extracting coefficients from a rational function.
Using generating functions to evaluate a sum
The target
Compute
Step one: write the known generating function
By the binomial theorem,
Step two: differentiate
Differentiating term by term turns into :
Step three: substitute
The left side becomes and the right becomes . Hence
The trick: to multiply the -th term of a generating function by , differentiate, then multiply by (and if the final substitution is , the extra may be omitted). This operation recurs whenever expectations or recurrences are handled.
Double-counting check
The same sum can be computed combinatorially: count "choose a -element subset of elements and mark one special element inside it".
- Choose the special element: ways.
- Each of the remaining elements independently decides membership: ways.
The total is .
Both routes agree, but the generating-function route does not require inventing a clever combinatorial story — only differentiating and substituting. That is the advantage the lecture stresses: creativity can be traded for algebra.
What this lecture establishes
- Cayley's theorem: labelled vertices support trees.
- The proof counts "rooted labelled trees with ordered edges" two ways: and ; equating gives the result.
- A generating function encodes a sequence as a formal power series; is a marker.
- ; a biased coin corresponds to .
- Disjoint union adds generating functions; Cartesian product (sizes adding) multiplies them.
- The sum of two dice is the product of their generating functions; finite coin-change problems give polynomials, unlimited ones give .
- Differentiation multiplies the -th term by , giving .
Comprehension check
1. Check Cayley's formula at , and count the labelled path-type and star-type trees at .
Reference answer: The formula gives . For : the path has labellings (mirror images are the same tree, hence the division by ); the star, with one centre joined to three leaves, has labellings (choose the centre). Total , matching the formula.
2. In the second count for Cayley's theorem, why are there choices at step ?
Reference answer: At the start of step the forest has trees. First choose the attachment point among the labelled vertices: ways. Then choose a tree to attach: it cannot be the tree containing the attachment point (that would not add an edge), leaving choices. Attaching the chosen tree's root to the attachment point determines the step uniquely. Multiplying from to gives .
3. Why is the "add one edge at a time" construction reversible? Describe the inverse.
Reference answer: Given the final rooted labelled tree with its edge order, find the edge added last and cut it. This splits the tree into two components: the subtree that was attached (its root is the lower endpoint) and the remainder. This uniquely recovers the previous forest and all earlier edges. Iterating returns to isolated vertices. Since every forward step was determined by "attachment point + chosen tree", the inverse is unique as well, so the map is a bijection.
4. Write the generating function for the sum of a six-sided and an eight-sided die, and explain why the coefficient of is .
Reference answer:
The coefficient of counts ordered pairs with , , : , five pairs. On expansion, receives exactly one contribution from each feasible pair — first factor , second factor — so the coefficient is .
5. Use generating functions to prove , and explain why differentiation multiplies by .
Reference answer: From , differentiate both sides:
At the right side is and the left is . Differentiation multiplies by because the derivative of is , pulling the exponent down as a coefficient; multiplying by afterwards restores the power , which is unnecessary when substituting .
6. With pennies, nickel and dimes, how many ways are there to make cents? Write the generating function and justify it.
Reference answer:
The three factors record the choices of – pennies, – nickel and – dimes respectively; the coefficient of after expansion counts the ways to make cents. For the only working combinations are and , so the answer is .
Relation to neighbouring lectures
This lecture closes the counting unit along two lines: double counting (Cayley's theorem) and generating functions (mechanising counting). Generating functions are still in embryo — only addition, multiplication and differentiation have been used. The next lecture applies them to recurrences, especially Fibonacci numbers; later, the Catalan formula comes out of a quadratic equation, without needing the elegant reflection bijection.
One sentence to keep: once a counting problem can be written as a combination of structures, it can be written as the algebra of functions — afterwards one computes algebra instead of inventing combinatorics.