简体   繁体   中英

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
  • the cost of the last level : 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)

So, T(n) = 5n - 3 which belong to 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. 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. Either the latter is a typo or you're off by one with the height. (and of course, what Saeed Amiri said.)

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):

assume you have:

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

  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).
  2. f(n) = Theta(n log b a * log k n) then T(n)=Theta(n log b a * log k+1 n).
  3. Otherwise is Theta(f(n)). (see detail of constraint in this case in CLRS or wiki, ...)

for detail see wiki.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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