问题
I understand how to calculate a function's complexity for the most part. The same goes for determining the order of growth for a mathematical function. [I probably don't understand it as much as I think I do, which is why I'm probably asking this.] For instance:
an^3 + bn^2 + cn + d
could be written as O(n^3) in big-o notation, since for large enough n
the values of the term bn^2 + cn + d
are insignificant in comparison to an^3
(the constant coefficients a, b, c and d are left out as well, as their contribution to the value become insignificant, too).
What I don't understand is, how does this work when the leading term is involved in some sort of division? For instance:
a/n^3 + bn^2
or n^3/a + bn^2
Let n=100, a=1000 and b=10 for the former formula, then we have
n^3/a = 100^3/1000 = 1000
and bn^2 = 10*100^2 = 100,000
or even more dramatic for the latter - in this case the leading term is not only growing slowly as above, but it's also shrinking, isn't it?:
a/n^3 = 1000/100^3 = 0.001
and bn^2 = 100,000
as above.
In both cases the second term contributes much more, so isn't it n^2
that actually determines the order of growth?
It gets even more complicated (for me, at least) when the leading term is followed by a subtraction (a/n^3 - bn^2
) or when the second term is also a division (n^3/a + n^2/b)
or when both are divisions but in mixed order (a/n^3 + n^2/b
), etc.
The list seems endless, so my general question is, how to understand and handle formulas that involve division (and subtraction) in order to determine the order of growth for a given function?
回答1:
A division is just a multiplication by the multiplicative inverse, so n^3/a == n^3 * a^-1
, and you can handle it the same way as any other coefficient.
With regards to substraction a*n^3 - b*n^2 <= a*n^3
, so it is also in O(n^3)
. Also, a*n^3 - b*n^2 >= a/2 * n^3
for large enough values of n
, and it is also in Omega(n^3)
. A more detailed explanation about substraction can be found in: Algorithm complexity when faced with substraction in value
big O notation is generally used for increasing (don't have to be monotonically) functions, and a decreasing function such as a/n
is not a good fit for it, though O(1/n)
seems to be still perfectly defined, AFAIK, and it is a subset of O(1)
(unless you take into account only discrete functions). However, this has very little value for algorithm's analysis, as a complexity of an algorithm cannot really shrink..
回答2:
There's a very simple rule for the type of questions you posted.
Suppose you're trying to find the order of growth of f(n)
, and you find some simple function g(n)
such that
lim {n -> inf} f(n) / g(n) = k
where k
is a positive finite constant. Then
f(n) = Theta(g(n))
(It's easy to see this from the calculus definitions.)
Now let's see how this applies to your examples:
lim {n -> inf} (a/n^3 + bn^2) / n^2 = b
so it's Theta(n^2)
.
lim {n -> inf} (a n^3 - bn^2) / n^3 = a
so it's Theta(n^2)
.
(of course, assuming a and b are positive.)
来源:https://stackoverflow.com/questions/29983533/complexity-determining-the-order-of-growth