2
$\begingroup$

I have this algorithm where:

$$ T(n) = \begin{cases} 1 & \text{if}\; n \le 1 \\ T(n/2) + 1 & \text{otherwise} \\ \end{cases} $$

So, evaluating for $T(0), T(1), T(2), T(3), \ldots, T(n),ドル I'm getting values like: $$ 1, 1, 2, 2, 3, 3, \ldots, n, n $$

I assume this is twice the sum of 1ドル$ to $n,ドル that would be the same as $n (n+1)$ or $n^2+2$.

Is my assumption ok?

asked Sep 11, 2013 at 5:05
$\endgroup$
3
  • 1
    $\begingroup$ I don't know where you get the sum of 1ドル$ to $n$. I recommend computing a few more values, at least until you get to $T(n) = 5$. To get a rough idea of how $T$ grows, ask yourself this question: if $n$ doubles, how does $T$ change? [P.S. moderator note: I erased the obsolete comments, now the induction case is $T(n/2)+1$.] $\endgroup$ Commented Sep 11, 2013 at 11:43
  • $\begingroup$ It's still not clear what is meant by $n/2$. $\endgroup$ Commented Sep 11, 2013 at 13:17
  • $\begingroup$ @YuvalFilmus Only if you assume $n$ to be integer. $\endgroup$ Commented Sep 11, 2013 at 14:34

1 Answer 1

3
$\begingroup$

Suppose that $n = 2^k$. Then $$ T(n) = T(2^k) = T(2^{k-1}) + 1 = T(2^{k-2}) + 2 = \cdots = T(2^0) + k = k + 1. $$ So $T(n) = \log_2 n + 1$ in that case. In the general case you would get something similar, $T(n) = \lfloor \log_2 n + 1\rfloor$.

answered Sep 11, 2013 at 13:19
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.