T(n)=T(n^1/4)+n^1/2+n
T(n^1/8)+n^1/4+n^1/2+n
.
.
T(n^1/2^k)+n^1/k-1+n^1/k-2......+n
I got k= loglog(n) but i am not able to solve the series by putting this k value into above series.
Substituting n = 2^m first gives us
T(2^m) = T(2^(m-1)) + 2^m
Now let S(m) = T(2^m). This vastly simplifies the recurrence relation to
S(m) = S(m/2) + 2^m
By the master theorem,
S(m) = O(2^m)
Finally,
T(n) = T(2^m) = S(m) = O(2^m) = O(2^(log_2 n)) = O(n).
Related
I came across a problem where I've to select the correct Big O for the function f(n) = n^5 + 2^log(n)...
I tried putting large values and found out that n^5 grows significantly as compare to 2^log(n)... But then someone told me that exponential functions grow significantly as compared to other functions... And I got confused again... To be honest I think 2^log(n) is not an exponential function... But because of my weak logarithmic concepts, I am unable to prove that...
I just want someone to tell me that yes n^5 is larger than 2^log(n) so that I can prove that 2^log(n) is not an exponential function...
Thanks in advance. :)
2^log(n) = (2/e)^log(n) * e^log(n) = a^log(n) * n where a = 2/e < 1 (assuming log is the natural logarithm).
It follows that f(n) = n^5 + 2^log(n) < n^5 + n and therefore f(n) = O(n^5).
[ EDIT ] In the general case of logarithms of an arbitrary base b, using that 2 = b^log_b(2) it follows that:
2^log_b(n) = (b^log_b(2))^(log_b(n))
= b^(log_b(2)*log_b(n))
= (b^log_b(n))^log_b(2)
= n^log_b(2)
= n^(1/log_2(b))
Therefore f(n) = n^5 + log_b(n) = O( n^5 + n^(1/log_2(b)) ) = O( n^max(5, 1/log_2(b)) ).
In particular, f(n) = O(n^5) for log_2(b) > 1/5 ⇔ b > 2^(1/5), which covers the common log bases of 2, e, 10.
O(2logn)=O(n) - this follows straight from the definition of logarithm.
More formally:
f(n)=2logn
log2f(n)=log2(2logn)=lognlog22=log2n
==>f(n)=n
==> O(2logn)=O(n)
==> O(n5 + 2logn)=O(n5 + n)=O(n5)
Is this recurrence relation O(infinity)?
T(n) = 49*T(n/7) + n
There are no base conditions given.
I tried solving using master's theorem and the answer is Theta(n^2). But when solving with recurrence tree, the solution comes to be an infinite series, of n*(7 + 7^2 + 7^3 +...)
Can someone please help?
Let n = 7^m. The recurrence becomes
T(7^m) = 49 T(7^(m-1)) + 7^m,
or
S(m) = 49 S(m-1) + 7^m.
The homogeneous part gives
S(m) = C 49^m
and the general solution is
S(m) = C 49^m - 7^m / 6
i.e.
T(n) = C n² - n / 6 = (T(1) + 1 / 6) n² - n / 6.
If you try the recursion method:
T(n) = 7^2 T(n/7) + n = 7^2 [7^2 T(n/v^2) + n/7] + n = 7^4 T(n/7^2) + 7n + n
= ... = 7^(2i) * T(n/7^i) + n * [7^0 + 7^1 + 7^2 + ... + 7^(i-1)]
When the i grows n/7^i gets closer to 1 and as mentioned in the other answer, T(1) is a constant. So if we assume T(1) = 1, then:
T(n/7^i) = 1
n/7^i = 1 => i = log_7 (n)
So
T(n) = 7^(2*log_7 (n)) * T(1) + n * [7^0 + 7^1 + 7^2 + ... + 7^(log_7(n)-1)]
=> T(n) = n^2 + n * [1+7+7^2+...+(n-1)] = n^2 + c*n = theta(n^2)
Usually, when no base case is provided for a recurrence relation, the assumption is that the base case is something T(1) = 1 or something along those lines. That way, the recursion eventually terminates.
Something to think about - you can only get an infinite series from your recursion tree if the recursion tree is infinitely deep. Although no base case was specified in the problem, you can operate under the assumption that there is one and that the recursion stops when the input gets sufficiently small for some definition of "sufficiently small." Based on that, at what point does the recursion stop? From there, you should be able to convert your infinite series into a series of finite length, which then will give you your answer.
Hope this helps!
The running time for this recurrence relation is O(nlogn). Since I am new to algorithm how would I show that mathematically?
T(n) = 2⋅T(n/2) + O(n)
T(n) = 2 ( 2⋅T(n/4) + O(n) ) + O(n) // since T(n/2) = 2⋅T(n/4) + O(n)
So far I can see that if I suppose n to be a power of 2 like n = 2m, then may be I can show that, but I am not getting the clear picture. Can anyone help me?
If you use the master theorem, you get the result you expected.
If you want to proof this "by hand", you can see this easily by supposing n = 2m is a power of 2 (as you already said). This leads you to
T(n) = 2⋅T(n/2) + O(n)
= 2⋅(2⋅T(n/4) + O(n/2)) + O(n)
= 4⋅T(n/4) + 2⋅O(n/2) + O(n)
= 4⋅(2⋅T(n/8) + O(n/4)) + 2⋅O(n/2) + O(n)
= Σk=1,...,m 2k⋅O(n/2k)
= Σk=1,...,m O(n)
= m⋅O(n)
Since m = log₂(n), you can write this as O(n log n).
At the end it doesn't matter if n is a power of 2 or not.
To see this, you can think about this: You have an input of n (which is not a power of 2) and you add more elements to the input until it contains n' = 2m Elements with m ∈ ℕ and log(n) ≤ m ≤ log(n) + 1, i.e. n' is the smalest power of 2 that is greater than n. Obviously T(n) ≤ T(n') holds and we know T(n') is in
O(n'⋅log(n')) = O(c⋅n⋅log(c⋅n)) = O(n⋅log(n) + n⋅log(c)) = O(n⋅log(n))
where c is a constant between 1 and 2.
You can do the same with the greatest power of 2 that is smaller than n. This gives leads you to T(n) ≥ T(n'') and we know T(n'') is in
O(n''⋅log(n'')) = O(c⋅n⋅log(c⋅n)) = O(n⋅log(n))
where c is a constant between 1/2 and 1.
In total you get, that the complexity of T(n) is bounded by the complexitys of T(n'') and T(n') wich are both O(n⋅log(n))and so T(n) is also in O(n⋅log(n)), even if it is not a power of 2.
What is the time complexity of the recurrence T(n) = 2T(n-1) + 4 ?
I'm having serious problems with this.
I tried:
T(n) = 2T(n-1)+4 = 2(2T(n-2)+4)+4 = 4T(n-2)+12= 4(2T(n-3)+4)+4 = 8T(n-3)+20 = 8(2T(n-4)+4)+4 =
16T(n-4)+36 =…
T(n) = 2^kT(n-k) + (4+2^(k+1))
so it looks like T(n) = 2^n + (4+2^(n+1)) but it doesn't seem right... please help :(
Your computation are wrong. I'm assuming here that T(0)=0
T(n) = 2T(n-1)+ 4
= 2(2T(n-2)+4)+ 4 = 4T(n-2)+ 12
= 4(2T(n-3)+4)+ 12 = 8T(n-3)+ 28
= 8(2T(n-4)+4)+ 28 = 16T(n-4)+ 60
= 16(2T(n-5)+4)+ 60 = 32T(n-5)+124
= 32(2T(n-6)+4)+124 = 64T(n-6)+252
Then now: look at the sequence
0,4,12,28,60,124,252,508,1020,2044,...
It is very tempting to add 4 to all these numbers:
4,8,16,32,64,128,256,512,1024,2048,...
Do you recognize it ? So the guess is clearly
T(n) = 2^(n+2) - 4
Now, you can easily prove it by induction.
By the way if T(0) is not equal to 0 the formula is
T(n) = 2^(n+2) - 4 + T(0)*2^n
Solving the recurrence relation, I found this:
T(n) = 2T(n-1) + 1
Let n = log m then, we get T(log m)=2T(log m - log 2) + 1
Assume S(a) = 2S(a/2) + 1
Applying Master's theorem we get:
n^logb^a = n^log2^2 = n.
n^log2^2-1 = 1, here € = 1
By first case we get: S(a) = O(a)
therefore T(n) = O(2^n)
I was going through slides of an algorithm class and came across following.
T(n) = 2T(n^(1/2)) + lg n
Rename: m = lg n => n = 2^m
T (2^m) = 2T(2^(m/2)) + m
Rename: S(m) = T(2^m) S(m) = 2S(m/2) + m
Can any one explain me how did the last equation come ? I'm not able to understand how S(m/2) came. Thank you.
It is just an argument substitution.
You have S(m) = T(f(m)), where f(m) = 2^m. Substitute m with m/2 and you'll get
S(m/2) = T(f(m/2)), f(m/2) = 2^(m/2)
Now you may rewrite left part T(f(m/2)) = T(2^(m/2)) = S(m/2)