How to solve for this recurrence T(n) = T(n − 1) + lg(1 + 1/n), T(1) = 1?

牧云@^-^@ 提交于 2019-12-05 12:40:35

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))

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))

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:

if you will do this till the end you will have

or in a short form:

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

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

You will see that the first part is

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!