问题
What is the time complexity of the recurrence T(n) = 2T(n-1) + 4 ?
I'm having serious problems with this. I tried:
T(n) = 2T(n-1)+4 = 2(2T(n-2)+4)+4 = 4T(n-2)+12= 4(2T(n-3)+4)+4 = 8T(n-3)+20 = 8(2T(n-4)+4)+4 = 16T(n-4)+36 =…
T(n) = 2^kT(n-k) + (4+2^(k+1))
so it looks like T(n) = 2^n + (4+2^(n+1)) but it doesn't seem right... please help :(
回答1:
Your computation are wrong. I'm assuming here that T(0)=0
T(n) = 2T(n-1)+ 4
= 2(2T(n-2)+4)+ 4 = 4T(n-2)+ 12
= 4(2T(n-3)+4)+ 12 = 8T(n-3)+ 28
= 8(2T(n-4)+4)+ 28 = 16T(n-4)+ 60
= 16(2T(n-5)+4)+ 60 = 32T(n-5)+124
= 32(2T(n-6)+4)+124 = 64T(n-6)+252
Then now: look at the sequence
0,4,12,28,60,124,252,508,1020,2044,...
It is very tempting to add 4 to all these numbers:
4,8,16,32,64,128,256,512,1024,2048,...
Do you recognize it ? So the guess is clearly
T(n) = 2^(n+2) - 4
Now, you can easily prove it by induction.
By the way if T(0) is not equal to 0 the formula is
T(n) = 2^(n+2) - 4 + T(0)*2^n
回答2:
Solving the recurrence relation, I found this:
回答3:
T(n) = 2T(n-1) + 1
Let n = log m then, we get T(log m)=2T(log m - log 2) + 1
Assume S(a) = 2S(a/2) + 1
Applying Master's theorem we get:
n^logb^a = n^log2^2 = n.
n^log2^2-1 = 1, here € = 1
By first case we get: S(a) = O(a)
therefore T(n) = O(2^n)
来源:https://stackoverflow.com/questions/22741590/what-is-the-time-complexity-of-the-recurrence-tn-2tn-1-4