简体   繁体   English

递归W(n)= 2W(floor(n / 2))+ 3

[英]The Recurrence W(n)= 2W(floor(n/2)) + 3

I have this recurrence: 我有这种情况:

W(n)= 2W(floor(n/2)) + 3
W(2)=2

My try is as follow: 我的尝试如下:

the tree is like this: 树是这样的:

W(n) = 2W(floor(n/2)) + 3
W(n/2) = 2W(floor(n/4)) + 3
W(n/4) = 2W(floor(n/8)) + 3 
...
  • the hight of the tree : I assume its lgn because the tree has 2 branches at every expanding process, not sure though :S 树的高度:我假设它的lgn因为树在每个扩展过程中都有2个分支,但不确定:S
  • the cost of the last level : 2^lgn * W(2) = 2n 最后一级的成本:2 ^ lgn * W(2)= 2n
  • the cost of all levels until level h-1 : 3 * sigma from 0 to lgn-1 of (2^i), which is a geometric series = 3 (n-1) 所有等级的成本直到等级h-1:3 * sigma从0到lgn-1(2 ^ i),这是一个几何序列= 3(n-1)

So, T(n) = 5n - 3 which belong to Theta(n) 那么,T(n)= 5n - 3属于Theta(n)

my question is: Is that right? 我的问题是:是吗?

Well, if you calculate W(4) , you find W(4) = 2*W(2) + 3 = 2*2 + 3 = 7 , but 5*4 - 3 = 17 , so your result for T(n) is not correct. 那么,如果你计算W(4) ,你会发现W(4) = 2*W(2) + 3 = 2*2 + 3 = 7 ,但是5*4 - 3 = 17 ,所以你的结果是T(n)不正确。 It is close, though, there's just a minor slip in your reasoning (or possibly in a certain other place). 然而,它很接近,你的推理(或者可能在某个其他地方)只是一个小的滑动。

Edit: To be specific, your calculation would work if W(1) was given, but it's W(2) in the question. 编辑:具体来说,如果给出了W(1) ,你的计算就会起作用,但在问题中它是W(2) Either the latter is a typo or you're off by one with the height. 要么是后者是一个错字,要么是一个身高的人。 (and of course, what Saeed Amiri said.) (当然,Saeed Amiri说过。)

I don't think it's exactly 5n-3 except n is 2 t , but your theta is right if you look at Master Theorem , there is no need to calculate it (but its good for startup): 我认为它不是5n-3除非n是2 t ,但是你的θ是正确的,如果你看一下Master定理 ,没有必要计算它(但它对启动有好处):

assume you have: 假设你有:

T(n) = aT(n/b) + f(n), where a>=1, b>1 then: T(n)= aT(n / b)+ f(n),其中a> = 1,b> 1则:

  1. if f(n) = n log b a-eps for any eps > 0 then T(n) = n log b a like your case, in which a=b=2, f(n) = O(1). 如果f(n)= n log b a-eps对于任何eps> 0那么T(n)= n log b a就像你的情况一样,其中a = b = 2,f(n)= O(1)。
  2. f(n) = Theta(n log b a * log k n) then T(n)=Theta(n log b a * log k+1 n). f(n)= Theta(n log b a * log k n)然后T(n)= Theta(n log b a * log k + 1 n)。
  3. Otherwise is Theta(f(n)). 否则是Theta(f(n))。 (see detail of constraint in this case in CLRS or wiki, ...) (在CLRS或wiki中查看本例中的约束细节,......)

for detail see wiki. 详情请见维基。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM