THEORY OF COMPUTATION / 4. TURING MACHINES
Turing Machines — The Ultimate Model of Computation
What ALL computers are, mathematically — and what they cannot do
EXPLANATION
The Turing Machine (TM) is the most powerful theoretical model of computation. Alan Turing invented it in 1936 — before electronic computers existed — to answer the question: "What does it mean to compute something?"
Formal definition — a TM is a 7-tuple (Q, Σ, Γ, δ, q0, qaccept, qreject):
- Q — finite set of states
- Σ — input alphabet (does not contain blank symbol ⊔)
- Γ — tape alphabet (Σ ⊂ Γ, contains ⊔ blank)
- δ — transition function: Q × Γ → Q × Γ × {L, R} (state, tape symbol → new state, write symbol, move Left or Right)
- q0 — start state
- qaccept — the accept state (machine halts and accepts)
- qreject — the reject state (machine halts and rejects)
How a TM works:
- Infinite tape divided into cells, each holding one symbol (initially: input on tape, rest blanks)
- Read/write head starts at leftmost input symbol
- Each step: read current cell → look up δ → write new symbol → move L or R → go to new state
- Halts when it reaches qaccept or qreject
- May LOOP FOREVER (never halts) — this is the key difference from DFA/PDA
Church-Turing Thesis (not provable, but universally accepted):
"Any function that can be computed by an algorithm can be computed by a Turing Machine."
Implication: TMs are as powerful as any real computer. Python, C, Java — all equivalent in power to a Turing Machine. They can all solve exactly the same set of problems (just at different speeds).
Decidable vs Recognizable vs Undecidable:
- Decidable (Recursive): TM always halts and gives correct answer. "Yes or No, always."
Examples: Is n prime? Is this DFA equivalent to that DFA? Does this string match this regex?
- Recognizable (Recursively Enumerable): TM halts and accepts if input is in language. May loop forever on inputs NOT in language.
- Undecidable: No TM can decide the language. Not just "we haven't found one" — PROVEN impossible.
The Halting Problem — the most famous undecidable problem:
"Given a program P and input I, does P halt on I?"
Proof by contradiction (Turing, 1936):
① Assume a decider H(P, I) exists that always says "halts" or "loops"
② Build program D(P): if H(P,P) says "halts" → loop forever; if H(P,P) says "loops" → halt
③ Run D on itself: D(D)
• If H(D,D) says "halts" → D loops forever → H was wrong
• If H(D,D) says "loops" → D halts → H was wrong
④ Contradiction either way → H cannot exist ∎
Implications of undecidability:
- Cannot write a perfect virus detector (would solve halting problem)
- Cannot write a perfect infinite-loop detector
- Cannot automatically verify all programs are correct (Rice's Theorem: all non-trivial properties of programs are undecidable)
- Gödel's incompleteness theorem is related: some true mathematical statements cannot be proven
Reductions: if problem A reduces to problem B (A ≤m B), then:
- If B is decidable → A is decidable
- If A is undecidable → B is undecidable
Used to prove new problems undecidable by reducing halting problem to them.DIAGRAM
TURING MACHINE STRUCTURE:
Infinite Tape: [a][a][b][b][⊔][⊔][⊔]...
↑
Read/Write Head
│
┌─────┴──────┐
│ Control │ ← current state
│ Unit │
└────────────┘
Transition: δ(q1, 'b') = (q2, 'X', R)
"In state q1, reading 'b':
write 'X', move Right, go to state q2"
TM for aⁿbⁿ (crosses off matching pairs):
Tape: [a][a][b][b]
Step 1: Replace leftmost 'a' with 'X', move right
[X][a][b][b]
Step 2: Scan right to find first 'b', replace with 'Y'
[X][a][Y][b]
Step 3: Move back left to find next 'a'
Step 4: Repeat until all matched
[X][X][Y][Y] → ACCEPT
DECIDABILITY LANDSCAPE:
┌──────────────────────────────────────────────┐
│ All Languages │
│ ┌─────────────────────────────────────┐ │
│ │ Recognizable (TM accepts) │ │
│ │ ┌──────────────────────────────┐ │ │
│ │ │ Decidable (TM always halts)│ │ │
│ │ │ • Primality testing │ │ │
│ │ │ • Sorting │ │ │
│ │ │ • All regular languages │ │ │
│ │ └──────────────────────────────┘ │ │
│ │ • Halting problem (recognizable │ │
│ │ but NOT decidable) │ │
│ └─────────────────────────────────────┘ │
│ • Complement of halting problem │
│ (not even recognizable) │
└──────────────────────────────────────────────┘CODE