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

前端 未结 5 904
故里飘歌
故里飘歌 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: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.)

提交回复
热议问题