问题
Given a question, I try to solve it and assume that I have found an algorithm. Now I do Time Complexity analysis for that algorithm and find that it runs in polynomial time. Now How can I make sure that my algorithm runs only in polynomial time and not pseudopolynomial time?
Or simply I can put up my question like this
Is there any way to find if an algorithm takes pseudo polynomial time ?
For example:
function isPrime(n):
for i from 2 to n - 1:
if (n mod i) = 0, return false
return true
We think that the above solution works in polynomial time but actually this solution has pesudo polynomial complexity.
回答1:
Given a question, I try to solve it and assume that I have found an algorithm.
Assumed.
Now I do Time Complexity analysis for that algorithm and find that it runs in polynomial time.
Polynomial time with respect to what? The numeric value of the input or the length of that value's encoding?
Now How can I make sure that my algorithm runs only in polynomial time and not pseudopolynomial time?
The algorithm runs in polynomial time if the running time is bounded by a polynomial function of the length of the encoding of the input. It is pseudopolynomial if its running time is bounded by a polynomial function of the value of the input when it is interpreted as a number.
Or simply I can put up my question like this: Is there any way to find if an algorithm takes pseudo polynomial time ?
Be careful about whether you are computing the complexity w.r.t. the value of the input or the length of the input's representation on the computer (whether the computer is a real physical computer or a conceptual computer doesn't matter).
function isPrime(n):
for i from 2 to n - 1:
if (n mod i) = 0, return false
return true
We think that the above solution works in polynomial time but actually this solution has pesudo polynomial complexity.
The running time of this function is bounded by a polynomial function of n
and by an exponential function of the length of the input's encoding.
The key to determining the answer is realizing that while the input is n
, the input size is log_2(n)
(on computers that represent numbers as binary).
来源:https://stackoverflow.com/questions/45222550/how-to-find-if-an-algorithm-takes-pseudopolynomial-time