CSE 111, Fall 2004

The Halting Problem

Last Update: 1 December 2004

Note: NEW or UPDATED material is highlighted


The Halting Problem provides an example of a non-computable problem.

Question: Is there a non-computable problem? That is, is there a problem such that there's no algorithm that computes its solution?

Note that if everything is computable, then "computability" isn't a very interesting concept! It turns out that there is a non-computable problem; moreover, it's an interesting problem, namely, the problem of determining whether any given computer program halts.

To prove that there is no computer program that can determine whether any arbitrary program halts, we need to prove that something does not exist. The best way to do this is to use a "proof by contradiction".

The Halting Problem: Is there a computer program (e.g., a program for a Turing machine) that takes two things as input:

  1. a computer program C, and
  2. C's input (suppose C's input is an integer; call it "i")
and that outputs:

Call the program (whose existence we're wondering about) "H". Since H's input is C and i, let's refer to H together with its two inputs as:

Notes:

  1. You can imagine that H(C,i) works as follows:

    1. H gives C its input, i,
    2. Then H simulates what C does with i.
    3. If C halts on i, then H outputs "yes" (i.e., "yes, C halts");
    4. Otherwise (if C loops on i), H outputs "no" (i.e., "no, C loops").

  2. However, we can't answer the question whether C halts on i by just running C on i, because:

Solution to the Halting Problem:
No! There is no such program H(C,i) that tells us whether a program halts on its input.

Proof (sketch):

Assume, for the sake of the argument, that there is such a program H.

Now consider another program, H*, which works as follows:
H* is just like H, except that:

Note: If H exists, so does H*. I.e., we can turn H into H* as follows:

Next, code C as a number, so that it can be treated as input to itself.

Now consider H*(C,C). (This step is called "diagonalization".)

I.e., suppose you code up program C as a number, use it as input to program C itself, and then let H* do its job on that pair of inputs. By the definition of H*, H*(C,C) loops if C halts on C, and it outputs "no" if C loops on C.

E.g., using our code above, the relevant line of H* will (replacing the infinite WHILE loop with the name "loop") become:

Now code H* as a number! And consider H*(H*,H*): In other words, code up H* as a number, use it as input to program H* itself, and then let H* do its job on that pair of inputs. Again, by the definition of H*, H*(H*,H*) loops if H* halts on H*, and it outputs "no" if H* loops on H*. But outputting "no" means that it halts!

E.g., using our code above, the relevant line of H* will (replacing the infinite WHILE loop with the name "loop") become:

So, If H* halts, then it loops, and if H* loops, then it halts. It loops if and only if it halts.

But that's a contradiction. So, there is no such H*. So, there is no such program as H. QED.

In other words, the Halting Problem is not computable.


Copyright © 2001 by William J. Rapaport (rapaport@cse.buffalo.edu)
file: 111F04/halting-problem-2004-12-01.html