0

Consider the recurrence equation

T(n) = 2T(n-1), if n>0
     = 1, otherwise

Then T(n) is (in big O order)

(A) O(n)
(B) O(2^n)
(C) O(1)
(D) O(log n)

My Try:

This has already asked here but there is no explanation. Using Substitution Method: $$T(n) = 2T(n−1)$$ $$= 2(2T(n−2)) = 2^2T(n-2)$$ $$= 2(2^2T(n−2)) = 2^3T(n-3)$$ $$....$$ $$= 2(2^{n-3}T(n−(n-2))) = 2^{n-2}T(n-(n-2))$$ $$= 2(2^{n-2}T(n−(n-1))) = 2^{n-1}T(n-(n-1)) = 2^{n-1}T(1) $$ $$= 2(2^{n-1}T(n−(n))) = 2^{n}T(n-(n)) = 2^{n}T(0) $$ $$T(n) = O(2^n)$$

This was from ISRO 2017 exam and official answer key is $O(1)$.

So, my question is that if there is two function calls with each $(n-1)$ each time, then how it can be $O(1)$?


There is another recurrence relation with little modification:

 T(n) = 2T(n-1) - 1, if n>0
      = 1, otherwise

AFAIK, it should be $O(2^n)$ again, but someone it explained as:

$$T(0) = 1 $$ $$T(1) = 2T(0)−1 = 1$$ $$T(2) = 2T(1)−1 = 1$$ $$T(3) = 2T(2)−1 = 1$$ $$T(4) = 2T(3)−1 = 1$$ $$...$$ So, $T(n) = 1$, for any value of n i.e. it remains constant. Time Complexity $= O(1)$.

I agreed with calculated values (but it should be $O(2^n)$ similar with this post).

Is there any difference between values and complexity?

If above logic will applied then $T(n) = 2T(n-1)$ will be $O(1)$, also $T(n) = 2T(n/2)$ will be $O(1)$ which is NOT correct, because it should be $O(n)$ according to Master theorem.

Could you please explain it?

  • Shouldn't arithmetic operations be constant time? I suppose it's a matter of perspective. Relative to more costly operations, one would consider arithmetic operations constant time. But the question only involves arithmetic operations. I am not a CS person. I wonder what the convention. – Argyll Mar 22 '18 at 12:59
  • @Argyll, yes. arithmetic operations should be constant time but $-1$ or $+1$ can affect values not complexity. – Mithlesh Upadhyay Mar 22 '18 at 13:05
  • well, was the question asking for value in big O or complexity in big O? – Argyll Mar 22 '18 at 13:09
  • @Argyll, generally, they ask for complexity. – Mithlesh Upadhyay Mar 22 '18 at 13:13
  • Doesnt that answer the question? If you consider all arithmetic operations as $O(n)$, I see the complexity of $T(n)$ as $O(T(0))$. Similarly $T(0)$ is considered as $O(0)$. I don't see why $O(T(n))$ wouldn't be $O(0)$. – Argyll Mar 22 '18 at 13:22
  • correction all arithmetic operations as $O(0)$ – Argyll Mar 22 '18 at 15:12
  • @Argyll: arithmetic operations take constant time, but not zero time. In any case, they are asking for a big O estimate of $T$ in the question. Typically, in applications, $T$ would measure the complexity of some function, so we look at a big O estimate for $T$ itself. – Carl Mummert Mar 23 '18 at 14:12
  • 1
    Another flaw in the question is that $O(1) \subseteq O(\log n) \subseteq O(n) \subseteq O(2^n)$, so if any answer is correct then so is $O(2^n)$. Now they might have put in the instructions to select the "best" answer, which would mean there is only one correct answer, but without that extra instruction there could be more than one correct answer. – Carl Mummert Mar 23 '18 at 14:15
  • @Carl, I agreed with you. – Mithlesh Upadhyay Mar 26 '18 at 05:37

1 Answers1

3

For $T(n) = 2T(n-1)$, $T(0) = 1$, the only solution is $T(n) = 2^n$, as you can check. That function is not $O(1)$, so there may be an error in the answer key.

For $T(n) = 2T(n-1) -1$, $T(0) = 1$, as you noticed the only solution is $T(n) = 1$. A recurrence like that has only one solution. Now $T(n) = 1$ is both $O(1)$ and $O(2^n)$. This example is a kind of pathological case. If $T(0)$ was anything larger than $1$ the result would no longer be $O(1)$, but the result would still be $O(2^n)$.

Remember that $f(n) \in O(g(n))$ if there is a a $C$ so that $f(n) \leq Cg(n)$. So if $f \in O(g)$ and $g \in O(h)$ then $f \in O(h)$ as well. So, in general, big O notation only gives an upper bound, not necessarily the best upper bound.

Carl Mummert
  • 81,604