Proving Big O notation - algorithm

Prove 5n^2+ 2n -1 = O(n^2).
This is what I have attempted so far:
5n^2 + 2n -1 <= 5n^2 + 2n^2 -n2 for n>1
5n^2+ 2n -1 <= 6n^2 for n>1
5n^2+ 2n -1 = O(n^2) [ c = 6n^2, n0 = 1 ]
Is this the right way of proving Big O notation?

To prove that your expression is O(n^2), you need to show that it is bounded by M*n^2, for some constant M and some minimum n value. By inspection, we can show that your expression is bounded by 10*n^2, for n=10:
For n = 10:
5n^2 + 2n -1 <= 10*n^2
500 + 20 - 1 <= 1000
519 <= 1000
We can also show that the expression is bounded by 10*n^2 for any value n greater than 10:
For n > 10:
5n^2 + 2n -1 <= 10*n^2
5*(10+i)^2 + 2*(10+i) -1 <= 10*(10+i)^2
5*(i^2 + 20i + 100) + 2i + 19 <= 10*(i^2 + 20i + 100)
2i + 19 <= 5*(i^2 + 20i + 100)
2i + 19 <= 5i^2 + 100i + 500
5i^2 + 98i + 481 >= 0, which is true for `i > 0`
Here is a link to the Wikipedia article on Big-O notation:
https://en.m.wikipedia.org/wiki/Big_O_notation
Update:
Note that in practice in order to label your expression O(n^2) we won't resort to such an ugly written proof. Instead, we will just recognize that the n^2 term will dominate the expression for large n, and that the overall behavior will be O(n^2). And your expression is also O(n^3) (and O(n^4), etc.).

it looks fine , I'm thinking that if you are doing it for your assignment work or other formal work then you can also do it in more formal way like for selecting the value of constant ( c ) such as by f(n)/g(n).Otherwise it looks correct.

We have f(n) = 5*n^2+2*n-1 and g(n) = n^2
In order to prove f(n) = O(g(n)), we just need to find 2 constants, namely c > 0 and n_0, s.t. f(n) <= c.g(n) for all n >= n0.
Let's choose some value of c = 5.5 say. Let's evaluate and plot f(n) and c*g(n). As we can see from the plot and we can also show it theoretically since n^2/2 - 2*n + 1 = (n^2-4*n+2)/2 = ((n-2)^2-2)/2 >= 0 for all n >= 4, it implies that 5*n^2+2*n-1 <= 5.5*n^2 for all n >= n0 = 4. Hence, f(n) = O(g(n)). (proved)

Related

Is it always possible to find a constant K to prove big O or big Omega?

So I have to figure out if n^(1/2) is Big Omega of log(n)^3. I am pretty sure that it is not, since n^(1/2) is not even in the bounds of log(n)^3; but I do not know how to prove it without limits. I know the definition without limits is
g(n) is big Omega of f(n) iff there is a constant c > 0 and an
integer constant n0 => 1 such that f(n) => cg(n) for n => n0
But can I really always find a constant c that will satisfy this?
for instance for log(n)^3=>c*n^(1/2) if c = 0.1 and n = 10 then we get 1=>0.316.
When comparing sqrt(n) with ln(n)^3 what happens is that
ln(n)^3 <= sqrt(n) ; for all n >= N0
How do I know? Because I printed out sufficient samples of both expressions as to convince myself which dominated the other.
To see this more formally, let's first assume that we have already found N0 (we will do that later) and let's prove by induction that if the inequality holds for n >= N0, it will also hold for n+1.
Note that I'm using ln in base e for the sake of simplicity.
Equivalently, we have to show that
ln(n + 1) <= (n + 1)^(1/6)
Now
ln(n + 1) = ln(n + 1) - ln(n) + ln(n)
= ln(1 + 1/n) + ln(n)
<= ln(1 + 1/n) + n^(1/6) ; inductive hypethesis
From the definition of e we know
e = limit (1 + 1/n)^n
taking logarithms
1 = limit n*ln(1 + 1/n)
Therefore, there exits N0 such that
n*ln(1 + 1/n) <= 2 ; for all n >= N0
so
ln(1 + 1/n) <= 2/n
<= 1
Using this above, we get
ln(n + 1) <= 1 + n^(1/6)
<= (n+1)^(1/6)
as we wanted.
We are now left with the task of finding some N0 such that
ln(N0) <= N0^(1/6)
let's take N0 = e^(6k) for some value of k that we will are about to find. We get
ln(N0) = 6k
N0^(1/6) = e^k
so, we only need to pick k such that 6k < e^k, which is possible because the right hand side grows much faster than the left.

What is the asymptotic time-complexity (big theta) of T(n) = log(n*n!)?

I think is O(n*log(n)) but I am not sure.
I tried log(n*n!) = log(n(n *n-1*n-2* ...* 1)) = nlog(n) + log(n) + log(n-1) + ... + log(1) <= nlog(n) + nlog(n) = 2nlog(n)
Can someone explain if this is correct?
Upper bound
log(n*n!) = log(n) + log(n!)
= log(n) + log(n) + log(n-1) + ... + log(2)
<= log(n) + (n-1)log(n)
= n*log(n)
Lower bound
log(n*n!) = log(n) + log(n) + log(n-1) + ... + log(2)
>= log(n) + (n-1)/2*log(n/2) ; 1st half >= log(n/2), 2nd >= 0
>= log(n/2) + (n-1)/2*log(n/2)
= (n+1)/2*log(n/2)
>= (n/2)log(n/2)
Note: Here I'm assuming log(2)>0, which is true if we are taking base-2 logarithms. This is a valid assumption because the relationship between logarithms of different bases is linear, and linear relationships are preserved by big-O.
Intuitively, we see that this is O(n*log(n)), right? But why is this true?
To see the reason we need to find C > 0 and N0 such that
(N0/2)log(N0/2) >= C*N0*log(N0)
which reduces to:
log(N0/2)/log(N0) >= 2*C
or
1 - log(2)/log(N0) >= 2*C
so, choosing C < 1/2, v.g. C = 1/4, the value of N0 only needs to verify:
log(N0) >= 2*log(2) = log(4).
so it is enough to pic N0 = 4.
Note that we had one inequality for two constants C and N0. This is why we had to pick one (which was good enough), and the deduce the other.

What's the upper bound of f(n) = n^4 + 100n^2 + 50?

I'm solving some exercises related to Big-O and I'm stuck on this one:
Exercise - Find upper bound for f(n) = n^4 + 100n^2 + 50
I tried to solve it step by step but something is wrong ...:
1.=> n^4 + 100n^2 + 50 <= O(g(n))
2.=> n^4 + 100n^2 + 50 <= Cn ** Added -n^4 to both sides
3.=> n^4 + 100n^2 + 50 -n^4 <= Cn -n^4
4.=> 100n^2 + 50 <= Cn - n^4 ** Put n in common
5.=> 100n^2 + 50 <= n(C - n^3) ** Divided n in the opposite site
6.=> (100n^2 + 50)/n <= C -n^3 ** Assumed 1 for n
7.=> 100 + 50 <= C - 1
8.=> 151 <= C
There is something wrong because the answer is c = 2 and n=11. I saw this very same question being asked on stackoverflow but without a step by step solution
It is quite simple to guess that the Upper bound for this function would be O(n^4), because k * n^4 can overpower any multiple of n^3 and other multiples of n lower than 4, after a certain value of n (where k is a multiple).
Let's take a few sample example:
n^4 < 2*n^4, for all n>1.
n^4 + n^3 < 2*n^4, for all n>2.
In your case, you need to find the coefficient K which would satisfy your equation, such that n^4 + 100n^2 + 50 <= k * (n^4).
I'll leave the correct equation to be solved by you, as the one which you've shown is plainly incorrect:
n^4 + 100n^2 + 50 <= O(g(n))
n^4 + 100n^2 + 50 <= O(n^4)
n^4 + 100n^2 + 50 <= k * n^4
n^4 + 100n^2 + 50 <= n^4 + 100*n^4 + 50*n^4
n^4 + 100n^2 + 50 <= 151 * (n^4)
// O(n^4) achieved, for all n >= 1.
You can solve this equation by converting it into a quadratic equation by substituting n^2 by t, and then equation reduces to:
t^2 + 100t + 50 <= k * t^2
// left for you to solve this.
// check for what value of `k` and `t`, this equation gets satisfied.
As far as I know, we can solve this in a simple way :-
n^4+100*n^2+50 -- Lets reduce this equation to quadratic by assuming n^2 = t
t^2+ 100*t+50 -- equation changes as mentioned
Using Quadratic formula and Solving this equation ( t^2+100*t+50) we get two answers.
t = -100 +/- sqrt( 100^2 - 4*1*50)/2.1
t = -71.8 & -128.2 -- now substituting the values back to n^2
n^2 = -71.8 & -128.2
n = sqrt(-71.8) & sqrt(-128.2) = 8.47 & 11.32 -- looking at the upper bound numbers and trying to solve what could be the first biggest number to substitute for n.
n = 11
Hence, we are taking n = 11 as the first biggest number that can be used for finding upper bound
Hope it helps!
n^4 + 100n^2 + 50 <= 2n^4 , for all n>=11.
Because, till 10, the n will have negative value.
The answer for n0 is 11 because if we assume n value =11 and c value as 2 ,then the condition satisfies for the big oh notation which is f(n)<=O g(n), put n=11 and solve the quadratic equation u will get your answer try using it mathematically.
Thanks .
We need to find, smallest rate of growth, g(n) such that
c g(n) >= f(n) for n>=k.
For some constant value of c and k, the above equation will hold true. We do not consider lower values of n. This means g(n) for low values of n is not important. For large values of n, g(n) will be the maximum rate of growth of f(n).
Here, f(n)= n^4 + 100 n^2 + 50
When n is very large, g(n) = n^4
Find c and k, so that c n^4 >= n^4 + 100 n ^2 + 50
If we discard, lower terms 100 n^2 and 50. we can say c should be 2.
2 n^4 >= n^4 .
To find value of k, try substituting n^2 = t, n^4 = t^2 and c=2,
2t^2 >= t^2 + 100t + 50
t^2 >= 100t +50
If i start putting values of t from 1, 2, 3, 4, 5,6, 7, 8, 9, and 10 and t^2 =100
At 10, i still have
100,00 <= 100, 00 +50
At t= 11, and t^2 = 121, i have below
14,641 >= 12150.
So my k will be 11.

Problems solving - Asymptotic analysis

I am new with running time. I couldn't solve this one.
Given
f(n) = log n^2 and g(n) = log n + 5
prove
f(n) = theta(g(n)).
can anyone help me?
This is trivial. From basic logarithm properties
f(n) = log(n^2) = 2*log(n)
You need
C1*g(n) <= f(n) <= C2*g(n)
i.e.
C1*(log(n) + 5) <= 2*log(n) (1)
and
2*log(n) <= C2*(log(n) + 5) (2)
If you rewrite (1) as
C1*log(n) + C1*5 <= log(n) + log(n)
it looks like C1 == 1 would be a good choice, so we get:
log(n) + 5 <= log(n) + log(n)
5 <= log(n)
n >= 32
So we get for C1 == 1 and for each n >= 32, (1) holds.
For (2), it's obvious that you can choose C2 == 2. At the end you get
for each n >= 32
g(n) <= f(n) <= 2*g(n)
QED.

Big-O/Big-Oh Notation Problem

I am going over the Big-Oh notation, and I have a problem understanding the solution to this question:
Is 2n + 10 ≡ O(n)?
Can we find c and n0?
2n + 10 <= cn
(c-2)n >= 10
n >= 10/(c-2)
Pick c = 3 and n0 = 10
There is also a graph used in this example:
I am confused as to how c = 3 and how n0 = 10? Can somebody please enlighten me?
I would solve 2n + 10 <= cn differently.
2n + 10 <= cn
2 + 10/n <= c //divide by n
c >= 2 + 10/n
Clearly c has to be bigger than 2. Now take your favorite number bigger than 2. Uhm. c=2.718. This gives
2n + 10 <= 2.718*n
10 <= 0.718*n //subtract 2n
13.93 <= n
Thus, 2n + 10 <= c*n. If we take c=2.718 and n bigger than 15. (15 because it's bigger than the limit, 13.93, and I like 15.)
Any c bigger than 2 works, c=100000000000000000000000 is fine too (but, it costs much ink and paper to write down.)
It might have been easier to take c=3. That would give
2n + 10 <= 3*n
10 <= n //subtract 2n
This makes 3 the most logical / natural solution.
To say a function f(n) is O(n) means you can find c and n0 such that for all n >= n0, f(n) <= cn.
To verify this in your case: if n >= 10, then:
f(n) = 2n + 10
<= 3n // because 10 <= n
= cn
So f(n) <= cn for all n >= 10, so f(n) is an O(n) function.
Note that other values for c and n0 work; you just need to find one pair.
In Big-O notation you drop numbers added and multiplied.
And also use the largest power.
10*N^3+ 23*N^2 + 43*N + 154 = O(N^3)
let f(n) = 2n+10
as per the Big-Oh notation,
f(n)<= c*g(n) ----->eq1(let)
2n+10 <= 2n+n
2n+10 <= 3n for n>=10 //here the constant 10 is replaced by n ---->eq2
comparing eq1 with eq2
c=3 and g(n)=n for value of n=10
f(n)= O(g(n)) = O(n)

Resources