-1

There is a recurrence relation as following:

T(n) = 2 T(n-1) + O(1) for n > 1 
otherwise, its T(n) = O(1)

By iteration , so far I got like,

T(n) = 2(2T(n-2) + 1) + 1 --- 1
T(n) = 2(2(2T(n-3) + 1) + 1) + 1 ---- 2
T(n) = 2(2(2(2T(n-4) + 1) + 1) + 1) + 1 ------3
T(n) = 2(2(2(2(2T(n-5) + 1) + 1) + 1) + 1) +1 ----- 4

I am not sure what to do next to find the upper bound time complexity. Could anyone please help me with this.

Dee
  • 483
  • 2
  • 11
  • 24
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders May 08 '15 at 07:28
  • Okay will keep in mind! – Dee May 08 '15 at 07:30
  • Your comments "otherwise, its T(n) = O(1)" is meaningless in the asymptotic framework. –  May 08 '15 at 09:45
  • The crux of this matter is that an asymptotic recurrence is not solved like a standard recurrence, as O(1) represents an unknown function, not a constant. –  May 08 '15 at 10:06
  • Can I please know why have I been voted down for this question? Thanks – Dee May 14 '15 at 05:49

2 Answers2

0

Look at step 4

T(n) = 2(2(2(2(2T(n-5) + 1) + 1) + 1) + 1) +1 ----- 4
T(n) = 2(2(2(2(2T(n-5))))) + 16 + 8 + 4 + 2 +1 = 
T(n) = 2^4* 2T(n-5) + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 =
T(n) = 2^4* 2T(n-5) + 2^5 -1 =

Similarly, if you do the same and develop one more time you get:

T(n) = 2^5 *2T(n-6) + 2^5 + 2^5-1
T(n) = 2^5 * 2T(n-6) + 2^6-1

By now we can understand that if we develop it until base of T(1), we get:

T(n) = .... = 2^(n) -1 

Note that this method is only giving intuition to solving the problem and IT IS NOT A PROOF.


To formally prove the claim you can use induction, and claim the hypothesis T(n) = 2^n -1:

Base: T(1) = 1 = 2^1 -1

Induction Hypothesis: For all k<n, T(k) = 2^k-1

Proof:

T(n) = 2T(n-1) +1 =(i.h.) 2* (2^(n-1) -1) + 1 = 2^n -2 + 1 = 2^n - 1

Remark: The T(1) base clause is actually C, and similarly T(n) = 2T(n-1)+C for some constant and not 1, but I use 1 for simplicity. The logic does not chagne at all when changing it to C.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Thanks so much for such a clear explanation! So now, the upper bound time complexity be O(2^n) according to the iteration solution 2^(n) -1 ? – Dee May 08 '15 at 07:27
  • @Dee yes. But remember that iteration method is NOT a formal proof. It's only good for intuition. – amit May 08 '15 at 07:31
  • Yes, but just wanted to try it using iteration (as I have never done before). So, that means the O(2^n) is the complexity for this function? – Dee May 08 '15 at 07:33
  • Also, can I please know, how did you get `2^5 -1` from `2^4 + 2^3 + 2^2 + 2^1 + 2^0` ? This was a bit unclear. – Dee May 08 '15 at 07:41
  • Sorry I understood that now :) – Dee May 08 '15 at 07:44
  • I disagree with the "Base case" T(1) = 1. We know nothing about T(1). All that we know is that asymptotically T(n) = 2T(n-1) + O(1). –  May 08 '15 at 09:24
  • @YvesDaoust `T(n) = 2 T(n-1) + O(1) for n > 1, otherwise, its T(n) = O(1)`. So, basically `T(1)` is in `O(1)`, or `T(1)=C`. If you wish to change it to any `C`, it is extremely easy and does not affect anything, basically, the same logic will apply in the same way. I will add for completeness a simple remark that we take `1` instead `C` for simplicirty but the same logic applies. – amit May 08 '15 at 09:46
  • No, "T(1) is in O(1)" is meaningless. T(1) is a constant, not a function, it cannot have an asymptotic behavior. You can write T(1)=1 and T(n)+2T(n-1)+1, which is an ordinary recurrence. But with the Big-O notation, things go quite differently. –  May 08 '15 at 09:49
  • @YvesDaoust The logic stays the same for changing 1 to C, it really doesn't matter, looking at it at 1 is simplification, but changing all the 1's to C's and applying the same logic changes nothing. – amit May 08 '15 at 09:51
  • You misunderstand: this is not about trading 1 for c. It is about using the definition of Big-O rigorously. –  May 08 '15 at 09:52
  • @YvesDaoust adding O(1) is not rigorous in the first place, it's a set, you cannot add a number to a set. This is a syntatic sugar in the first place. But meh. – amit May 08 '15 at 09:55
0

By the definition of O(1), we know that for some constants N and c, for all n >= N

T(n+1) <= 2 T(n) + c

So, recognizing the pattern of a geometric series,

T(N+1) <= 2 T(N) + c
T(N+2) <= 2 T(N+1) + c <= 4 T(N) + 2 c + c
T(N+3) <= 2 T(N+2) + c <= 8 T(N) + 4 c + 2 c + c
...
T(N+k) <= 2^k T(N) + (2^k-1) c

Then replacing N + k by n,

T(n) <= 2^(n-N) T(N) + (2^(n-N)-1) c <= 2^n (2^(-N) (T(N) + c))

which proves that T(n) is O(2^n).

  • `which proves that T(n) is O(2^n)`. This is not a proof. The claim `T(N+k) <= 2^k T(N) + (2^k-1) c` is not proven, the `...` part is NOT a proof. You need to use induction (for example) to prove it. – amit May 08 '15 at 09:52
  • I am sorry that you can't follow my argument. I suggest you to check http://en.wikipedia.org/wiki/Big_O_notation. –  May 08 '15 at 09:54
  • I am very much aware of the definition, and it had NOTHING to do with it. You failed to prove the claim `T(N+k) <= 2^k T(N) + (2^k-1) c`, which you later base your prove on an unproven claim. It's fun as intuition, but it's NOT a proof. – amit May 08 '15 at 09:56
  • I added a justification which is quite sufficient for a proof (except bad faith). –  May 08 '15 at 09:59