How to approve or disprove a statement-Time Complexity - algorithm

For all functions f, log_2(f(n)) + O(n) = O(n).
I have tried disapproving it by taking the limit. But got infinity as a result. Is it right?

The statement is not true. As a counterexample f(n) = n^n. Therefore, log(f(n)) = n log(n) and n log n + O(n) is not in O(n).

Related

Is log(n-f(n)) big theta of log(n)

The problem is that I need to know if log(n-f(n)) is big theta of log(n), where f(n) is a lower order function than n, e.g., log(n) or sqrt(n).
I tried to use some log rules and plotting seems to confirm the bound, but I can't get it exactly.
As f(n) is a lower order function than n, f(n) = o(n). Hence, n-o(n) < 2n and n - o(n) = O(n). Also, n - o(n) > n - 0.01 n <=> 0.01 n > o(n) (0.01 can be specified with the o(n)). Therfore, n - o(n) = Omega(n), and n-o(n) = Theta(n).
As log function is an increasing function we can say log(n-o(n)) = Theta(log(n)).

Calculating execution time of an algorithm

I have this algorithm:
S(n)
if n=1 then return(0)
else
S(n/3)
x <- 0
while x<= 3n^3 do
x <- x+3
S(n/3)
Is 2 * T(n/3) + n^3 the recurrence relation?
Is T(n) = O(n^3) the execution time?
The recurrence expression is correct. The time complexity of the algorithm is O(n^3).
The recurrence stops at T(1).
Running an example for n = 27 helps deriving a general expression:
T(n) = 2*T(n/3)+n^3 =
= 2*(2*T(n/9)+(n/3)^3)+n^3 =
= 2*(2*(2*T(n/27)+(n/9)^3)+(n/3)^3)+n^3 =
= ... =
= 2*(2*2*T(n/27)+2*(n/9)^3+(n/3)^3)+n^3 =
= 2*2*2*T(n/27)+2*2*(n/9)^3+2*(n/3)^3+n^3
From this example we can see that the general expression is given by:
Which is equivalent to:
Which, in turn, can be solved to the following closed form:
The dominating term in this expression is (1/25)*27n^3 (2^(log_3(n)) is O(n), you can think of it as 2^(log(n)*(1/log(3))); dropping the constant 1/log(3) gives 2^log(n) = n), thus, the recurrence is O(n^3).
2 * T(n/3) + n^3
Yes, I think this is a correct recurrence relation.
Time complexity:
while x<= 3n^3 do
x <- x+3
This has a Time complexity of O(n^3). Also, at each step, the function calls itself twice with 1/3rd n. So the series shall be
n, n/3, n/9, ...
The total complexity after adding each depth
n^3 + 2/27 * (n^3) + 4/243 * (n^3)...
This series is bounded by k*n^3 where k is a constant.
Proof: if it is considered as a GP with a factor of 1/2, then the sum
becomes 2*n^3. Now we can see that at each step, the factor is
continuously decreasing and is less than half. Hence the upper bound is less than 2*n^3.
So in my opinion the complexity = O(n^3).

Algorithm complexity, solving recursive equation

I'm taking Data Structures and Algorithm course and I'm stuck at this recursive equation:
T(n) = logn*T(logn) + n
obviously this can't be handled with the use of the Master Theorem, so I was wondering if anybody has any ideas for solving this recursive equation. I'm pretty sure that it should be solved with a change in the parameters, like considering n to be 2^m , but I couldn't manage to find any good fix.
The answer is Theta(n). To prove something is Theta(n), you have to show it is Omega(n) and O(n). Omega(n) in this case is obvious because T(n)>=n. To show that T(n)=O(n), first
Pick a large finite value N such that log(n)^2 < n/100 for all n>N. This is possible because log(n)^2=o(n).
Pick a constant C>100 such that T(n)<Cn for all n<=N. This is possible due to the fact that N is finite.
We will show inductively that T(n)<Cn for all n>N. Since log(n)<n, by the induction hypothesis, we have:
T(n) < n + log(n) C log(n)
= n + C log(n)^2
< n + (C/100) n
= C * (1/100 + 1/C) * n
< C/50 * n
< C*n
In fact, for this function it is even possible to show that T(n) = n + o(n) using a similar argument.
This is by no means an official proof but I think it goes like this.
The key is the + n part. Because of this, T is bounded below by o(n). (or should that be big omega? I'm rusty.) So let's assume that T(n) = O(n) and have a go at that.
Substitute into the original relation
T(n) = (log n)O(log n) + n
= O(log^2(n)) + O(n)
= O(n)
So it still holds.

Big -O notation

Hey i have a question.
say t(n) = O(n log(n)) and u know that this is true.
and then your given these statements and told to say whether they must be true or false. t(n) = n^4 and t(n) = O(N^4)
The statement t(n) = n^4 is false while the statement t(n) = O(N^4) is true. Why?
You have to remember that when you write t(n) = O(n log(n)) and t(n) = O(N^4), what it actually means is that t(n) is in O(...), not that it's equal to it (as O(...) is a set of functions and a function can not be equal to a set of functions). However when you write f(n) = n^4, that means that f(n) is equal to n^4.
Now if f(n) is in O(n log n), it is also in O(n^4) because O(n^4) is a superset of O(n log n). However it can not be equal to n^4, because n^4 is not in O(n log n).
The idea of Big O notation is that it represents an abstracted function of time, it focuses on the slowest part of your algorithm and ignores things that affect execution time (i.e. t(n)) but don't actually make a huge difference.
For exmaple, if your function works on a set of items of size n and just loops through them performing some calculations then you'd say t(n) = O(n). Say you performed some operation only on a few of elements according to some criteria, you would still still say t(n) = O(n) but the actual time taken t(n) would not be a function of n directly, hence t(n) = nx would not hold true.
Look at the second equation in this. From this equation, t(n) = n^4 = O(n^4) is obvious.
t(n) = O(n log n) is false, because ∀M>0,x, ∃n>x, t(n) = n^4 > M(n log n).
(if n > log n and n>M, n^4 > M*n^3 = M(n * n^2) > M(n * log n) = M(n log n), and n>log n when (roughly) n>5)

Big-Oh Notation

if T(n) is O(n), then it is also correct to say T(n) is O(n2) ?
Yes; because O(n) is a subset of O(n^2).
Assuming
T(n) = O(n), n > 0
Then both of the following are true
T(n) = O(2n)
T(n) = O(n2)
This is because both 2n and n2 grow as quickly as or more quickly than just plain n. EDIT: As Philip correctly notes in the comments, even a value smaller than 1 can be the multiplier of n, since constant terms may be dropped (they become insignificant for large values of n; EDIT 2: as Oli says, all constants are insignificant per the definition of O). Thus the following is also true:
T(n) = O(0.2n)
In fact, n2 grows so quickly that you can also say
T(n) = o(n2)
But not
T(n) = Θ(n2)
because the functions given provide an asymptotic upper bound, not an asymptotically tight bound.
if you mean O(2 * N) then yes O(n) == O(2n). The time taken is a linear function of the input data in both cases
I disagree with the other answer that says O(N) = O(N*N). It is true that the O(N) function will finish in less time than O(N*N), but the completion time is not a function of n*n so it really isnt true
I suppose the answer depends on why u r asking the question
O also known as Big-Oh is a upper bound. We can say that there exists a C such that, for all n > N, T(n) < C g(n). Where C is a constant.
So until an unless the large co-efficient in T(n) is smaller or equal to g(n) then that statement is always valid.

Resources