Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asymptotic complexity of T(n)=T(n-1)+1/n [closed]

There is an algorithm which has the time complexity

    T(n)=T(n-1)+1/n if n>1
        =1          otherwise

I am solving for its asymptotic complexity, and getting order as 'n' but the answer given is 'log n'. Is it correct? If it is log n, then why?

like image 889
sandepp Avatar asked Mar 27 '13 09:03

sandepp


2 Answers

It can be easily seen (or proven formally with induction) that T(n) is the sum of 1/k for the values of k from 1 to n. This is the nth harmonic number, Hn = 1 + 1/2 + 1/3 + ... + 1/n.

Asymptotically, the harmonic numbers grow on the order of log(n). This is because the sum is close in value to the integral of 1/x from 1 to n, which is equal to the natural logarithm of n. In fact, Hn = ln(n) + γ + O(1/n) where γ is a constant. From this, it is easy to show that T(n) = Θ(log(n)).

like image 180
interjay Avatar answered Nov 15 '22 20:11

interjay


For more details:

With H(N) = 1 + 1/2 + 1/3 + ... + 1/N

the function x :-> 1/x is a decreasing function so :

enter image description here

We sum from 1 to N the left part and for the right part we sum from 2 to N and we add 1, we get:

enter image description here

Then we calculate the left and right parts : ln(N+1) <= H(N) <= 1 + ln(N)

this implies H(N)/ln(N) -> 1 hence H(N)=Θ(log(N))

(from http://fr.wikipedia.org/wiki/S%C3%A9rie_harmonique#.C3.89quivalent_de_Hn)

like image 37
Tony Morris Avatar answered Nov 15 '22 20:11

Tony Morris