Computer Science and IT‹›⌂ 0:00 studied

Module 7 · 15 min read · Free chapter

Turing machines and undecidability

In short

A Turing machine reads and writes an infinite tape and can carry out any algorithm. A language is recursive if some machine always halts with the answer, and RE if some machine accepts it. The halting problem is RE but undecidable, and Rice's theorem makes every non-trivial property of a machine's language undecidable.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: a clerk with an endless register

Picture a clerk at a railway reservation counter with a very long paper register, a pencil and an eraser. He can look at only one box of the register at a time, but he can move his finger one box left or right, rub out what is written and write something new. He also carries a small card of rules, such as "if you are on rule 3 and see an aa, write XX, move right and switch to rule 4".

That clerk is a Turing machine. A finite automaton could only read once, from left to right, and a pushdown automaton could only use the top of a stack. The clerk can go back, reread and overwrite as often as he likes, and his register never runs out.

It turns out that this simple clerk can carry out every algorithm that any computer can. But it also turns out that there are perfectly clear yes-or-no questions that no clerk, and no computer, can ever answer correctly for every input. Such questions are called undecidable, and this chapter proves that they exist.

Why it matters. Undecidability is why no program can check, in general, whether another program will hang. GATE asks about it every year in the form "which of these problems is decidable?", and the answers follow from a few clean theorems.

The Turing machine and a worked example

A Turing machine (TM) is a 7-tuple M=(Q,Σ,Γ,δ,q0,B,F)M = (Q, \Sigma, \Gamma, \delta, q_0, B, F). Here Γ\Gamma is the tape alphabet, which contains the input alphabet Σ\Sigma and the blank BB, and the tape is infinite. The transition function δ:Q×Γ→Q×Γ×{L,R}\delta : Q \times \Gamma \to Q \times \Gamma \times \{L, R\} reads the symbol under the head, writes a symbol, moves one cell left or right, and changes state.

So δ(q0,a)=(q1,X,R)\delta(q_0, a) = (q_1, X, R) means: in state q0q_0 reading aa, write XX, move right and enter q1q_1. The machine halts when no move is defined, and it accepts if it enters a final state. On some inputs a TM may never halt at all, and this possibility is the source of everything interesting in this chapter.

Let us build a TM for {anbn∣n≥1}\{a^n b^n \mid n \ge 1\}. The plan is to cross off one aa and one matching bb in each round, writing XX over the aa and YY over the bb:

  • In q0q_0, an aa is replaced by XX and the machine moves right into q1q_1; a YY means all aa's are done, so it moves right into q3q_3.
  • In q1q_1, the machine skips aa's and YY's moving right, and the first bb is replaced by YY as it turns left into q2q_2.
  • In q2q_2, the machine skips aa's and YY's moving left, and on reaching an XX it steps right into q0q_0.
  • In q3q_3, the machine skips YY's moving right, and a blank sends it into the accepting state q4q_4.

On aabbaabb the tape goes aabb→XaYb→XXYYaabb \to XaYb \to XXYY, and then q3q_3 walks over YYYY to the blank and accepts. On aabaab the machine finds no bb for the second XX and halts in q1q_1 without accepting. Each round walks across the input, so the machine takes O(n2)O(n^2) moves.

Why it matters. This marking trick is how a TM does what a PDA cannot, such as checking anbncna^n b^n c^n with a third mark ZZ. Writing on the tape gives it unlimited, rewritable memory.

Variants are equivalent, and the Church–Turing thesis

People have tried to make Turing machines stronger in many ways: several tapes, several tracks on one tape, a tape infinite in both directions, an option to stay in place, or nondeterministic choices. Every one of these accepts exactly the same languages as the basic machine. They may be faster or more convenient, but they never decide anything new.

The costs of simulation differ, and GATE sometimes asks about them. A single-tape machine simulates tt steps of a multi-tape machine in O(t2)O(t^2) steps, by keeping all tapes side by side as tracks. A deterministic machine simulates a nondeterministic one by trying all branches in breadth-first order, which can take exponential time.

Even weak-looking machines reach the same power once they have enough memory. A finite automaton with two stacks can simulate a tape, using one stack for the cells to the left of the head and one for the cells to the right. So a two-stack PDA is as powerful as a Turing machine, while a one-stack PDA is not.

Because so many different models agree, we accept the Church–Turing thesis: every function that can be computed by an algorithm, in the intuitive sense, can be computed by a Turing machine. It is a thesis, not a theorem, because "algorithm" in the everyday sense has no formal definition to prove it against. Every real programming language is equivalent to a TM, given unlimited memory.

Why it matters. The thesis lets us say "no algorithm exists" when we prove "no Turing machine exists". That is what makes undecidability results statements about all computers, not about one model.

Recursive and recursively enumerable languages

Because a TM may loop, there are two levels of "solving" a language. A language LL is recursive, or decidable, if some TM halts on every input and accepts exactly the strings in LL. Such a machine is called a decider, and it always answers yes or no.

A language is recursively enumerable (RE) if some TM accepts exactly the strings in LL. On a string in LL the machine eventually says yes, but on a string outside LL it may say no or may run forever. Every recursive language is RE, but not the other way round.

The key link between them is the complement theorem: LL is recursive if and only if both LL and L‾\overline{L} are RE. Given recognisers for both, run them side by side, one step each in turn; exactly one of them must eventually accept, and that tells you the answer. It follows that if LL is RE but not recursive, then L‾\overline{L} is not even RE.

Operation Recursive RE
Union and intersection Yes Yes
Complement Yes No
Concatenation and star Yes Yes
Difference Yes No

For union of RE languages, the two recognisers must again be run in parallel. If you run the first one to completion, it may loop forever on a string that the second one would have accepted.

Why it matters. Recursive means "has an algorithm that always answers", and RE means "has a search that confirms yes-answers". Many GATE options turn on this difference, especially around complements.

The halting problem, proved by diagonalisation

Every TM can be written as a finite string, called its code ⟨M⟩\langle M \rangle, so a TM can receive another TM as input. The halting problem asks: given ⟨M⟩\langle M \rangle and an input ww, does MM halt on ww? We now prove that no TM can decide it.

Suppose, for contradiction, that a decider HH exists, so that H(⟨M⟩,w)H(\langle M \rangle, w) always halts and says "yes" if MM halts on ww and "no" otherwise. Build a new machine DD that takes a code ⟨M⟩\langle M \rangle, runs H(⟨M⟩,⟨M⟩)H(\langle M \rangle, \langle M \rangle), and then does the opposite. If HH says "yes", DD loops forever, and if HH says "no", DD halts.

Now feed DD its own code. By the way DD was built:

D(⟨D⟩) halts  ⟺  H(⟨D⟩,⟨D⟩)=no  ⟺  D(⟨D⟩) does not haltD(\langle D \rangle) \text{ halts} \iff H(\langle D \rangle, \langle D \rangle) = \text{no} \iff D(\langle D \rangle) \text{ does not halt}

This is impossible, so HH cannot exist, and the halting problem is undecidable. The argument is called diagonalisation because DD is built to differ from every machine MiM_i on the diagonal entry, the input ⟨Mi⟩\langle M_i \rangle, so DD cannot be any row of the table.

The same idea settles the acceptance problem ATM={⟨M,w⟩∣M accepts w}A_{\mathit{TM}} = \{\langle M, w \rangle \mid M \text{ accepts } w\}. It is RE, because a universal TM can simulate MM on ww and accept if MM does. It is not recursive, by the argument above, so by the complement theorem ATM‾\overline{A_{\mathit{TM}}} is not even RE.

There is also a counting reason why hard languages must exist. Every TM is a finite string, so there are only countably many TMs. But the languages over Σ\Sigma are the subsets of Σ∗\Sigma^*, and there are uncountably many of them, so most languages have no TM at all.

Why it matters. This proof is the root of every undecidability result you will meet. Descriptive papers ask for it in full, and GATE asks for its consequences, such as which languages are RE but not recursive.

Reductions and Rice's theorem

Once one problem is known to be undecidable, we prove others undecidable by reduction. Problem AA reduces to problem BB, written A≤mBA \le_m B, if a computable function ff turns every instance ww of AA into an instance f(w)f(w) of BB with w∈A  ⟺  f(w)∈Bw \in A \iff f(w) \in B. Then a decider for BB would give a decider for AA.

So the rules are: if A≤mBA \le_m B and BB is decidable, AA is decidable, and if A≤mBA \le_m B and AA is undecidable, BB is undecidable. The same holds for RE: if BB is RE, so is AA. The direction matters, because hardness flows from AA to BB.

Example. Is "does MM halt on the blank tape?" decidable? Reduce the halting problem to it. Given ⟨M,w⟩\langle M, w \rangle, build a machine M′M' that erases its own input, writes ww and then runs MM. Then M′M' halts on the blank tape exactly when MM halts on ww, so a decider for the blank-tape problem would decide halting, which is impossible.

Doing such a reduction for every question is tiring, and Rice's theorem does them all at once. It says that every non-trivial property of the language of a TM is undecidable. A property is non-trivial if some RE languages have it and some do not.

So all of these are undecidable: is L(M)L(M) empty, is it finite, is it regular, is it context-free, does it contain ε\varepsilon, and is L(M)=Σ∗L(M) = \Sigma^*. But Rice's theorem says nothing about properties of the machine rather than its language. "Does MM have more than 5 states?" is decidable by reading the code, and "does MM move left within its first 10 steps on input ε\varepsilon?" is decidable by simulating 10 steps.

Why it matters. Rice's theorem answers most "is this decidable?" questions in seconds. The only skill is to check whether the question is about the language or about the machine's own structure or a bounded number of steps.

The Chomsky hierarchy and the decidability table

All the machines of this module fit into one ladder, the Chomsky hierarchy. Each level has a kind of grammar and a kind of machine, and each level is strictly larger than the one below it. A linear bounded automaton (LBA) is a TM that may use only the tape cells holding its input.

Type Grammar Production form Machine
Type 0 Unrestricted (RE) α→β\alpha \to \beta Turing machine
Type 1 Context-sensitive α→β\alpha \to \beta, ∣α∣≤∣β∣\lvert \alpha \rvert \le \lvert \beta \rvert Linear bounded automaton
Type 2 Context-free A→αA \to \alpha Pushdown automaton
Type 3 Regular A→aBA \to aB or A→aA \to a Finite automaton

Putting the deterministic and decidable classes in their places, the full chain of strict containments is:

Regular⊂DCFL⊂CFL⊂CSL⊂Recursive⊂RE\text{Regular} \subset \text{DCFL} \subset \text{CFL} \subset \text{CSL} \subset \text{Recursive} \subset \text{RE}

The table below collects the decidability results of this whole module. Read a row to see how far up the ladder a question stays decidable.

Problem Regular CFL CSL Recursive RE
Membership D D D D U
Emptiness D D U U U
Finiteness D D U U U
Equivalence D U U U U
Universality, is L=Σ∗L = \Sigma^* D U U U U
Is the intersection of two such languages empty D U U U U
Closed under complement Yes No Yes Yes No

Here D means decidable and U means undecidable. A useful tool behind several U entries is the Post Correspondence Problem (PCP): given pairs of strings, can you pick a sequence of pairs whose top halves and bottom halves spell the same string? PCP is undecidable, and reducing it to grammars shows that CFG ambiguity and CFL intersection-emptiness are undecidable.

Why it matters. A GATE question on decidability almost always lands on one cell of this table. Remember the pattern that regular languages are decidable in every row and RE languages in almost none. Then you can answer without any derivation.

Descriptive lens: how written papers ask this

Written papers ask about Turing machines and undecidability in four ways:

  1. Design a Turing machine for a language, such as anbncna^n b^n c^n, and trace it on a short input.
  2. Define and contrast recursive and recursively enumerable languages, with their closure properties.
  3. Prove that the halting problem is undecidable, or use a reduction to prove another problem undecidable.
  4. Explain the Church–Turing thesis, Rice's theorem or the Chomsky hierarchy, with examples.

Practice questions (our own, not previous-year questions).

  1. Design a Turing machine that accepts {anbncn∣n≥1}\{a^n b^n c^n \mid n \ge 1\}, and trace its run on aabbccaabbcc. (10 marks)
  2. Prove that a language LL is recursive if and only if both LL and L‾\overline{L} are recursively enumerable. (10 marks)
  3. Prove that the halting problem for Turing machines is undecidable. (15 marks)

Answer plan for question 3.

  • State the halting problem precisely: given the code ⟨M⟩\langle M \rangle and an input ww, decide whether MM halts on ww.
  • Explain that every TM has a finite code, so a machine can take another machine, or itself, as input.
  • Assume for contradiction that a decider HH exists that always halts with the correct answer.
  • Construct DD, which runs H(⟨M⟩,⟨M⟩)H(\langle M \rangle, \langle M \rangle) and loops if the answer is yes but halts if the answer is no.
  • Run DD on ⟨D⟩\langle D \rangle and show that it halts exactly when it does not halt, which is a contradiction.
  • Conclude that HH cannot exist, and add that the halting problem is RE but not recursive, so its complement is not RE.

Quick revision

  • A TM is (Q,Σ,Γ,δ,q0,B,F)(Q, \Sigma, \Gamma, \delta, q_0, B, F) with δ:Q×Γ→Q×Γ×{L,R}\delta : Q \times \Gamma \to Q \times \Gamma \times \{L, R\} and an infinite tape.
  • Multi-tape, nondeterministic and two-way tape machines, and two-stack PDAs, are all equivalent to the basic TM.
  • A single tape simulates tt multi-tape steps in O(t2)O(t^2) steps.
  • The Church–Turing thesis says every algorithm is a TM, and it is a thesis, not a theorem.
  • Recursive means a TM halts on every input, and RE means a TM accepts exactly the language.
  • LL is recursive exactly when LL and L‾\overline{L} are both RE.
  • The halting problem and ATMA_{\mathit{TM}} are RE but not recursive, and their complements are not RE.
  • If A≤mBA \le_m B and AA is undecidable, then BB is undecidable.
  • Rice's theorem makes every non-trivial property of L(M)L(M) undecidable.
  • The hierarchy is regular ⊂ DCFL ⊂ CFL ⊂ CSL ⊂ recursive ⊂ RE, with FA, PDA, LBA and TM as machines.
  • Membership is decidable up to recursive, emptiness only up to CFL, and equivalence only for regular languages.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. The transition function of a standard Turing machine is:

  2. A Turing machine does not accept a string ww. Then on ww it:

  3. Which machine is as powerful as a Turing machine?

  4. A single-tape TM simulates tt steps of a multi-tape TM in:

  5. The Church–Turing thesis is:

  6. If LL and L‾\overline{L} are both recursively enumerable, then LL is:

  7. Recursively enumerable languages are NOT closed under:

  8. The language ATM={⟨M,w⟩∣M accepts w}A_{\mathit{TM}} = \{\langle M, w \rangle \mid M \text{ accepts } w\} is:

  9. Why must some languages be non-RE?

  10. If A≤mBA \le_m B and AA is undecidable, then:

  11. By Rice's theorem, which problem is undecidable?

  12. Context-sensitive languages are accepted by:

  13. Which chain of strict containments is correct?

  14. Emptiness is decidable for which classes?

  15. Which problem is decidable?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Why is the halting problem undecidable?

A decider for it would let us build a machine that halts on its own code exactly when it does not, which is impossible.

What is the difference between recursive and RE languages?

For a recursive language a machine always halts with yes or no; for an RE language it may loop on strings outside the language.

What does Rice's theorem say?

Every non-trivial property of the language recognised by a Turing machine is undecidable.

Is the Church–Turing thesis a theorem?

No. It is a widely accepted thesis, because the informal idea of an algorithm cannot be proved equal to a formal model.

More in this module: Theory of computation

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Theory of Computation (IIT Kanpur, 106104148): Turing machines, decidability, reductions and Rice's theorem