東京大学 新領域創成科学研究科 メディカル情報生命専攻 2015年8月実施 問題7
Author
zephyr
Description
自然数 n(≥1) の大きさの入力データを処理するアルゴリズムの最悪計算時間を T(n)、実数 x 以下の最大の整数を ⌊x⌋ と記述する。
c≥1 を定数とする。
T(n) が T(1)=c および n>1 のとき以下の再帰的定義をみたす場合を各々考える。
- (1) T(n)=T(⌊3n/4⌋)+cn
- (2) T(n)=2T(n−1)+cn
- (3) T(n)=T(n−1)+c(n2+n)
- (4) T(n)=T(⌊n/2⌋)+c
- (5) T(n)=2T(⌊n/2⌋)+cn
各再帰的定義を満たす T(n) が属する最小のクラスを以下の計算量クラスから選び、その証明を示せ。
O(1),O(logn),O(n),O(nlogn),O(n2),O(n3),O(3n)
Let T(n) denote the worst-case running time of an algorithm that processes input data of size n(≥1). Let ⌊x⌋ be the largest integer that is equal to or smaller than real number x. Let c(≥1) be a constant number. Suppose that T(n) meets T(1)=c and each of the following recurrences for n>1:
- (1) T(n)=T(⌊3n/4⌋)+cn
- (2) T(n)=2T(n−1)+cn
- (3) T(n)=T(n−1)+c(n2+n)
- (4) T(n)=T(⌊n/2⌋)+c
- (5) T(n)=2T(⌊n/2⌋)+cn
From the following complexity classes, select the smallest class for T(n) that satisfies each of the above recurrences, and prove the property:
O(1),O(logn),O(n),O(nlogn),O(n2),O(n3),O(3n)
Kai
(1)
For this recurrence, we use the Master Theorem. The recurrence is of the form T(n)=aT(n/b)+f(n).
- Here, a=1, b=34, and f(n)=cn.
- We need to compare f(n) with nlogba.
First, compute logba:
logba=log4/31=0
According to the Master Theorem:
- If f(n)=O(nc) where c<logba, then T(n)=Θ(nlogba).
- If f(n)=Θ(nlogba), then T(n)=Θ(nlogbalogn).
- If f(n)=Ω(nc) where c>logba and af(n/b)≤kf(n) for some k<1 and sufficiently large n, then T(n)=Θ(f(n)).
In this case, f(n)=cn=Θ(n) and logba=0.
Since c>logba, we have T(n)=Θ(n).
Thus, T(n)∈O(n).
(2)
To solve this recurrence, we can use the iterative method:
T(n)=2T(n−1)+cn=2[2T(n−2)+c(n−1)]+cn=22T(n−2)+2c(n−1)+cn=22[2T(n−3)+c(n−2)]+2c(n−1)+cn=23T(n−3)+22c(n−2)+2c(n−1)+cn⋮=2kT(n−k)+ci=0∑k−12i(n−i)
When k=n−1:
T(n)=2n−1T(1)+ci=0∑n−22i(n−i)=2n−1c+ci=0∑n−22i(n−i)=2n−1c+c(i=1∑n−12i(n−i+1)−i=0∑n−22i(n−i))=2n−1c+c(2n+i=1∑n−22i(n−i+1)−i=1∑n−22i(n−i)−n)=2n−1c+c(2n−n+i=1∑n−22i)
The sum ∑i=1n−22i is a geometric series:
i=1∑n−22i=2n−1−2
Thus, T(n) can be approximated by:
T(n)=Θ(2n)=O(3n)
Thus, T(n)∈O(3n).
(3)
This is a non-homogeneous linear recurrence relation. We can use the iterative method:
T(n)=T(n−1)+c(n2+n)=T(n−2)+c((n−1)2+(n−1))+c(n2+n)=T(n−3)+c((n−2)2+(n−2))+c((n−1)2+(n−1))+c(n2+n)⋮=T(1)+ck=1∑n−1(k2+k)=c+ck=1∑n−1(k2+k)
The sum ∑k=1n−1k2 is:
k=1∑n−1k2=6(n−1)n(2n−1)
And the sum ∑k=1n−1k is:
k=1∑n−1k=2(n−1)n
Therefore:
T(n)=c+c(6(n−1)n(2n−1)+2(n−1)n)=c+c(6(n−1)n(2n−1+3))=c+c(6(n−1)n(2n+2))=c+3c(n−1)n(n+1)=Θ(n3)
Thus, T(n)∈O(n3).
(4)
For this recurrence, we again use the Master Theorem. The recurrence is of the form T(n)=aT(n/b)+f(n).
- Here, a=1, b=2, and f(n)=c.
- We need to compare f(n) with nlogba.
First, compute logba:
logba=log21=0
According to the Master Theorem:
- If f(n)=O(nc) where c<logba, then T(n)=Θ(nlogba).
- If f(n)=Θ(nlogba), then T(n)=Θ(nlogbalogn).
- If f(n)=Ω(nc) where c>logba and af(n/b)≤kf(n) for some k<1 and sufficiently large n, then T(n)=Θ(f(n)).
In this case, f(n)=c=Θ(1) and logba=0.
Since f(n) is Θ(n0):
T(n)=Θ(logn)
Thus, T(n)∈O(logn).
(5)
For this recurrence, we use the Master Theorem. The recurrence is of the form T(n)=aT(n/b)+f(n).
- Here, a=2, b=2, and f(n)=cn.
- We need to compare f(n) with nlogba.
First, compute logba:
logba=log22=1
According to the Master Theorem:
- If f(n)=O(nc) where c<logba, then T(n)=Θ(nlogba).
- If f(n)=Θ(nlogba), then T(n)=Θ(nlogbalogn).
- If f(n)=Ω(nc) where c>logba and af(n/b)≤kf(n) for some k<1 and sufficiently large n, then T(n)=Θ(f(n)).
In this case, f(n)=cn=Θ(n) and logba=1.
Since f(n)=Θ(nlogba), we have:
T(n)=Θ(nlogn)
Thus, T(n)∈O(nlogn).
Summary
- Recurrence 1: T(n)=Θ(n), T(n)∈O(n).
- Recurrence 2: T(n)=Θ(2n), T(n)∈O(2n).
- Recurrence 3: T(n)=Θ(n3), T(n)∈O(n3).
- Recurrence 4: T(n)=Θ(logn), T(n)∈O(logn).
- Recurrence 5: T(n)=Θ(nlogn), T(n)∈O(nlogn).
Knowledge
主定理 递归 复杂度分析
解题技巧和信息
对于递归方程的时间复杂度分析,使用主定理是一个强大的工具。主定理适用于形如 T(n)=aT(n/b)+f(n) 的递归式。需要注意 a,b,f(n) 的取值以及 f(n) 和 nlogba 的比较来判断最终的时间复杂度。对于不能用主定理的递归式,可以使用展开迭代法逐步分析。
重点词汇
- Recurrence Relation 递归关系
- Master Theorem 主定理
- Time Complexity 时间复杂度
- Logarithm 对数
- Big O Notation 大 O 符号
参考资料
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, 3rd Edition. MIT Press, Chapter 4.