【问题标题】:solving the recurrence T(floor[n/2]) + T(ceil[n/2]) + n - 1求解递归 T(floor[n/2]) + T(ceil[n/2]) + n - 1
【发布时间】: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


【解决方案1】:

这是很久以前的事了,但是这里有

第 1 步我们丢弃了 ceil 和 floor。这是正确的吗?

我宁愿说

T(floor(n/2)) + T(floor[n/2)) <= T(floor(n/2)) + T(ceil[n/2)) 
T(floor(n/2)) + T(ceil[n/2)) <= T(ceil(n/2)) + T(ceil[n/2)) 

如果它们不相等,它们相差 1(您可以忽略任何常数)

第二个问题是如何去掉常数因子 1 ?

你忽略它。其背后的原因是:即使常数很大 10^100 与 n 变大时的大小相比也会很小。在现实生活中,您不能真正忽略非常大的常数,但这就是现实生活和理论的不同之处。无论如何,1 产生的差异最小。

最后用代入法证明比较好

你可以证明你喜欢什么,有些只是更简单。更简单通常更好,但除此之外,“更好”没有任何意义。所以我的答案是否定的。

【讨论】:

  • 您好,感谢您的回答,我确实使用了替代方法来检查我是否正确,看来我不是。我编辑了我的帖子,你能看一下吗?等一下,我一分钟再编辑一次。
猜你喜欢
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 2012-06-24
  • 2013-01-18
相关资源
最近更新 更多