Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O Notation of an expression

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

I know that 7n is cut off, but I don't know if I should keep the n^2 coefficient or not.

Thanks

like image 432
devoured elysium Avatar asked Jan 17 '10 17:01

devoured elysium


People also ask

What are the rules of Big O notation?

With Big O notation, we use the size of the input, which we call " n." So we can say things like the runtime grows "on the order of the size of the input" ( O ( n ) O(n) O(n)) or "on the order of the square of the size of the input" ( O ( n 2 ) O(n^2) O(n2)).


1 Answers

You should drop any coefficients because the question is really asking "on the order of", which tries to characterize it as linear, exponential, logarithmic, etc... That is, when n is very large, the coefficient is of little importance.

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

You can also think about this in a graphical sense... that is, if you graph the function 4n^2 + 7n for larger and larger values of n, a mathematician might say "it looks like n^2". Granted, it would have to be a fairly liberal mathematician, as this isn't a rigorous statement, but that's basically what O(...) is trying to convey.

like image 125
Michael Bray Avatar answered Sep 19 '22 22:09

Michael Bray