Turing Machines

The Ultimate Model of Computation

CS305 - Formal Language Theory

Use ← → arrows to navigate

Big Picture: The Chomsky Hierarchy

We have been climbing a ladder of computational power. The Turing Machine sits at the very top.

TypeGrammarMachine
3RegularDFA / NFA
2Context-FreePDA
1Context-SensitiveLBA
0UnrestrictedTM

Key Idea

Each level strictly contains the one below it. TMs can do everything the lower models can, and more.

The Turing Machine Model

A TM consists of three parts: an infinite tape, a read/write head, and a finite control.

TM replaces 0->X and 1->Y. Click Step.

The Three Components

  • Tape: Divided into cells, each holding one symbol. Extends infinitely. Blank (B) fills unused cells.
  • Head: Points to one cell. Can read, write, and move left or right.
  • Finite Control: Finite set of states + transition function.

Analogy

Think of a TM as the simplest possible general-purpose computer. The tape is RAM (but infinite). The head is the read/write mechanism. The finite control is the CPU running a fixed program.

Formal Definition: The 7-Tuple

M = (Q, Σ, Γ, δ, q0, B, F)
SymbolNameDescription
QStatesFinite set of states
ΣInput alphabetSymbols in input (Σ ⊂ Γ, B ∉ Σ)
ΓTape alphabetAll tape symbols (Σ ∪ {B} ⊆ Γ)
δTransition fnQ × Γ → Q × Γ × {L, R}
q0Start stateq0 ∈ Q
BBlankB ∈ Γ but B ∉ Σ
FFinal statesF ⊆ Q (accepting)

Input vs Tape Alphabet

Σ is what the input is made of (e.g., {0, 1}). Γ includes Σ, the blank B, and work symbols (e.g., X, Y for marking).

Example: Q = {q0, q1, q2, q3, q4} Σ = {0, 1} Γ = {0, 1, X, Y, B} q0 = q0, B = B, F = {q4}

Watch Out

The blank B is in Γ but NOT in Σ. The input never contains blanks -- blanks only appear as "empty" cells on the tape.

Reading TM Transitions

Each transition: what to write, where to move, which state next.

δ( q , X ) = ( p , Y , D )    current  read    next write dir

Example

δ(q1, 0) = (q2, X, R)

"In state q1, reading 0: write X, move right, go to q2."

Click a transition to animate it.

Halting

The TM halts when δ(q, X) is undefined.

  • Accept: Halt in q ∈ F
  • Reject: Halt in q ∉ F
  • Loop: Never halt (possible!)

Warning

Unlike DFAs, a TM might never stop. This is a fundamental feature that leads to undecidability.

Example: Palindrome Checker TM

TM for { w ∈ {a,b}* | w = wR }. Strategy: match first & last chars, shrink inward.

q0

Transition Diagram

Strategy

  1. Read leftmost unmatched symbol, mark it X
  2. Remember it (state encodes a vs b)
  3. Scan right to rightmost unmatched symbol
  4. If it matches, mark it X, scan back left
  5. Repeat until all matched or mismatch found

Key Idea

The state acts as 1-bit memory — it "remembers" which character was seen on the left, then verifies the match on the right.

Palindrome Checker: Transition Function

Complete δ for M = ({q0, qa, qb, qca, qcb, qback, qacc}, {a,b}, {a,b,X,B}, δ, q0, B, {qacc})

StateReadNext StateWriteMove
q0aqaXR
q0bqbXR
q0Xq0XR
q0BqaccBR
qaaqaaR
qabqabR
qaXqcaXL
qaBqcaBL
qbaqbaR
qbbqbbR
qbXqcbXL
qbBqcbBL
StateReadNext StateWriteMove
qcaaqbackXL
qcaXqaccXR
qcbbqbackXL
qcbXqaccXR
qbackaqbackaL
qbackbqbackbL
qbackXq0XR

Reading the Table

Highlighted rows lead to qacc (accept). Missing entries → reject (halt with no transition). E.g., qca reading b = mismatch → reject.

7 States, 19 Transitions

Even a "simple" palindrome check needs this many transitions. TM programming is low-level — every read/write/move must be specified.

TM Programming Techniques

Building TMs for complex tasks uses a few recurring "tricks."

1. Marking Symbols

Replace a symbol with a "marked" version (e.g., 0 → X) to remember you processed it.

2. Shifting

To insert or delete a symbol, shift all symbols right/left by one cell. Requires O(n) steps per shift.

Insert 'A' at position 3: Before: [ a ][ b ][ c ][ d ] After: [ a ][ b ][ A ][ c ][ d ]

3. Multiple Tracks

Treat each tape cell as a tuple. E.g., one track for data, one for markers.

Multiple Tracks (single tape): +-------+-------+-------+-------+ | (0,#) | (1,*) | (1,#) | (0,#) | +-------+-------+-------+-------+ Track 1: 0 1 1 0 (data) Track 2: # * # # (markers)

4. Subroutines

Design a TM for a subtask, then "call" it by entering its start state.

Key Idea

These techniques show TMs can simulate structured programming: variables (marked cells), arrays (tape regions), and function calls (subroutines).

Example: Binary Incrementer TM

TMs can compute functions, not just decide languages. Here: add 1 to a binary number.

Strategy

  • Start head at rightmost bit
  • If 0: change to 1, done
  • If 1: change to 0 (carry), move left
  • If B: write 1 (carry into new position)
δ(q0, 0) = (qf, 1, R) -- flip, halt δ(q0, 1) = (q0, 0, L) -- carry δ(q0, B) = (qf, 1, R) -- overflow

Analogy

This is exactly how you add 1 by hand: start from the right, flip bits, and carry the 1 leftward until you find a 0 to absorb it.

Multi-Tape Turing Machines

A multi-tape TM has k tapes, each with its own independent read/write head.

2-tape TM: copies Tape 1 to Tape 2. Click Step.

Transition Form

δ(q, a1, a2) = (p, b1, b2, D1, D2)

Reads all heads at once, writes to all, moves each independently.

Why It Matters

Multi-tape TMs are much easier to program. Use one tape for input, one as scratch space. They are often the "go-to" model for algorithm design in theory.

Equivalence: Multi-Tape = Single-Tape

Every k-tape TM can be simulated by a single-tape TM. Here's exactly how.

Step 0 / 3

How it Works (k=2 tapes)

  1. Encode: Interleave k tapes onto one tape using 2k tracks per cell — each tape gets a data track and a head-marker track
  2. Scan: To simulate one step, sweep the entire tape left-to-right to find all * markers and read the symbols under each virtual head
  3. Update: Sweep again to write new symbols and move each * marker left or right
  4. Repeat: Each original step → two full sweeps of the single tape

Cost of Simulation

Each step of the k-tape TM needs O(n) steps on the single tape (to sweep and find markers). After t steps the tape grows to at most t cells, so total cost is O(t²) — polynomial slowdown only.

Why This Matters

Adding more tapes does NOT increase the class of languages recognized. Convenience yes, extra power no. Same languages, just slower.

Nondeterministic Turing Machines

An NTM can have multiple possible transitions for the same (state, symbol) pair.

Deterministic: δ(q1, a) = (q2, b, R) (exactly ONE choice) Nondeterministic: δ(q1, a) = { (q2, b, R), (q3, a, L), (q5, c, R) } (MULTIPLE choices)

Acceptance

An NTM accepts if at least one computation path reaches an accepting state.

Equivalence Theorem

NTMs are equivalent in power to DTMs. Every NTM can be simulated by a DTM using BFS of the computation tree.

The Catch

The DTM simulation may be exponentially slower: O(ct) steps. Whether this blowup is necessary is the P vs NP problem!

Analogy

NTM = "lucky guesser" that always picks the right branch. DTM = methodical searcher trying every branch. Same problems solved, potentially different speeds.

Challenge: Predict the TM Output

Given this TM:

δ(q0, 0) = (q0, 1, R) δ(q0, 1) = (q0, 0, R) δ(q0, B) = (qf, B, L)

Input tape: 0 1 1 0

What will the tape contain when the TM halts?

Three Possible Outcomes of a TM

Unlike DFAs/PDAs that always finish reading input, a TM on input w can do one of three things:

Accept

Enters an accept state and halts

Reject

Enters a reject state and halts

Loop Forever

Never halts — runs infinitely

The third possibility — looping — is what makes TMs fundamentally different from DFAs and PDAs.

Why Looping is a Problem

If you run a TM and it hasn't halted after 1 billion steps, you cannot tell whether it will eventually halt or loop forever. There is no general algorithm to decide this — that is the Halting Problem (covered in the UTM lecture).

Analogy

DFA/PDA = a test that always finishes and gives you a grade.
TM = a test that might finish... or might keep grading forever. You're stuck waiting, unsure if it will ever return your paper.

Deciders vs Recognizers

We classify TMs based on whether they always halt, which defines two fundamental language classes.

Decider (Recursive)

A TM that halts on every input — it always says YES or NO, never loops.

  • If w ∈ L → accept (halt)
  • If w ∉ L → reject (halt)
  • The language L is called decidable (or recursive)

Examples: {anbncn}, {palindromes}, {connected graphs}

Recognizer (RE / Recursively Enumerable)

A TM that accepts strings in L but may loop forever on strings not in L.

  • If w ∈ L → accept (halt)
  • If w ∉ L → reject OR loop forever
  • The language L is called recognizable (or RE)

Example: ATM = {⟨M,w⟩ | M accepts w}

Key Relationship

Every decider is also a recognizer (halting is a special case of not-looping). But NOT every recognizer is a decider. Decidable ⊂ Recognizable.

The Church-Turing Thesis

The most important philosophical claim in computer science — proposed independently by Alonzo Church and Alan Turing in 1936.

"Every function that would naturally be regarded as computable can be computed by a Turing Machine."

In plain English: if there is an algorithm for it, a TM can do it.

✔ What It IS

A thesis — a claim / hypothesis, not a proven fact
Universally supported — every proposed model is equivalent to TMs
Battle-tested — no counterexample in 90 years

Evidence: Lambda calculus, recursive functions, RAM machines, Post systems, quantum computers — all compute exactly what TMs compute. Nothing more.

✘ What It is NOT

Not a theorem — cannot be formally proved (no rigorous definition of "algorithm" to prove against)
Not about speed — TMs can be astronomically slow; this says nothing about efficiency
Not unfalsifiable — could theoretically be disproved by finding a "computable" function no TM can compute

Exam Alert

If an exam asks "Is the Church-Turing Thesis a theorem?" the answer is NO. It is a widely accepted hypothesis. Saying it is "proved" is always wrong.

Challenge: Fix the TM Bug

This TM should accept {anbn | n >= 1} but has a bug:

δ(q0, a) = (q1, X, R) -- mark an 'a' δ(q1, a) = (q1, a, R) -- skip a's δ(q1, Y) = (q1, Y, R) -- skip Y's δ(q1, b) = (q2, Y, L) -- mark a 'b' δ(q2, a) = (q2, a, L) -- scan left δ(q2, Y) = (q2, Y, L) -- scan left δ(q2, X) = (q0, X, R) -- restart δ(q0, Y) = (q3, Y, R) -- all a's done? δ(q3, Y) = (q3, Y, R) -- skip Y's δ(q3, B) = (q4, B, R) -- ACCEPT ← BUG IS HERE!

What is wrong? What input breaks it?

TM Variants: All Equivalent!

Many TM variations exist. Remarkably, they all have the exact same computational power.

VariantDescription
Multi-tapek independent tapes + heads
Multi-trackEach cell holds a tuple
2-way infiniteTape infinite in both directions
Stay optionHead can stay put (L, R, or S)
Multi-headMultiple heads on one tape
NondeterministicMultiple transitions per pair
EnumeratorPrints strings instead of accepting

Why This Matters

This robustness is evidence for the Church-Turing Thesis. No matter how you tweak the TM model, you get the same computable functions.

Encoding TMs as Strings

Any TM M can be encoded as a binary string ⟨M⟩. This allows TMs to take other TMs as input!

Step 1 / 4

Why This Matters

Once TMs are strings, we can feed a TM description to another TM. This enables:

  • Universal TM — one TM that simulates any other TM
  • Diagonalization — proves some problems are undecidable
  • Self-reference — a TM can reason about its own code

Countability Argument

There are only countably many TMs (each is a finite binary string), but uncountably many languages (by Cantor's diagonal argument). Therefore most languages have no TM — they are undecidable.

TM Configurations & Computation

An Instantaneous Description (ID) is a snapshot of a TM at one moment: the tape contents, head position, and current state — written as a single string.

Step 0 / 4

How to Read an ID

The ID string uqv means:

  • u = tape symbols left of the head
  • q = the current state (inserted where the head is)
  • v = symbol under the head + everything to its right

The head reads the first symbol of v.

Computation = ID Sequence

ID1 ⊢ ID2 ⊢ ID3 ⊢ … ⊢ IDn

⊢ means "yields in one step." The full sequence is a computation history — a complete record of what the TM did.

Why IDs Matter

IDs let us write an entire computation as a mathematical object (a sequence of strings). This is essential for formal proofs — e.g., proving a language is undecidable by encoding computation histories.

Summary & Cheat Sheet

The TM in One Paragraph

A Turing Machine has a finite control, an infinite tape, and a read/write head. It reads, writes, moves L/R, changes state. It is the most powerful standard model of computation.

Formal Definition

M = (Q, Σ, Γ, δ, q0, B, F) δ: Q x Γ --> Q x Γ x {L, R} ID: X1...Xi-1 q Xi...Xn

Key Techniques

  • Marking symbols (0 → X)
  • Zig-zag / shuttle strategy
  • Multiple tracks
  • Subroutines via states

Language Hierarchy

Regular ⊂ CFL ⊂ Decidable ⊂ RE ⊂ All DFA PDA TM(halts) TM

Must-Know Facts

  • Church-Turing Thesis is NOT a theorem
  • All TM variants are equivalent
  • NTM -> DTM may be exponentially slower
  • TMs = modern computers in power (not speed)
  • RE but not Recursive: Halting Problem
  • Recursive = RE ∩ co-RE

Quiz: Multiple Choice

Quiz: Trace the TM

Given the TM for {0n1n} with transitions:

δ(q0,0)=(q1,X,R) δ(q1,0)=(q1,0,R) δ(q1,Y)=(q1,Y,R) δ(q1,1)=(q2,Y,L) δ(q2,0)=(q2,0,L) δ(q2,Y)=(q2,Y,L) δ(q2,X)=(q0,X,R) δ(q0,Y)=(q3,Y,R) δ(q3,Y)=(q3,Y,R) δ(q3,B)=(q4,B,R) -- q4 = accept

On input 01, what is the sequence of IDs?

Quiz: Design a TM

Design a TM that accepts L = { w ∈ {a,b}* | w contains equal numbers of a's and b's }

Hint: You can use the marking technique.

Which strategy works?