Undecidable Problems
Why some yes-or-no questions can never be answered correctly by any algorithm for every input, and how that differs from a problem that is only slow.
1Decidable problems
A decision problem is one with a yes or no answer. It is decidable if an algorithm can be written that gives a correct answer for every possible input.
“Is this number even?” is decidable. One short procedure answers it for any whole number:
PROCEDURE isEven(n)
{
RETURN(n MOD 2 = 0)
}
It was checked against every number from 0 to 2,000 and was right each time, and the reasoning shows why it can't fail: a number is even when, and only when, dividing by 2 leaves remainder 0. Many familiar questions are decidable the same way: Is this value in the list? Is this list sorted? Is this number prime?
2Undecidable problems
An undecidable problem is one for which no algorithm can be constructed that is always capable of giving a correct yes or no answer. It isn't that nobody has written the algorithm yet; it has been proven that no such algorithm can exist.
The best-known example is the halting problem: given any program and its input, will the program eventually stop, or run forever? In 1936, Alan Turing proved that no algorithm can answer this correctly for every program and input.
The idea behind the proof. Suppose someone claims to have a procedure willStop(program) that always answers correctly. Build a contrary program that asks willStop about itself and then does the opposite: if the answer is “it will stop,” it loops forever; if the answer is “it runs forever,” it stops at once. Whatever willStop says about the contrary program is wrong. So no procedure like willStop can be right about every program.
3Some inputs can still be answered
An undecidable problem may have some instances (specific inputs) that an algorithm can answer. What does not exist is one algorithm that answers all of them. Here are three instances of the halting problem:
| Program | Will it stop? | How we know |
|---|---|---|
x ← 4 | Yes | No loops at all: it runs three statements and stops (it displays 12). |
x ← 5 | No | x goes 5, 3, 1, −1, … It is always odd, so it never equals 0. |
n ← 27, halve if even, else 3n + 1, until n = 1 | Yes, for 27 | Running it shows it stops after 111 passes, climbing as high as 9,232 on the way. |
The third program is worth a closer look:
n ← 27
passes ← 0
REPEAT UNTIL (n = 1)
{
IF (n MOD 2 = 0)
{
n ← n / 2
}
ELSE
{
n ← 3 * n + 1
}
passes ← passes + 1
}
DISPLAY(passes)
It displays 111. Every starting number from 1 to 1,000 stops too (the slowest is 871, with 178 passes), and computers have checked an enormous range of starting numbers without finding one that runs forever. Yet no one has proved that it stops for every starting number. Each instance we try is answerable by running it; the general question is still open. That is the pattern undecidable problems have: individual cases can be settled, but no single algorithm settles them all.
A decidable problem has an algorithm that gives a correct yes or no answer for every input. An undecidable problem has no such algorithm, and this is proven, not just unsolved. Some instances of an undecidable problem can still be answered; no algorithm answers all of them.
4Why “just run it” doesn't work
It is tempting to decide “will it stop?” by running the program and watching. Here are two programs:
count ← 0
REPEAT UNTIL (count = 5000)
{
count ← count + 1
}
count ← 1
REPEAT UNTIL (count = 5000)
{
count ← count + 2
}
After 1,000 steps, both are still running and look the same. With 50,000 steps, Program 1 stops after 10,002 steps, but Program 2 is still going. Is it about to stop, or never? Watching longer can't settle it: whatever limit you pick, some program stops just after that limit. Here we can see that Program 2 counts only odd numbers and skips 5,000, but that is reasoning about this one program, not a method that works for every program.
5Too slow vs. impossible
Don't mix these up. An unreasonably slow problem has a correct algorithm that takes too long; a heuristic or more patience can help. An undecidable problem has no correct algorithm for all inputs at all; a faster computer, a cleverer programmer, or more time cannot change that.
6Worked examples
Is the number prime?
Is “Is the whole number n prime?” decidable? This procedure tries every possible divisor up to the square root of n.
PROCEDURE isPrime(n)
{
IF (n < 2)
{
RETURN(false)
}
d ← 2
REPEAT UNTIL (d * d > n)
{
IF (n MOD d = 0)
{
RETURN(false)
}
d ← d + 1
}
RETURN(true)
}
- 1The loop always ends:
dgrows by 1 each pass, sod * d > neventually becomes true. - 2When it ends, it has tried every possible divisor that matters, so the answer is correct.
- 3Results:
isPrime(1)→ false,isPrime(2)→ true,isPrime(91)→ false,isPrime(97)→ true. It matched a direct check for every n from 0 to 500.
No instance can be solved?
True or false: “Because the halting problem is undecidable, no program's stopping behavior can ever be determined.”
- 1An undecidable problem may have some instances with an algorithmic solution.
- 2A program with no loops always stops;
xcounting down by 2 from 5 never reaches 0, so that loop never stops.
A faster computer
A classmate says: “The halting problem is only undecidable because computers are too slow. A fast enough computer could do it.” Respond.
- 1Undecidable means no algorithm can always give a correct answer, regardless of time.
- 2A faster computer runs algorithms faster; it doesn't create an algorithm that cannot exist.
Three questions
Classify each yes-or-no question: (a) Is this list in increasing order? (b) Given any program and input, will it run forever? (c) Is there a delivery route under 500 miles through these 60 stops?
- 1(a) Compare each pair of neighbors once: an algorithm always answers, quickly.
- 2(b) This is the halting problem.
- 3(c) An algorithm can check every route, so it is decidable, but the number of routes grows factorially.
7Common mistakes
Some instances can be answered. Undecidable means no single algorithm answers every instance correctly.
Answering many instances, even millions, does not make a problem decidable. Decidable needs one algorithm that is right for all inputs.
Unreasonable: an algorithm exists but is too slow. Undecidable: no algorithm can always be correct. More speed only helps the first.
Hard or unsolved is not the same as undecidable. Undecidable is a proven fact about a specific problem.
“The halting problem is undecidable: no algorithm can be constructed that always correctly answers whether any program will stop. Some programs can still be analyzed, such as one with no loops, but no single algorithm works for every program. This is different from a problem that runs in unreasonable time, where an algorithm exists but is too slow.”
Use the definition, say that some instances can still be answered, and separate “impossible” from “too slow.”