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
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
Everyone reading or writing about complexity of algorithms should know exactly what the Landau Symbols and asymptotic notations are, otherwise they don't really understand what is going on, or simply have an approximate (and often misleading) idea.
To simplify (a lot), let
f
andg
be two functionsf : N -> N
andg : N -> N
. We say thatf is O(g)
if and only if there's a constantM > 0
such that|f(n)| < M|g(n)|
, for alln > M
. That is, more informally, starting from a big value ofn
, all the valuesf(n)
are smaller than a multiple ofg(n)
(ie,g
grows faster thanf
).That definition is equivalent to
So, let's take
f(n) = 4n^2 + 7n
andg(n) = n^2
, and try to provef is O(g)
(I will omit{n -> +oo}
):This implies that there is a
M
such thatn > M ==> |f(n)| < M|g(n)|
, and thusf is O(g)
.So technically it is correct to say
4n^2 + 7n is O(4n^2)
, as it is correct to say4n^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. So iff is O(e^n)
andf is O(n^2)
, we are more interested into knowing thatf is O(n^2)
, since this is much more restrictive.VERY IMPORTANT note
What is extremelly important when choosing an algorithm is to understand that big-O notations refers to asymptotic cases, that is, when you consider extremely, unimaginable huge inputs, that can go well beyond the computational power available in the known universe (ie, infinite input sets, expressed mathematically by
{n -> +oo}
).For practical uses (ie, not so huge inputs), when choosing an algorithm, sure, you will observe candidate algorithms big-O notations, but you must be sure that the chosen algorithm is well adapted (and performs better) for your (expected) input.
Finally, usually better performing algorithms are more difficult to understand and to implement properly. You must consider this fact as well when choosing an algorithm (ie, is the time I will spend debugging and fixing my implementation of this algorithm considerably superior to the time I would have to wait with another algorithm, with a worse big-O notation?. If so, you should consider the simpler, less efficient algorithm, as the overall solution would be more efficient).
A statement like
means that for some constant multiplier
c
, the expressioncn²
will eventually overtake4n² + 7n
. It's technically not incorrect to leave the coefficient in there —O(n²)
andO(4n²)
mean the exact same thing, because any constantc
for the former can be replaced byc/4
for the latter. However, such a thing is less clear, possibly misleading, and definitely nonstandard.It's O(n^2). Constant factors "move into the O". You only keep the largest exponent since this is the one dominating. And you can leave out coefficients since when comparing different algorithms even very large coefficients result in smaller total numbers than having a larger exponent (with n large enough).
The coefficients aren't relevant in Big O notation, so it's just O(n2). As Wikipedia explains:
Mathematically speaking, you would write O(4n²). It means that the complexity function of your algorithms behaves as n->4n² towards positive infinite.
But in computer science/algorithm, you would only write O(n²), which is sufficient to categorize your algorithm.
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.