In CLRS (Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein), for a function
f(n) = an2 + bn + c
they said
Suppose we take the constants c1 = a/4, c2 = 7a/4, and n0 = 2·max(|b|/a, √(|c|/a)).
Then 0 ≤ c1n2 ≤ an2 + bn + c ≤ c2n2 for all n ≥ n0.
Therefore f(n) is Θ(n2).
But they didn't specify how values of these constants came ?
I tried to prove it but couldn't.
Please tell me how these constants came ?
c1
andc2
can be chosen arbitrarily, as long as0 < c1 < a
anda < c2 < infinity
.n0
is then calculated from this, so that the inequality0 <= c1*n^2 <= an^2 + bn + c <= c2*n^2
is satisfied for alln>=n0
.To prove that any polynomial f(n)=a0+a1*n+a2*n^2+a3*n^3+...+am*n^m is theta(n^m), follow two simple steps. step 1. show that f(n) is bigOh(n^m) step 2. show that f(n) is bigOmega(n^m)
If both the above conditions hold good, then definitely f(n) is bigTheta(n^m).
This is a generalization. By putting m=2, you get f(n) is bigTheta(n^2) Simple.. isn't it?
There's nothing special about those particular constants (other than the fact that in context of a certain
n
value they will satisfy the theta-ness off
). No magic. If you can find other positive constants whereby the relation holds that is just as valid (in fact,c1
can beka
for any0<k<1
). Although since they're there, let's analysec1
:We need it to satisfy the following inequality:
Let's take their value:
c1 = a/4
. For whatn
can we guarantee that the inequality holds? We could solve:The quadratic has solutions at
(-b +- sqrt(b^2-3ac)) / (3a/2)
, only the positive of which is of any interest since we have a positive leading coefficient polynomial, so we requiren > 2 * (sqrt(b^2-3ac) - b) / 3a
which is well defined assumingb^2 >= 3ac
(and if not, then the quadratic is positive definite, which is even better, since its >=0 everywhere and the inequality holds for all n). I should point out that this is a valid solution, although we'll do a bit more work until we arrive at the book's representation.We can split our analysis into 2 cases. First, let's assume
|b|/a >= sqrt(|c|/a)
. So we can bound from above the inside of thesqrt(...)
with4b^2
, and requiren > 2/3 * [sqrt(4b^2)-b]/a
which can be upper bounded by2/3 * 3|b|/a = 2|b|/a
.Second case, let's assume
|b|/a < sqrt(|c|/a)
. So we can bound from above the inside of thesqrt(...)
with4a|c|
, and requiren > 2/3 * [sqrt(4a|c|)-b]/a
which can be upper bounded by2/3 * 3*sqrt(a|c|)/a = 2sqrt(|c|/a)
.So in each case we see that when we take
max(|b|/a, sqrt(|c|/a))
our inequality holds whenn > 2 * max
Similarly for
c2
.well its easy 1.c1<=a + b/n + c/n^2 Now here a is >0 while b,c are either positive or negative Now we must choose value of n such that b/n + c/n^2 never exceeds a in RHS of above eqution cause else it will become negative and so will c1. but by definition c1 is a positive constant
So we want to make sure a>b/n+c/n^2
if we choose n=2*max(|b|/a, sqrt(|c|/a) ) we will get b/n + c/n^2 as an expression whose value is less than a/2+a/4.
thus a+b/n+c/n^2 will have maximum value as a+a/2+a/4 and minimum value as a-(a/2+a/4) which is nothing but values of c2 and c1.
c1=a-a/2-a/4= a/4 c2=a+a/2+a/4= 7a/4
This concept can be extended to any values for any polynomial..
cheers!!!
The idea is to (for big enough n) "trap" the function of interest between two "pure" growth functions (that have only a single constant of proportionality). In this figure two quadratic functions (drawn in red and blue) are trapped between two pure growth functions (drawn in black), and the minimum possible value of n0 in each case is indicated.
So once you've picked your values of c1 and c2, you can find the value of n0 by intersecting your function with the two pure growth functions and taking the rightmost intersection.
But you don't care about the getting smallest possible value for n0 — we're doing asymptotics here, so any big enough value will do — so you can use approximations to get an upper bound on it.
See davin's answer for how to get the upper bound for n0 into the form given in CLRS.
P(n) = an2 + bn + c = an2( 1 + b / ( an ) + c / ( an2 )) = an2( 1 ± ( | b | / a ) / n ± ( √( | c | / a ) / n )2
so if we take for example q = max( | b | / a, √( | c | / a )) than
P(n) ≤ an2( 1 + ( q / n ) + ( q / n )2 ) and if we take n0 = q than we'll get the second constant
c2 = 3a analogically for the lower bound