Confused on big O notation

只愿长相守 提交于 2019-12-04 01:54:30

问题


According to this book, big O means:

f(n) = O(g(n)) means c · g(n) is an upper bound on f(n). Thus there exists some constant c such that f(n) is always ≤ c · g(n), for large enough n (i.e. , n ≥ n0 for some constant n0).

I have trubble understanding the following big O equation

3n2 − 100n + 6 = O(n2), because I choose c = 3 and 3n2 > 3n2 − 100n + 6;

How can 3 be a factor? In 3n2 − 100n + 6, if we drop the low order terms -100n and 6, aren't 3n2 and 3.n2 the same? How to solve this equation?


回答1:


Let's look at the definition you posted for f(n) in O(g(n)):

f(n) = O(g(n)) means c · g(n) is an upper bound on f(n). Thus there exists some constant c such that f(n) is always ≤ c · g(n), for large enough n (i.e. , n ≥ n0 for some constant n0).

So, we only need to find one 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.

Note that here I've switched to strict inequalities, which is really only a matter of taste, but I prefer this latter convention. Now, we can re-state the Big-O definition in possibly more easy-to-understand terms:

... we can say that f(n) is O(g(n)) if we can find a constant c such that f(n) is less than c·g(n) or all n larger than n0, i.e., for all n>n0.

Now, let's look at your function f(n)

f(n) = 3n^2 - 100n + 6                                   (*)

Let's describe your functions as a sum of it's highest term and another functions

f(n) = 3n^2 + h(n)                                       (**)
h(n) = 6 - 100n                                          (***)

We now study the behaviour of h(n) and f(n), respectively:

h(n) = 6 - 100n
what can we say about this expression?

    => if n > 6/100, then h(n) < 0, since 6 - 100*(6/100) = 0

        => h(n) < 0, given n > 6/100                     (i)

f(n) = 3n^2 + h(n)
what can we say about this expression, given (i)?

    => if n > 6/100, the f(n) = 3n^2 + h(n) < 3n^2

         => f(n) < c*n^2, with c=3, given n > 6/100      (ii)

Ok!

  • From (ii) we can choose constant c=3, given that we choose the other constant n0 as larger than 6/100. Lets choose the first integer that fulfils this: n0=1.

Hence, we've shown that (+) golds for constant set **(c,n0) = (3,1), and subsequently, f(n) is in O(n^2).

For a reference on asymptotic behaviour, see e.g.

  • https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-o-notation



回答2:


I'll take the liberty to slightly paraphrase the question to:

Why do

and

have the same asymptotic complexity.

For that to be true, the definition should be in effect both directions.


First:


let




Then for

the inequality is always satisfied.
The other way around:


let



We have a parabola opened upwards, therefore there is again some

after which the inequality is always satisfied.


回答3:


y=3n^2 (top graph) vs y=3n^2 - 100n + 6

Consider the sketch above. By your definition, 3n^2 only needs to be bigger than 3n^2 - 100n + 6 for large enough n (i.e. , n ≥ n0 for some constant n0). Let that n0 = 5 in this case (it could be something a little smaller, but it's clear which graph is bigger by n=5 so we'll just go with that).

Clearly from the graph, 3n^2 >= 3n^2 - 100n + 6 in the range we've plotted. The only way for 3n^2 - 100n + 6 to get bigger than 3n^2 then is for it to grow more steeply.

But the gradients of 3n^2 and 3n^2 - 100n + 6 are 6n and 6n - 100 respectively, so 3n^2 - 100n + 6 can't grow more steeply, therefore must always be underneath.

So your definition holds - 3n^2 - 100n + 6 <= 3n^2 for all n>=5




回答4:


I am not an expert, but this looks a lot similar to what we just had in our real analysis course.

Basically if you have something like f(n) = 3n^2 − 100n + 6, the "fastest growing" term "wins" the other terms, when you have really really big n.

So in this case 3n^2 surpasses what ever 100n is, when the n is really big.

Another example would be something like f(n) = n/n^2 or f(n) = n! * n^2.

The first one gets smaller, as n simply cannot "keep up" with n^2. In the second example n! clearly grows faster than n^2, so I guess the answer for that should be f(n) = n! then, because the n^2 technically stops mattering with big n.

And terms like +6, which have no n affecting them are constants and matter even less as they cannot grow even if n grows.

It is all about what happends when n is really big. If your n is 34934854385754385463543856, then n^2 is hell of a bigger than 100n, because n^2 = n * n = 34934854385754385463543856 * 34934854385754385463543856.



来源:https://stackoverflow.com/questions/34411786/confused-on-big-o-notation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!