G27
Self-checkedTier 1 10 min readtoc.undecidability-halting

Halting problem and reductions

The first undecidable problems: the acceptance problem A_TM and the halting problem for Turing machines, proved undecidable by diagonalisation (and by counting machines against languages), the universal machine showing A_TM is recognisable, mapping reductions and how they transfer undecidability (A_TM to HALT, E_TM, EQ_TM), the unrecognisable complement of A_TM, and a map of which problems are decidable, recognisable, co-recognisable or neither.

Why it exists

Earlier pages showed that some problems about machines are decidable (is a DFA's language empty?). This page proves the opposite: no algorithm can decide whether a given Turing machine accepts a given input, or whether it halts. This is the most famous negative result of computer science and a staple of GATE: which of these problems is undecidable, which is recognisable, what does a reduction prove.

Prerequisites

You need the notions of decider and recogniser and the theorem about a language and its complement (decidable and recognisable languages).

Precise definitions

Fix an encoding ⟨M⟩\langle M \rangle of Turing machines as strings (TM variants). Then:

  • ATM={⟨M,w⟩:M is a Turing machine that accepts the string w}A_{TM} = \{\langle M, w \rangle : M \text{ is a Turing machine that accepts the string } w\} (the acceptance problem);
  • HALTTM={⟨M,w⟩:M halts on input w}HALT_{TM} = \{\langle M, w \rangle : M \text{ halts on input } w\} (the halting problem; halting means accepting or rejecting, not looping).

A mapping (many-one) reduction from AA to BB, written A≤mBA \le_m B, is a computable function ff (computed by a machine that always halts) with

w∈A  ⟺  f(w)∈Bfor every w.w \in A \iff f(w) \in B \quad \text{for every } w.

Reduction theorems. If A≤mBA \le_m B and BB is decidable then AA is decidable; if A≤mBA \le_m B and BB is recognisable then AA is recognisable. Contrapositive: if AA is undecidable and A≤mBA \le_m B then BB is undecidable. To prove a new problem undecidable, reduce a known undecidable problem to it.

How it works

ATMA_{TM} is recognisable

The universal machine UU on input ⟨M,w⟩\langle M, w \rangle simulates MM on ww and accepts if MM accepts. If MM loops, UU loops; so UU recognises ATMA_{TM} but does not decide it. (A program that decodes the description and simulates agrees with direct simulation on all test strings.)

ATMA_{TM} is undecidable: diagonalisation

Idea. List all Turing machines M1,M2,…M_1, M_2, \ldots (possible, because a machine is a finite string: all strings can be listed by length and alphabetically, and for every size there are finitely many machines; with kk states and gg tape symbols there are exactly (2kg+1)kg(2kg + 1)^{kg} transition tables, if each entry is a move or missing: 2525 for k=1k = 1, g=2g = 2, found by enumeration) and the strings s1,s2,…s_1, s_2, \ldots in the same way. Make a table with a row for each machine and a column for each string; the entry is "accepts" or not. The diagonal machine DD accepts sis_i exactly when MiM_i does not accept sis_i. Then DD differs from every MiM_i in the entry of column ii, so DD is not in the list: the language of DD is recognised by no machine of the list. If ATMA_{TM} were decidable, DD could be built, and DD is a machine, so it is in the list: contradiction.

Here is the table for six real machines (accepts, rejects, or runs for more than 600600 moves without halting) on the first six strings, with the diagonal row computed by flipping the diagonal:

The diagonal argument on six real machines: start

Computed by a script, not typed
machine \ inputεabaaabba
M1: recogniser: accepts strings containing b, loops on a*
M2: decider for a^n b^n
M3: accepts every string
M4: rejects every string
M5: decider: contains a
M6: decider: even length
D: accepts s_i exactly when M_i does not accept s_i

Step 0

Each row is a machine run on the strings ε, a, b, aa, ab, ba (the strings s_1 ... s_6). The last row is built from the diagonal by flipping it. The same construction works for an infinite list of all machines: the language of D is recognised by no machine in the list.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc5-traces.py

The last row differs from row ii at column ii, for every ii (a machine that does not halt does not accept, so the flipped entry is "accepts").

The proof in words (Turing's argument). Suppose a machine HH decides ATMA_{TM}. Build DD: on input ⟨M⟩\langle M \rangle, run HH on ⟨M,⟨M⟩⟩\langle M, \langle M \rangle \rangle and do the opposite (accept if HH rejects, reject if HH accepts). Run DD on its own description ⟨D⟩\langle D \rangle: DD accepts ⟨D⟩\langle D \rangle iff HH says DD does not accept ⟨D⟩\langle D \rangle, iff DD does not accept ⟨D⟩\langle D \rangle. Contradiction. So no such HH exists.

A counting version. The set of Turing machines is countable (strings over a finite alphabet), the set of languages over {0,1}\{0, 1\} is uncountable (it has the size of the set of infinite 0-1 sequences, by Cantor's diagonal argument). So most languages are recognised by no machine at all; the undecidable problems are not rare curiosities.

The halting problem

HALTTMHALT_{TM} is undecidable because ATM≤mHALTTMA_{TM} \le_m HALT_{TM}. Given ⟨M,w⟩\langle M, w \rangle construct ⟨M′,w⟩\langle M', w \rangle where M′M' is MM with every place where MM would halt and reject replaced by an endless loop (move right for ever). Then M′M' halts on ww exactly when MM accepts ww. A decider for HALTTMHALT_{TM} would therefore decide ATMA_{TM}. The table checks the equivalence on six machines and four strings each:

A_TM reduces to the halting problem: start

Computed by a script, not typed
pair (M, w)M on wM' on wM accepts ⇔ M' halts
M1 on ε
M1 on a
M1 on b
M1 on aa
M2 on ε
M2 on a
M2 on b
M2 on aa
M3 on ε
M3 on a
M3 on b
M3 on aa
M4 on ε
M4 on a
M4 on b
M4 on aa
M5 on ε
M5 on a
M5 on b
M5 on aa
M6 on ε
M6 on a
M6 on b
M6 on aa

Step 0

The reduction maps (M, w) to (M', w), where M' behaves like M but loops for ever wherever M would halt and reject. M' halts on w exactly when M accepts w. If the halting problem were decidable, so would be acceptance.

0 / 24
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc5-traces.py

For example the machine "reject every string" halts on every input but accepts none; its transformed version loops everywhere. HALTTMHALT_{TM} is recognisable (simulate and accept when the simulation halts).

Unrecognisable languages

ATMA_{TM} is recognisable and undecidable, so by the theorem of the previous page its complement ATM‾\overline{A_{TM}} is not recognisable. Likewise HALTTM‾\overline{HALT_{TM}} is not recognisable. These are the first examples of languages that no machine recognises in a natural way.

More undecidable problems by reduction

  • ETM={⟨M⟩:L(M)=∅}E_{TM} = \{\langle M \rangle : L(M) = \emptyset\} is undecidable: from ⟨M,w⟩\langle M, w \rangle build MwM_w that ignores its input, runs MM on ww and accepts if MM accepts. Then L(Mw)≠∅L(M_w) \ne \emptyset iff MM accepts ww (this is the construction behind Rice's theorem). ETME_{TM} is not recognisable but its complement (non-empty language) is recognisable: dovetail over all strings.
  • EQTMEQ_{TM} (do two machines recognise the same language) is undecidable: ETM≤mEQTME_{TM} \le_m EQ_{TM}, comparing MM with a machine that rejects everything.
  • REGULARTMREGULAR_{TM} (is the language regular) is undecidable (a similar reduction, quoted).
  • Post correspondence problem, equivalence of two context-free grammars, whether a grammar is ambiguous, Hilbert's tenth problem (integer roots of polynomials), and the validity of sentences in first-order logic are undecidable (quoted); the first is the usual starting point for proofs about grammars.

A map of the problems

problemdecidablerecognisablecomplement recognisable
ADFAA_{DFA}, EDFAE_{DFA}, EQDFAEQ_{DFA}, ACFGA_{CFG}, ECFGE_{CFG}yesyesyes
ATMA_{TM}, HALTTMHALT_{TM}noyesno
ATM‾\overline{A_{TM}}, HALTTM‾\overline{HALT_{TM}}nonoyes
ETME_{TM}nonoyes
EQTMEQ_{TM}nonono (quoted)

Worked examples

EX 1Is it decidable

Easy

Does a given DFA accept a given string: decidable. Does a given Turing machine accept a given string: undecidable. Does a given Turing machine halt within 10001000 moves on a given input: decidable (simulate 10001000 moves). Does a given Turing machine halt on a given input at all: undecidable.

EX 2Direction of a reduction

Easy

To show a new problem BB undecidable, reduce from an undecidable AA to BB (A≤mBA \le_m B). Reducing BB to AA would only show that BB is no harder than AA. From ATM≤mHALTTMA_{TM} \le_m HALT_{TM} we conclude HALTTMHALT_{TM} is undecidable; from HALTTM≤mATMHALT_{TM} \le_m A_{TM} (also true) we would conclude nothing about HALTTMHALT_{TM}.

EX 3Why the transformation works

Medium

Machine MM: rejects every string (no transitions). M′M': loops right for ever on every symbol. For any ww, MM halts (and rejects), M′M' never halts: so M∉ATMM \notin A_{TM}-with-ww and M′∉HALTTMM' \notin HALT_{TM}-with-ww, consistent with "M accepts ww iff M′M' halts on ww". Machine "accept every string": both accept and halt immediately. Rows of the table above show this for all six machines.

EX 4A recognisable, undecidable set

Medium

{⟨M⟩:M halts on the empty input}\{\langle M \rangle : M \text{ halts on the empty input}\} is recognisable (run MM on ε\varepsilon and accept if it halts) and undecidable (reduction from HALTTMHALT_{TM}: from ⟨M,w⟩\langle M, w \rangle build a machine that first writes ww and then behaves like MM). Its complement (MM runs for ever on ε\varepsilon) is not recognisable.

EX 5Typical questions

GATE level

(a) "The halting problem is:" undecidable but recursively enumerable. (b) "Which is undecidable: whether a TM accepts ww / whether a DFA accepts ww / whether a CFG is empty?" The first. (c) "The complement of the halting problem is r.e." False. (d) "If A≤mBA \le_m B and AA is undecidable then BB:" is undecidable. (e) "If A≤mBA \le_m B and BB is r.e. then AA:" is r.e. (f) "The set of all Turing machines that accept at least one string:" recursively enumerable and not recursive.

Cheat box

Cheat box

  • ATMA_{TM} and HALTTMHALT_{TM}: recognisable, undecidable; their complements are not recognisable.
  • Proof by diagonalisation: DD accepts ⟨M⟩\langle M \rangle iff MM does not accept ⟨M⟩\langle M \rangle; D(⟨D⟩)D(\langle D \rangle) contradicts itself.
  • Reduction A≤mBA \le_m B: w∈A  ⟺  f(w)∈Bw \in A \iff f(w) \in B; undecidable AA gives undecidable BB (reduce FROM the known one).
  • ATM≤mHALTA_{TM} \le_m HALT: replace rejecting halts by loops. ETME_{TM}, EQTMEQ_{TM}, REGULARTMREGULAR_{TM} undecidable.
  • Decidable for DFAs and CFGs: acceptance, emptiness (DFA equivalence too); not for TMs.

Traps and GATE patterns

Connections

The construction of MwM_w above is the key step of Rice's theorem (Rice's theorem); the distinction between recognisable and decidable comes from the previous page (decidable and recognisable languages); the universal machine and encoding come from the page on variants (TM variants).

PYQs

6 previous year questions from your book. I re-solved each one myself (0 by running or simulating, 6 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2018 · MCQ · 2 marksQ8 · page 242 of the book
Consider the following problems. L(G) denotes the
language generated by a grammar G. L(M) denotes the
language
accepted
by
a
machine
M.
(I) For an unrestricted grammar G and a string w,
whether w∈L(G).
(II) Given a Turing machine M, whether L(M) is
regular.
(III)Given two grammar G1 and G2, whether L(G1) =
L(G2).
(IV) Given an NFA N, whether there is a deterministic
PDA P such that N and P accept the same language.
Which one of the following statement is correct?
  • (a)Only I and II are undecidable
  • (b)Only III is undecidable
  • (c)Only II and IV are undecidable.
  • (d)Only I, II and III are undecidable.
GATE 2017 · Set 2 · MCQQ9 · page 242 of the book
Let L(R) be the language represented by regular
expression R. Let L(G) be the language generated by a
context free grammar G. Let L(M) be the language
accepted by a Turing machine M.
Which of the following decision problems are
undecidable?
I. Given a regular expression R and a string w, is w ∈
L(R)?
II. Given a context-free grammar G, is L(G) = ∅?
III. Given a context-free grammar G, is L(G) = Σ* for
some alphabet Σ?
IV. Given a Turing machine M and a string w, is w
∈L(M)?
  • (a)I and IV only
  • (b)II and III only
  • (c)II, III and IV only
  • (d)III and IV only
GATE 2016 · Set 2 · MCQ · 2 marksQ12 · page 242 of the book
Consider the following languages.
L1 = { ⟨M⟩ | M takes at least 2016 steps on some input },
L2 = { ⟨M⟩ | M takes at least 2016 steps on all inputs },
and
L3 = { ⟨M⟩ | M accepts ε },
Where for each Turing machine M, ⟨M⟩ denotes a specific
encoding of M.
Which one of the following is TRUE?
  • (a)L1 is recursive and L2, L3 are not recursive
  • (b)L2 is recursive and L1, L3 are not recursive
  • (c)L1, L2 are recursive and L3 is not recursive
  • (d)L1, L2, L3 are recursive
GATE 2014 · Set 2 · MCQ · 1 markQ16 · page 243 of the book
Let A≤mB denotes that language A is mapping reducible
(also known as many-to-one reducible) to language
B.Which one of the following is FALSE?
  • (a)If A≤mB and B is recursive then A is recursive.
  • (b)If A≤mB and A is undecidable then B is undecidable.
  • (c)If A≤mB and B is recursively enumerable then A is recursively enumerable
  • (d)If A≤mB and B is not recursively enumerable then A is not recursively enumerable
year not read yetQ18 · page 243 of the book
Which of the following is/are undecidable?
1. G is a CFG. Is L(G) = φ?
2. G is a CFG. Is L(G) = Σ*?
3. M is a Turing machine. Is L(M) regular?
4. A is a DFA and N is an NFA. Is L(A) = L(N)?
  • (a)3 only
  • (b)3 and 4 only
  • (c)1, 2 and 3 only
  • (d)2 and 3 only
GATE 2005 · MCQ · 2 marksQ28 · page 244 of the book
Consider three decision problems P1, P2 and P3. It is
known that P1 is decidable and P2 is undecidable.
  • (a)P3 is decidable if P1 is reducible to P3.
  • (b)P3 is undecidable if P3 is reducible to P2
  • (c)P3 is undecidable if P2 is reducible to P3
  • (d)P3 is decidable if P3 is reducible to P2's complement

Self-check

Question 1

Why is ATMA_{TM} recognisable but not decidable?

Show answer

A universal machine accepts exactly the accepted pairs, but a decider would allow the diagonal machine, which accepts ⟨M⟩\langle M \rangle iff MM does not, and cannot exist.

Question 2

What does the reduction ATM≤mHALTTMA_{TM} \le_m HALT_{TM} do to the machine?

Show answer

It makes every rejecting halt of MM an endless loop, so the new machine halts on ww exactly when MM accepts ww.

Question 3

Is the complement of the halting problem recognisable?

Show answer

No: if both it and the halting problem were recognisable, the halting problem would be decidable.

Question 4

To prove a problem BB undecidable, which way must the reduction go?

Show answer

From a known undecidable problem AA to BB: A≤mBA \le_m B.

Revision card

One-screen revision

  • ATMA_{TM}, HALTHALT: recognisable, undecidable. Complements: not recognisable.
  • Diagonalisation: DD does the opposite of MM on ⟨M⟩\langle M \rangle.
  • A≤mBA \le_m B: undecidable AA ⇒\Rightarrow undecidable BB. ATM≤mHALTA_{TM} \le_m HALT, ETME_{TM}, EQTMEQ_{TM}.
  • DFA and CFG acceptance/emptiness decidable; TM versions not.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 4.2 (the diagonalisation method, Theorem 4.11) and Section 5.1 (undecidable problems from language theory, Theorem 5.1)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Chapter 9 (undecidability), Sections 9.1 to 9.3

Verification

scripts/enum-checks/toc_compute.py: the diagonal table (six real machines on six strings, each entry recomputed with a second simulator) has a diagonal row that flips the diagonal and so differs from every machine in its own column; the number of machines with k states and g tape symbols is (2kg + 1)^(kg) (brute-force enumeration for k = 1, g = 2 gives 25), so machines can be listed by size; the reduction M -> M' (every halting rejection replaced by an endless loop) was checked on six machines and all strings up to length 7: M' halts exactly when M accepts, M' never halts and rejects, and M' makes the same number of moves as M when M accepts; the universal program decodes a machine description and simulates it with the same verdict and number of moves as direct simulation. The proofs that A_TM and HALT_TM are undecidable (self-reference, no machine is simulated by the decider), the transfer by reductions to E_TM, EQ_TM and REGULAR_TM, the Post correspondence problem, Hilbert's tenth problem and the classification of EQ_TM as neither recognisable nor co-recognisable are standard textbook results, QUOTED and not machine-proved (needs verification). Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never