AP Computer Science Principles · Study guide

Undecidable Problems

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

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:

ProgramWill it stop?How we know
x ← 4
y ← x * 3
DISPLAY(y)
YesNo loops at all: it runs three statements and stops (it displays 12).
x ← 5
REPEAT UNTIL (x = 0)
{
x ← x - 2
}
Nox goes 5, 3, 1, −1, … It is always odd, so it never equals 0.
n ← 27, halve if even, else 3n + 1, until n = 1Yes, for 27Running 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.

The key idea

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:

Program 1
count ← 0
REPEAT UNTIL (count = 5000)
{
  count ← count + 1
}
Program 2
count ← 1
REPEAT UNTIL (count = 5000)
{
  count ← count + 2
}
Figure 1. If a program stops, watching proves it. If it hasn't stopped yet, watching can't tell “never” from “not yet.”

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

Figure 2. “Too slow” is about time: an algorithm exists. “Undecidable” is about existence: no algorithm can always be right, however much time it gets.

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

Worked Example 1 Decidable or not?

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)
}
  1. 1
    The loop always ends: d grows by 1 each pass, so d * d > n eventually becomes true.
  2. 2
    When it ends, it has tried every possible divisor that matters, so the answer is correct.
  3. 3
    Results: isPrime(1) → false, isPrime(2) → true, isPrime(91) → false, isPrime(97) → true. It matched a direct check for every n from 0 to 500.
Answer: Decidable: this algorithm gives a correct yes or no for every whole number.
Worked Example 2 True or false

No instance can be solved?

True or false: “Because the halting problem is undecidable, no program's stopping behavior can ever be determined.”

  1. 1
    An undecidable problem may have some instances with an algorithmic solution.
  2. 2
    A program with no loops always stops; x counting down by 2 from 5 never reaches 0, so that loop never stops.
Answer: False. Many instances can be answered; what can't exist is one algorithm that answers every instance.
Worked Example 3 Respond to a claim

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.

  1. 1
    Undecidable means no algorithm can always give a correct answer, regardless of time.
  2. 2
    A faster computer runs algorithms faster; it doesn't create an algorithm that cannot exist.
Answer: Incorrect. Speed helps problems that are slow, not problems that are undecidable.
Worked Example 4 Classify

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. 1
    (a) Compare each pair of neighbors once: an algorithm always answers, quickly.
  2. 2
    (b) This is the halting problem.
  3. 3
    (c) An algorithm can check every route, so it is decidable, but the number of routes grows factorially.
Answer: (a) decidable and reasonable; (b) undecidable; (c) decidable but unreasonably slow.

7Common mistakes

Common Mistake 1 · Thinking “undecidable” means “no case can be solved”

Some instances can be answered. Undecidable means no single algorithm answers every instance correctly.

Common Mistake 2 · Thinking some solved cases make it decidable

Answering many instances, even millions, does not make a problem decidable. Decidable needs one algorithm that is right for all inputs.

Common Mistake 3 · Mixing up undecidable and unreasonable

Unreasonable: an algorithm exists but is too slow. Undecidable: no algorithm can always be correct. More speed only helps the first.

Common Mistake 4 · Calling a problem undecidable because it is hard

Hard or unsolved is not the same as undecidable. Undecidable is a proven fact about a specific problem.

Explain undecidability like this

“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.”