4

I got stuck in this recurrence:

T(n) = T(n − 1) + lg(1 + 1/n), T(1) = 1?

for a while and it seems the master method cannot be applied on this one.

user1952500
  • 6,611
  • 3
  • 24
  • 37
Bee
  • 55
  • 4

3 Answers3

6

We have:

lg(1 + 1/n) = lg((n + 1) / n) = lg(n+1) - lg(n)

Hence:

T(n) - T(n - 1) = lg(n + 1) - lg(n)

T(n-1) - T(n - 2) = lg(n) - lg(n - 1)

...

T(3) - T(2) = lg(3) - lg(2)

T(2) - T(1) = lg(2) - lg(1)

Adding and eliminating, we get:

T(n) - T(1) = lg(n + 1) - lg(1) = lg(n + 1)

or T(n) = 1 + lg(n + 1)

Hence T(n) = O(lg(n))

user1952500
  • 6,611
  • 3
  • 24
  • 37
  • I agree for a computer (and I am not the one who has downvoted your answer), but then the equation is fundamentally incorrect. If it should be valid on an algorithmic basic on a _Computer_, it should be rewritten as `T(n) = T(n - 1) + lg(1 + 1/n) + k` where `k` is a constant or `O(1)`. In that case, the solution will turn out to be `T(n) = n * k + lg(n + 1)` or `O(n)`. – user1952500 Dec 08 '15 at 05:03
1

Same answer as the other correct answer here, just proved differently.

All the following equations are created from the given recurrence:

  • T(n) = T(n-1) + Log((n+1)/n)
  • T(n-1) = T(n-2) + Log(n/(n-1))
  • .
  • .
  • .
  • T(2) = T(1) + Log(3/2)

Summing all RHS and LHS in the above equations results in:

  • T(n) = T(1) + Log(3/2) + Log(4/3) + ... + Log((n+1)/n)

Since Log(a) + Log(b) = Log(ab),

  • T(n) = 1 + Log((n+1)/2)
  • T(n) = Log(10) + Log((n+1)/2) = Log(5n + 5) assuming that base was 10 and using 1 = Log1010

Therefore T(n) = O(Log(5n + 5)) = O(Log(n))

displayName
  • 13,888
  • 8
  • 60
  • 75
1

It is not linear as some people claim. It is O(log(n)). Here is mathematical analysis:


If you will start unrolling recursion you will get:

enter image description here

if you will do this till the end you will have

enter image description here

or in a short form:

enter image description here

Once you approximate the sum with an integral, you will get:

enter image description here

Finally if you will take a limit x -> infinity:

You will see that the first part is enter image description here

Which gives you a final solution log(x + 1) which is O(log(n))

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • The summation has a closed form solution for finite `n` outside the limit in this case. – user1952500 Dec 08 '15 at 05:07
  • @user1952500 :-) this is embarrasing. Anyway there is nothing wrong with this solution apart from it overcomplicates things. It can also show what to do if there is no close form solution – Salvador Dali Dec 08 '15 at 05:19
  • no worries. I was about to proceed the same route as the limit allows us to quickly compute the solution mentally. Just tried out the other way :) – user1952500 Dec 08 '15 at 05:21