问题
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.
回答1:
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))
回答2:
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))
回答3:
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))
来源:https://stackoverflow.com/questions/34148070/how-to-solve-for-this-recurrence-tn-tn-%e2%88%92-1-lg1-1-n-t1-1