跳到主要内容

東京大学 情報理工学系研究科 コンピュータ科学専攻 2017年8月実施 専門科目II 問題3

Author

祭音Myyura (co-authored with GPT 5.6 SOL)

Description

Let L(v)L(v) denote the set of leaves in the descendants of node vv in a tree, and p(v,w)p(v,w) denote the number of edges of the simple path from node vv to node ww. For a non-leaf node vv, maxwL(v)p(v,w)\max_{w\in L(v)}p(v,w) is called the height of vv. Let the height of a leaf be 00. The height of the root of a tree is called the height of the tree.

Here, we have a binary tree TnT_n with height n0n\ge0, in which each node vv must have one of the following properties.

  • vv is a leaf.
  • vv has only one child, and the height of vv is 11.
  • vv has two children, and the heights of the two children of vv differ by 11.

Let NnN_n denote the number of nodes in TnT_n for n0n\ge0. Let r=(1+5)/2r=(1+\sqrt5)/2.

Answer the following questions.

(1) Calculate N5N_5.

(2) Express NnN_n in terms of Nn1N_{n-1} and Nn2N_{n-2} for n2n\ge2.

(3) Prove that NnrnN_n\ge r^n for every n0n\ge0.

(4) Prove that Nnrn+2N_n\le r^{n+2} for every n0n\ge0.

(5) Consider the problem of assigning each of given NnN_n integers to a distinct node of TnT_n. The integer assigned to each node vv must be no smaller than any of the integers assigned to vv's children. Show an O(rn)O(r^n) algorithm that computes such an assignment, with a proof that the algorithm runs indeed in O(rn)O(r^n) time. Note that the NnN_n integers may not be sorted in the input.

题目描述

树中结点 vv 的高度为它到后代叶结点的简单路径的最大边数,叶结点高度为 00,树的高度是根结点的高度。给定高度为 n0n\ge0 的二叉树 TnT_n,每个结点满足以下一种情形:

  • 是叶结点;
  • 只有一个孩子,且自身高度为 11
  • 有两个孩子,且两棵子树的高度相差 11

NnN_nTnT_n 的结点数,r=(1+5)/2r=(1+\sqrt5)/2

(1)求 N5N_5

(2)对 n2n\ge2,用 Nn1,Nn2N_{n-1},N_{n-2} 表示 NnN_n

(3)证明 NnrnN_n\ge r^n

(4)证明 Nnrn+2N_n\le r^{n+2}

(5)将给定的 NnN_n 个未排序整数一一放到 TnT_n 的结点上,使每个父结点的整数不小于任一孩子。给出 O(rn)O(r^n) 算法并证明复杂度。

Kai

(1)与(2)

N0=1,N1=2N_0=1,N_1=2。当 n2n\ge2 时,根的两棵子树高度分别为 n1,n2n-1,n-2,所以

Nn=Nn1+Nn2+1.N_n=N_{n-1}+N_{n-2}+1.

于是

N2=4,N3=7,N4=12,N5=20.N_2=4,\quad N_3=7,\quad N_4=12,\quad \boxed{N_5=20}.

等价地,若 Fibonacci 数满足 F0=0,F1=1F_0=0,F_1=1,则 Nn=Fn+31N_n=F_{n+3}-1

(3)

r2=r+1r^2=r+1rn=rn1+rn2r^n=r^{n-1}+r^{n-2}。基例 N0=1=r0N_0=1=r^0N1=2>rN_1=2>r 成立。若结论对 n1,n2n-1,n-2 成立,则

Nn=Nn1+Nn2+1rn1+rn2=rn.N_n=N_{n-1}+N_{n-2}+1 \ge r^{n-1}+r^{n-2}=r^n.

故结论由归纳法成立。

(4)

Mn=Nn+1M_n=N_n+1,则 Mn=Mn1+Mn2M_n=M_{n-1}+M_{n-2},且 M0=2r2M_0=2\le r^2M1=3r3M_1=3\le r^3。若结论对前两项成立,则

Mnrn+1+rn=rn+2.M_n\le r^{n+1}+r^n=r^{n+2}.

因此 Nn<Mnrn+2N_n<M_n\le r^{n+2}

(5)

先任意放置所有整数,再按后序遍历处理结点。处理结点 vv 时,若较大的孩子值大于 vv 的值,就交换二者并继续沿该孩子向下筛。此时两棵孩子子树已经分别满足最大堆序,故下筛完成后以 vv 为根的整棵子树也满足要求。

高度为 nn 的根结点下筛耗时 O(n)O(n)。设总耗时为 CnC_n,则

Cn=Cn1+Cn2+O(n).C_n=C_{n-1}+C_{n-2}+O(n).

按 Fibonacci 递推展开,并用 Fk=O(rk)F_k=O(r^k),有

Cn=O ⁣(Fn+k=2nkFnk+1)=O ⁣(rn(1+k=2nkrk1))=O(rn).C_n =O\!\left(F_n+\sum_{k=2}^n kF_{n-k+1}\right) =O\!\left(r^n\left(1+\sum_{k=2}^n\frac{k}{r^{k-1}}\right)\right) =O(r^n).

初始放置和后序遍历也只需 O(Nn)=O(rn)O(N_n)=O(r^n),故总复杂度为 O(rn)O(r^n)