Labs ICT
โญ Pro Login

The Halting Problem

The most famous undecidable problem โ€” and why no algorithm can solve it.

The Halting Problem

The halting problem is the most famous undecidable problem in computer science. It asks: given a description of a Turing machine M and an input w, does M halt on w? Alan Turing proved in 1936 that no algorithm can solve this problem for all possible inputs.

The Proof (by Contradiction)

Assume there exists a TM H that decides the halting problem. H takes as input (M, w) where M is a TM description and w is an input. H outputs "accept" if M halts on w, and "reject" if M loops forever.

Construction of a paradoxical TM D:

  D takes as input a TM description โŸจMโŸฉ:
  1. Run H on input (M, M)
  2. If H says M halts on M โ†’ D loops forever
  3. If H says M loops on M โ†’ D halts

Now consider: what does D do on input โŸจDโŸฉ?

  Case 1: D halts on โŸจDโŸฉ
    โ†’ H(D, D) should say "halts"
    โ†’ But D is defined to LOOP when H says "halts"
    โ†’ Contradiction!

  Case 2: D loops on โŸจDโŸฉ
    โ†’ H(D, D) should say "loops"
    โ†’ But D is defined to HALT when H says "loops"
    โ†’ Contradiction!

Both cases lead to contradiction.
Therefore H cannot exist.
The halting problem is UNDECIDABLE.

Implications

The halting problem has profound consequences:

1. No perfect virus scanner exists
   (You can't detect all malicious programs that
   would harm your system)

2. No perfect optimizing compiler exists
   (You can't always determine if a code block
   is unreachable)

3. No general program verifier exists
   (You can't check if two programs are equivalent)

4. The Rice Theorem follows:
   ANY non-trivial property of the language
   recognized by a TM is undecidable.

Practical consequence:
  You can write programs that detect many bugs,
  but you can NEVER write a program that detects
  ALL bugs in all programs. There will always be
  edge cases that no static analyzer can handle.

Related Results

The halting problem is just the tip of the iceberg. Many related problems are also undecidable:

- Equivalence problem: Do two TMs recognize the
  same language? (Undecidable)

- Emptiness problem: Does a TM recognize the empty
  language? (Undecidable for TMs, decidable for DFAs)

- Membership problem: Does a TM accept a given
  string? (Recognizable but not decidable)

These results show that the limits of computation
are not edge cases โ€” they are fundamental and
widespread.

๐Ÿงช Quick Quiz

The halting problem is: