Theory of Computation

The formal limits of machines: automata and the languages they recognise, the Chomsky hierarchy, undecidability, and what P versus NP actually claims.

If you remember nothing else

  • A DFA and an NFA recognise exactly the same class of languages — non-determinism adds convenience, never power.
  • The Chomsky hierarchy in order: regular ⊂ context-free ⊂ context-sensitive ⊂ recursively enumerable, each strictly contained in the next.
  • Finite automata have no memory beyond their state, which is why they cannot recognise aⁿbⁿ — counting is unbounded.
  • The halting problem is undecidable: no algorithm can decide, for every program and input, whether that program halts.
  • NP-complete means in NP and NP-hard; NP-hard alone means at least as hard as everything in NP but possibly not in NP itself.
  • P vs NP asks whether every problem whose solution is quickly verifiable is also quickly solvable. It is open.

Languages and the Chomsky hierarchy

  • An alphabet is a finite non-empty set of symbols.
  • A string is a finite sequence of symbols from ; denotes the empty string, of length 0.
  • is the set of all strings over , including ; excludes .
  • A language is any subset of — possibly empty, possibly infinite.
TypeGrammarLanguage classRecognised byProduction form
Type 3RegularRegularFinite automaton or
Type 2Context-freeContext-freePushdown automaton — a single non-terminal on the left
Type 1Context-sensitiveContext-sensitiveLinear bounded automaton, never shortening
Type 0UnrestrictedRecursively enumerableTuring machineAny

Each class is strictly contained in the one above it: regular ⊂ context-free ⊂ context-sensitive ⊂ recursively enumerable.

Finite automata

DFANFA
Transitions per symbolExactly oneZero, one or many
ε-transitionsNot allowedAllowed
AcceptanceThe single run ends in an accepting stateSome run ends in an accepting state
States neededCan be exponentially moreOften far fewer
Ease of constructionHarderEasier
Expressive powerIdenticalIdentical
  • Moore machine — output depends on the state alone. Mealy machine — output depends on the state and the current input, so it can react one step sooner. They are interconvertible; a Moore machine may need one extra state.
  • DFA minimisation (Myhill-Nerode / table-filling) removes unreachable states and merges equivalent ones. The minimal DFA for a regular language is unique up to renaming, which is why minimisation can prove two DFAs equivalent.

Regular expressions and the pumping lemma

OperationNotationMeaning
Union or Either
ConcatenationOne then the other
Kleene starZero or more repetitions — includes
Kleene plusOne or more repetitions

Kleene's theorem ties the pieces together: a language is regular iff it is described by a regular expression, iff it is accepted by a finite automaton. The three formulations are interchangeable.

Plain textProving L = {aⁿbⁿ | n >= 0} is not regular
Assume L is regular with pumping length p. Choose w = a^p b^p, which is in L and has length 2p >= p. By condition (2), |xy| <= p, so xy lies entirely inside the a's. Therefore y consists only of a's, and by (1) y has at least one a. Pump with i = 2: xy^2 z has MORE a's than b's. That string is not in L, contradicting condition (3). Therefore L is not regular. Intuition: a finite automaton has finitely many states, so it cannot count an unbounded number of a's in order to match them against b's.
Regular languages are closed underYes/No
Union, concatenation, Kleene starYes
Intersection, complement, differenceYes
Reversal, homomorphismYes

Regular languages are closed under every common operation — a useful proof technique: if closure would produce a known non-regular language, the original cannot be regular.

Context-free grammars and pushdown automata

Plain textA grammar and the language it generates
S -> aSb | ε generates { a^n b^n | n >= 0 } Derivation of aaabbb: S => aSb => aaSbb => aaaSbbb => aaabbb This is the canonical context-free but NOT regular language - the stack of a PDA does the counting that a finite automaton cannot. S -> aSb | bSa | SS | ε generates strings with equal a's and b's.
Plain textThe classic ambiguous expression grammar
E -> E + E | E * E | id The string id + id * id has two parse trees: (id + id) * id and id + (id * id) Fix by encoding precedence and associativity into the grammar: E -> E + T | T (+ is lower precedence, left associative) T -> T * F | F (* binds tighter) F -> id | ( E ) This is exactly what a compiler's parser needs, which is why the same grammar reappears in compiler design.
Normal formProduction shapeUsed for
Chomsky Normal Form (CNF) or The CYK parsing algorithm; every derivation of a length-n string takes exactly steps
Greibach Normal Form (GNF) — a terminal firstGuarantees a terminal is consumed at every step, so no left recursion
Context-free languages are closed underYes/No
Union, concatenation, Kleene starYes
IntersectionNo
ComplementNo
Intersection with a regular languageYes

The standard counterexample: {aⁿbⁿcᵐ} and {aⁿbᵐcᵐ} are both context-free, but their intersection {aⁿbⁿcⁿ} is not.

Turing machines and decidability

Recursive (decidable)Recursively enumerable
The machineAlways halts, accepting or rejectingHalts and accepts on strings in the language; may loop forever on strings not in it
Also calledTuring-decidableTuring-recognisable, semi-decidable
Closed under complementYesNo
ExampleIs this string a palindrome?Does this program halt on this input?

The halting problem

Plain textThe diagonalisation argument
Suppose HALTS(P, I) exists and decides whether program P halts on input I. Construct: function TROUBLE(P): if HALTS(P, P): loop forever else: halt Now ask: does TROUBLE(TROUBLE) halt? If it halts -> then HALTS(TROUBLE, TROUBLE) returned true -> so TROUBLE took the "loop forever" branch -> it does NOT halt. Contradiction. If it loops -> then HALTS(TROUBLE, TROUBLE) returned false -> so TROUBLE took the "halt" branch -> it DOES halt. Contradiction. Both cases are impossible, so HALTS cannot exist.

P, NP and NP-completeness

ClassDefinitionExamples
PDecidable by a deterministic Turing machine in polynomial timeSorting, shortest path, primality testing, linear programming
NPA proposed solution can be verified in polynomial timeSAT, Hamiltonian cycle, subset sum, graph colouring
NP-hardEvery problem in NP reduces to it in polynomial time. Need not be in NP — need not even be decidableHalting problem, TSP optimisation version
NP-completeIn NP and NP-hard — the hardest problems that are still in NPSAT, 3-SAT, clique, vertex cover, Hamiltonian cycle, subset sum

Self-check

10 questions on Theory of Computation · nothing is stored

0/10 answered
Question 1

1.Compared to a DFA, an NFA can recognise:

Question 2

2.The language {aⁿbⁿ | n ≥ 0} is not regular because:

Question 3

3.The pumping lemma can be used to prove that a language:

Question 4

4.Context-free languages are NOT closed under:

Question 5

5.A grammar is ambiguous if:

Question 6

6.Which is true of the halting problem?

Question 7

7.A problem that is NP-hard but not NP-complete:

Question 8

8.Which machine model recognises exactly the context-free languages?

Question 9

9.Rice's theorem states that:

Question 10

10.In the Chomsky hierarchy, which containment is correct?