Why does n^O(1) mean polynomial time?

后端 未结 1 622
無奈伤痛
無奈伤痛 2021-01-06 16:10

An algorithm runs in polynomial time if it\'s runtime is O(nk) for some k. However, I\'ve also seen polynomial time defined as time nO(1).

I ha

相关标签:
1条回答
  • 2021-01-06 16:34

    When you have an expression like "the runtime is O(n)" or "the runtime is O(n2)," the O(n) and O(n2) terms aren't actual functions. Instead, they're placeholders for some other function with some property. For example, take this statement:

    The runtime of the algorithm is O(n)

    This statement really means

    There is some function f(n) where the runtime of the algorithm is f(n) and f(n) = O(n)

    For example, if a function's actual runtime is 137n + 42, the statement "the runtime of the algorithm is O(n)" is true because there is some function (namely, f(n) = 137n + 42) where the runtime of the algorithm is f(n) and f(n) = O(n).

    Given this, let's think about what the statement "the runtime of the algorithm is nO(1)" means. This statement is equivalent to

    There is some function f(n) where the runtime of the algorithm is nf(n) and f(n) = O(1)

    Now that we've gotten the terminology clearer, what exactly does this mean? Intuitively, a function is O(1) if it's eventually bounded from above by some constant. Therefore, any function f(n) that's O(1) must satisfy f(n) ≤ k once n gets sufficiently large. Therefore, at least intuitively, nO(1) means "n raised to some power that's at most k," which sounds like the definition of a polynomial function.

    Of course, there's that pesky issue of constant factors. The function 137n3 is definitely O(n3), but it has a huge constant term in front. On the other hand, if we have a function of the form nO(1), there isn't a constant term in front of the n3. How do we handle this?

    This is where we can get cute with the math. In the case of 137n3, note that when n > 1, we have

    137n3 = nlogn137 n3 = n3 + logn 137

    Notice that this is n raised to the power of logn 137. Although it might look like the function logn 137 grows as n grows larger, it actually has the opposite behavior: it decreases as n grows. The reason for this is that we can use the change of base formula to rewrite logn 137 as

    logn 137 = log 137 / log n

    Which clearly decreases in the long term when log n decreases. Therefore, the expression 3 + logn137 ends up being bounded from above by some constant, so it's O(1).

    Using this technique, it's possible to convert O(nk) to nO(1) by choosing the exponent of n to be k plus the log base n of the constant factor in front of the nk term that comes up in the big-O notation. Similarly, we can convert back from nO(1) to O(nk) by choosing k to be any constant that upper-bounds the function hidden by the O(1) term in the exponent of n.

    Hope this helps!

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