Turing Machines

The Ultimate Model of Computation

Finite Control q₀

CS305 - Formal Language Theory

Arrow keys or Space to navigate

1 / 23

Big Picture: The Chomsky Hierarchy

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

+-----------------------------------------------+ | Type 0: Recursively Enumerable (TM) | | | | +----------------------------------------+ | | | Type 1: Context-Sensitive (LBA) | | | | | | | | +----------------------------------+ | | | | | Type 2: Context-Free (PDA) | | | | | | | | | | | | +----------------------------+ | | | | | | | Type 3: Regular (DFA/NFA) | | | | | | | +----------------------------+ | | | | | +----------------------------------+ | | | +----------------------------------------+ | +-----------------------------------------------+
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.

2 / 23

Motivation: Why Go Beyond PDAs?

PDAs are powerful, but there are simple-sounding languages they cannot recognize.

Languages PDAs Cannot Handle

  • { anbncn | n ≥ 1 } — needs to "count" three things at once, but a stack can only match two
  • { ww | w ∈ {a,b}* } — exact copy. The stack reverses, so it can match wwR but not ww
  • { an | n is a perfect square } — requires arithmetic beyond stack counting

The Core Limitation

A PDA's stack is LIFO (last-in, first-out). It can only access the top. It cannot "go back" or "look deeper" without destroying data above.

Watch a PDA Fail

Input Tape
Stack
$
Click a language above to see why a PDA cannot handle it.
3 / 23

Alan Turing and the Origins

The Year: 1936

  • No electronic computers existed yet
  • Mathematicians were asking: "What does it mean for a problem to be computable?"
  • Alan Turing, a 23-year-old Cambridge mathematician, published "On Computable Numbers"
  • He invented an abstract machine — not to build, but to define computation itself

Key Idea

Turing didn't design a computer. He defined what "computable" means. His machine is a mathematical object, not an engineering blueprint.

The Historical Context

  • David Hilbert (1928) posed the Entscheidungsproblem: "Is there a mechanical procedure to decide all mathematical truths?"
  • Alonzo Church (1936) answered "no" using lambda calculus
  • Alan Turing (1936) independently answered "no" using his machines — and proved undecidability of the Halting Problem

Historical Note

Church and Turing arrived at the same answer by different paths. Their equivalence is part of what gives us confidence in the Church-Turing Thesis (Slide 17).

4 / 23

The Turing Machine Model

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

Head Finite Control State: q0
Demo: TM replaces 0→X and 1→Y, then accepts. Click Step or Run.

The Three Components

  • Tape: Divided into cells, each holding one symbol. Extends infinitely in both directions. Blank symbol (B) fills unused cells.
  • Head: Points to one cell at a time. Can read the symbol, write a new symbol, and move left or right.
  • Finite Control: A finite set of states plus a transition function that dictates behavior.

Analogy

Think of a Turing Machine 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.

5 / 23

Key Differences from PDA

The Turing Machine lifts every major restriction the PDA has.

FeaturePDATM
InputRead onlyRead & Write
Head movementLeft to right onlyLeft & Right
MemoryStack (LIFO)Infinite tape
Memory accessTop of stack onlyAny cell
Input handlingSeparate input tapeInput on same tape
AcceptanceFinal state or empty stackFinal state + halt
PowerContext-freeRecursively Enumerable

PDA

Input (one-way →)

Turing Machine

Tape (← →)
6 / 23

Formal Definition: The 7-Tuple

A Turing Machine is formally defined as M = (Q, Σ, Γ, δ, q0, B, F)

SymbolNameDescription
QStatesFinite set of states
ΣInput alphabetSymbols that appear in input (Σ ⊂ Γ, B ∉ Σ)
ΓTape alphabetAll symbols that can appear on tape (Σ ∪ {B} ⊆ Γ)
δTransition fnQ × Γ → Q × Γ × {L, R}
q0Start stateq0 ∈ Q
BBlank symbolB ∈ Γ but B ∉ Σ
FFinal statesF ⊆ Q (accepting states)

Key Idea: Input vs Tape Alphabet

Σ is what the input is made of (e.g., {0, 1}). Γ is everything that can be on the tape, including Σ, the blank B, and any extra "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 (the blank) F = {q4}

Watch Out

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

7 / 23

The Transition Function

Each transition has three parts: what to write, where to move, and which state to enter.

δ( q , X ) = ( p , Y , D ) | | | | | | | | | +-- Direction: L (left) or R (right) | | | +------ Symbol to WRITE on tape | | +---------- New state to enter | +------------------- Symbol currently READ from tape +----------------------- Current state

Reading a Transition

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

Means: "If you are in state q1 and the head reads 0, then:

  • Write X over the 0
  • Move the head right
  • Go to state q2"
Before: After: ...[ 0 ][ 1 ] ...[ X ][ 1 ] ^ ^ q1 q2

Halting

The TM halts when δ(q, X) is undefined — there is no transition for the current state and tape symbol.

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

Warning

Unlike DFAs and PDAs, a TM might never stop. This is not a bug — it's a fundamental feature that leads to undecidability.

Try It: Transition Explorer

Click a transition above to see it animated.
8 / 23

Instantaneous Description (ID)

An ID captures the complete configuration of a TM at any point in its computation.

Format: X1X2...Xi-1 q Xi...Xn

The state symbol q appears immediately before the symbol the head is currently reading.

Tape: ...B [ 0 ] [ 1 ] [ 1 ] [ 0 ] B... ^ state q2 ID: 0 q2 1 1 0 - "0" is to the left of the head - "q2" is the current state - "1 1 0" is at and to the right - head reads the first symbol after the state: "1"

Moves as ID Transitions

If δ(q, Xi) = (p, Y, R) then:

X1...Xi-1 q Xi Xi+1...Xn | v (move right) X1...Xi-1 Y p Xi+1...Xn

If δ(q, Xi) = (p, Y, L) then:

X1...Xi-2 Xi-1 q Xi...Xn | v (move left) X1...Xi-2 p Xi-1 Y...Xn

Key Idea

IDs let us write an entire computation as a sequence:
ID1 |- ID2 |- ID3 |- ... |- IDn
where |- means "yields in one step."

Try It: ID Transitions

Click a transition to visualize it on the tape.
9 / 23

Example 1: TM for { 0n1n | n ≥ 1 }

Strategy: Zig-zag across the tape, crossing off one 0 and one 1 on each pass.

The Zig-Zag Strategy

Pass 1: [ 0 ][ 0 ][ 1 ][ 1 ] ^ Cross off leftmost 0 (write X) Scan right to find leftmost 1 Cross it off (write Y) Scan left back to start [ X ][ 0 ][ Y ][ 1 ] Pass 2: Skip X's, find next 0 Cross it off (write X) Scan right, skip Y's, find 1 Cross it off (write Y) Scan left back [ X ][ X ][ Y ][ Y ] Check: No more 0's? No more 1's? All matched => ACCEPT!

Transition Table

StateReadWriteMoveNext
q00XRq1
q0YYRq3
q100Rq1
q1YYRq1
q11YLq2
q200Lq2
q2YYLq2
q2XXRq0
q3YYRq3
q3BBRq4

q4 is the accept state. States: q0=find 0, q1=scan right for 1, q2=scan left, q3=verify done.

10 / 23

Trace of TM on Input "0011"

Interactive simulator for the 0n1n zig-zag TM. Load an input and step through.

(only 0s and 1s) State: q0 Find next 0
Step 0 of 0

Key Idea: Why It Works

Each pass crosses off exactly one 0 and one 1. If all 0s and 1s get crossed off together, the counts were equal. If we run out of 1s before 0s (or vice versa), the TM halts in a non-accepting state.

11 / 23

Example 2: TM as a Transducer — Binary Increment

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

Strategy

  • Start with the head at the rightmost bit
  • If it's 0: change to 1, done
  • If it's 1: change to 0 (carry), move left, repeat
  • If it's B (blank, went past all digits): write 1 (carry into new position)
δ(q0, 0) = (qf, 1, R) -- flip 0->1, halt δ(q0, 1) = (q0, 0, L) -- flip 1->0, carry δ(q0, B) = (qf, 1, R) -- carry into blank

Interactive Simulator

(0s and 1s)
State: q0

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.

12 / 23

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've processed it. This is what our 0n1n TM did.

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 holding a tuple of symbols. 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) Γ includes all pairs: (0,#),(0,*), (1,#),(1,*), etc.

4. Subroutines

Design a TM for a subtask (e.g., "shift right"), then "call" it by entering its start state, with a designated "return" state that hands control back.

Key Idea

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

13 / 23

Multi-Tape Turing Machines

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

Demo: 2-tape TM copies input from Tape 1 to Tape 2. Click Step or Run.

Transition

δ(q, a1, a2, ..., ak) = (p, b1, b2, ..., bk, D1, D2, ..., Dk)

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

Equivalence Theorem

Every multi-tape TM can be simulated by a single-tape TM. The single tape encodes all k tapes using multiple tracks and markers for head positions. This costs at most a polynomial slowdown.

14 / 23

Nondeterministic Turing Machines

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

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

Acceptance

An NTM accepts if at least one computation path reaches an accepting state. Think of it as exploring a tree of possibilities.

Equivalence Theorem

NTMs are equivalent in power to deterministic TMs. Every NTM can be simulated by a DTM (using breadth-first search of the computation tree).

But There's a Catch

The simulation may be exponentially slower. If the NTM runs in time t, the DTM simulation might take O(ct) steps. Whether this exponential blowup is necessary is the famous P vs NP problem!

Analogy

An NTM is like a "lucky guesser" — it always picks the right branch. A DTM is like a methodical searcher who has to try every branch. They solve the same problems, but the guesser might be much faster.

15 / 23

Decidable vs. Recognizable Languages

TMs introduce a crucial distinction based on whether they always halt.

Decidable (Recursive)

Input w | v +--------+ +-----+ | TM |----->| YES | (accept) | | +-----+ | ALWAYS | | HALTS | +-----+ | |----->| NO | (reject) +--------+ +-----+ Guaranteed: one of these two.

The TM always halts, either accepting or rejecting. You always get a definitive answer.

Recognizable (Recursively Enumerable)

Input w | v +--------+ +-----+ | TM |----->| YES | (accept) | | +-----+ | MIGHT | | LOOP | +----------+ | |----->| LOOP ... | (no answer) +--------+ +----------+ If w is in L: TM accepts. If w is NOT in L: TM might loop forever!

The TM accepts all strings in L, but may loop forever on strings not in L.

Try It: Classify These Languages

Click a language above to classify it.

Warning: This Distinction Matters!

If a TM loops on input w, you can never be sure whether it will eventually halt or loop forever. There is no general way to detect this — that's the Halting Problem.

16 / 23

The Church-Turing Thesis

The most important philosophical claim in computer science.

The Church-Turing Thesis

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

Equivalently: if there's an algorithm for it, a TM can do it.

What It IS

  • A thesis (claim / belief / hypothesis)
  • Supported by overwhelming evidence
  • Every proposed model of computation (lambda calculus, recursive functions, Post systems, RAM machines, ...) turned out to be equivalent to TMs
  • No one has ever found a counterexample

What It is NOT

  • Not a theorem — it cannot be formally "proved"
  • Not about efficiency (a TM might be astronomically slow)
  • Could theoretically be "falsified" if someone found a new model that computes something TMs can't

Common Mistake

Students often treat it as a proven fact. It is not a theorem. It is a universally accepted belief backed by 90 years of evidence. No proof exists because "effective procedure" has no formal definition outside of it.

17 / 23

Turing Machines and Modern Computers

Your laptop is, in theory, no more powerful than a Turing Machine.

What They Share

+-------------+ +-----------------+ | Your Laptop | | Turing Machine | +-------------+ +-----------------+ | CPU | <=> | Finite Control | | RAM + Disk | <=> | Infinite Tape | | Program | <=> | Transition fn | | Input file | <=> | Initial tape | +-------------+ +-----------------+ Same languages recognized. Same functions computed.

If a problem can be solved by any computer ever built, a TM can solve it too (and vice versa).

The Differences

  • Speed: Real computers are enormously faster (parallelism, caches, O(1) random access vs. O(n) tape traversal)
  • Memory: Real computers have finite memory. TMs have infinite tape. But we can always "buy more RAM" — the TM just assumes we always have enough.
  • Practicality: Nobody programs TMs. They're a theoretical tool for reasoning about what can and can't be computed.

Analogy

A TM is to a computer what a free-body diagram is to a bridge. Nobody builds a bridge from a free-body diagram directly — but it tells you whether the bridge can stand.

18 / 23

Recursive and RE Languages

The landscape of all languages, organized by TM decidability.

Click an example to see where it lives.

co-RE Languages

A language is co-RE if its complement is RE.

Key Theorem

A language L is recursive (decidable) if and only if both L and its complement are RE.

L is recursive ⇔ L ∈ RE ∩ co-RE

RE co-RE +--------+ +--------+ | A_TM | | ~A_TM | | | | | | +----+ | | +----+ | | |Rec.| | | |Rec.| | | +----+ | | +----+ | +--------+ +--------+ Recursive = RE ∩ co-RE

If L is RE but not co-RE (like the Halting Problem), then L is undecidable.

19 / 23

Closure Properties

Which operations preserve decidability and recognizability?

OperationRecursive (Decidable)RE (Recognizable)
Union (L1 ∪ L2)ClosedClosed
Intersection (L1 ∩ L2)ClosedClosed
Complement (&overline;L})ClosedNOT Closed
Concatenation (L1L2)ClosedClosed
Kleene Star (L*)ClosedClosed

Why Recursive is Closed Under Complement

If TM M always halts and decides L, just swap accept and reject. The new TM decides the complement. Easy!

Why RE is NOT Closed Under Complement

If M might loop on strings not in L, "swapping accept/reject" doesn't help — you can't swap "loop forever" with "accept." If RE were closed under complement, then RE ∩ co-RE = RE, making everything decidable. But the Halting Problem shows that's false.

20 / 23

TM Variants: All Equivalent!

Many variations of the TM have been proposed. Remarkably, they all have the exact same computational power.

The Variants

VariantDescription
Multi-tapek independent tapes + heads
Multi-trackEach cell holds a tuple of symbols
2-way infiniteTape extends infinitely in both directions
Stay optionHead can stay put (L, R, or S)
Multi-headMultiple heads on one tape
NondeterministicMultiple transitions per (state, symbol)
EnumeratorPrints strings instead of accepting
Standard single-tape TM: ...[ ][ ][ a ][ b ][ c ][ ][ ]... ^ finite control is equivalent to ALL of these: Multi-tape: k tapes Multi-head: k heads, 1 tape 2-way infinite: ...<----tape---->... Stay option: head can stay NTM: branching choices

Why This Matters

This robustness is evidence for the Church-Turing Thesis. No matter how you tweak the TM model, you get the same class of computable functions. The notion of computability seems to be a natural, model-independent concept.

21 / 23

Summary & Cheat Sheet

The TM in One Paragraph

A Turing Machine is an abstract device with a finite control, an infinite tape, and a read/write head. It reads a symbol, writes a symbol, moves left or right, and changes state. It is the most powerful standard model of computation — anything computable is TM-computable.

Formal Definition

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

Key Techniques

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

Language Classes

Decidable (Recursive): TM halts on ALL inputs. Closed: ∪, ∩, complement, concat, * RE (Recognizable): TM accepts w ∈ L; may loop if w ∉ L. Closed: ∪, ∩, concat, * NOT closed: complement Recursive = RE ∩ co-RE

Must-Know Facts

  • Church-Turing Thesis is NOT a theorem
  • All TM variants (multi-tape, NTM, etc.) are equivalent
  • NTM → DTM simulation may be exponentially slower
  • TMs = modern computers in power (not speed)
  • RE but not Recursive: Halting Problem

Hierarchy Reminder

Regular ⊂ CFL ⊂ Decidable ⊂ RE ⊂ All Languages DFA PDA TM(halts) TM
22 / 23

Challenge Quiz: Turing Machines

Question 1 of 3

23 / 23