What is an easy way for finding C and N when proving the Big-Oh of an Algorithm?

前端 未结 5 902
故里飘歌
故里飘歌 2020-12-24 09:14

I\'m starting to learn about Big-Oh notation.

What is an easy way for finding C and N0 for a given function?

Say, for example:

(n+1)5

相关标签:
5条回答
  • 2020-12-24 09:32

    Usually the proof is done without picking concrete C and N0. Instead of proving f(n) < C * g(n) you prove that f(n) / g(n) < C.

    For example, to prove n3 + n is O(n3) you do the following:

    (n3 + n) / n3 = 1 + (n / n3) = 1 + (1 / n2) < 2 for any n >= 1. Here you can pick any C >= 2 with N0 = 1.

    0 讨论(0)
  • 2020-12-24 09:34

    It's going to depend greatly on the function you are considering. However, for a given class of functions, you may be able to come up with an algorithm.

    For instance, polynomials: if you set C to any value greater than the leading coefficient of the polynomial, then you can solve for N0.

    0 讨论(0)
  • 2020-12-24 09:36

    After you understand the magic there, you should also get that big-O is a notation. It means that you do not have to look for these coefficients in every problem you solve, once you made sure you understood what's going on behind these letters. You should just operate the symbols according to the notaion, according to its rules.

    There's no easy generic rule to determine actual values of N and c. You should recall your calculus knowledge to solve it.

    The definition to big-O is entangled with definition of the limit. It makes c satisfy:

    c > lim |f(n)/g(n)|, given n approaches +infinity.

    If the sequence is upper-bounded, it always has a limit. If it's not, well, then f is not O(g). After you have picked concrete c, you will have no problem finding appropriate N.

    0 讨论(0)
  • 2020-12-24 09:49

    You can pick a constant c by adding the coefficients of each term in your polynomial. Since

    | n5 + 5n4 + 0n3 + 10n2 + 5n1 + 1n0 | <= | n5 + 5n5 + 0n5 + 10n5 + 5n5 + 1n5 |

    and you can simplify both sides to get

    | n5 + 5n4 + 10n2 + 5n + 1 | <= | 22n5 |

    So c = 22, and this will always hold true for any n >= 1.

    It's almost always possible to find a lower c by raising N0, but this method works, and you can do it in your head.

    (The absolute value operations around the polynomials are to account for negative coefficients.)

    0 讨论(0)
  • 2020-12-24 09:49

    You can check what the lim abs(f(n)/g(n)) is when n->+infitity and that would give you the constant (g(n) is n^5 in your example, f(n) is (n+1)^5).

    Note that the meaning of Big-O for x->+infinity is that if f(x) = O(g(x)), then f(x) "grows no faster than g(x)", so you just need to prove that lim abs(f(x)/g(x)) exists and is less than +infinity.

    0 讨论(0)
提交回复
热议问题