Preparations
First, lets state, loosely, the definition of f
being in O(g(n))
(note: O(g(n))
is a set of functions, so to be picky, we say that f
is in O(...)
, rather than f(n)
being in O(...)
).
If a function f(n) is in O(g(n)), then c · g(n) is an upper bound on
f(n), for some constant c such that f(n) is always ≤ c · g(n),
for large enough n (i.e. , n ≥ n0 for some constant n0).
Hence, to show that f(n)
is in O(g(n))
, we need to find a set of constants (c, n0) that fulfils
f(n) < c · g(n), for all n ≥ n0, (+)
but this set is not unique. I.e., the problem of finding the constants (c, n0) such that (+) holds is degenerate. In fact, if any such pair of constants exists, there will exist an infinite amount of different such pairs.
Showing that f ∈ O(n^4)
Now, lets proceed and look at the example that confused you
Find an upper asymptotic bound for the function
f(n) = n^4 + 100n^2 + 50 (*)
One straight-forward approach is to express the lower-order terms in (*)
in terms of the higher order terms, specifically, w.r.t. bounds (... < ...
).
Hence, we see if we can find a lower bound on n
such that the following holds
100n^2 + 50 ≤ n^4, for all n ≥ ???, (i)
We can easily find when equality holds in (i) by solving the equation
m = n^2, m > 0
m^2 - 100m - 50 = 0
(m - 50)^2 - 50^2 - 50 = 0
(m - 50)^2 = 2550
m = 50 ± sqrt(2550) = { m > 0, single root } ≈ 100.5
=> n ≈ { n > 0 } ≈ 10.025
Hence, (i)
holds for n ≳ 10.025
, bu we'd much rather present this bound on n
with a neat integer value, hence rounding up to 11
:
100n^2 + 50 ≤ n^4, for all n ≥ 11, (ii)
From (ii)
it's apparent that the following holds
f(n) = n^4 + 100n^2 + 50 ≤ n^4 + n^4 = 2 · n^4, for all n ≥ 11, (iii)
And this relation is exactly (+)
with c = 2
, n0 = 11
and g(n) = n^4
, and hence we've shown that f ∈ O(n^4)
. Note again, however, that the choice of constants c
and n0
is one of convenience, that is not unique. Since we've shown that (+)
holds for on set of constants (c,n0
), we can show that it holds for an infinite amount of different such choices of constants (e.g., it naturally holds for c=10
and n0=20
, ..., and so on).