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.

  • 1
    $\begingroup$ We have a reference question with ample material about solving recurrences, in particular this answer. $\endgroup$ – Raphael Jan 30 '13 at 20:54
  • $\begingroup$ Your substitution proves nothing. You use the claim to derive the claim -- that's not very helpful. $\endgroup$ – Raphael Jan 30 '13 at 20:56
  • 1
    $\begingroup$ @Raphael This is proof by induction. $\endgroup$ – Yuval Filmus Jan 31 '13 at 0:27
  • 1
    $\begingroup$ 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). $\endgroup$ – Yuval Filmus Jan 31 '13 at 0:28
  • 1
    $\begingroup$ @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". $\endgroup$ – Raphael Jan 31 '13 at 9:34

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

  • $\begingroup$ 1) The asker explicitly asks for how to do the "direct" proof. 2) Please type your solution; you can use LaTeX. $\endgroup$ – Raphael Jan 31 '13 at 9:36

An easy way to solve recurrence relations

Using your example:

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

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


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)


Now, let

      n/2^k = 1

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

      also k = logn .... (2)


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

  • 1
    $\begingroup$ Technically, you still need to prove the result correct; "..." is a good way to find a hypothesis, though. $\endgroup$ – Raphael Aug 7 '13 at 8:12
  • $\begingroup$ you're right .. then you use Mathematical Induction to prove $\endgroup$ – Raidenlee Nov 27 '13 at 3:49

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