Some standard books on Algorithms produce this:
0 ≤ f(n) ≤ c⋅g(n) for all n > n0
While defining big-O, can anyone explain to me what this means, using a strong example which can help me to visualize and understand big-O more precisely?
Some standard books on Algorithms produce this:
0 ≤ f(n) ≤ c⋅g(n) for all n > n0
While defining big-O, can anyone explain to me what this means, using a strong example which can help me to visualize and understand big-O more precisely?
Assume you have a function f(n)
and you are trying to classify it - is it a big O of some other function g(n)
.
The definition basically says that f(n)
is in O(g(n))
if there exists two constants C,N such that
f(n) <= c * g(n) for each n > N
Now, let's understand what it means.
Start with the n>N
part - it means, we do not "care" for low values of n
, we only care for high values, and if some (final number of) low values do not follow the criteria - we can silently ignore them by choosing N
bigger then them.
Have a look on the following example:
Though we can see that for low values of n: n^2 < 10nlog(n)
, the second quickly catches up and after N=10
we get that for all n>10
the claim 10nlog(n) < n^2
is correct, and thus 10nlog(n)
is in O(n^2)
.
The constant c
means we can also tolerate some multiple by constant factor, and we can still accept it as desired behavior (useful for example to show that 5*n
is O(n)
, because without it we could never find N
such that for each n > N
: 5n < n
, but with the constant c
, we can use c=6 and show 5n < 6n
and get that 5n
is in O(n)
.
This question is a math problem, not an algorithmic one.
You can find a definition and a good example here: https://math.stackexchange.com/questions/259063/big-o-interpretation
As @Thomas pointed out, Wikipedia also has a good article on this: http://en.wikipedia.org/wiki/Big_O_notation
If you need more details, try to ask a more specific question.