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)
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)
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).
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 have to programmatically determine the value of the expression:
S = log(x1y1 + x2y2 + x3y3 ...)
Using only the values of:
lxi = log(xi)
lyi = log(yi)
Calculating anti-logs of each of lxi and lyi would probably be impractical and is not desired ...
Is there any way this evaluation can be broken down into a simple summation?
EDIT
I saw a C function somewhere that does the computation in a simple summation:
double log_add(double lx, double ly)
{
double temp,diff,z;
if (lx<ly) {
temp = lx; lx = ly; ly = temp;
}
diff = ly-lx;
z = exp(diff);
return lx+log(1.0+z);
}
The return values are added for each pair of values, and this seems to be giving the correct answer. But I'm not able to figure out how and why it's working!
The direct way is to perform two exponentiations:
ln(x+y) = ln(eln(x) + eln(y))
The log_add function uses a slightly different approach to get the same result with only one:
ln(x+y) = ln((x+y)x/x)
= ln((x+y)/x) + ln(x)
= ln(1 + y/x) + ln(x)
= ln(1 + eln(y/x)) + ln(x)
= ln(1 + eln(y)-ln(x)) + ln(x)
how can i calculate the polynomial that has the tangent lines (1) y = x where x = 1, and (2) y = 1 where x = 365
I realize this may not be the proper forum but I figured somebody here could answer this in jiffy.
Also, I am not looking for an algorithm to answer this. I'd just like like to see the process.
Thanks.
I guess I should have mentioned that i'm writing an algorithm for scaling the y-axis of flotr graph
The specification of the curve can be expressed as four constraints:
y(1) = 1, y'(1) = 1 => tangent is (y=x) when x=1
y(365) = 1, y'(365) = 0 => tangent is (y=1) when x=365
We therefore need a family of curves with at least four degrees of freedom to match these constraints; the simplest type of polynomial is a cubic,
y = a*x^3 + b*x^2 + c*x + d
y' = 3*a*x^2 + 2*b*x + c
and the constraints give the following equations for the parameters:
a + b + c + d = 1
3*a + 2*b + c = 1
48627125*a + 133225*b + 365*c + d = 1
399675*a + 730*b + c = 0
I'm too old and too lazy to solve these myself, so I googled a linear equation solver to give the answer:
a = 1/132496, b = -731/132496, c = 133955/132496, d = -729/132496
I will post this type of question in mathoverflow.net next time. thanks
my solution in javascript was to adapt the equation of a circle:
var radius = Math.pow((2*Math.pow(365, 2)), 1/2);
var t = 365; //offset
this.tMax = (Math.pow(Math.pow(r, 2) - Math.pow(x, 2), 1/2) - t) * (t / (r - t)) + 1;
the above equation has the above specified asymptotes. it is part of a step polynomial for scaling an axis for a flotr graph.
well, you are missing data (you need another point to determine the polynomial)
a*(x-1)^2+b*(x-1)+c=y-1
a*(x-365)^2+b*(x-365)+c=y-1
you can solve the exact answer for b
but A depends on C (or vv)
and your question is off topic anyways, and you need to revise your algebra