【发布时间】:2015-03-18 15:06:01
【问题描述】:
我有以下复发:
T(n) = c for n = 1.
T(n) = T(floor[n/2]) + T(ceil[n/2]) + n - 1 for n > 1.
对我来说它看起来像合并排序,所以我猜递归的解决方案是 Θ(nlogn)。根据我的主方法:
a) Θ(1) for n = 1 (constant time).
b) If we drop the floor and ceil we have: (step1)
T(N) = 2T(N/2) + n - 1 => a = 2, b = 2.
logb(a) (base b) = lg(2) = 1 so n^lg(2) = n^1 = n
仔细一看,我们知道我们有master方法的案例2:
if f(n) = Θ(log(b)a) our solution to the recurrence is T(n) = Θ(log(b)a log(2)n)
解决方案确实是 T(n) = Θ(nlogn) 但我们偏离了我的常数因子 1。 我的第一个问题是: 在第 1 步,我们放弃了 ceil 和 floor。它是否正确 ?第二个问题是我如何摆脱常数因子 1 ?我放弃了吗?或者我应该将它命名为 d 并证明 n - 1 确实是 n (如果是,我如何证明它?)。最后用替换方法证明它更好吗?
编辑:如果我们使用我们得到的替换方法:
We guess that the solution is O(n). We need to show that T(n) <= cn.
Substitutting in the recurrence we obtein
T(n) <= c(floor[n/2]) + c(ceil[n/2]) + n/2 - 1 = cn + n/2 - 1
所以它不是归并排序?我错过了什么?
【问题讨论】:
-
你错过的是 cn + n/2 - 1 = (c+1/2)n - 1 不小于 cn 对于 n 大。
-
谢谢你。所以确实是nlogn?
标签: algorithm big-o time-complexity recurrence