Turing Machines
Exam Importance: Very High
TM design questions appear in every paper (100% frequency). Recent trend: Unary arithmetic (addition, multiplication). The Halting Problem is a guaranteed theory question (5 marks). This module typically carries 10-15 marks.
Introduction to Turing Machines
What is a Turing Machine?
A Turing Machine (TM) is the most powerful computational model. It consists of an infinite tape, a read/write head, and a finite control. Unlike PDA, the TM can move in both directions and can write on the tape.
TM vs PDA vs FA
| Feature | FA | PDA | TM |
|---|---|---|---|
| Memory | None (only state) | Stack (LIFO) | Infinite tape (random access) |
| Head movement | Right only | Right only | Left and Right |
| Write capability | No | Stack only | Yes (on tape) |
| Languages | Regular | Context-Free | Recursively Enumerable |
| Power | Lowest | Medium | Highest (universal) |
Structure of a Turing Machine: Infinite tape with R/W head that can move left or right
Formal Definition of Turing Machine
7-Tuple Definition
A TM is a 7-tuple: M = (Q, Σ, Γ, δ, q₀, B, F) where:
- Q = Finite set of states
- Σ = Input alphabet (Σ ⊆ Γ, B ∉ Σ)
- Γ = Tape alphabet
- δ = Transition function: Q × Γ → Q × Γ × {L, R}
- q₀ = Initial state
- B = Blank symbol (B ∈ Γ)
- F = Set of final/halting states
Transition Notation
Meaning: In state q, reading symbol X:
- Change to state p
- Write symbol Y (replacing X)
- Move head in direction D (L = Left, R = Right)
δ(q₁, 0) = (q₂, X, R)
In state q₁, reading 0:
- Go to state q₂
- Write X over the 0
- Move head Right
TM Configuration and Transitions
Instantaneous Description (ID)
The configuration of a TM is written as: α q β
- α = tape contents to the left of head
- q = current state
- β = tape contents from head position onwards (first symbol is under head)
Configuration: 01 q₃ 110B
Tape: ...B 0 1 1 1 0 B...
Head is on the first '1' after the state symbol
Current state: q₃
Move Notation
We write α₁q₁β₁ ⊢ α₂q₂β₂ to show one move, and ⊢* for multiple moves.
TM Design Patterns
TM for L = {0ⁿ1ⁿ | n ≥ 1}
Strategy:
- Mark leftmost unmarked 0 with X
- Move right to find leftmost unmarked 1
- Mark it with Y
- Move left to find next unmarked 0
- Repeat until all 0s are marked
- Verify all 1s are also marked
Transitions:
Trace for "0011":
TM for L = {aⁿbⁿcⁿ | n ≥ 1}
Strategy:
- Mark one 'a' with 'X'
- Move right, mark one 'b' with 'Y'
- Move right, mark one 'c' with 'Z'
- Return to beginning
- Repeat until all marked
Key Insight: This language is NOT context-free! PDAs cannot recognize it because they need to count a's, b's, AND c's simultaneously. TM can because it has unlimited tape for marking.
Unary Arithmetic (Exam Trend!)
Recent Exam Trend
Questions on unary addition and multiplication have appeared in Dec 2023 and Dec 2024. This is a HIGH-PROBABILITY topic!
Unary Notation
In unary notation, a number n is represented by n consecutive 1s:
- 0 = (empty or special symbol)
- 1 = 1
- 2 = 11
- 3 = 111
- 4 = 1111
TM for Unary Addition
Add two unary numbers: 111 + 11 = 11111 (3 + 2 = 5)
Input format: 1ⁿ 0 1ᵐ (n + m with 0 as separator)
Output: 1ⁿ⁺ᵐ
Strategy:
- Replace the 0 (separator) with 1
- Erase one 1 from the end (to compensate)
Transitions:
Trace for 111+11 (3+2):
| Step | Tape | Description |
|---|---|---|
| Start | 111011B | Input: 111 0 11 |
| 1 | 111011B | Skip 1 |
| 2 | 111011B | Skip 1 |
| 3 | 111011B | Found 0 |
| 4 | 111111B | Replace 0→1, skip |
| 5 | 111111B | Skip 1 |
| 6 | 111111B | Found blank |
| 7 | 11111BB | Erase last 1 |
| Done | 11111 | Result: 5 |
TM for Unary Multiplication
Multiply: 11 × 111 = 111111 (2 × 3 = 6)
Input format: 1ⁿ 0 1ᵐ 0 (with separator 0s)
Output: 1ⁿˣᵐ
Strategy (Copy Machine):
- For each 1 in the first number:
- Copy the entire second number to a result area
- Mark the processed 1 in first number
- When first number exhausted, clean up markers
Key Insight: Multiplication is repeated addition. For 2 × 3, copy "111" twice to get "111111".
States needed:
- q₀: Mark a 1 in first number, go to copy
- q₁: Find second number
- q₂: Mark a 1 in second number
- q₃: Find result area, write 1
- q₄: Return to second number for next 1
- q₅: All of second number copied, return to first
- q₆: Clean up phase
Variants of Turing Machines
All variants are equivalent in power to the standard TM. They may differ in convenience but not in capability.
1. Multi-tape TM
Has multiple tapes, each with its own head.
- Transition: δ(q, X₁, X₂, ..., Xₖ) = (p, Y₁, D₁, Y₂, D₂, ..., Yₖ, Dₖ)
- More convenient for complex algorithms
- Can be simulated by single-tape TM
2. Multi-head TM
Single tape with multiple read/write heads.
- All heads move independently
- Equivalent to multi-tape TM
3. Non-deterministic TM (NTM)
Multiple possible transitions from each configuration.
- δ: Q × Γ → P(Q × Γ × {L, R})
- Accepts if ANY computation path accepts
- Equivalent to deterministic TM in power (but may differ in time)
4. Two-way Infinite Tape
Tape extends infinitely in both directions.
- Standard TM has tape infinite only to the right
- Can be simulated by standard TM (interleave positions)
5. Universal Turing Machine (UTM)
A TM that can simulate any other TM!
- Takes as input: encoded TM M + input w
- Simulates M on w
- Foundation of stored-program computers
- Proves TMs are programmable
Church-Turing Thesis
Any computation that can be performed by any computing device can be performed by a Turing Machine.
TM captures the intuitive notion of "computability."
The Halting Problem
Guaranteed Exam Question!
The Halting Problem appears in almost every paper (Dec 22, May 23, Dec 23, May 24, May 25). Master this proof!
The Halting Problem
Question: Given a Turing Machine M and input w, does M halt on w?
Answer: This problem is UNDECIDABLE - no algorithm can solve it for all cases!
Proof by Contradiction
Step 1: Assume H exists
Assume there exists a TM H that decides the halting problem:
- H(M, w) = "yes" if M halts on w
- H(M, w) = "no" if M loops forever on w
Step 2: Construct D using H
Build a new machine D that:
- Takes input M (description of a TM)
- Runs H(M, M) - asks "does M halt on itself?"
- If H says "yes" (M halts on M) → D loops forever
- If H says "no" (M loops on M) → D halts
Step 3: Run D on D
What happens when we run D(D)?
- If D(D) halts → H(D,D) said "yes" → D should loop (contradiction!)
- If D(D) loops → H(D,D) said "no" → D should halt (contradiction!)
Step 4: Conclusion
Both cases lead to contradiction. Therefore, our assumption is wrong.
H cannot exist! The Halting Problem is undecidable.
How to write the Halting Problem proof (5 marks)
Power of Turing Machines
What TM CAN Compute
- All recursive (decidable) languages
- All recursively enumerable languages
- Any algorithm that can be described
- All computable functions
- Simulating any other computational model
What TM CANNOT Compute
| Problem | Description |
|---|---|
| Halting Problem | Does TM M halt on input w? |
| Emptiness Problem | Is L(M) = ∅? |
| Equivalence Problem | Is L(M₁) = L(M₂)? |
| Post Correspondence | Given tiles, can we match them? |
Decidable vs Undecidable
- Decidable (Recursive): TM always halts with yes/no answer
- Semi-decidable (RE): TM halts on "yes", may loop on "no"
- Undecidable: No TM can decide it
Practice Questions
5 Mark Questions
Definition:
A Turing Machine is a 7-tuple M = (Q, Σ, Γ, δ, q₀, B, F) where:
- Q = Finite set of states
- Σ = Input alphabet
- Γ = Tape alphabet (Σ ⊂ Γ)
- δ = Transition function: Q × Γ → Q × Γ × {L, R}
- q₀ = Initial state
- B = Blank symbol
- F = Set of final states
Working:
- Input is written on an infinite tape
- Read/write head starts at leftmost input symbol
- TM starts in initial state q₀
- At each step, based on current state and symbol under head:
- Change to new state
- Write a symbol (possibly same)
- Move head Left or Right
- TM halts when reaching a final state or no transition defined
Statement:
The Halting Problem asks: Given a TM M and input w, does M halt on w?
This problem is undecidable - no algorithm exists that can solve it for all cases.
Proof by Contradiction:
- Assume TM H exists that decides halting:
- H(M,w) = "yes" if M halts on w
- H(M,w) = "no" if M loops forever
- Construct D using H:
- D(M): If H(M,M) = "yes" then loop forever, else halt
- Run D(D):
- If D(D) halts → H said "no" → D should loop (contradiction!)
- If D(D) loops → H said "yes" → D should halt (contradiction!)
- Contradiction proves H cannot exist.
Significance: Shows computational limits - some problems cannot be solved by any computer.
Definition:
A Universal Turing Machine (UTM) is a TM that can simulate any other TM.
Input:
- Encoded description of TM M
- Input string w
Working:
- UTM reads the encoding of M
- UTM simulates M's behavior on w
- UTM produces same result as M would on w
Significance:
- One machine can do everything any TM can do
- Foundation of stored-program computers
- Programs are data (can be manipulated)
- Proves TMs are programmable
Analogy: A general-purpose computer that runs any program.
10 Mark Questions
Input Format: 1ⁿ 0 1ᵐ (two unary numbers separated by 0)
Output: 1ⁿ⁺ᵐ
Strategy:
- Find the separator 0
- Replace 0 with 1 (merging the two numbers)
- Erase one 1 from the end (to compensate for added 1)
TM Definition:
- Q = {q₀, q₁, q₂, q₃, qf}
- Σ = {1, 0}
- Γ = {1, 0, B}
- F = {qf}
Transitions:
Trace for 11+111 (2+3=5):
| ID | Action |
|---|---|
| q₀ 110111 | Start |
| 1 q₀ 10111 | Skip 1 |
| 11 q₀ 0111 | Skip 1 |
| 111 q₁ 111 | Replace 0→1 |
| 1111 q₁ 11 | Skip 1 |
| 11111 q₁ 1 | Skip 1 |
| 111111 q₁ B | Found end |
| 11111 q₂ 1 | Go back |
| 1111 q₃ 1B | Erase 1 |
| 111 q₃ 11B | Go back |
| ... continue to start | |
| qf 11111 | Done! Result: 5 |
Strategy:
- Mark leftmost unmarked 0 with X
- Move right to find leftmost unmarked 1
- Mark it with Y
- Return left to find next unmarked 0
- Repeat until all 0s marked
- Accept if all 1s also marked
TM Definition:
- Q = {q₀, q₁, q₂, q₃, q₄}
- Σ = {0, 1}
- Γ = {0, 1, X, Y, B}
- F = {q₄}
Transitions:
Trace for "0011":
q₀0011 ⊢ Xq₁011 ⊢ X0q₁11 ⊢ Xq₂0Y1 ⊢ q₂X0Y1 ⊢ Xq₀0Y1 ⊢ XXq₁Y1 ⊢ XXYq₁1 ⊢ XXq₂YY ⊢ Xq₂XYY ⊢ XXq₀YY ⊢ XXYq₃Y ⊢ XXYYq₃B ⊢ XXYYBq₄ ✓
Input Format: 1ⁿ 0 1ᵐ 0 (n × m)
Output: 1ⁿˣᵐ
Strategy:
Multiplication = repeated copying. For each 1 in first number, copy entire second number to result area.
- Mark one 1 in first number (as X)
- Go to second number
- For each 1 in second number:
- Mark it (as Y)
- Go to result area, write 1
- Return to second number
- Restore second number (Y → 1)
- Return to first number, find next unmarked 1
- Repeat until first number exhausted
States:
- q₀: Start, mark 1 in first number
- q₁: Move to second number
- q₂: Mark 1 in second number
- q₃: Go to result area
- q₄: Write 1 in result, return
- q₅: Return to second number
- q₆: Restore second number
- q₇: Return to first number
- qf: Accept
Key Transitions (simplified):
Note: Full implementation has many more transitions for navigation and restoration.