Big O Expression Designation

If I have an algorithm that moves 4n ^ 2 + 7n to execute, what is its O? O (4n ^ 2)? O (N ^ 2)?

I know that 7n is disabled, but I don’t know whether I should store the coefficient n ^ 2 or not.

thanks

+6
big-o time-complexity asymptotic-complexity
source share
6 answers

You should discard any coefficients because the question really asks “in order”, which tries to characterize it as linear, exponential, logarithmic, etc. That is, when n is very large, the coefficient is a little important.

This also explains why you roll + 7n, because when n is very large, this term has relatively little meaning for the final answer. If you are familiar with calculus, you can say lim n-> inf (4 * n ^ 2 + 7n) ~ = lim n-> inf (4 * n ^ 2) ~ = lim n-> inf (n ^ 2)

You can also think about it in a graphical sense ... that is, if you draw a function 4n ^ 2 + 7n for large and large values ​​of n, the mathematician can say "it looks like n ^ 2". Of course, this should have been a fairly liberal mathematician, since this is not a strict statement, but what O (...) basically does.

+14
source share

Odds are not related to Big O notation, so just O (n 2 ). As Wikipedia explains :

[...] the coefficients become insignificant when compared with any other order of expression, such as an expression containing the term n 3 or n 2 .

+11
source share

Everyone who reads or writes about the complexity of the algorithms must know for sure that the Landau Symbols and asymptotic notation , otherwise they really do not understand what is happening, or simply have an approximate (and often misleading) idea.

To simplify (a lot), let f and g be two functions of f : N -> N and g : N -> N We say that f is O(g) if and only if there exists a constant M > 0 such that |f(n)| < M|g(n)| |f(n)| < M|g(n)| for all n > M That is, more informally, starting with a large value of n , all values ​​of f(n) less than a multiple of g(n) (i.e., g grows faster than f ).

This definition is equivalent to

 f is O(g) <==> There is K >= 0 such that lim{n -> +oo} |f(n)|/|g(n)| = K 

So, take f(n) = 4n^2 + 7n and g(n) = n^2 and try to prove f is O(g) (I omit {n -> +oo} ):

 lim |f(n)|/|g(n)| = lim f(n)/g(n) = lim (4n^2 + 7n) / n^2 = 4 + lim 7n/n^2 = = 4 + lim 7/n = 4 + 0 = 4 

It follows that there exists M such that n > M ==> |f(n)| < M|g(n)| n > M ==> |f(n)| < M|g(n)| and therefore f is O(g) .

So it’s technically correct to say 4n^2 + 7n is O(4n^2) , since it’s right to say 4n^2 + 7n is O(n^3) , 4n^2 + 7n is O(e^n) and so on. But to be meaningful, we are interested in the lower bound. Therefore, if f is O(e^n) and f is O(n^2) , we are more interested in knowing that f is O(n^2) , since this is much more restrictive.

VERY IMPORTANT note

What is especially important when choosing an algorithm is to understand that the big-O notation refers to asymptotic cases, that is, when you consider extremely unimaginable huge inputs that can go far beyond the computational power available in a known universe (i.e. infinite input sets expressed mathematically with {n -> +oo} ).

For practical use (i.e., not very large inputs) when choosing an algorithm, of course, you will observe algorithms of a large number of applications with candidate algorithms, but you must be sure that the selected algorithm is well adapted (and works better) for your (expected ) input.

Finally, usually more efficient algorithms are more difficult to understand and implement correctly. You should also take this fact into account when choosing an algorithm (i.e., this is the time that I will spend debugging and fixing my implementation of this algorithm, significantly exceeding the time I will have to wait with another algorithm, with worse than the note "big O "? If so, you should consider a simpler and less efficient algorithm, since the overall solution will be more efficient).

+9
source share

It O (n ^ 2). Constant factors "go to O." You only save the largest exponent since it dominates. And you can leave the coefficients, because when comparing different algorithms, even very large coefficients lead to lower total numbers than to a larger indicator (with sufficiently large n).

+6
source share

Type operator

 4n² + 7n = O(n²) 

means that for some constant factor c expression cn² will eventually overtake 4n² + 7n . It is technically incorrect to leave the coefficient there - O(n²) and O(4n²) means the same, because any constant c for the former can be replaced by c/4 for the latter. However, such a thing is less clear, perhaps misleading and definitely non-standard.

+4
source share

Mathematically speaking, you should write O (4n²). This means that the complexity function of your algorithms behaves like n-> 4n² to positive infinite.

But in computer science / algorithm you should write only O (n²), which is enough to classify your algorithm.

+1
source share

All Articles