# Is it possible to write a program that calculates an algorithm's time complexity? [duplicate]

Title is self explanatory.

Is it somehow related to Turing's Halting problem (which is undecidable)?

## marked as duplicate by Juho, David Richerby complexity-theory StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Dec 10 '18 at 16:38

Yes, you are right. There is no program that can calculate all algorithms' time complexity.

More formally speaking, there is no Turing machine which, given any Turing machine $$T$$ and an input of length $$n$$, it can calculate a finite upper bound for the computation time that will be used by $$T$$ and that input in case that computation does halt, or infinity in case that computation does not halt. The reason is simple, as you have mentioned, there is no Turing machine that can decide whether an arbitrarily computation will run forever or not in the first place. In fact, we cannot algorithmically tell even whether a given Turing machine halts on the empty input.

Notice that a trivial time complexity of all algorithms is an upper bound of running forever for any input. There are also trivial lower bounds such as whether the computation will run for less than for 7 steps. Those trivial time complexities have to be excluded from our discussion so as to keep our discussion interesting.

We can ask refined questions. For example, does an arbitrary Turing machine run in time $$O(1)$$? It turns out this question is undecidable as well.

For more information, you may want to read Verifying Time Complexity of Turing Machines by David Gajser.

• Thank you for the answer and the reference! – ihavenoidea Dec 10 '18 at 18:40
• You are welcome! – Apass.Jack Dec 10 '18 at 19:34

Yes, not possible. Halting problem can be reduced to this problem.

Let $$L_{com}=\{| M \ is \ a \ TM \ with\ n\ input \}$$ which is decided by $$M_{com}$$. Then it can be shown that Halting Problem $$D$$ can be reduced to this problem.

Let $$D = "On\ input\ "$$

• Encode/Map $$w$$ to unique number $$n$$ if $$w$$ is accepted by $$M$$ else make $$n=-1$$.
• Feed $$$$ to $$M_{com}$$, if complexity is positive accept otherwise reject.

Now, since we know Halting problem is undecidable and it is reducible to the problem of finding complexity of turing machine, the problem of finding complexity of turing machine is undecidable.

• Thanks for your answer, I still don't have the background to fully understand how to reduce an algorithm to another (and so to 100% understand your explanation) but I'll come back to your answer once I study more on this subject. – ihavenoidea Dec 10 '18 at 18:42
• Sure. You are welcome. @ihavenoidea – Mr. Sigma. Dec 11 '18 at 0:19