Elective Theory of Computation

Theory of ComputationUnit 412 min read

Turing Machines: Computability, Halting, and Universal Machines

Unit 4 of Theory of Computation explores Turing Machines (TMs) as the mathematical model of computation, covering their structure, computability, the halting problem, and universal machines. Learn how TMs define computable functions, solve decision problems, and why some problems (like the halting problem) are inherent

TAKEAWAYS:

  • A Turing Machine is a formal model of computation with a tape, head, and finite states that can solve any algorithmic problem if it is computable.
  • Computability theory classifies problems into decidable (TMs can always solve), undecidable (no TM can solve), and semi-decidable (TMs can recognize but not reject).
  • The halting problem is a classic undecidable problem: no TM can determine whether another TM will halt on a given input.
  • A universal Turing Machine can simulate any other TM, proving that all TMs are equivalent in computational power.
  • Turing Machines compute functions by transforming input strings into output strings via state transitions and tape operations.
  • Real-world applications include compilers (code generation), cryptography (algorithm verification), and AI (decision-making limits).

1. What is a Turing Machine?

A Turing Machine (TM) is a mathematical abstraction of a computing device that manipulates symbols on a tape according to a set of rules. It consists of:

graph TD
    A["Turing Machine"] --> B["Finite Control"]
    A --> C["Tape"]
    A --> D["Head"]
    B --> E["Finite set of states Q"]
    B --> F["Alphabet Σ"]
    B --> G["Transition function δ"]
    C --> H["Infinite tape with cells"]
    D --> I["Read/write head"]

Key Components

Component Description Example (for L = {aⁿbⁿ})
Tape Infinite sequence of cells, each holding a symbol from Σ ∪ {blank}. `...
Head Reads/writes symbols and moves left/right. Moves right after reading a.
States (Q) Finite set of states, including start (q₀) and accept/reject states (qₐ, qᵣ). Q = {q₀, q₁, q₂, qₐ, qᵣ}.
Alphabet (Σ) Finite set of symbols (input + blank). Σ = {a, b, c, blank}.
Transition Function (δ) Maps (state, symbol) → (new state, write symbol, move direction). δ(q₀, a) = (q₀, a, R).
startabbccblankcabq₀q₁q₂qₐqᵣ
Finite Automaton for L = {aⁿbⁿcⁿ | n ≥ 0} (simplified state transitions)

How a TM Works

  1. Start: TM begins in state q₀ with the input string on the tape.
  2. Read: Head reads the current symbol.
  3. Transition: Apply δ to move to a new state, write a symbol, and shift the head.
  4. Halt: TM stops if it reaches an accept (qₐ) or reject (qᵣ) state.


2. Turing Machines and Computability

A problem is Turing-computable if a TM can solve it. This includes:

  • Decision problems: Yes/No answers (e.g., "Does this string belong to L?").
  • Function computation: Mapping inputs to outputs (e.g., f(n) = n + 2).

Example: TM for L = {aⁿbⁿcⁿ | n ≥ 0}

Goal: Accept strings like abc, aabbcc, aaabbbccc, reject others.

TM Design Steps

  1. States: q₀ (start), q₁ (count as), q₂ (count bs), q₃ (count cs), qₐ (accept), qᵣ (reject).
  2. Transitions:
    • Move right, count as → switch to q₁.
    • In q₁, erase a, move right; if b found, switch to q₂.
    • In q₂, erase b, move right; if c found, switch to q₃.
    • In q₃, erase c, move right. If tape is blank, accept; else reject.

Trace for Input aabbcc

Step Tape State Current State Action
1 aabbcc q₀ Move right → q₁
2 aabbcc q₁ Erase a, move right
3 abbcc q₁ Erase a, move right
4 bbcc q₁ Read b → switch to q₂
5 bbcc q₂ Erase b, move right
6 bcc q₂ Erase b, move right
7 cc q₂ Read c → switch to q₃
8 cc q₃ Erase c, move right
9 c q₃ Erase c, move right
10 blank q₃ Move right → qₐ (accept)


3. Computing Functions with TMs

A TM computes a function f: Σ* → Σ* by:

  1. Writing the input string on the tape.
  2. Following transitions to transform the tape.
  3. Halting with the output string written.

Example: TM for f(n) = n + 2

Input: Unary string aⁿ (e.g., aaa = 3). Output: aⁿ⁺² (e.g., aaaaa = 5).

TM Steps

  1. Start in q₀, move right to end of input.
  2. Write aa (add 2), move left.
  3. Halt in qₐ.

Trace for Input aaa (n=3)

Step Tape State State Action
1 aaa q₀ Move right → q₁
2 aaa q₁ Move right → q₂
3 aaa q₂ Write a, move left
4 aaaa q₂ Write a, halt → qₐ

Output: aaaaa (5).



4. The Halting Problem

Definition: Given a TM M and input w, does M halt on w? Proof of Undecidability (by contradiction):

  1. Assume a TM H solves the halting problem.
  2. Construct a TM D that uses H to enter an infinite loop if H(w) = "halts".
  3. Apply D to itself: D(D) cannot decide if it halts, leading to a contradiction.

Why It Matters

  • No algorithm can predict whether another program will finish.
  • Real-world analogy: A bank’s loan approval system cannot guarantee it will always terminate (e.g., recursive checks).

graph TD
    A["Assume H exists"] --> B["Construct D"]
    B --> C["D(w) = if H(w) = halt then loop else accept"]
    C --> D["Apply D to itself: D(D)"]
    D --> E["Contradiction: D(D) cannot decide"]

5. Universal Turing Machine

A Universal TM (UTM) can simulate any other TM. Key properties:

  • Equivalence: All TMs are computationally equivalent to a UTM.
  • Implementation: Uses a tape to encode another TM’s description and input.
graph TD
    A["UTM Tape"] --> B["Encoded TM M"]
    A --> C["Input w"]
    B --> D["Transition Table of M"]
    D --> E["Simulate M on w"]
    E --> F["Output: M(w)"]
How a UTM encodes and simulates another TM

How a UTM Works

  1. Encode: Represent the input TM M and its input w as a string on the tape.
  2. Simulate: Step through M’s transitions using M’s transition table.
  3. Output: Mimic M’s accept/reject behavior.

Real-World Parallel: Compilers

  • A compiler (like GCC) acts like a UTM: it reads high-level code (input TM) and simulates it on hardware (UTM).


6. Decidable vs. Undecidable Problems

Property Decidable Problems Undecidable Problems
Definition TM can always answer Yes/No. No TM can solve it.
Example Empty language (L = ∅). Halting problem.
Semi-decidable Recognizable but not rejectable. E.g., "Does this TM accept?"

Example: Empty Language

TM for L = ∅:

  • Always reject any input.
  • Transition: δ(q₀, any symbol) = (qᵣ, X, R).

In the Real World

  1. eSewa (Nepal Government)

    • Idea Used: Computability of decision problems.
    • How: eSewa’s backend checks if a citizen’s input (e.g., tax form) is valid using TMs-like state machines. If the form is malformed (e.g., missing fields), it rejects it (like a TM in qᵣ).
  2. Khalti’s Transaction System

    • Idea Used: Function computation (f(n) = n + fee).
    • How: When you pay ₹100 for a Daraz order, Khalti’s system computes the total as 100 + transaction_fee (like our f(n) = n + 2 TM). The TM analogy helps model how the system processes inputs to outputs deterministically.
  3. Ncell’s Network Routing

    • Idea Used: Universal simulation (UTM-like behavior).
    • How: Ncell’s core routers act like a UTM, simulating paths for millions of calls/data packets. Each packet’s route is determined by a set of rules (like a TM’s transition table), and the router dynamically "rewrites" the path based on network conditions (tape modifications).
  4. Bank Loan Approval (Nepal’s NMB, Global IME)

    • Idea Used: Halting problem limitations.
    • How: A bank’s loan approval system runs checks (credit score, collateral) in loops. If the system enters an infinite loop (e.g., waiting for a document that never arrives), it cannot predict this in advance—just like the halting problem. This is why banks use timeouts or human oversight for critical decisions.

Exam Tip

What Examiners Look For

  1. Definitions: Always define TMs with all components (tape, head, states, transitions).

    • ❌ "A TM is a machine that computes things."
    • ✅ "A TM is a 7-tuple (Q, Σ, Γ, δ, q₀, qₐ, qᵣ) where..."
  2. Traces: For TM design questions, show step-by-step tape changes (like the aⁿbⁿcⁿ example). Use tables or Mermaid diagrams.

  3. Halting Problem: Know the proof by contradiction and why it’s undecidable. Mention Rice’s Theorem if asked about non-trivial undecidable properties.

  4. Universal TM: Explain how it encodes another TM and simulates it. Compare to real-world compilers/interpreters.

  5. Function Computation: For f(n) questions, show:

    • Input format (e.g., unary aⁿ).
    • TM steps to transform input to output.
    • Example trace (like f(n) = n + 2).

Common Mistakes to Avoid

  • Forgetting the blank symbol in the tape alphabet.
  • Not handling edge cases (e.g., empty input in aⁿbⁿ).
  • Confusing decidable (TM can solve) vs. recognizable (TM can accept but may loop).
  • Skipping the halt states (qₐ/qᵣ) in TM design.

Quick Revision Table

Topic Key Idea Example
TM Definition 7-tuple: states, alphabet, transitions, tape, head. M = (Q, Σ, Γ, δ, q₀, qₐ, qᵣ).
Computability Problems solvable by a TM. L = {aⁿbⁿcⁿ} is decidable.
Halting Problem Undecidable: no TM can predict if another TM halts. Proof by contradiction.
UTM Simulates any TM using encoded instructions. Like a compiler for hardware.
Function TM Transforms input strings to output strings. f(n) = n + 2 via tape modifications.

In the real world

  • eSewa (Nepal) uses decidable problems to verify transactions: when you pay a bill, eSewa’s backend TM checks if your account has enough balance (a computable decision) before processing. If the balance check is undecidable (e.g., due to recursive loops in the system), the payment would hang indefinitely.
  • Pathao’s ride-hailing algorithm relies on Turing-computable functions to calculate fare prices based on distance and time (e.g., fare = base + (distance * rate) + time_bonus). The function is deterministic and halts, ensuring fair pricing.
  • NTC’s network monitoring faces undecidable problems when detecting malicious traffic: while it can recognize known attack patterns (semi-decidable), it cannot prove a packet is harmless (halting problem analogy—some patterns may loop indefinitely in analysis).

Based on the PU BE Computer (PU) syllabus for Theory of Computation, unit 4.

Discussion

Loading…