返回题库

子树移除

Subtree Removal

专题
Probability / 概率
难度
L3

题目详情

给定一棵有根树。每一步中,你等概率随机选择当前树中的一个节点,并删除以该节点为根的整棵子树(包含该节点)。重复直到只剩下根节点。求期望需要多少步。

You are given a rooted tree. On each step, you randomly choose a node and remove the subtree rooted by that node, and the node itself, until all subtrees have been removed and only the root remains. Find the expected number of steps in this process.

解析

设整棵树(含根)当前节点数为 nn。对任意节点 ii,要让它被删除,必须在某一步选到它自己或它的某个祖先。

Depth(i)\mathrm{Depth}(i) 为节点 ii 的祖先个数(不含自身),则“选到祖先链上的任一点”这个事件的大小为 Depth(i)+1\mathrm{Depth}(i)+1 个节点,因此

Pr(i 在下一步被删除)=Depth(i)+1n.\Pr(i\text{ 在下一步被删除})=\frac{\mathrm{Depth}(i)+1}{n}.

用指示变量 XiX_i 表示“节点 ii 在某一步中是被选中的节点(从而触发一次删除)”。总步数为

X=X1+X2++Xn.X=X_1+X_2+\cdots+X_n.

在给定节点 ii 最终会被删除的前提下,ii 自己成为触发删除的被选节点的条件概率为

Pr(i 被选中i 被删除)=1Depth(i)+1.\Pr(i\text{ 被选中}\mid i\text{ 被删除})=\frac{1}{\mathrm{Depth}(i)+1}.

由期望线性性得到

E[X]=iE[Xi]=i1Depth(i)+1.\mathbb{E}[X]=\sum_{i}\mathbb{E}[X_i]=\sum_{i}\frac{1}{\mathrm{Depth}(i)+1}.

Original Explanation

Every node in the tree has an equal probability of being selected. If a node is to be removed, we know that either the node itself, or one of the node's ancestors must have been chosen.

The number of ancestors of a node ii is Depth[i]\textrm{Depth}[i]

P(i is removed)=Depth[i] / Size of Tree\begin{equation} P(i \ \textrm{is removed}) = \textrm{Depth}[i] \ / \ \textrm{Size of Tree} \end{equation} P(i is chosen)=1 / Size of Tree\begin{equation} P(i \ \textrm{is chosen}) = 1 \ / \ \textrm{Size of Tree} \end{equation} P(i is removed  i is chosen)=1\begin{equation} P(i \ \textrm{is removed} \ | \ i \textrm{ is chosen} ) =1 \end{equation}

We can use Bayes theorem to solve this problem where: P(AB)=P(BA)P(A)P(B)P(A|B) = \frac{P(B | A) * P(A)}{P(B)} and therefore P(i is chosen  i is removed)=1/Depth[i]P(i\ \textrm{is chosen} \ |\ i\ \textrm{is removed}) = 1 / \textrm{Depth}[i]

We now use Linearity of Expectation to find the expected number of subtree removals. Define indicator variable Xi=1X_i = 1 if ii node was chosen.

X=X1+X2XnX = X_1 + X_2 … X_n E[X]=i=1nE[Xi]E[X] = \sum_{i=1}^n E[X_i] E[X]=1P(Xi was chosen i was removed)E[X] = \sum 1* P(X_i \text{ was chosen } | i \text{ was removed}) E[X]=i=1n1Depth[i]E[X] = \sum_{i=1}^n \frac{1}{\text{Depth}[i]}