CS 637: Mathematics-II

Lecture 8: What is Universal Computation?/div>

The Universal Turing Machine Concept

What is a Universal Turing Machine?

A Universal Turing Machine (UTM) is a single Turing machine \(U\) that can simulate any other Turing machine \(M\) on any input \(w\).

Key Insight: The UTM takes as input:

  1. An encoding of the Turing machine \(M\): denoted \(\langle M \rangle\)
  2. An input string \(w\)

The UTM then simulates \(M\) running on \(w\), producing the same output that \(M(w)\) would produce.

Input ⟨M⟩, w (Encoded TM + Input) Universal TM U Simulates M on input w M(w) Output M(w) (Same as direct execution)

Historical Significance

The Universal Turing Machine, proposed by Alan Turing in 1936, is one of the most profound concepts in computer science:

  • Foundation of programmability: The idea that a single machine can execute any algorithm by reading a program
  • Stored-program concept: Programs and data are both stored in memory (modern computers)
  • Church-Turing Thesis: Any effectively computable function can be computed by a Turing machine
  • Undecidability: The halting problem shows fundamental limits of computation
Modern Analogy: A Universal Turing Machine is like a general-purpose computer that can run any program. The encoding \(\langle M \rangle\) is like the program code, and \(w\) is the input data.

Why This Matters

Understanding UTMs helps us grasp:

  1. The fundamental nature of computation itself
  2. How compilers and interpreters work
  3. The theoretical limits of what computers can and cannot do
  4. The concept of recursively enumerable languages

How the Universal Turing Machine Works

Formal Definition

A Universal Turing Machine \(U\) has the following components:

  • Input format: \(\langle M \rangle \# w\), where:
    • \(\langle M \rangle\) is an encoding of TM \(M = (Q, \Sigma, \Gamma, \delta, q_0, \sqcup, F)\)
    • \(\#\) is a separator symbol
    • \(w\) is the input string for \(M\)
  • Simulation process: \(U\) maintains:
    • The current state of \(M\)
    • The tape contents of \(M\)
    • The head position of \(M\)
  • Execution: For each step, \(U\) looks up the transition in \(\langle M \rangle\) and updates accordingly

Simulation Algorithm

Step 1: Parse the encoding \(\langle M \rangle\) to extract states, alphabet, and transition function

Step 2: Initialize simulated tape with input \(w\), head at position 0, state = \(q_0\)

Step 3: Repeat until halting state reached:

  1. Read current symbol under simulated head
  2. Look up transition \(\delta(\text{current_state}, \text{symbol})\) in \(\langle M \rangle\)
  3. Update simulated state, write symbol, move head according to transition
  4. If no transition exists or halting state reached, halt

Theorem: Existence of Universal TM

Claim: There exists a Turing machine \(U\) such that for every Turing machine \(M\) and input \(w\):

\[ U(\langle M \rangle, w) = M(w) \]

Proof Sketch:

  1. Any TM can be encoded as a finite string over a fixed alphabet
  2. A TM can parse this encoding to extract transition rules
  3. A TM can maintain state, tape, and position information
  4. A TM can simulate one step of another TM by table lookup
  5. Therefore, \(U\) can simulate arbitrarily many steps of \(M\)
Computational Overhead: The UTM is slower than running \(M\) directly. If \(M\) takes \(t(n)\) steps on input of length \(n\), then \(U\) might take \(O(t(n) \cdot |\langle M \rangle|^2)\) steps due to table lookup overhead.

Three-Tape UTM Architecture

A practical UTM often uses three tapes for efficiency:

  1. Tape 1: Stores the encoding \(\langle M \rangle\) (read-only)
  2. Tape 2: Simulates \(M\)'s tape (working tape)
  3. Tape 3: Stores current state of \(M\) and head position

This architecture makes simulation more efficient and easier to understand conceptually.

Recursively Enumerable Languages

Definition: Recursively Enumerable (RE)

A language \(L\) is recursively enumerable (also called Turing-recognizable) if there exists a Turing machine \(M\) such that:

  • For all \(w \in L\): \(M\) halts and accepts \(w\)
  • For all \(w \notin L\): \(M\) either rejects or loops forever

Key Point: An RE language recognizer may not halt on inputs not in the language.

πŸ”₯ Example: Infinite "Hell" String

Problem: Consider the language:

\[ L_{\text{hell}} = \{ w \in \{h,e,l,o\}^* \mid w \text{ contains "hello" as a substring} \} \]

Scenario: What happens if we give the TM an infinite string that never contains "hello"?

The Infinite Hell String:

hellhellhellhellhellhellhellhellhellhellhell...

This string repeats "hell" forever but never contains "hello".

Result: A TM searching for "hello" will scan forever, never halting. The string is not in \(L_{\text{hell}}\), but the TM cannot recognize this fact in finite time!

TM for \(L_{\text{hell}}\)

Machine Design: A 5-state TM that searches for "hello":

  • \(q_0\): Initial state, searching for 'h'
  • \(q_1\): Found 'h', searching for first 'e'
  • \(q_2\): Found 'he', searching for first 'l'
  • \(q_3\): Found 'hel', searching for second 'l'
  • \(q_4\): Found 'hell', searching for 'o'
  • \(q_{\text{accept}}\): Found "hello" - ACCEPT!

Behavior:

  • On input "hello world" β†’ Halts and accepts βœ“
  • On input "hell" β†’ Halts and rejects βœ—
  • On input "hellhellhell..." (infinite) β†’ Loops forever ∞

Recursive vs. Recursively Enumerable

Recursive (Decidable)
  • TM always halts
  • Always gives yes/no answer
  • Example: \(L = \{1^n \mid n \ge 0\}\)
RE but not Recursive
  • TM may loop forever
  • Accepts members, may not halt on non-members
  • Example: Halting problem
Critical Insight: The class RE (recursively enumerable languages) is strictly larger than the class R (recursive/decidable languages). There exist problems that can be recognized but not decided!

Encoding Turing Machines

Why Encoding Matters

To build a Universal TM, we need a systematic way to represent any TM as a string. This encoding \(\langle M \rangle\) must capture all components of the TM.

Encoding Scheme

Step 1: Assign numeric IDs

  • States: \(q_0, q_1, q_2, \ldots \rightarrow 0, 1, 2, \ldots\)
  • Tape symbols: \(\sqcup, 1, a, b, \ldots \rightarrow 0, 1, 2, 3, \ldots\)
  • Directions: \(L \rightarrow 0\), \(R \rightarrow 1\)

Step 2: Encode each transition

For transition \(\delta(q_i, a) = (q_j, b, D)\), encode as:

0i+1 1 0a+1 1 0j+1 1 0b+1 1 0D+1

Where we use unary encoding (strings of 0s) separated by 1s as delimiters.

Step 3: Concatenate all transitions

Separate each transition encoding with "11":

(transition₁) 11 (transitionβ‚‚) 11 (transition₃) 11 ...

Properties of the Encoding

  • Uniqueness: Each TM has a unique encoding
  • Parsability: Given \(\langle M \rangle\), we can reconstruct \(M\)
  • Finite length: Any TM with finitely many states/symbols has finite encoding
  • Alphabet: Encodings use only \(\{0, 1\}\)
Important: Not every binary string is a valid TM encoding. Invalid strings can be treated as encoding a TM that immediately halts or can be mapped to a default TM.

Alternative Encoding Schemes

Many encoding schemes work, as long as they're:

  1. Complete: Capture all TM information
  2. Decidable: Can verify if string is valid encoding
  3. Efficient: Parsing doesn't add too much overhead

Common alternatives: ASCII, XML-like format, binary packed format

Complete Example: Encoding \(M\) for \(L = \{1^n \mid n > 0\}\)

Problem Statement

Design a Turing machine that accepts strings of the form \(1^n\) where \(n > 0\), and show its complete encoding for use in a Universal TM.

Machine Design

TM Definition: \(M = (Q, \Sigma, \Gamma, \delta, q_0, \sqcup, F)\)

  • \(Q = \{q_0, q_1, q_{\text{accept}}, q_{\text{reject}}\}\)
  • \(\Sigma = \{1\}\)
  • \(\Gamma = \{1, \sqcup\}\)
  • \(q_0 = q_0\) (initial state)
  • \(F = \{q_{\text{accept}}\}\)

Transition Function \(\delta\):

  1. \(\delta(q_0, 1) = (q_1, 1, R)\) β€” Read first 1, move right
  2. \(\delta(q_1, 1) = (q_1, 1, R)\) β€” Continue reading 1s
  3. \(\delta(q_1, \sqcup) = (q_{\text{accept}}, \sqcup, R)\) β€” Reached end, accept
  4. \(\delta(q_0, \sqcup) = (q_{\text{reject}}, \sqcup, R)\) β€” Empty string, reject
qβ‚€ q₁ q_acc q_rej 1/1,R 1/1,R βŠ”/βŠ”,R βŠ”/βŠ”,R

Step-by-Step Encoding

Numeric Assignment:

  • States: \(q_0 = 0, q_1 = 1, q_{\text{accept}} = 2, q_{\text{reject}} = 3\)
  • Symbols: \(\sqcup = 0, 1 = 1\)
  • Directions: \(L = 0, R = 1\)

Transition Encodings:

1. \(\delta(q_0, 1) = (q_1, 1, R)\):

State=0, Read=1, NewState=1, Write=1, Dir=R(1)
0ΒΉ 1 0Β² 1 0Β² 1 0Β² 1 0Β² = 0 1 00 1 00 1 00 1 00

2. \(\delta(q_1, 1) = (q_1, 1, R)\):

State=1, Read=1, NewState=1, Write=1, Dir=R(1)
0Β² 1 0Β² 1 0Β² 1 0Β² 1 0Β² = 00 1 00 1 00 1 00 1 00

3. \(\delta(q_1, \sqcup) = (q_{\text{accept}}, \sqcup, R)\):

State=1, Read=0, NewState=2, Write=0, Dir=R(1)
0Β² 1 0ΒΉ 1 0Β³ 1 0ΒΉ 1 0Β² = 00 1 0 1 000 1 0 1 00

4. \(\delta(q_0, \sqcup) = (q_{\text{reject}}, \sqcup, R)\):

State=0, Read=0, NewState=3, Write=0, Dir=R(1)
0¹ 1 0¹ 1 0⁴ 1 0¹ 1 0² = 0 1 0 1 0000 1 0 1 00

Complete Encoding \(\langle M \rangle\):

0 1 00 1 00 1 00 1 00 11 00 1 00 1 00 1 00 1 00 11 00 1 0 1 000 1 0 1 00 11 0 1 0 1 0000 1 0 1 00
Result: This binary string is the complete description of the TM. A Universal TM can parse this encoding and simulate the original machine's behavior on any input!

Complete Example: Designing TM for \(L = \{1^{n^2} \mid n > 0\}\)

Problem Statement

Design a Turing machine that accepts exactly those strings consisting of \(1^{n^2}\) ones, where \(n\) is a positive integer.

Examples:

  • \(n=1\): \(1^1 = 1\) βœ“
  • \(n=2\): \(1^4 = 1111\) βœ“
  • \(n=3\): \(1^9 = 111111111\) βœ“
  • \(n=4\): \(1^{16} = 1111111111111111\) βœ“

But reject: \(11\), \(111\), \(11111\) (not perfect squares)

Algorithm Strategy

The key insight: \(n^2 = 1 + 3 + 5 + \cdots + (2n-1)\)

High-Level Approach:

  1. Use a counter on a separate tape track to track which odd number to delete (1st, 3rd, 5th, etc.)
  2. In iteration \(k\), delete exactly \(2k-1\) symbols from the input
  3. After each deletion, increment the counter
  4. If we delete exactly all symbols with no remainder, accept
  5. Otherwise, reject

Example for \(n=3\) (9 ones):

  • Iteration 1 (k=1): Delete 1 symbol β†’ 1 1 1 1 1 1 1 1 (8 remain)
  • Iteration 2 (k=2): Delete 3 symbols β†’ 1 1 1 1 1 (5 remain)
  • Iteration 3 (k=3): Delete 5 symbols β†’ βŠ” (0 remain) βœ“ Perfect!

Detailed Two-Tape TM Design

Using 2 tapes for clarity:

  • Tape 1: Input tape containing \(1^{n^2}\)
  • Tape 2: Counter tape (stores current iteration number in unary)

Tape Alphabet: \(\Gamma = \{1, X, C, \sqcup\}\)

  • \(1\): Original input symbol
  • \(X\): Marked/deleted symbol
  • \(C\): Counter symbol on tape 2

State Groups:

  • \(q_{\text{init}}\): Initialize counter with one C
  • \(q_{\text{copy}}\): Copy counter value (read \(k\) Cs, prepare to delete \(2k-1\) symbols)
  • \(q_{\text{del}_1}, q_{\text{del}_2}, \ldots\): Delete \(2k-1\) symbols from tape 1
  • \(q_{\text{incr}}\): Increment counter on tape 2
  • \(q_{\text{check}}\): Check if all input symbols deleted
  • \(q_{\text{accept}}, q_{\text{reject}}\): Final states

Algorithm in Detail:

Phase 1: Initialize Counter

  • Write one C on tape 2 (counter = 1)
  • Move to Phase 2

Phase 2: Read Counter Value

  • Count number of Cs on tape 2 β†’ this is \(k\)
  • Compute \(2k-1\) (the number of symbols to delete this iteration)
  • Mark these symbols on tape 1 with X

Sub-algorithm for computing \(2k-1\):

  1. Read \(k\) Cs from tape 2, marking each as we go
  2. For each C, mark TWO symbols on tape 1 (this gives \(2k\))
  3. Unmark one symbol (giving \(2k-1\))

Phase 3: Check if More Symbols Remain

  • Scan tape 1 for any unmarked 1s
  • If unmarked 1s exist: Increment counter on tape 2, go to Phase 2
  • If all marked (all Xs): Accept!
  • If we can't mark enough symbols: Reject

Phase 4: Increment Counter

  • Add one more C to tape 2
  • Return to Phase 2

Detailed Execution Trace for \(n=2\) (input: \(1111\))

Initial Configuration:
Tape 1: (q_init) 1 1 1 1
Tape 2: βŠ”

After Phase 1 (Initialize):
Tape 1: 1 1 1 1
Tape 2: C ← counter = 1

Iteration 1 (k=1, delete 2Γ—1-1 = 1 symbol):
Tape 1: X 1 1 1 ← marked 1 symbol
Tape 2: C
More symbols remain β†’ increment counter
Tape 2: C C ← counter = 2

Iteration 2 (k=2, delete 2Γ—2-1 = 3 symbols):
Tape 1: X X X X ← marked 3 more symbols (all now marked!)
Tape 2: C C

Verification:
All symbols on tape 1 are X (none are 1)
Total deleted: 1 + 3 = 4 = 2Β² βœ“

ACCEPT! βœ“

Execution Trace for \(n=3\) (input: \(111111111\))

Initial:
Tape 1: 1 1 1 1 1 1 1 1 1 (9 symbols)
Tape 2: βŠ”

After Initialize:
Tape 2: C ← counter = 1

Iteration 1 (k=1, delete 1):
Tape 1: X 1 1 1 1 1 1 1 1 (8 remain)
Tape 2: C C ← increment to 2

Iteration 2 (k=2, delete 3):
Tape 1: X X X X 1 1 1 1 1 (5 remain)
Tape 2: C C C ← increment to 3

Iteration 3 (k=3, delete 5):
Tape 1: X X X X X X X X X (0 remain)
Tape 2: C C C

Total deleted: 1 + 3 + 5 = 9 = 3Β² βœ“
ACCEPT! βœ“

Rejection Example (input: \(11111\) - not a perfect square)

Initial:
Tape 1: 1 1 1 1 1 (5 symbols)

Iteration 1 (k=1, delete 1):
Tape 1: X 1 1 1 1 (4 remain)
Tape 2: C C ← counter = 2

Iteration 2 (k=2, delete 3):
Tape 1: X X X X 1 (1 remains)
Tape 2: C C C ← counter = 3

Iteration 3 (k=3, need to delete 5):
Only 1 symbol remains, but need to delete 5!
Cannot complete deletion β†’ REJECT! βœ—

(Note: 1 + 3 = 4, but we have 5 symbols total,
so 5 is not a perfect square)
Time Complexity: For input of length \(n^2\), the machine makes approximately \(O(n^3)\) moves. Each of the \(n\) iterations requires scanning the tape (\(O(n^2)\) length) and the counter (\(O(n)\) length).

Correctness Proof

Claim: The TM accepts input \(w\) if and only if \(|w| = n^2\) for some \(n > 0\).

Proof:

Invariant: After \(k\) iterations, the machine has deleted exactly \(\sum_{i=1}^{k} (2i-1) = k^2\) symbols, and the counter contains \(k+1\) Cs.

Base case (k=1):

  • After iteration 1: deleted \(2(1)-1 = 1\) symbol
  • Counter has 2 Cs (ready for k=2)
  • Total deleted: \(1 = 1^2\) βœ“

Inductive step:

  • Assume after \(k\) iterations: \(k^2\) symbols deleted, counter = \(k+1\)
  • In iteration \(k+1\): delete \(2(k+1)-1 = 2k+1\) symbols
  • Total deleted: \(k^2 + (2k+1) = k^2 + 2k + 1 = (k+1)^2\) βœ“
  • Counter increments to \(k+2\)

Termination:

  • If \(|w| = n^2\): After \(n\) iterations, exactly \(n^2\) symbols deleted, all symbols marked β†’ Accept
  • If \(|w| \neq n^2\): Either:
    • After some iteration, symbols remain but next odd number is larger than remaining symbols β†’ Reject
    • Or: symbols exhausted but some unmarked in middle β†’ Reject

Why the counter is necessary: Without the counter, the TM cannot "remember" which odd number it's currently processing. The counter stores the iteration number \(k\), allowing the machine to compute \(2k-1\) correctly each iteration.

Single-Tape Implementation

A single-tape TM can simulate this by using different tape regions:

  • Left region: Counter symbols (Cs)
  • Delimiter: Special symbol (#) separating counter from input
  • Right region: Input symbols (1s and Xs)

Example configuration:

C C C # X X X 1 1 1 1

This shows counter=3, with some symbols already marked. The TM shuttles back and forth between the counter and input regions.

Interactive Universal TM Simulator

Simulate TM for \(1^n\) (n > 0)

State: qβ‚€ | Position: 0 | Step: 0

Execution Log

Search for "hello" in String

How to Use

  1. Top Simulator: Tests strings of form \(1^n\) with \(n > 0\)
  2. Bottom Simulator: Searches for substring "hello"
  3. "Show Hell Loop": Demonstrates infinite loop on "hellhellhell..."
  4. Watch state transitions and tape movements in real-time
πŸ