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)
... ... B 1 1 0 1 1 0 B B B R/W Head Finite Control (State: q₃) L R

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

δ(q, X) = (p, Y, D)

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)
Example

δ(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)
Example

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₃

B
0
1
1
1
0
B

Move Notation

We write α₁q₁β₁ α₂q₂β₂ to show one move, and ⊢* for multiple moves.

TM Design Patterns

Classic Example

TM for L = {0ⁿ1ⁿ | n ≥ 1}

Strategy:

  1. Mark leftmost unmarked 0 with X
  2. Move right to find leftmost unmarked 1
  3. Mark it with Y
  4. Move left to find next unmarked 0
  5. Repeat until all 0s are marked
  6. Verify all 1s are also marked

Transitions:

-- Mark a 0 with X δ(q₀, 0) = (q₁, X, R) -- Mark 0, move right to find 1 -- Skip over 0s and Ys to find 1 δ(q₁, 0) = (q₁, 0, R) δ(q₁, Y) = (q₁, Y, R) -- Mark a 1 with Y δ(q₁, 1) = (q₂, Y, L) -- Mark 1, go back left -- Go back to find next 0 δ(q₂, 0) = (q₂, 0, L) δ(q₂, Y) = (q₂, Y, L) δ(q₂, X) = (q₀, X, R) -- Found X, look for next 0 -- Acceptance: all 0s marked, verify all 1s marked δ(q₀, Y) = (q₃, Y, R) -- No more 0s, check 1s δ(q₃, Y) = (q₃, Y, R) δ(q₃, B) = (q₄, B, R) -- Accept! (q₄ is final)

Trace for "0011":

q₀ 0011B
X q₁ 011B
X0 q₁ 11B
X0 q₂ Y1B (marked 1)
X q₂ 0Y1B
q₂ X0Y1B
X q₀ 0Y1B
XX q₁ Y1B
XXY q₁ 1B
XX q₂ YYB
X q₂ XYYB
XX q₀ YYB
XX q₃ YYB (no more 0s)
XXYY q₃ B
XXYYB q₄ B ✓ Accept!
Classic Example

TM for L = {aⁿbⁿcⁿ | n ≥ 1}

Strategy:

  1. Mark one 'a' with 'X'
  2. Move right, mark one 'b' with 'Y'
  3. Move right, mark one 'c' with 'Z'
  4. Return to beginning
  5. 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

Complete Example

Add two unary numbers: 111 + 11 = 11111 (3 + 2 = 5)

Input format: 1ⁿ 0 1ᵐ (n + m with 0 as separator)

Output: 1ⁿ⁺ᵐ

Strategy:

  1. Replace the 0 (separator) with 1
  2. Erase one 1 from the end (to compensate)

Transitions:

-- Move right to find 0 (separator) δ(q₀, 1) = (q₀, 1, R) δ(q₀, 0) = (q₁, 1, R) -- Replace 0 with 1 -- Move right to find end δ(q₁, 1) = (q₁, 1, R) δ(q₁, B) = (q₂, B, L) -- Found end, go back -- Erase last 1 δ(q₂, 1) = (q₃, B, L) -- Erase one 1 -- Go back to beginning (optional, for clean output) δ(q₃, 1) = (q₃, 1, L) δ(q₃, B) = (qf, B, R) -- Done! (qf is final)

Trace for 111+11 (3+2):

StepTapeDescription
Start111011BInput: 111 0 11
1111011BSkip 1
2111011BSkip 1
3111011BFound 0
4111111BReplace 0→1, skip
5111111BSkip 1
6111111BFound blank
711111BBErase last 1
Done11111Result: 5

TM for Unary Multiplication

Complete Example

Multiply: 11 × 111 = 111111 (2 × 3 = 6)

Input format: 1ⁿ 0 1ᵐ 0 (with separator 0s)

Output: 1ⁿˣᵐ

Strategy (Copy Machine):

  1. For each 1 in the first number:
  2. Copy the entire second number to a result area
  3. Mark the processed 1 in first number
  4. 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:

  1. Takes input M (description of a TM)
  2. Runs H(M, M) - asks "does M halt on itself?"
  3. If H says "yes" (M halts on M) → D loops forever
  4. 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.

Exam Answer Format

How to write the Halting Problem proof (5 marks)

1. Assume a TM H exists that decides halting: H(M,w) = "yes" if M halts on w H(M,w) = "no" if M loops on w 2. Construct machine D: D(M): Run H(M,M) If H says "yes" → loop forever If H says "no" → halt 3. Consider D(D): Case 1: D(D) halts → H(D,D) = "no" (by D's definition) → D loops on D (contradiction!) Case 2: D(D) loops → H(D,D) = "yes" (by D's definition) → D halts on D (contradiction!) 4. Both cases contradict. Therefore H cannot exist. The Halting Problem is UNDECIDABLE.

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

What is a Turing Machine? Explain its working. 5 Marks

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:

  1. Input is written on an infinite tape
  2. Read/write head starts at leftmost input symbol
  3. TM starts in initial state q₀
  4. 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
  5. TM halts when reaching a final state or no transition defined
Explain the Halting Problem 5 Marks

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:

  1. Assume TM H exists that decides halting:
    • H(M,w) = "yes" if M halts on w
    • H(M,w) = "no" if M loops forever
  2. Construct D using H:
    • D(M): If H(M,M) = "yes" then loop forever, else halt
  3. 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!)
  4. Contradiction proves H cannot exist.

Significance: Shows computational limits - some problems cannot be solved by any computer.

Write note on Universal Turing Machine 5 Marks

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:

  1. UTM reads the encoding of M
  2. UTM simulates M's behavior on w
  3. 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

Design TM to add two unary numbers 10 Marks

Input Format: 1ⁿ 0 1ᵐ (two unary numbers separated by 0)

Output: 1ⁿ⁺ᵐ

Strategy:

  1. Find the separator 0
  2. Replace 0 with 1 (merging the two numbers)
  3. 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:

δ(q₀, 1) = (q₀, 1, R) -- Skip 1s δ(q₀, 0) = (q₁, 1, R) -- Replace 0 with 1 δ(q₁, 1) = (q₁, 1, R) -- Skip 1s in second number δ(q₁, B) = (q₂, B, L) -- Found end δ(q₂, 1) = (q₃, B, L) -- Erase last 1 δ(q₃, 1) = (q₃, 1, L) -- Go back δ(q₃, B) = (qf, B, R) -- Done

Trace for 11+111 (2+3=5):

IDAction
q₀ 110111Start
1 q₀ 10111Skip 1
11 q₀ 0111Skip 1
111 q₁ 111Replace 0→1
1111 q₁ 11Skip 1
11111 q₁ 1Skip 1
111111 q₁ BFound end
11111 q₂ 1Go back
1111 q₃ 1BErase 1
111 q₃ 11BGo back
... continue to start
qf 11111Done! Result: 5
Design TM for L = {0ⁿ1ⁿ | n ≥ 1} 10 Marks

Strategy:

  1. Mark leftmost unmarked 0 with X
  2. Move right to find leftmost unmarked 1
  3. Mark it with Y
  4. Return left to find next unmarked 0
  5. Repeat until all 0s marked
  6. Accept if all 1s also marked

TM Definition:

  • Q = {q₀, q₁, q₂, q₃, q₄}
  • Σ = {0, 1}
  • Γ = {0, 1, X, Y, B}
  • F = {q₄}

Transitions:

δ(q₀, 0) = (q₁, X, R) -- Mark 0 with X δ(q₁, 0) = (q₁, 0, R) -- Skip remaining 0s δ(q₁, Y) = (q₁, Y, R) -- Skip Ys δ(q₁, 1) = (q₂, Y, L) -- Mark 1 with Y, go back δ(q₂, 0) = (q₂, 0, L) -- Go back over 0s δ(q₂, Y) = (q₂, Y, L) -- Go back over Ys δ(q₂, X) = (q₀, X, R) -- Found X, look for next 0 δ(q₀, Y) = (q₃, Y, R) -- All 0s done, verify 1s δ(q₃, Y) = (q₃, Y, R) -- Skip Ys δ(q₃, B) = (q₄, B, R) -- All done, accept!

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₄ ✓

Design TM to multiply two unary numbers 10 Marks

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.

  1. Mark one 1 in first number (as X)
  2. Go to second number
  3. For each 1 in second number:
    • Mark it (as Y)
    • Go to result area, write 1
    • Return to second number
  4. Restore second number (Y → 1)
  5. Return to first number, find next unmarked 1
  6. 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):

δ(q₀, 1) = (q₁, X, R) -- Mark 1 in first num δ(q₁, 1) = (q₁, 1, R) -- Skip to separator δ(q₁, 0) = (q₂, 0, R) -- Found separator δ(q₂, 1) = (q₃, Y, R) -- Mark 1 in second num δ(q₃, 1) = (q₃, 1, R) -- Skip to result area δ(q₃, 0) = (q₃, 0, R) δ(q₃, B) = (q₄, 1, L) -- Write 1 in result -- Continue with return and restore logic...

Note: Full implementation has many more transitions for navigation and restoration.