1

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.

Raphael
  • 73,212
  • 30
  • 182
  • 400
Oxymoron
  • 223
  • 1
  • 2
  • 4

2 Answers2

1

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

A.B.
  • 441
  • 1
  • 6
  • 17
0

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

Raidenlee
  • 5
  • 3