東京大学 新領域創成科学研究科 メディカル情報生命専攻 2024年1月実施 問題7
Author
水月, 祭音Myyura, zephyr
Description
入力データサイズ n に対して計算時間 T(n) を考える。
以下の再帰方程式 (1)~(3) それぞれについて T(n) の計算量を n の式で表せ。
必要に応じて O() などのランダウ記法を用いてもよい。
ただし、T(0)=1 とし、⌊x⌋ は x を超えない最大の整数を表す。
(1) T(n)=T(n−1)+2n
(2) T(n)=T(⌊n/3⌋)+1
(3) T(n)=2T(⌊n/2⌋)+n+1
以下の (4) の命題は成り立つか?最初に真偽を述べ、それが正しいことを証明せよ。
(4) T(n)∈O(2n) と T(n)∈O(en) は同値である。
以下の (5) の再帰方程式が成り立つとき、T(n) の計算量を n の式で表せ。ただし、a は正の定数である。
(5) T(n)=aT(⌊n/2⌋)+⌊nlog2n⌋
Let T(n) be the computation time in terms of the input size n. For each of the recurrences (1)-(3) shown below, write the complexity of T(n) in terms of n. You may use Landau notations such as O() if need be. Assume that T(0)=1. Note that ⌊x⌋ is the maximum integer that is not larger than x.
(1) T(n)=T(n−1)+2n
(2) T(n)=T(⌊n/3⌋)+1
(3) T(n)=2T(⌊n/2⌋)+n+1
Does the following proposition (4) hold true? State true or false first and prove it.
(4) T(n)∈O(2n) if and only if T(n)∈O(en).
Given the following recurrence (5), write the complexity of T(n) in terms of n. a is a positive constant.
(5) T(n)=aT(⌊n/2⌋)+⌊nlog2n⌋
设 T(n) 为输入大小 n 的计算时间。对于下列递推关系(1)-(3),写出 T(n) 关于 n 的复杂度。若有必要,可以使用朗道符号如 O()。假设 T(0)=1。注意,⌊x⌋ 是小于等于 x 的最大整数。
(1) T(n)=T(n−1)+2n
(2) T(n)=T(⌊n/3⌋)+1
(3) T(n)=2T(⌊n/2⌋)+n+1
以下命题(4)是否成立?首先陈述真伪并证明。
(4) T(n)∈O(2n) 当且仅当 T(n)∈O(en)。
给定下列递推关系(5),写出 T(n) 关于 n 的复杂度。a 是一个正常数。
(5) T(n)=aT(⌊n/2⌋)+⌊nlog2n⌋
题目描述
设 T(n) 为输入规模 n 对应的运行时间,规定 T(0)=1,且 ⌊x⌋ 表示不超过 x 的最大整数。可按需使用大 O 等 Landau 记号。
- 分别求下列递推的 T(n) 复杂度:
(1)(2)(3)T(n)=T(n−1)+2n,T(n)=T(⌊n/3⌋)+1,T(n)=2T(⌊n/2⌋)+n+1.
- 先判断下列命题真伪,再证明:
T(n)∈O(2n)⟺T(n)∈O(en).
- 对正常数 a,求递推
T(n)=aT(⌊n/2⌋)+⌊nlog2n⌋
的复杂度;应根据 a 与分治临界值的关系处理必要的不同情形。
- 递推关系求解:用求和、递归展开和递归树分别处理逐一递减、按三分之一缩小及二分支递推。
- 主定理:比较 aT(n/2) 中的子问题总贡献与 nlogn 的非递归工作,按参数 a 给出分段渐近界。
- 指数复杂度类比较:严格区分底数为 2 与 e 的指数增长率,通过定义判断两个大 O 集合是否等价。
Kai
Written by 水月.
(1)
T(n)=T(n−1)+2n=2n+2(n−1)+2(n−2)+⋯+2=O(n2)
(2)
T(n)=T(⌊n/3⌋)+1=O(logn)
(3)
Note that
nT(n)=n/2T(⌊n/2⌋)+1+n1
hence
2kT(2k)=T(1)+k+O(1)
∴T(2k)∈Θ(k2k)
that is
T(n)∈O(nlogn)
(4)
The statement is False.
Counter example: T(n)=2.5n.
(5)
See Generic form of Master Theorem
T(n)=O(nlog2n)T(n)=O(nlog22n)T(n)=O(nlog2a)if a<2if a=2if a>2
Kai
Written by zephyr.
解题思路
这道题目主要涉及递归方程的复杂度分析和渐进符号的性质。我们需要运用主定理、迭代法等技巧来解决递归方程,并理解大 O 符号的性质来证明命题。对于最后一个问题,我们需要根据递归方程的形式来判断使用哪种方法求解。
1. T(n)=T(n−1)+2n
To solve this recurrence, we can use the iteration method:
T(n)=T(n−1)+2n
=[T(n−2)+2(n−1)]+2n
=[T(n−3)+2(n−2)]+2(n−1)+2n
=⋯
=T(0)+2[1+2+⋯+(n−1)+n]
=1+2[2n(n+1)]
=1+n(n+1)
=n2+n+1
Therefore, T(n)=O(n2).
2. T(n)=T(⌊n/3⌋)+1
This recurrence fits the form of the Master Theorem with a=1, b=3, and f(n)=1.
Since f(n)=O(nlog31−ϵ) for ϵ>0, we are in case 1 of the Master Theorem.
Therefore, T(n)=Θ(nlog31)=Θ(logn).
3. T(n)=2T(⌊n/2⌋)+n+1
This recurrence also fits the form of the Master Theorem with a=2, b=2, and f(n)=n+1.
Since f(n)=Θ(nlog22)=Θ(n), we are in case 2 of the Master Theorem.
Therefore, T(n)=Θ(nlogn).
4. T(n)∈O(2n) if and only if T(n)∈O(en)
This proposition is false. Let's examine both directions:
Part 1: If T(n)∈O(2n), then T(n)∈O(en)
This direction is true.
Proof: Assume T(n)∈O(2n). By definition, ∃c>0 and n0>0 such that T(n)≤c2n for all n≥n0.
We know that 2n=(eln2)n=enln2.
Since ln2<1, we have enln2<en for all n>0.
Therefore, T(n)≤c2n=cenln2<cen for all n≥n0.
This means T(n)∈O(en).
Part 2: If T(n)∈O(en), then T(n)∈O(2n)
This direction is false.
Consider the function T(n)=en∈O(en).
limn→∞2nen=limn→∞(2e)n=∞
This means that for any constant c, there will always be some n where en>c2n.
Therefore, the statement "if T(n)∈O(en), then T(n)∈O(2n)" is false.
Conclusion
The original proposition is false because while O(2n)⊂O(en), the reverse inclusion does not hold. There are functions in O(en) that grow faster than any function in O(2n).
A correct statement would be: If T(n)∈O(2n), then T(n)∈O(en), but the converse is not necessarily true.
5. T(n)=aT(⌊n/2⌋)+⌊nlog2n⌋
Let's analyze this recurrence for different cases of a. We'll ignore the floor functions for the asymptotic analysis as they don't affect the overall complexity.
Case 1: a<2
In this case, we can use a variant of the Master Theorem. We have:
T(n)=aT(n/2)+nlogn
Here, f(n)=nlogn. We need to compare nlog2a with f(n)=nlogn.
Since a<2, we have nlog2a<n. Therefore, f(n)=nlogn is asymptotically larger.
Thus, T(n)=Θ(nlogn) when a<2.
Case 2: a=2
When a=2, we have:
T(n)=2T(n/2)+nlogn
This case doesn't fit directly into the Master Theorem. Let's solve it by substitution:
T(n)=2T(n/2)+nlogn =2[2T(n/4)+(n/2)log(n/2)]+nlogn =4T(n/4)+nlog(n/2)+nlogn =4T(n/4)+nlogn+nlogn−n =4T(n/4)+2nlogn−n
Continuing this process, we get:
T(n)=nT(1)+nlogn∑i=1logn2−n∑i=1logn1 =nT(1)+2nlog2n−nlogn
Therefore, when a=2, T(n)=Θ(nlog2n).
Case 3: 2<a<4
In this case, we again compare nlog2a with nlogn.
Since 2<a<4, we have 1<log2a<2.
This means n<nlog2a<n2, but nlogn is asymptotically smaller than nlog2a.
Therefore, when 2<a<4, T(n)=Θ(nlog2a).
Case 4: a≥4
When a≥4, we have log2a≥2.
In this case, nlog2a≥n2, which is asymptotically larger than nlogn.
Therefore, when a≥4, T(n)=Θ(nlog2a).
Summary
- For 0<a<2: T(n)=Θ(nlogn)
- For a=2: T(n)=Θ(nlog2n)
- For a≥2: T(n)=Θ(nlog2a)
Knowledge
难点思路
第 4 小题的证明和第 5 小题的复杂度分析较为困难。对于第 4 小题,关键是理解指数函数的性质和大 O 符号的定义。对于第 5 小题,由于不能直接应用主定理,需要通过猜测和归纳证明的方法来解决。
解题技巧和信息
- 对于简单的递归方程,可以尝试使用迭代法直接求解。
- 对于符合形式的递归方程,优先考虑使用主定理。
- 当递归方程不能直接应用主定理时,可以尝试猜测复杂度并使用归纳法证明。
- 在处理渐进符号时,要注意指数函数、对数函数等的性质。
常见算法复杂度排序(从低到高):
O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(2n)<O(n!)
重点词汇
- recurrence relation 递归关系
- Master Theorem 主定理
- iteration method 迭代法
- induction proof 归纳证明
- asymptotic notation 渐进符号
- floor function 下取整函数
参考资料
- Introduction to Algorithms (CLRS), Chapter 4: Divide-and-Conquer
- Algorithm Design (Kleinberg & Tardos), Chapter 5: Divide and Conquer
- The Art of Computer Programming (Knuth), Volume 1: Fundamental Algorithms, Section 1.2.11: Asymptotic Representations