Unit 4 · Algorithms & Simulation · Lesson 4.9

Undecidable Problems

Some problems aren’t slow or hard. They’re impossible — no algorithm can exist, on any computer, ever. Meet the most famous one, and learn to never confuse “impossible” with “takes forever.”

Big Idea 3 — Algorithms & ProgrammingCED 3.18 · recognition, not proof

Before this lesson

No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Programs, Algorithms region — the far edge of it. 4.7 found problems that are slow. Today’s problems aren’t slow. No algorithm for them can exist at all, on any computer, ever.

2

The Hook

Two cards.

Card 1: “The sentence on Card 2 is true.”

Card 2: “The sentence on Card 1 is false.”

Is Card 1 true? Then Card 2 is true, so Card 1 is false. Is Card 1 false? Then Card 2 is false, so Card 1 is true. There is no answer that works. Sit with that for a second — because a computer program can be built to do exactly the same thing.

3

The Idea

A decision problem has a yes-or-no answer: “Is this number even?” “Is this name in the list?” A problem is decidable if an algorithm can be written that gives the correct yes-or-no answer for every input.

An undecidable problem is one for which no algorithm can be constructed that always gives a correct yes-or-no answer. Not a slow one. Not a hard one. None.

The famous one: the halting problem

“Given any program and its input, will the program eventually stop, or run forever?” For lots of individual programs you can tell. But suppose someone claims they wrote halts(program, input) that answers correctly for every program. Build this:

Python

def contrarian(program):
    if halts(program, program):
        while True:    # loop forever
            pass
    else:
        return        # stop at once

AP Pseudocode

PROCEDURE contrarian(program)
{ IF (halts(program, program))
  { REPEAT UNTIL (false)
    { } }
  ELSE
  { RETURN } }

contrarian asks halts what a program will do, then does the opposite. Now feed contrarian to itself. Whatever halts predicts, contrarian does the other thing — Card 1 and Card 2 again. So a perfect halts can’t exist. That’s all you need: the exam asks you to recognize undecidable problems, not prove anything.

Unreasonable ≠ undecidable. This is the most-missed distinction of the unit.
Unreasonable time (4.7)Undecidable (today)
Is there an algorithm?YesNo — can’t exist
The problemIt would take too longNo algorithm always answers correctly
Faster computer helps?A little — never enoughNot at all
ExampleBest route through 50 citiesThe halting problem

If an answer choice says “undecidable because it would take too long,” it’s wrong.

Some instances can still be answered. An undecidable problem can have plenty of cases where you can tell — you’ll do some below. What doesn’t exist is one algorithm that handles all of them.
4

Try It — Be the Halting Checker

Part 1. For these three small programs, you can decide. Will each one stop?

Part 2. Now you’re halts, and someone runs contrarian(contrarian). What do you predict?

5

Vocabulary

decidable problem Programs
A yes-or-no problem for which an algorithm can be written that gives the correct answer for every input. (“Is this number even?”)
undecidable problem Programs
A problem for which no algorithm can be constructed that always gives a correct yes-or-no answer. Some instances may be solvable; all of them never are.
halting problem Programs
Deciding whether any given program will eventually stop on a given input. The standard example of an undecidable problem.
6

Check

Five questions. At least one is baited with “it would take too long.”

0 of 5 answered
7

Impact Check

The perfect virus scanner can’t exist

Could someone write a program that looks at any other program and correctly says whether it will ever do something malicious? It runs into the same wall as the halting problem: in general, deciding what an arbitrary program will eventually do is undecidable.

So real antivirus software uses — you guessed it — heuristics: suspicious patterns, known signatures, odd behavior. It catches most threats and misses some, forever. That’s not a failure of effort. It’s a limit of computing itself.

8

Connections

Came from 4.7 — unreasonable time was the first limit. This is the second, and harder, one.
Came from 4.8 — heuristics are how real software works around limits it can’t remove.
Returns in 4.13 — the three-way sort: efficient, unreasonable, or undecidable.
Returns on the exam — recognition only. Know that undecidable problems exist, know the halting problem, and never confuse “impossible” with “slow.”
← 4.8 Heuristics