# Algorithm to determine if given algorithm runs in polynomial time [duplicate]

In general, the undecidability of the halting problem prohibits the general determination of an algorithm's complexity. However, I can see no reason why the halting problem prohibits one from deciding if a given algorithm runs in polynomial time, since the halting problem doesn't reduce to determining whether an algorithm is polynomial. So what research is there on it? Or what mistake am I making in my reasoning?

• You can easily modify this reduction cstheory.stackexchange.com/a/5006/4896 to show that deciding if a Turing machine runs in $O(n^2)$ or $\Omega(f(n))$ time, for any $f(n) = \omega(n^2)$, is undecidable. – Sasho Nikolov Feb 22 '15 at 5:03
• This is not a research-level question. It is, as you say, undecidable whether an algorithm terminates for a specific input. So how could it be decidable whether an algorithm terminates for all inputs and always within polynomial time? – David Richerby Feb 22 '15 at 9:34

It might not be the Halting Problem exactly, but it is also not possible to decide if a given algorithm runs in polynomial time or not. The reason is the same as many other undecidable problems; using Kleene's Recursion theorem, an algorithm can always query if itself is a polynomial time algorithm, and can adopt the opposite behavior.

Example properties of algorithms that are decidable are things such as:

• If an algorithm's source code has less than 1000 characters
• If an algorithm, on a given input $x$, will halt within 500 steps
• If an algorithm's source code has a particular symbol in it

That is, properties of an algorithm that have nothing to do with its execution (or only a limited part of its execution) tend to be decidable, while any nontrivial property of a program's execution will likely be undecidable.

Also, in general, any property of an algorithm related to its output is undecidable (this is formalized by Rice's theorem), although running time is not a property of its output.

• I agree with you philosophically, but is Rice's theorem really applicable to the question? Rice's theorem, in the statement I am familiar with, talks about the decidability of the problem "Does the language decided by Turing machine $M$ have property $P$?". This does not capture questions about the running time of $M$. – Sasho Nikolov Feb 22 '15 at 6:09
• @SashoNikolov: I think Rice's theorem does apply. The formal statement of Rice's theorem says that it's undecidable to determine whether the language a TM computes is in C for any non-trivial complexity class C. Here non-trivial means that there exists a TM deciding a language in C, and a TM deciding a language not in C. Clearly this holds when C = P. – Huck Bennett Feb 22 '15 at 6:27
• @HuckBennett P is non-trivial but the problem you are describing is "$L(M) \in \mathsf{P}?$". This is not the same as "does $M$ run in polynomial time?". For example $M$ could be the trial-division algorithm for deciding if the input is a prime number. This $M$ runs in exponential time but decides PRIMES, which is, famously, in P. – Sasho Nikolov Feb 22 '15 at 6:34
• Rice's theorem is applicable to the statement I made that immediately preceded it ('any property of an algorithm related to its output is undecidable') but is a bit of a non sequitur in relation to the rest of the post. I will edit to remove the confusion. – Joe Bebel Feb 22 '15 at 6:41
• Can you put a reference to the decidable examples you gave. Specifically the one with 500 steps? How do you prove this? – TheJKFever May 8 '15 at 2:04