Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

n^2 log n complexity

I am just a bit confused. If time complexity of an algorithm is given by

enter image description here

what is that in big O notation? Just enter image description here or we keep the log?

like image 695
darxsys Avatar asked Feb 02 '14 12:02

darxsys


People also ask

Which is faster log N or n 2?

So, O(N*log(N)) is far better than O(N^2) . It is much closer to O(N) than to O(N^2) . But your O(N^2) algorithm is faster for N < 100 in real life. There are a lot of reasons why it can be faster.

What is n log2 n?

N log2N - time involves applying a logarithmic algorithm N times. Quadratic time - algorithms of this type typically involve applying a linear algorithm N times.

What is the time complexity of n log n?

It has a guaranteed running time complexity of O ( n l o g ( n ) ) O(n \ log (n)) O(n log(n)) in the best, average, and worst case. It essentially follows two steps: Divide the unsorted list into sub-lists until there are N sub-lists with one element in each ( N is the number of elements in the unsorted list).

What is n2 complexity?

A function with a quadratic time complexity has a growth rate of n2. If the input is size 2, it will do four operations. If the input is size 8, it will take 64, and so on.


2 Answers

If that's the time-complexity of the algorithm, then it is in big-O notation already, so, yes, keep the log. Asymptotically, there is a difference between O(n^2) and O((n^2)*log(n)).

like image 131
Martin Dinov Avatar answered Sep 20 '22 11:09

Martin Dinov


A formal mathematical proof would be nice here.

Let's define following variables and functions:
N - input length of the algorithm,
f(N) = N^2*ln(N) - a function that computes algorithm's execution time.

Let's determine whether growth of this function is asymptotically bounded by O(N^2).

According to the definition of the asymptotic notation [1], g(x) is an asymptotic bound for f(x) if and only if: for all sufficiently large values of x, the absolute value of f(x) is at most a positive constant multiple of g(x). That is, f(x) = O(g(x)) if and only if there exists a positive real number M and a real number x0 such that

|f(x)| <= M*g(x) for all x >= x0 (1)

In our case, there must exists a positive real number M and a real number N0 such that: |N^2*ln(N)| <= M*N^2 for all N >= N0 (2)

Obviously, such M and x0 do not exist, because for any arbitrary large M there is N0, such that ln(N) > M for all N >= N0 (3)

Thus, we have proved that N^2*ln(N) is not asymptotically bounded by O(N^2).

References:
1: - https://en.wikipedia.org/wiki/Big_O_notation

like image 20
Alex T Avatar answered Sep 18 '22 11:09

Alex T