Take the 2-minute tour ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

This question already has an answer here:

I'm having a real hard time solving recurrences using the substitution method.

Show that: $T(n) = T(n/2) + 1$ is $O(\lg n)$

I thought this to be relatively easy:

We have to show that $T(n) \leq c \lg n$

Substitution gives me:

$\qquad \begin{align} T(n) &\leq c \lg(n/2) + 1 \\ &= c \lg n - c \lg 2 + 1 \\ &= c \lg n - c + 1 \\ &\leq c \lg n \end{align}$

for every c.

I was under the impression this was it, but when I was looking for an answer, I came around a much more elaborate answer on the web, given involving subtracting a constant. I don't get why that's needed, I thought I had shown what was needed.

Any help would be greatly appreciate, starting Monday I'm enrolled in an algorithms class and I don't want to get behind!

We are using the CLRS book (surprise) and though I appreciate the amount of information in it, I'd rather have some more resources. I've really enjoyed a datastructures class and I really think I can enjoy this as well, but more resources would be very much appreciated.

share|improve this question
1  
We have a reference question with ample material about solving recurrences, in particular this answer. –  Raphael Jan 30 '13 at 20:54
    
Your substitution proves nothing. You use the claim to derive the claim -- that's not very helpful. –  Raphael Jan 30 '13 at 20:56
1  
@Raphael This is proof by induction. –  Yuval Filmus Jan 31 '13 at 0:27
1  
Your solution looks fine, though you'd better write it as an induction, i.e $T(n) = T(n/2) + 1 \leq c\lg(n/2) + 1$ and so on. You also need to take care of the base case, and notice that you get a condition on $c$ (not all $c$ work). –  Yuval Filmus Jan 31 '13 at 0:28
1  
@YuvalFilmus No, it's not. What is written there could be used as the inductive step, true. Given the level of the question, I would not assume that Oxymoron knows what happens there; the question even says "substitution method". –  Raphael Jan 31 '13 at 9:34
show 2 more comments

marked as duplicate by Raphael Aug 7 '13 at 8:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

Luckily i had 2 day ago the algorithm exam and so i was able to solve your question :-) When solving recurrences try first to use the Master method, if you can't succeed than try other methods.

enter image description here

share|improve this answer
    
1) The asker explicitly asks for how to do the "direct" proof. 2) Please type your solution; you can use LaTeX. –  Raphael Jan 31 '13 at 9:36
add comment

An easy way to solve recurrence relations

Using your example:

T(n) = T(n/2)+1 is O(lgn)

T(1) = 0 .....suitable basecase

STEP 1

Solving the recurrence relation

 T(n) = T(n/2) + 1

      = T(n/4) + 1 + 1

      = T(n/8) + 1 + 1 + 1
      :
      :
      = T(n/2^k) + k    ...(3)      (this step summarizes the recursive steps)

STEP 2

Now, let

      n/2^k = 1

      n = 2^k .... (1)

      also k = logn .... (2)

STEP 3

Substituting (1) and (2) in (3)

 T(n) = T(n/n) + logn

      = T(1) + logn

      = 0 + logn    (recall T(1) = 0)

      = logn

Hence: Order is O(logn)

note: logn is of base 2

share|improve this answer
1  
Technically, you still need to prove the result correct; "..." is a good way to find a hypothesis, though. –  Raphael Aug 7 '13 at 8:12
    
you're right .. then you use Mathematical Induction to prove –  Rock Lee Nov 27 '13 at 3:49
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.