Theory of Computation

Complete exam notes Β· Beginner to Advanced Β· With tricks, analogies & examples

0. What is Theory of Computation? EASY

🌍 Real World Analogy
TOC is basically asking: "What can a computer actually do? What are its limits?" Think of it like asking: "What can a human brain do, and what is impossible for it?" TOC answers this for machines.

TOC studies three big questions:

Key Terms β€” Plain English

TermPlain EnglishExample
Alphabet (Ξ£)Set of allowed symbolsΞ£ = {0,1} or {a,b,c}
StringSequence of symbols from alphabet"0101", "abc"
Language (L)Set of strings (that follow some rule)All strings starting with 'a'
Ξ΅ (epsilon)Empty string β€” has zero charactersLike an empty box
Ξ£* (sigma star)ALL possible strings over Ξ£ including Ξ΅All binary strings ever
AutomatonAn abstract machine that reads input and decidesA traffic light controller

1. Chomsky Hierarchy ⭐ EXAM FAV

🌍 Analogy
Think of it like levels of brain power. A simple calculator (DFA) can only check patterns. A robot with a notebook (PDA) can do more. A full computer (TM) is the most powerful. Each higher level CONTAINS all lower levels.
Type 0 β€” Unrestricted Grammar β†’ Turing Machine (most powerful)
Type 1 β€” Context-Sensitive Grammar β†’ Linear Bounded Automaton
Type 2 β€” Context-Free Grammar (CFG) β†’ Pushdown Automaton (PDA)
Type 3 β€” Regular Grammar β†’ Finite Automaton (DFA/NFA) (simplest)
TypeGrammarMachineExample Language
Type 3RegularFA (DFA/NFA)aⁿ, (a+b)*ab
Type 2Context-FreePDAaⁿbⁿ
Type 1Context-SensitiveLBAaⁿbⁿcⁿ
Type 0UnrestrictedTMHalting Problem
🧠 Memory Trick
"Regular Cooks Prepare Tasty"
R = Regular (Type 3) β†’ C = Context-Free (Type 2) β†’ P = Context-Sensitive (Type 1) β†’ T = Turing (Type 0)
OR: Every Type 3 is Type 2, but NOT vice versa (like every square is a rectangle but not every rectangle is a square)

2. Deterministic Finite Automaton (DFA) ⭐ VERY IMP

🌍 Analogy β€” Turnstile at a metro
A metro turnstile is a DFA! It has 2 states: LOCKED and UNLOCKED.
Input coin β†’ goes UNLOCKED. Push β†’ goes LOCKED. That's it. Simple, deterministic, one step at a time.

Formal Definition

A DFA is a 5-tuple: M = (Q, Ξ£, Ξ΄, qβ‚€, F)

SymbolMeaningExample
QFinite set of states{q0, q1, q2}
Ξ£Input alphabet{0, 1}
Ξ΄Transition function Ξ΄: Q Γ— Ξ£ β†’ QΞ΄(q0, 0) = q1
qβ‚€Start state (initial)q0
FSet of final/accepting states{q2}

Key Rules of DFA

Example β€” DFA accepting strings ending in '1'

Language: L = {w ∈ {0,1}* | w ends with 1}

States: q0 (start), q1 (finalβ˜…) q0 --0--> q0 q0 --1--> q1 ← accepting state q1 --0--> q0 q1 --1--> q1 Reading "101": start: q0 read '1': q0 β†’ q1 read '0': q1 β†’ q0 read '1': q0 β†’ q1 ← ends in q1 (final) βœ“ ACCEPTED Reading "100": start: q0 read '1': q0 β†’ q1 read '0': q1 β†’ q0 read '0': q0 β†’ q0 ← ends in q0 (not final) βœ— REJECTED

Transition Table (same example)

StateInput: 0Input: 1
β†’q0 (start)q0q1
β˜…q1 (final)q0q1
🧠 Exam Trick β€” Drawing DFA
Step 1: Figure out what "states" you need to REMEMBER while reading input.
Step 2: For "strings ending in X" β†’ 2 states: "last was X" and "last was not X".
Step 3: For "exactly k occurrences" β†’ need k+1 states (count 0 to k).
Step 4: Always add a dead/trap state if some transition is missing.

3. Non-Deterministic Finite Automaton (NFA) ⭐ VERY IMP

🌍 Analogy β€” Magic guessing machine
NFA is like a magic genie who always guesses the right path! When there's a choice, it splits into MULTIPLE copies of itself and explores ALL paths simultaneously. If ANY copy reaches a final state β†’ ACCEPTED.

DFA vs NFA β€” Differences

FeatureDFANFA
Transitions per stateExactly 1 per symbol0, 1, or MORE per symbol
Ξ΅-transitionsNot allowedAllowed (optional)
Transition functionΞ΄: Q Γ— Ξ£ β†’ QΞ΄: Q Γ— Ξ£ β†’ 2^Q (power set)
AcceptanceOne path must end in FANY path ending in F
PowerSame as NFA!Same as DFA!
⚑ KEY THEOREM
NFA and DFA are EQUIVALENT in power.
Every NFA can be converted to an equivalent DFA. They both accept exactly Regular Languages. NFA just makes it easier to design β€” DFA is easier to implement.

Example β€” NFA for strings ending in 'ab'

Ξ£ = {a, b}, L = all strings ending with "ab" States: q0 (start), q1, q2 (finalβ˜…) q0 --a--> {q0, q1} ← non-deterministic! Two choices on 'a' q0 --b--> {q0} q1 --b--> {q2} q2 --a--> {} ← no transition (dead) q2 --b--> {} Testing "aab": Start: {q0} Read 'a': {q0, q1} ← both copies alive Read 'a': {q0, q1} ← q0 reads 'a'β†’{q0,q1}, q1 reads 'a'β†’{} Read 'b': {q0, q2} ← q0 reads 'b'β†’{q0}, q1... wait q1 reads 'b'β†’{q2} Final set {q0, q2}: q2 ∈ F β†’ βœ“ ACCEPTED

4. Ξ΅-NFA (Epsilon-NFA) MEDIUM

🌍 Analogy β€” Free teleportation
Ξ΅-transitions are like free teleportation β€” you move to another state WITHOUT consuming any input. Like going through a door that costs nothing.

Ξ΅-Closure

Ξ΅-closure(q) = the set of ALL states reachable from q using ONLY Ξ΅-transitions (including q itself).

πŸ“ Example
If q0 β†’Ξ΅β†’ q1 β†’Ξ΅β†’ q2, and q1 β†’Ξ΅β†’ q3, then:
Ξ΅-closure(q0) = {q0, q1, q2, q3}
Ξ΅-closure(q1) = {q1, q2, q3}
Ξ΅-closure(q2) = {q2}

How to process input in Ξ΅-NFA

  1. Start with Ξ΅-closure(qβ‚€)
  2. For each input symbol a:
    • Find all states reachable via a from current set
    • Take Ξ΅-closure of that result
  3. Accept if final set contains any state in F
🧠 Trick
Ξ΅-closure is like saying: "Wherever I am, I also teleport to all rooms connected by free doors." Always compute Ξ΅-closure FIRST (before reading input) and AFTER every transition.

5. NFA β†’ DFA Conversion (Subset Construction) ⭐ MOST IMP

🌍 Analogy
In NFA, multiple copies run in parallel. Subset construction says: let's track ALL active copies as ONE combined state. So {q0, q1, q2} becomes a single DFA state called "q0q1q2".

Algorithm (Subset Construction)

  1. Start state of DFA = Ξ΅-closure(qβ‚€) of NFA
  2. For each new DFA state (set of NFA states) and each input symbol:
    • Find all NFA states reachable via that symbol from any state in the set
    • Take Ξ΅-closure of the result β†’ this is the new DFA state
  3. A DFA state is final if it contains ANY NFA final state
  4. Repeat until no new states

Full Worked Example

NFA over {a,b}: accepts strings ending in 'a'

NFA States: q0 (start), q1 (final)
Ξ΄_NFA:  q0 --a--> {q0, q1}
        q0 --b--> {q0}
        q1 --a--> {}
        q1 --b--> {}

Step 1: DFA start = {q0}

DFA StateOn 'a'On 'b'Final?
β†’{q0}{q0,q1}{q0}No (q1 not in {q0})
β˜…{q0,q1}{q0,q1}{q0}YES (q1 ∈ {q0,q1})

Only 2 DFA states needed! (often NFAβ†’DFA can blow up to 2ⁿ states though)

⚠️ Exam Warning: In worst case, an NFA with n states needs 2ⁿ DFA states. But often the actual number is much less. Always check if some subsets are unreachable!

6. Minimization of DFA MEDIUM

🌍 Analogy β€” Merging duplicate rooms
If two rooms in a house behave EXACTLY the same way (same doors lead to same rooms), why keep both? Merge them! Minimization = removing redundant states.

Table Filling Algorithm (Myhill-Nerode)

  1. Create a table with all pairs of states (qi, qj)
  2. Mark all pairs where one is final and other is not β†’ DISTINGUISHABLE
  3. For each unmarked pair (qi, qj) and each symbol a:
    • If Ξ΄(qi,a) and Ξ΄(qj,a) are already marked β†’ mark (qi,qj)
  4. Repeat until no new pairs can be marked
  5. All UNMARKED pairs β†’ merge those states
🧠 Quick Trick
Two states are equivalent (can be merged) if: starting from either state, reading ANY string always leads to accept/reject in the SAME way. States are distinguishable if there exists ANY string that causes different outcomes.

7. Regular Expressions ⭐ VERY IMP

🌍 Analogy β€” Like phone number format rules
A regular expression is like a pattern rule. "A valid Indian phone number starts with 6,7,8,9 followed by 9 digits" β€” that's a regex!

Basic Operators

OperatorSymbolMeaningExample
Uniona + b or a|ba OR b(0+1) = {0,1}
Concatenationaba followed by bab = {ab}
Kleene Stara*Zero or more a'sa* = {Ξ΅, a, aa, aaa,...}
Kleene Plusa+One or more a'sa+ = {a, aa, aaa,...}
Optionala?Zero or one aa? = {Ξ΅, a}

Important Identities (Memorize!)

// These are guaranteed exam questions
Ξ΅*  = Ξ΅
βˆ…*  = Ξ΅
a** = a*           (star is idempotent)
a+  = aa*  = a*a
a?  = (Ξ΅ + a)
βˆ… Β· a = βˆ…          (nothing Β· anything = nothing)
Ξ΅ Β· a = a           (empty concat = identity)
βˆ… + a = a           (union with empty language = a)
(a+b)* = (a*b*)*    (very common identity)

Common Language β†’ Regex Patterns

Language DescriptionRegular Expression
Strings starting with aa(a+b)*
Strings ending with b(a+b)*b
Strings containing 'ab'(a+b)*ab(a+b)*
Strings of even length((a+b)(a+b))*
Strings with even no. of a's(b*ab*a)*b*
All strings over {a,b}(a+b)*
At least one 'a'(a+b)*a(a+b)*
Exactly two a'sb*ab*ab*

Arden's Theorem (for FA β†’ Regex)

If R = Q + RP where P does not contain Ξ΅, then R = QP*

🧠 Arden's Trick
Think of it like algebra: R = Q + RP β†’ subtract RP β†’ R(1-P) = Q β†’ R = Q/(1-P) = QΒ·P* (Since in languages, 1/(1-P) = P* by geometric series analogy!)

8. Regular Grammar MEDIUM

Types

TypeRule FormExample
Right Linear (RRG)A β†’ aB or A β†’ a or A β†’ Ξ΅S β†’ aS | bS | Ξ΅
Left Linear (LRG)A β†’ Ba or A β†’ a or A β†’ Ξ΅S β†’ Sa | Sb | Ξ΅
Key Fact
Both RRG and LRG generate Regular Languages. You can convert between them and DFA/NFA/Regex freely. But you CANNOT mix left and right in the same grammar (that breaks it).

9. Pumping Lemma for Regular Languages ⭐ EXAM FAVOURITE

🌍 Analogy β€” The rubber band test
If a language is regular, it's like a rubber band β€” you can stretch (pump) the middle part and the string should still be in the language. If stretching breaks the language β†’ it's NOT regular!

The Lemma (Statement)

PUMPING LEMMA
If L is a regular language, then βˆƒ a pumping length p β‰₯ 1 such that
for every string w ∈ L with |w| β‰₯ p,
w can be split as w = xyz where:

1. |xy| ≀ p (xy is in first p characters)
2. |y| β‰₯ 1 (y is non-empty β€” this is what gets pumped)
3. βˆ€ i β‰₯ 0, xy^i z ∈ L (pumping any # of times keeps it in L)

How to PROVE a language is NOT regular (exam procedure)

PROOF TEMPLATE β€” Learn this by heart!
Assume L is regular. Then pumping length p exists.
Choose w = [pick a clever string in L, length β‰₯ p]. Usually: aα΅–bα΅–, 0α΅–1α΅– etc.
Split w = xyz with |xy| ≀ p, |y| β‰₯ 1.
β†’ Since |xy| ≀ p, both x and y consist only of the first symbol!
β†’ y = aᡏ for some k β‰₯ 1
Pump: Let i = 0 or i = 2: xy⁰z or xy²z
Show contradiction: the pumped string is NOT in L.
Conclude: Contradiction! L is NOT regular. β– 

Classic Example β€” aⁿbⁿ is NOT regular

πŸ“ Full Proof
Assume L = {aⁿbⁿ | n β‰₯ 0} is regular. Let p be pumping length.
Choose w = aα΅–bα΅– ∈ L, |w| = 2p β‰₯ p βœ“
Split: w = xyz, |xy| ≀ p, |y| β‰₯ 1
β†’ Since |xy| ≀ p, x and y are entirely in the 'a' part
β†’ x = aΛ’, y = aᡏ (k β‰₯ 1), z = aᡖ⁻˒⁻ᡏbα΅–
Pump i=0: xy⁰z = xz = aᡖ⁻ᡏbα΅–
This has fewer a's than b's β†’ NOT in L βœ—
Contradiction! L is NOT regular. β– 
🧠 Trick β€” Choose w wisely!
For "aⁿbⁿ type" β†’ choose w = aα΅–bα΅–
For "equal symbols" β†’ choose boundary where counts are equal
For primes β†’ choose w = aα΅– where p is prime
Always pick w so that pumping y (which is in the first part) breaks the balance!

10. Closure Properties of Regular Languages MEDIUM

🌍 Analogy
"Closure" means: if I do this operation on regular languages, do I STAY in the regular language family? Like adding two even numbers always gives an even number β€” even numbers are "closed" under addition.
OperationClosed?Method
Union (L₁ βˆͺ Lβ‚‚)βœ… YESNFA construction or product automaton
Intersection (L₁ ∩ Lβ‚‚)βœ… YESProduct automaton (both must accept)
Concatenation (L₁·Lβ‚‚)βœ… YESConnect final states of L₁ to start of Lβ‚‚
Kleene Star (L*)βœ… YESAdd Ξ΅ from final states back to start
Complement (LΜ„)βœ… YESSwap final and non-final states in DFA
Difference (L₁ - Lβ‚‚)βœ… YESL₁ ∩ Lβ‚‚Μ„
Reversal (Lα΄Ώ)βœ… YESReverse all transitions
Homomorphismβœ… YESReplace each symbol per function
🧠 Trick
Regular languages are closed under ALL standard operations. So if both L₁ and Lβ‚‚ are regular, any combination of union, intersection, complement, concat, star β†’ still regular!

11. Context-Free Grammar (CFG) ⭐ VERY IMP

🌍 Analogy β€” English grammar rules
English grammar says: Sentence β†’ Noun Phrase + Verb Phrase. Noun Phrase β†’ Article + Noun. CFG is exactly this! Rules that expand symbols into other symbols, until you get the actual string.

Formal Definition

CFG = (V, T, P, S) where:

SymbolMeaningExample
VVariables (non-terminals), uppercase{S, A, B}
TTerminals (actual symbols), lowercase{a, b}
PProduction rules A β†’ Ξ±S β†’ aSb | Ξ΅
SStart symbolS

Classic CFG Examples

Language: aⁿbⁿ (equal a's and b's)

S β†’ aSb | Ξ΅
Derivation of "aabb":
S β†’ aSb β†’ aaSbb β†’ aaΞ΅bb β†’ aabb βœ“

Language: Balanced Parentheses

S β†’ SS | (S) | Ξ΅
"(())" : S β†’ (S) β†’ (SS) β†’ ((S)S) β†’ ((S)) β†’ (()) β†’ (())

Language: Palindromes over {a,b}

S β†’ aSa | bSb | a | b | Ξ΅

Language: aⁿbᡐ where n β‰  m

S β†’ AB | BA
A β†’ aA | a        (generates aⁿ, n β‰₯ 1)
B β†’ bB | b        (generates bᡐ, m β‰₯ 1)
... (more complex, needs careful construction)

Parse Trees and Derivations

Two Types of Derivation
Leftmost Derivation (LMD): Always expand the leftmost variable first.
Rightmost Derivation (RMD): Always expand the rightmost variable first.

A grammar is Ambiguous if a string has TWO different parse trees (or two different LMDs).

Ambiguous Grammar Example

G: E β†’ E+E | E*E | id
String: id + id * id has TWO parse trees:
  Tree 1: (id + id) * id  β†’ + is root
  Tree 2: id + (id * id)  β†’ * is root  ← AMBIGUOUS!

12. Simplification of CFG MEDIUM

To simplify CFG, remove 4 types of useless things:

1. Useless Symbols

A symbol is useful if it: (a) can derive a terminal string, AND (b) is reachable from S.

2. Ξ΅-Productions (Remove Nullable Variables)

  1. Find all nullable variables (A β†’ Ξ΅ directly, or A β†’ B where B is nullable)
  2. For every production with nullable var, add new production with that var removed
  3. Remove all A β†’ Ξ΅ rules (except S β†’ Ξ΅ if Ξ΅ ∈ L)

3. Unit Productions (A β†’ B)

  1. Find all unit pairs: (A,B) where A ⟹* B by unit steps
  2. For each (A,B), add A β†’ Ξ± for every B β†’ Ξ± (non-unit)
  3. Remove all unit rules

4. Order to Apply

🧠 Correct ORDER (very important for exams!)
Step 1: Remove Ξ΅-productions
Step 2: Remove unit productions
Step 3: Remove useless symbols
If done in wrong order, results may differ!

13. Normal Forms: CNF and GNF ⭐ EXAM IMP

Chomsky Normal Form (CNF)

CNF RULE
Every production must be EITHER:
β€’ A β†’ BC (two variables only)
β€’ A β†’ a (single terminal only)
β€’ S β†’ Ξ΅ (only if Ξ΅ ∈ L)

Converting to CNF β€” Algorithm

  1. Remove Ξ΅-productions
  2. Remove unit productions
  3. Remove useless symbols
  4. For any terminal 'a' in long rules: replace 'a' with new variable Tₐ, add Tₐ β†’ a
  5. Break rules with 3+ variables: A β†’ BCD becomes A β†’ BX, X β†’ CD
πŸ“ CNF Conversion Example
Original: S β†’ aBC
Step 4: Replace 'a' β†’ Tₐ: S β†’ Tₐ BC, Tₐ β†’ a
Step 5: 3 vars β†’ Break: S β†’ Tₐ X, X β†’ BC, Tₐ β†’ a  ← CNF! βœ“

Greibach Normal Form (GNF)

GNF RULE
Every production: A β†’ a Ξ±
where 'a' is a terminal and Ξ± is a (possibly empty) string of variables.
Every RHS starts with a terminal!
🧠 CNF vs GNF Memory Trick
CNF: "Two variables or One terminal" β†’ C for "Couple or Singleton"
GNF: "Terminal FIRST, then variables" β†’ G for "Go terminal first"

GNF is useful because: each step in derivation consumes exactly ONE terminal β†’ useful for parsing proofs.

14. Pushdown Automaton (PDA) ⭐ VERY IMP

🌍 Analogy β€” FA with a stack of plates
PDA = NFA + a stack (like a pile of plates β€” you can only add/remove from the top). The stack gives it infinite memory, which is why it's more powerful than DFA. This is what allows it to match aⁿbⁿ β€” push n A's, then pop one for each b!

Formal Definition

PDA = (Q, Ξ£, Ξ“, Ξ΄, qβ‚€, Zβ‚€, F) where:

SymbolMeaning
QFinite states
Ξ£Input alphabet
Ξ“Stack alphabet
Ξ΄Transition: Q Γ— (Ξ£βˆͺ{Ξ΅}) Γ— Ξ“ β†’ 2^(QΓ—Ξ“*)
qβ‚€Start state
Zβ‚€Initial stack symbol (bottom marker)
FFinal states

Transition Notation

Ξ΄(q, a, X) = {(r, Ξ³)} means: "In state q, reading input 'a', with 'X' on top of stack β†’ go to state r, replace X with Ξ³ on stack"

Stack Operations

You writeMeaning
Ξ³ = Ξ΅POP X from stack (replace with nothing)
Ξ³ = XNo change to stack (keep X)
Ξ³ = YXPUSH Y (Y goes on top of X)
Ξ³ = YREPLACE X with Y

Two Acceptance Modes

ModeAccept when...
By Final StateInput exhausted AND in final state (stack can have anything)
By Empty StackInput exhausted AND stack is empty (state doesn't matter)

Both modes are equivalent β€” you can convert between them!

PDA for aⁿbⁿ

States: q0 (start), q1, q2 (final)
Stack alphabet: {A, Zβ‚€}

Phase 1 (reading a's): push A for each 'a'
  Ξ΄(q0, a, Zβ‚€) = {(q0, AZβ‚€)}   ← first 'a': push A on Zβ‚€
  Ξ΄(q0, a, A)  = {(q0, AA)}    ← each 'a': push A on A

Phase 2 (reading b's): pop A for each 'b'
  Ξ΄(q0, b, A)  = {(q1, Ξ΅)}     ← first 'b': pop A, go to q1
  Ξ΄(q1, b, A)  = {(q1, Ξ΅)}     ← each b: pop A
  Ξ΄(q1, Ξ΅, Zβ‚€) = {(q2, Zβ‚€)}   ← stack has only Zβ‚€ β†’ accept

Input "aabb":
  (q0, aabb, Zβ‚€) β†’ (q0, abb, AZβ‚€) β†’ (q0, bb, AAZβ‚€)
  β†’ (q1, b, AZβ‚€) β†’ (q1, Ξ΅, Zβ‚€) β†’ (q2, Ξ΅, Zβ‚€) βœ“ ACCEPTED
🧠 PDA Design Strategy
For matching patterns (aⁿbⁿ): PUSH first type, POP on second type
For palindromes: PUSH first half, POP second half
For ww^R (reverse): same as palindrome
Non-determinism helps! You can guess the middle point.

15. Pumping Lemma for Context-Free Languages ⭐ IMP

CFL PUMPING LEMMA
If L is CFL, βˆƒ pumping length p β‰₯ 1 such that every w ∈ L with |w| β‰₯ p can be split as w = uvxyz where:
1. |vy| β‰₯ 1 (v or y is non-empty)
2. |vxy| ≀ p (middle part ≀ p)
3. βˆ€ i β‰₯ 0: uvⁱxyⁱz ∈ L (pump both v and y together)
🧠 Key Difference from RL Pumping Lemma
RL: w = xyz, pump y alone (one part)
CFL: w = uvxyz, pump v and y SIMULTANEOUSLY (two parts)
Think: CFL has a stack, so two things are being tracked!

Classic Example β€” aⁿbⁿcⁿ is NOT CFL

πŸ“ Proof
Assume L = {aⁿbⁿcⁿ} is CFL. Let p be pumping length.
Choose w = aα΅–bα΅–cα΅– ∈ L
Split w = uvxyz, |vxy| ≀ p, |vy| β‰₯ 1
β†’ Since |vxy| ≀ p, vxy cannot span all three sections (a,b,c)
β†’ Case 1: v,y only in a's and b's. Pump i=2: more a's+b's but same c's β†’ NOT in L βœ—
β†’ Case 2: v,y only in b's and c's. Pump i=2: same a's but more b's+c's β†’ NOT in L βœ—
β†’ Case 3: v or y spans two symbol types (e.g., aᡏbΚ²). Pump i=2: order violated β†’ NOT in L βœ—
All cases β†’ contradiction! aⁿbⁿcⁿ is NOT CFL. β– 

16. Turing Machine (TM) ⭐ MOST IMP

🌍 Analogy β€” The ultimate supercomputer on a tape
Imagine a robot with a pencil reading a tape of paper (infinite in both directions). It can: read the current symbol, write a new symbol, move left or right, change its internal state. That's a Turing Machine β€” the most powerful computer model possible!

Formal Definition

TM = (Q, Ξ£, Ξ“, Ξ΄, qβ‚€, B, F) where:

SymbolMeaning
QFinite states
Ξ£Input alphabet (Ξ£ βŠ‚ Ξ“)
Ξ“Tape alphabet (includes blank B)
δδ: Q Γ— Ξ“ β†’ Q Γ— Ξ“ Γ— {L, R}
qβ‚€Start state
BBlank symbol (represents empty tape)
FFinal/accepting states

Transition Meaning

Ξ΄(q, a) = (r, b, L) means: In state q, reading 'a' β†’ go to state r, write 'b', move Left

Configuration (Instantaneous Description)

Written as: Ξ± q Ξ² where q = current state, Ξ± = tape left of head, Ξ² = tape at+right of head

TM for aⁿbⁿ β€” Step by Step

Idea: Repeatedly cross off one 'a' and one 'b' until all done.

Input: "aabb"
Tape: [a][a][b][b][B]

Strategy:
  β†’ Replace leftmost 'a' with X
  β†’ Scan right to find leftmost 'b', replace with Y  
  β†’ Go back left, find next 'a'...
  β†’ When no more 'a': verify no more 'b' remain

States: q0(start), q1(scan right for b), q2(scan left for a),
        q3(check all done), q_accept, q_reject

Ξ΄(q0, a) = (q1, X, R)   ← cross off 'a', go right to find 'b'
Ξ΄(q1, a) = (q1, a, R)   ← skip over a's
Ξ΄(q1, Y) = (q1, Y, R)   ← skip over already-marked Y's
Ξ΄(q1, b) = (q2, Y, L)   ← cross off 'b', go left
Ξ΄(q2, a) = (q2, a, L)   ← scan left
Ξ΄(q2, Y) = (q2, Y, L)   ← scan left past Y's
Ξ΄(q2, X) = (q0, X, R)   ← found X, go right for next pair
Ξ΄(q0, Y) = (q3, Y, R)   ← no more a's, check no b's
Ξ΄(q3, Y) = (q3, Y, R)   ← skip Y's
Ξ΄(q3, B) = (q_accept, B, R)  ← all matched! ACCEPT

TM Acceptance Modes

ModeMeaning
AcceptTM enters q_accept state
RejectTM enters q_reject state (explicit rejection)
LoopTM runs forever (never halts) β€” the problem!
Church-Turing Thesis
Any algorithm that can be executed by ANY computer can also be simulated by a Turing Machine. TM is the universal model of computation β€” nothing is more powerful.

17. Variants of Turing Machines MEDIUM

VariantDescriptionPower
Standard TM1 tape, infinite rightBase
Multi-tape TMk tapes, k heads= Standard TM
Non-deterministic TMMultiple choices at each step= Standard TM
2-way infinite tapeTape extends left AND right= Standard TM
Universal TM (UTM)Simulates ANY TM given its description= Standard TM
Linear Bounded Automaton (LBA)TM restricted to input-length tapeCSL (Type 1)
🧠 Trick β€” All TM variants = same power!
No matter how many tapes, heads, or non-determinism you add β†’ still same as basic TM. The only exception is LBA (restricted TM) which is strictly weaker.

Universal Turing Machine (UTM)

A UTM takes as input: ⟨M, w⟩ (encoding of TM M and input w) β†’ simulates M on w. This is the theoretical basis of modern programmable computers!

18. Decidability & Undecidability ⭐ VERY IMP

🌍 Analogy β€” Questions with definitive answers
Decidable = "Is 7 prime?" β€” a computer can always answer YES or NO.
Undecidable = "Will this program run forever?" β€” no computer can EVER answer this reliably, even in principle.

Language Classification

ClassAlso CalledTM BehaviorExample
Decidable (Recursive)RecursiveAlways halts with accept/rejectaⁿbⁿ, all CFLs
Semi-Decidable (RE)Recursively EnumerableHalts on accept; may loop on rejectA_TM
Not REβ€”No TM can recognize itComplement of Halting Problem

The Halting Problem (HP)

HALTING PROBLEM β€” Undecidable!
HP: Given TM M and input w, does M halt on w?

A_TM = {⟨M, w⟩ | TM M accepts input w} β€” also undecidable!

These are the most famous undecidable problems β€” guaranteed in exams.

Proof that A_TM is Undecidable (Diagonalization)

πŸ“ Proof Sketch
Assume H is a TM that decides A_TM (decides if ⟨M,w⟩ accepts).
Construct D(⟨M⟩): runs H(⟨M, ⟨M⟩⟩) then does the OPPOSITE:
  β€’ If H says "M accepts ⟨M⟩" β†’ D REJECTS
  β€’ If H says "M rejects ⟨M⟩" β†’ D ACCEPTS
Run D on itself: D(⟨D⟩):
  β€’ If D accepts ⟨D⟩ β†’ by definition, D rejects β†’ contradiction!
  β€’ If D rejects ⟨D⟩ β†’ by definition, D accepts β†’ contradiction!
Both cases are impossible β†’ H cannot exist β†’ A_TM is undecidable. β– 

Reduction

To prove language L is undecidable: reduce a known undecidable problem to L.

If A is undecidable and A β‰€β‚˜ B (A reduces to B), then B is also undecidable.

🧠 Know These Undecidable Problems
β€’ Halting Problem (HP)
β€’ A_TM (acceptance problem)
β€’ E_TM (does TM accept empty language?) β€” undecidable
β€’ EQ_TM (are two TMs equivalent?) β€” undecidable
β€’ Rice's Theorem: ANY non-trivial property of TM-recognized languages is undecidable!

Rice's Theorem (Big Shortcut!)

RICE'S THEOREM
Any non-trivial semantic property of Turing Machines is undecidable.

Non-trivial = property that is TRUE for some TMs and FALSE for others.
Semantic = about the language it recognizes (not about implementation).

Examples of undecidable (by Rice's): "Does TM accept empty string?", "Does TM accept at least 3 strings?", "Is L(M) regular?"

19. Complexity Theory: P, NP, NP-Complete ADVANCED

🌍 Analogy
P: Problems a computer can solve quickly (like finding shortest path in a map).
NP: Problems where if someone gives you an answer, you can CHECK it quickly β€” but finding the answer yourself might be slow.
Like a jigsaw puzzle: checking if it's solved is easy, solving from scratch is hard!

Definitions

ClassDefinitionExamples
PSolvable in polynomial time O(nᡏ)Sorting, shortest path, primality testing
NPSolution verifiable in polynomial timeSAT, TSP, Graph Coloring, Subset Sum
NP-HardEvery NP problem reduces to it; at least as hard as NPHalting problem, TSP optimization
NP-CompleteIn NP AND NP-Hard (hardest problems in NP)SAT, 3-SAT, Vertex Cover, Clique
The Big Question: P = NP?
The most famous unsolved problem in computer science!
If P = NP: every problem whose solution can be checked fast can also be solved fast.
Most experts believe P β‰  NP but it has never been proven.

Polynomial-Time Reductions

To show B is NP-Complete:

  1. Show B ∈ NP (give a polynomial verifier)
  2. Show some NP-Complete problem A β‰€β‚š B (A reduces to B in poly time)

Important NP-Complete Problems to Know

ProblemDescription
SATBoolean formula satisfiability (first proven NP-C by Cook)
3-SATSAT with clauses of exactly 3 literals
Vertex CoverFind k vertices that touch all edges
CliqueFind k mutually connected vertices
Hamiltonian PathPath visiting every node exactly once
TSP (decision)Is there a tour of cost ≀ k?
Subset SumDoes a subset sum to target T?
🧠 Relationships to Remember
P βŠ† NP βŠ† NP-Hard
NP-Complete = NP ∩ NP-Hard
If any NP-Complete problem is in P β†’ P = NP (and world breaks!)
Venn diagram: P is a circle inside NP; NP-Complete sits on the boundary of NP; NP-Hard extends beyond NP.

20. Quick Revision Cheat Sheet πŸš€

Hierarchy Summary

Language ClassMachineGrammarClosed Under
RegularDFA/NFARegularEverything
CFLPDACFGUnion, Concat, Star (NOT ∩, complement)
CSLLBACSGAll boolean ops
RE (r.e.)TM (halts on accept)UnrestrictedUnion, Concat, Star, ∩
RecursiveTM (always halts)β€”All boolean ops

CFL Closure Properties

OperationCFL closed?
Unionβœ… YES
Concatenationβœ… YES
Kleene Starβœ… YES
Intersection❌ NO (counterexample: {aⁿbⁿcⁿ})
Complement❌ NO
Intersection with Regularβœ… YES

Must-Know Non-Regular Languages

Must-Know Non-CFL Languages

Key Formulae Flashcards

MUST MEMORISE
FactValue
Max DFA states for n-state NFA2ⁿ
Min states DFA for strings divisible by kk states
|Ξ£*| (all strings over Ξ£)Infinite (countably)
|Power set of Q| (|2^Q|)2^|Q|
Pumping length for Myhill-Nerode# of equivalence classes
TM tape alphabet vs inputΞ“ βŠ‡ Ξ£, B ∈ Ξ“, B βˆ‰ Ξ£

Last-Minute Exam Tips

⚠️ Top 5 things students get wrong:
1. DFA must have transition for EVERY state-symbol pair β€” don't forget dead states!
2. NFAβ†’DFA: the empty set βˆ… can be a DFA state too (dead/trap state)
3. Pumping Lemma: |y| β‰₯ 1 (NOT |y| β‰₯ 0) β€” y must be non-empty!
4. CFL Pumping Lemma: pump v AND y together (both by i), not separately
5. Undecidable β‰  Non-RE β€” Halting Problem IS RE (semi-decidable), just not decidable!
🧠 Final Memory Map
Regular β†’ DFA/NFA/Regex ← all equivalent ← prove NOT regular with Pumping Lemma
CFL β†’ CFG/PDA ← equivalent ← prove NOT CFL with CFL Pumping Lemma
Decidable β†’ TM always halts ← closed under complement
RE β†’ TM halts on yes ← NOT closed under complement
NP-Complete β†’ hardest in NP ← show by reduction from known NP-C problem