Raw

Lecture 14. Automata theory

Overview

This is the final topic of the course, and it is where the strands we have been spinning finally braid together. Automata theory is built from sets and tuples (Lecture 2), from functions viewed as a special kind of relation (Lecture 3), from the Boolean/combinational logic of gates and adders (Lecture 5), and from the propositional connectives whose set-theoretic shadows — union, intersection, complement — reappear here as operations on languages (Lecture 6). What is genuinely new is a single idea that none of those earlier chapters possessed: state, a finite memory of the relevant past that lets a device behave differently on the same input depending on where it currently is.

We begin, as the slides do, with a concrete hardware question — the difference between combinational and sequential logic — and then peel away wires and gates until only the essential mechanism remains: a control unit that occupies one of finitely many states and moves between them as it reads symbols. That abstraction is the finite automaton, the central object of this chapter.

From the hardware contrast we build up, in order: the vocabulary of alphabets, strings, and languages; the general abstract machine (tape, reader, control unit); the deterministic finite automaton (DFA) with its extended transition function δ^\hat\delta, its runs, its notion of acceptance, and the language L(M)L(M) it recognises; the output-producing Mealy and Moore machines; the nondeterministic automaton (NFA) with ε\varepsilon-moves and the subset construction that flattens it back to a DFA; the identification of finite automata with the regular languages and regular expressions (Kleene) and the closure properties of that class; and finally the pumping lemma, which lets us prove that some languages — famously {0n1n}\{0^n1^n\} — lie beyond the reach of any finite automaton. We close with a signpost toward the richer machines (pushdown automata, Turing machines) that lie past the horizon of this course.

Unlike earlier drafts of these notes, this chapter proves what it states. Every theorem comes with a complete argument, and every automaton comes with a trace or an induction that certifies it does what we claim. Deliberately out of scope: Turing machines beyond a mention, and the DFA-minimisation algorithm (Myhill–Nerode is noted but not developed).


14.1 Combinational versus sequential logic

Digital circuits split into two kinds according to what their outputs depend on, and the split is exactly the doorway into automata theory.

Definition (combinational logic). A combinational circuit is one whose outputs are determined only by the current inputs. It has no memory: present the same input bits and you always get the same output bits.

This is precisely the world of Lecture 5. A combinational circuit computes a Boolean function f:{0,1}n{0,1}mf:\{0,1\}^n\to\{0,1\}^m, and everything we said there — truth tables, canonical normal forms, Karnaugh-map minimisation — describes it completely. Typical building blocks are adders, subtractors, multiplexers, encoders, decoders, and the arithmetic-logic unit (ALU) assembled from them.

Adder example. A full adder turns three input bits — two operands a,ba,b and a carry-in cinc_{\text{in}} — into a sum bit and a carry-out that are Boolean functions of those inputs alone:

s=abcin,cout=(ab)(acin)(bcin).s = a \oplus b \oplus c_{\text{in}}, \qquad c_{\text{out}} = (a\wedge b)\vee(a\wedge c_{\text{in}})\vee(b\wedge c_{\text{in}}).

Chaining nn full adders (a ripple-carry adder) is still combinational: the output word is a fixed function of the current input word. Nowhere is any past input remembered.

Definition (sequential logic). A sequential circuit is one whose outputs depend on the current inputs and on an internal state that summarises relevant past inputs. The state is held in memory elements (flip-flops), and updates are typically paced by a clock signal.

Because the same input can now produce different outputs depending on the stored state, sequential circuits can count, remember, detect patterns over time, and implement protocols — none of which a combinational circuit can do. A pocket calculator’s “+” key does something different depending on what you typed before; a turnstile behaves differently locked versus unlocked; a traffic light cycles. Each needs memory of the past.

State diagram of a turnstile: a coin moves it from Locked to Unlocked and a push moves it back — behaviour depends on the current state

The flip-flop stores one bit of state. The atomic memory element of sequential hardware is the flip-flop: a one-bit register that holds a 00 or a 11 between clock ticks and updates on the tick according to its inputs. A circuit with kk flip-flops has 2k2^k possible stored configurations, i.e. 2k2^k states. This is the physical incarnation of the abstract “state” of an automaton: a finite automaton with nn states can be realised by a sequential circuit using log2n\lceil \log_2 n\rceil flip-flops plus combinational logic to compute the next state and the output. Combinational logic (Lecture 5) and sequential logic (this lecture) are therefore not rivals but layers: the combinational part computes the transition function, the flip-flops hold the state between ticks.

Remark (the pivot word is state). The instant a circuit needs to “remember where it is”, it becomes sequential, and the natural mathematical model of “a device that lives in one of finitely many states” is the finite automaton. Automata theory is, in effect, the algebra of sequential behaviour — the abstract theory of what memory-bounded machines can and cannot do.


14.2 Alphabets, strings, and languages

Before we can say what a machine reads or accepts, we need vocabulary for its input. All of it is elementary set theory (Lecture 2) applied to sequences.

Definition (alphabet, string, length). An alphabet Σ\Sigma is a finite, non-empty set whose elements are called symbols. A string (or word) over Σ\Sigma is a finite sequence of symbols from Σ\Sigma. The length w|w| of a string ww is the number of symbols in it. The unique string of length 00 is the empty string, written ε\varepsilon.

For example, over Σ={0,1}\Sigma=\{0,1\} the strings of length 2\le 2 are ε, 0, 1, 00, 01, 10, 11\varepsilon,\ 0,\ 1,\ 00,\ 01,\ 10,\ 11. Note ε=0|\varepsilon|=0 and, e.g., 0110=4|0110|=4.

Definition (concatenation). The concatenation of strings u=a1amu=a_1\cdots a_m and v=b1bnv=b_1\cdots b_n is uv=a1amb1bnuv=a_1\cdots a_m b_1\cdots b_n, of length m+nm+n. Concatenation is associative, and ε\varepsilon is its identity: εw=wε=w\varepsilon w = w\varepsilon = w. We write wkw^k for ww concatenated with itself kk times, with w0=εw^0=\varepsilon.

Remark (a monoid, echoing Lecture 3). The set of all strings under concatenation, with identity ε\varepsilon, is a monoid — an associative operation with a unit. This is the same algebraic pattern you met when composing relations and functions in Lecture 3; there, composition of functions was associative with the identity function as unit. String concatenation is that structure made concrete.

Definition (Σ\Sigma^\ast, Σ+\Sigma^+, Σk\Sigma^k). Σk\Sigma^k is the set of all strings of length exactly kk. The Kleene star Σ=k0Σk\Sigma^\ast=\bigcup_{k\ge 0}\Sigma^k is the set of all finite strings over Σ\Sigma (including ε\varepsilon); Σ+=k1Σk\Sigma^+=\bigcup_{k\ge 1}\Sigma^k excludes ε\varepsilon.

Substrings, prefixes, and suffixes are defined the obvious way: xx is a prefix of ww if w=xyw=xy for some yy, a suffix if w=yxw=yx for some yy, and a substring if w=uxvw=uxv for some u,vu,v.

Definition (language). A language over Σ\Sigma is any subset LΣL\subseteq\Sigma^\ast.

A language is just a set of strings — possibly finite ({01,10}\{01,10\}), possibly infinite ({0n1n:n0}\{0^n1^n:n\ge 0\}), possibly empty (\varnothing), possibly all of Σ\Sigma^\ast. Because languages are sets, the Boolean operations of Lecture 6 apply verbatim: union L1L2L_1\cup L_2, intersection L1L2L_1\cap L_2, and complement L=ΣL\overline{L}=\Sigma^\ast\setminus L. Two operations are special to strings:

  • Concatenation of languages: L1L2={uv:uL1, vL2}L_1L_2=\{uv : u\in L_1,\ v\in L_2\}.
  • Kleene star of a language: L=k0LkL^\ast=\bigcup_{k\ge 0}L^k, where L0={ε}L^0=\{\varepsilon\} and Lk+1=LkLL^{k+1}=L^kL. Thus LL^\ast is all concatenations of zero or more strings from LL.

Common pitfall. ε\varepsilon (the empty string) and \varnothing (the empty language) are different objects, and both differ from {ε}\{\varepsilon\} (the language containing only the empty string). Note L=L\varnothing=\varnothing but L{ε}=LL\{\varepsilon\}=L, and ={ε}\varnothing^\ast=\{\varepsilon\}.

The central question of the chapter can now be phrased crisply: which languages can a finite-memory machine recognise?


14.3 The abstract machine and the idea of state

We first describe a general abstract machine, so that stronger machines appear later as the same picture with more memory. It consists of an input tape holding a string of symbols, a reader (head) positioned over one cell, and a control unit that is always in some internal state. In one step the machine may read the current symbol, inspect and change auxiliary memory, change the control-unit state, possibly write to the tape, and move the reader.

Definition (configuration). A snapshot of an abstract machine at one instant — its configuration — is the triple (current control state, current memory contents, position of the reader). A computation is a sequence of configurations, each obtained from the previous one by a single legal step.

Definition (finite-state machine). A finite-state machine (finite automaton) is the most restricted such machine: it has no auxiliary memory beyond its control state, its reader moves one symbol per step in a single direction (left to right), and its control unit has a finite set of states S={s0,s1,,sN}S=\{s_0,s_1,\dots,s_N\}. Its control may be deterministic (each situation has exactly one next move) or nondeterministic (a situation may permit several).

The deck writes a general finite machine as the seven-tuple

M=(S,  I,  O,  f,  g,  s0,  F),M=(S,\; I,\; O,\; f,\; g,\; s_0,\; F),

where SS is the state set, II the input alphabet, OO the output alphabet, ff the transition (“movement”) function, gg the output (“exit”) function, s0s_0 the start state, and FF the set of final states. This form covers machines that produce output (Mealy/Moore, §14.6). When we care only whether a string is accepted — not what is printed — we discard OO and gg and obtain the acceptor studied next.


14.4 Deterministic finite automata

Definition (DFA). A deterministic finite automaton is a five-tuple

M=(Q,Σ,δ,q0,F),M=(Q,\Sigma,\delta,q_0,F),

where

  • QQ is a finite set of states;
  • Σ\Sigma is a finite input alphabet;
  • δ:Q×ΣQ\delta : Q \times \Sigma \to Q is the transition function: given the current state and the symbol just read, it names the unique next state;
  • q0Qq_0 \in Q is the start state;
  • FQF \subseteq Q is the set of accepting (final) states.

The word deterministic means δ\delta is a total function: for every state and every input symbol there is exactly one next state — no choices, no missing moves. A DFA has no output alphabet; its “answer” is simply whether it halts in an accepting state.

Cross-reference (Lecture 3). The transition function δ\delta is exactly a function, and — as Lecture 3 established — a function is a special binary relation δ(Q×Σ)×Q\delta\subseteq (Q\times\Sigma)\times Q that is total (defined on every input pair) and single-valued (at most one output). Determinism is nothing more than “δ\delta is a genuine function, not merely a relation.” When we drop those two conditions in §14.7 we get exactly the relational — nondeterministic — machine.

Definition (transition table). Because δ\delta has a small finite domain, we list it completely as a transition table with one row per state and one column per input symbol; the entry is the next state. The start state is marked with an arrow \rightarrow and each accepting state with a star \ast.

Definition (state diagram). The same δ\delta is drawn as a labelled directed graph (Lecture 10): one node per state, and an edge qaqq \xrightarrow{a} q' for each transition δ(q,a)=q\delta(q,a)=q'. The start state has an incoming arrow “from nowhere”; accepting states are drawn as double circles. Reading a string means walking the graph, following the edge labelled by each symbol in turn.

The extended transition function

δ\delta tells us what one symbol does. To speak of whole strings we lift it to δ^:Q×ΣQ\hat\delta:Q\times\Sigma^\ast\to Q.

Definition (extended transition function). Define δ^\hat\delta by recursion on the length of the string:

δ^(q,ε)=q,δ^(q,wa)=δ(δ^(q,w),a),\hat\delta(q,\varepsilon)=q, \qquad \hat\delta(q, wa)=\delta\big(\hat\delta(q,w),\,a\big),

where wΣw\in\Sigma^\ast and aΣa\in\Sigma.

So δ^(q0,w)\hat\delta(q_0,w) is the single state reached after reading all of ww from q0q_0. The following bookkeeping lemma — that δ^\hat\delta respects concatenation — is used repeatedly (in the product and subset constructions, and in the pumping lemma), so we prove it once.

Lemma 14.1 (concatenation law for δ^\hat\delta). For every state qq and all strings u,vΣu,v\in\Sigma^\ast,

δ^(q,uv)=δ^(δ^(q,u),v).\hat\delta(q,uv)=\hat\delta\big(\hat\delta(q,u),\,v\big).

Proof. Fix qq and uu; induct on v|v|.

Base v=0|v|=0, i.e. v=εv=\varepsilon. Then uv=uuv=u, so the left side is δ^(q,u)\hat\delta(q,u). The right side is δ^(δ^(q,u),ε)\hat\delta(\hat\delta(q,u),\varepsilon), which equals δ^(q,u)\hat\delta(q,u) by the first clause of the definition. The two sides agree.

Inductive step. Suppose the identity holds for a string ww; we prove it for v=wav=wa with aΣa\in\Sigma. Using the definition of δ^\hat\delta, then the induction hypothesis, then the definition again:

δ^(q,uwa)=δ^(q,(uw)a)=δ(δ^(q,uw),a)=IHδ(δ^(δ^(q,u),w),a)=δ^(δ^(q,u),wa).\hat\delta(q,u\,wa) =\hat\delta(q,(uw)a) =\delta\big(\hat\delta(q,uw),a\big) \overset{\text{IH}}{=}\delta\big(\hat\delta(\hat\delta(q,u),w),a\big) =\hat\delta\big(\hat\delta(q,u),wa\big).

This is the claim for v=wav=wa, completing the induction. \blacksquare

Runs, acceptance, and the language of a DFA

Fix a DFA M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) and an input w=a1a2anw=a_1a_2\cdots a_n.

Definition (run). The run of MM on ww is the sequence of states r0,r1,,rnr_0,r_1,\dots,r_n with r0=q0r_0=q_0 and ri=δ(ri1,ai)r_i=\delta(r_{i-1},a_i) for 1in1\le i\le n. Equivalently ri=δ^(q0,a1ai)r_i=\hat\delta(q_0,a_1\cdots a_i). Because MM is deterministic, each string has exactly one run.

Definition (acceptance and language). MM accepts ww if its run ends in an accepting state, δ^(q0,w)F\hat\delta(q_0,w)\in F; otherwise MM rejects ww. The language recognised by MM is

L(M)={wΣδ^(q0,w)F}.L(M)=\{\,w\in\Sigma^\ast \mid \hat\delta(q_0,w)\in F\,\}.

Definition (regular language — first form). A language LL is regular if L=L(M)L=L(M) for some DFA MM.

Design principle. Building a DFA for a target language means choosing states that record exactly the finite amount of information about the input read so far that is needed to decide acceptance — and no more, since only finitely many states are allowed. Each state is an equivalence class of histories: two input prefixes that must be treated identically from now on may share a state. (This idea, made precise, is the Myhill–Nerode theorem, which we only name here.)


14.5 Worked DFA designs

We now design and verify several DFAs. Each verification is either a full trace or an induction certifying L(M)L(M) is what we claim.

Example A — even number of $1$s

Task. Over Σ={0,1}\Sigma=\{0,1\}, accept exactly the strings with an even number of $1$s (zero is even, so ε\varepsilon is accepted).

Design idea. The only fact we must remember is the parity of the count of $1$s. Parity has two values, so two states suffice. Reading 00 leaves parity unchanged; reading 11 flips it.

Formal definition.

Q={qe,qo},Σ={0,1},q0=qe,F={qe},Q=\{q_{\text{e}},q_{\text{o}}\},\quad \Sigma=\{0,1\},\quad q_0=q_{\text{e}},\quad F=\{q_{\text{e}}\},

State on 0 on 1
  qe\rightarrow\ \ast\ q_{\text{e}} qeq_{\text{e}} qoq_{\text{o}}
qoq_{\text{o}} qoq_{\text{o}} qeq_{\text{e}}

State diagram (in words). Two nodes. qeq_{\text{e}} is a double circle with the start arrow into it; qoq_{\text{o}} is a single circle. Each state has a self-loop labelled 0. Two arrows labelled 1 cross between the states, one each way.

DFA accepting an even number of 1s: states "even" (accepting) and "odd", each with a 0 self-loop and 1-edges crossing between them

Trace on 1001 (two $1$s, should accept):

Step State before Symbol State after
start qeq_{\text{e}}
1 qeq_{\text{e}} 1 qoq_{\text{o}}
2 qoq_{\text{o}} 0 qoq_{\text{o}}
3 qoq_{\text{o}} 0 qoq_{\text{o}}
4 qoq_{\text{o}} 1 qeq_{\text{e}}

Ends in qeFq_{\text{e}}\in F: accepted. On 1011 (three $1$s) the run is qeqoqoqeqoq_{\text{e}}\to q_{\text{o}}\to q_{\text{o}}\to q_{\text{e}}\to q_{\text{o}}, ending outside FF: rejected. Both correct.

We can do better than spot checks and prove correctness.

Proposition 14.2. For every w{0,1}w\in\{0,1\}^\ast, δ^(qe,w)=qe\hat\delta(q_{\text{e}},w)=q_{\text{e}} if ww has an even number of $1$s, and =qo=q_{\text{o}} otherwise. Consequently L(M)={w:#1(w) is even}L(M)=\{w:\#_1(w)\text{ is even}\}, where #1(w)\#_1(w) is the number of $1$s in ww.

Proof. Induct on w|w|. Base: w=εw=\varepsilon has #1=0\#_1=0 (even) and δ^(qe,ε)=qe\hat\delta(q_{\text{e}},\varepsilon)=q_{\text{e}}. Step: write w=xaw=xa and assume the claim for xx. If a=0a=0 then #1(w)=#1(x)\#_1(w)=\#_1(x) and, since δ(q,0)=q\delta(q,0)=q for both states, δ^(qe,xa)=δ^(qe,x)\hat\delta(q_{\text{e}},xa)=\hat\delta(q_{\text{e}},x); the parity and the state both stay put, so the correspondence persists. If a=1a=1 then #1(w)=#1(x)+1\#_1(w)=\#_1(x)+1 (parity flips) and δ(,1)\delta(\,\cdot\,,1) swaps the two states (state flips); flipping both sides of the induction hypothesis preserves the correspondence. Finally wL(M)w\in L(M) iff δ^(qe,w)=qeF\hat\delta(q_{\text{e}},w)=q_{\text{e}}\in F iff #1(w)\#_1(w) is even. \blacksquare

Example B — ends in 01

Task. Over {0,1}\{0,1\}, accept the strings that end in 01.

Design idea (suffix matching). Let each state record the longest suffix of the input read so far that is a prefix of the pattern 01. The prefixes of 01 are ε\varepsilon, 0, 01, giving three states: AA (no useful suffix), BB (ends in 0), CC (ends in 01, accepting).

State on 0 on 1
 A\rightarrow\ A BB AA
BB BB CC
 C\ast\ C BB AA

Reading the table. From BB (“…0”) a further 0 still ends in 0 (B\to B); a 1 completes 01 (C\to C). From CC (“…01”) a 0 ends in 0 (B\to B); a 1 ends in 11, no useful suffix (A\to A).

Traces. 0101: ABCBCA\to B\to C\to B\to C (accept — indeed ends 01). 011: ABCAA\to B\to C\to A (reject — ends 11).

Remark (a template). Example B generalises: to accept strings ending in a fixed pattern pp, use one state per prefix of pp (the states of the Knuth–Morris–Pratt string matcher). To accept strings containing pp as a substring, do the same but make the “matched all of pp” state absorbing and accepting — see Example C.

Example C — contains the substring 001

Task. Over {0,1}\{0,1\}, accept every string that has 001 somewhere inside.

Design idea. Track progress toward matching 001; once matched, latch into an accepting sink.

State meaning on 0 on 1
 S0\rightarrow\ S_0 no progress S1S_1 S0S_0
S1S_1 seen 0 S2S_2 S0S_0
S2S_2 seen 00 S2S_2 S3S_3
 S3\ast\ S_3 seen 001 (done) S3S_3 S3S_3

Two subtle entries. From S2S_2 (“…00”) reading 0 keeps the last two symbols 00, so we stay at S2S_2 (not fall back). From S1S_1 (“…0”) reading 1 gives suffix 01, which shares no prefix with 001, so we reset to S0S_0.

Trace on 01001 (contains 001 at positions 3–5): S00S11S00S10S21S3S_0\xrightarrow{0}S_1 \xrightarrow{1}S_0\xrightarrow{0}S_1\xrightarrow{0}S_2\xrightarrow{1}S_3 — accept. Trace on 0100 (no 001): S0S1S0S1S2S_0\to S_1\to S_0\to S_1\to S_2 — reject. Correct.

Common pitfall. A frequent bug is sending S2S_2 back to S0S_0 or S1S_1 on reading 0. But ...000 still ends in 00, so we must stay at S2S_2; forgetting this makes the DFA miss matches like 0001. Always ask: after this symbol, what is the longest pattern-prefix that is now a suffix of the input?

Example D — binary numbers divisible by 33

Task. Over {0,1}\{0,1\}, reading a binary numeral most-significant bit first, accept exactly those whose value is divisible by 33. (Take ε\varepsilon to denote 00, which is divisible by 33.)

Design idea (running remainder). Let the state be the value read so far modulo 33: states r0,r1,r2r_0,r_1,r_2. Appending a bit bb changes the value nn to 2n+b2n+b, so the remainder rr becomes (2r+b)mod3(2r+b)\bmod 3. Compute all six entries:

  • r0r_0: b=00b=0\to 0, b=11b=1\to 1;
  • r1r_1: b=0(2)r2b=0\to (2)\to r_2, b=1(3)0r0b=1\to (3)\equiv 0\to r_0;
  • r2r_2: b=0(4)1r1b=0\to (4)\equiv 1\to r_1, b=1(5)2r2b=1\to (5)\equiv 2\to r_2.
State on 0 on 1
  r0\rightarrow\ \ast\ r_0 r0r_0 r1r_1
r1r_1 r2r_2 r0r_0
r2r_2 r1r_1 r2r_2

Traces. 110 =6=6: r0r1r0r0r_0\to r_1\to r_0\to r_0 — accept (6=326=3\cdot2). 1001 =9=9: r0r1r2r1r0r_0\to r_1\to r_2\to r_1\to r_0 — accept (9=339=3\cdot3). 101 =5=5: r0r1r2r2r_0\to r_1\to r_2\to r_2 — reject (5mod3=25\bmod3=2). All correct.

Proposition 14.3. After reading numeral ww, the DFA of Example D is in state rjr_j where j=value(w)mod3j=\mathrm{value}(w)\bmod 3.

Proof. Induct on w|w|. Base w=εw=\varepsilon: value 00, state r0r_0. Step: w=xbw=xb. By IH the state after xx is rjr_{j} with j=value(x)mod3j=\mathrm{value}(x)\bmod 3. Then value(xb)=2value(x)+b\mathrm{value}(xb)=2\,\mathrm{value}(x)+b, so value(xb)mod3=(2j+b)mod3\mathrm{value}(xb)\bmod3=(2j+b)\bmod3, which is exactly the column the table follows from rjr_j on bb. Hence the state after xbxb is r(2j+b)mod3r_{(2j+b)\bmod 3}. \blacksquare

Since F={r0}F=\{r_0\}, acceptance holds iff value(w)0(mod3)\mathrm{value}(w)\equiv 0\pmod 3, as required. Remark. The same trick — “state = residue of a running quantity” — gives DFAs for divisibility by any fixed modulus and for many counting conditions.

Example E — a “dead state” and totality

Task. Accept strings that begin with 1.

State on 0 on 1
 qstart\rightarrow\ q_{\text{start}} qdeadq_{\text{dead}} qyesq_{\text{yes}}
 qyes\ast\ q_{\text{yes}} qyesq_{\text{yes}} qyesq_{\text{yes}}
qdeadq_{\text{dead}} qdeadq_{\text{dead}} qdeadq_{\text{dead}}

Once the first symbol is seen the verdict is fixed, so qyesq_{\text{yes}} and qdeadq_{\text{dead}} are absorbing (all their edges are self-loops).

Common pitfall (totality). A DFA’s δ\delta must be defined for every (state, symbol) pair. When a partial design “has no move” for some case — here, a string starting with 0 should never be accepted — you must add an explicit dead (trap) state that absorbs all such inputs and is non-accepting. Diagrams often omit the dead state for readability, but formally it is present. Forgetting it turns your DFA into an NFA (or into nonsense).


14.6 Machines with output: Mealy and Moore

DFAs answer a yes/no question. Real sequential circuits usually also emit output as they run (think of a controller driving actuators). Adding an output alphabet Δ\Delta and an output function to the finite automaton gives a finite-state transducer, and there are two standard flavours, distinguished by where the output lives. (There are no accepting states here: a transducer transforms an input string into an output string.)

Definition (Moore machine). A Moore machine is M=(Q,Σ,Δ,δ,λ,q0)M=(Q,\Sigma,\Delta,\delta,\lambda,q_0) where output depends only on the current state, λ:QΔ\lambda:Q\to\Delta. Passing through states r0,r1,,rnr_0,r_1,\dots,r_n on an input of length nn, it emits λ(r0)λ(r1)λ(rn)\lambda(r_0)\lambda(r_1)\cdots\lambda(r_n) — one symbol per state entered, including an initial output before any input, hence n+1n+1 output symbols.

Definition (Mealy machine). A Mealy machine has the same shape but its output depends on the current state and the input symbol, so output is attached to transitions: λ:Q×ΣΔ\lambda:Q\times\Sigma\to\Delta. On an input of length nn it emits exactly nn output symbols, one per transition.

Both are captured by the transducer tuple M=(S,I,O,f,g,s0,F)M=(S,I,O,f,g,s_0,F), in which FF is left unused because a transducer has no accepting states: ff is the transition function δ\delta, gg is the output function λ\lambda, and O=ΔO=\Delta. The sole difference is whether gg may read the current input symbol (Mealy) or not (Moore).

Shared example — a running-parity reporter

Reuse the parity machine of Example A, but now report the running parity of $1$s after each symbol instead of only accepting at the end. States EE (“even so far”) and OO (“odd so far”); output alphabet {0,1}\{0,1\} (report 00 for even, 11 for odd).

The same running-parity machine as Moore (output attached to each state) and as Mealy (output attached to each edge)

Moore version (output labels the state, λ(E)=0, λ(O)=1\lambda(E)=0,\ \lambda(O)=1):

State output λ\lambda on 0 on 1
E\rightarrow E 0 EE OO
OO 1 OO EE

On input 1011 it visits E,O,O,E,OE,O,O,E,O and prints 01101 (the leading 0 is EE’s output before any symbol is read).

Mealy version (output labels the transition — report the parity after the step):

State on 0 (next / out) on 1 (next / out)
E\rightarrow E EE / 0 OO / 1
OO OO / 1 EE / 0

On the same input 1011 it prints 1101 — the Moore output without the initial symbol.

What the comparison shows. Moore output is stable while the machine sits in a state (convenient in hardware, where the output is just a function of the flip-flop contents) and carries one extra initial symbol. Mealy output reacts on the same step the input arrives and often needs fewer states. The two are equivalent in power:

Theorem 14.4 (Mealy \Leftrightarrow Moore, informal). For every Moore machine there is a Mealy machine producing the same output (minus the initial symbol) on every input, and vice versa.

Construction sketch. Moore \to Mealy: set the Mealy output on the transition into a state to that state’s Moore output, i.e. λMealy(q,a)=λMoore(δ(q,a))\lambda_{\text{Mealy}}(q,a)= \lambda_{\text{Moore}}(\delta(q,a)). Mealy \to Moore: split each state into copies tagged by the output symbol most recently produced, so “which output to emit” becomes a property of the (new) state; the number of states grows by at most a factor of Δ|\Delta|. Both directions are routine inductions on input length, which we omit. \square

Cross-reference (sequential circuits). A Mealy/Moore machine is the mathematical model of the sequential circuit of §14.1: the flip-flops store the state qq, one block of combinational logic (Lecture 5) computes the next state δ(q,a)\delta(q,a) from the stored state and the current input, and a second block computes the output λ\lambda. Moore outputs depend on the register only; Mealy outputs also read the current input wire.


14.7 Nondeterministic finite automata

Determinism forces exactly one move per (state, symbol). Relaxing that gives a model that is frequently far easier to design.

Definition (NFA). A nondeterministic finite automaton is a five-tuple N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F) where the transition function returns a set of states,

δ:Q×(Σ{ε})2Q,\delta:Q\times(\Sigma\cup\{\varepsilon\})\to 2^{Q},

allowing (i) several next states on a symbol, (ii) no next state (the empty set — a dead end), and (iii) ε\varepsilon-transitions δ(q,ε)\delta(q,\varepsilon) that change state without reading input. Everything else is as for a DFA.

Cross-reference (Lecture 3). Where a DFA’s δ\delta was a function, an NFA’s is a general relation ΔQ×(Σ{ε})×Q\Delta\subseteq Q\times(\Sigma\cup\{\varepsilon\})\times Q presented as a set-valued map. This is precisely the loosening — “drop single-valuedness and totality” — foreshadowed in §14.4.

Definition (acceptance for NFAs). NN accepts ww if there exists at least one run — one way of resolving the choices, possibly taking ε\varepsilon-moves — that reads all of ww and ends in an accepting state. Think of it as existential: the machine accepts iff some computational path succeeds. (L(N)L(N) is defined as the set of accepted strings, exactly as before.)

To make “reachable via ε\varepsilon-moves” precise we need one notion.

Definition (ε\varepsilon-closure). For a set SQS\subseteq Q, the ε\varepsilon-closure E(S)E(S) is the set of all states reachable from some state of SS by following zero or more ε\varepsilon-transitions. (Formally, the smallest set containing SS and closed under qδ(q,ε)q\mapsto\delta(q,\varepsilon).)

We can now give the NFA analogue of δ^\hat\delta, this time set-valued:

δ^N(q,ε)=E({q}),δ^N(q,wa)=E ⁣(pδ^N(q,w)δ(p,a)),\hat\delta_N(q,\varepsilon)=E(\{q\}),\qquad \hat\delta_N(q,wa)=E\!\Big(\textstyle\bigcup_{p\in \hat\delta_N(q,w)}\delta(p,a)\Big),

and NN accepts ww iff δ^N(q0,w)F\hat\delta_N(q_0,w)\cap F\neq\varnothing. (If there are no ε\varepsilon-moves, EE is the identity and the formulas simplify.)

Example F — “ends in 01”, the easy way

An NFA can guess where the final 01 begins:

State on 0 on 1
 p0\rightarrow\ p_0 {p0,p1}\{p_0,p_1\} {p0}\{p_0\}
p1p_1 \varnothing {p2}\{p_2\}
 p2\ast\ p_2 \varnothing \varnothing

State p0p_0 loops on every symbol (staying in “still reading the bulk”); on a 0 it also forks into p1p_1, betting that this 0 starts the final 01. From p1p_1 a 1 confirms the bet and reaches accepting p2p_2; any other continuation from p1p_1 or p2p_2 dies. The string is accepted iff some fork survives to the end in p2p_2, i.e. iff the last two symbols are 01.

Remark (why NFAs matter — succinctness). Consider Lk={w{0,1}:the k-th symbol from the end is 1}L_k=\{w\in\{0,1\}^\ast : \text{the } k\text{-th symbol from the end is } 1\}. An NFA needs only k+1k+1 states (guess the position, then count kk more symbols). But any DFA for LkL_k needs at least 2k2^k states, because it must remember the last kk symbols — the 2k2^k possible windows are pairwise distinguishable. Nondeterminism buys exponential succinctness, even though — as we now prove — it buys no extra recognising power.


14.8 The subset construction: NFA \to DFA

The pivotal theorem of the chapter says nondeterminism is a convenience, not a new capability. The construction is the natural one: run the NFA and, at each step, track the set of all states it could currently be in. That set is a single DFA state.

Subset construction turning an NFA into a DFA: each DFA state is a set of NFA states, and a set is accepting if it contains an NFA accepting state

Theorem 14.5 (Rabin–Scott, subset construction). For every NFA N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F) there is a DFA DD with L(D)=L(N)L(D)=L(N).

Proof. We first treat the case with no ε\varepsilon-transitions, then note the one change needed for the general case.

Construction. Define D=(QD,Σ,Δ,S0,FD)D=(Q_D,\Sigma,\Delta,S_0,F_D) by

QD=2Q,S0={q0},Δ(S,a)=qSδ(q,a),FD={SQ:SF}.Q_D=2^{Q},\qquad S_0=\{q_0\},\qquad \Delta(S,a)=\bigcup_{q\in S}\delta(q,a),\qquad F_D=\{\,S\subseteq Q : S\cap F\neq\varnothing\,\}.

DD is a legitimate DFA: Δ\Delta is a total single-valued function on QD×ΣQ_D\times\Sigma (a union of sets is one well-defined set, even when it is empty), and QDQ_D is finite (it has 2Q2^{|Q|} elements). Write Δ^\hat\Delta for DD’s extended transition function.

Key lemma. For every wΣw\in\Sigma^\ast,

Δ^({q0},w)=δ^N(q0,w),\hat\Delta(\{q_0\},w)=\hat\delta_N(q_0,w),

i.e. the DFA state after reading ww is exactly the set of NFA states reachable on ww. We prove it by induction on w|w|.

Base w=εw=\varepsilon: Δ^({q0},ε)={q0}\hat\Delta(\{q_0\},\varepsilon)=\{q_0\} by definition of Δ^\hat\Delta, and δ^N(q0,ε)={q0}\hat\delta_N(q_0,\varepsilon)=\{q_0\} (no ε\varepsilon-moves). Equal.

Step w=xaw=xa: using the definition of Δ^\hat\Delta, then the induction hypothesis, then the definition of Δ\Delta, then the definition of δ^N\hat\delta_N:

Δ^({q0},xa)=Δ(Δ^({q0},x),a)=IHΔ(δ^N(q0,x),a)= ⁣ ⁣pδ^N(q0,x) ⁣ ⁣δ(p,a)=δ^N(q0,xa).\hat\Delta(\{q_0\},xa) =\Delta\big(\hat\Delta(\{q_0\},x),a\big) \overset{\text{IH}}{=}\Delta\big(\hat\delta_N(q_0,x),a\big) =\!\!\bigcup_{p\in \hat\delta_N(q_0,x)}\!\!\delta(p,a) =\hat\delta_N(q_0,xa).

This closes the induction.

Conclusion. Now chase the definitions:

wL(D)    Δ^({q0},w)FD    Δ^({q0},w)F    δ^N(q0,w)F    wL(N),w\in L(D) \iff \hat\Delta(\{q_0\},w)\in F_D \iff \hat\Delta(\{q_0\},w)\cap F\neq\varnothing \iff \hat\delta_N(q_0,w)\cap F\neq\varnothing \iff w\in L(N),

using the key lemma at the third step and the NFA acceptance definition at the last. Hence L(D)=L(N)L(D)=L(N).

The ε\varepsilon-case. If NN has ε\varepsilon-moves, replace the start state by S0=E({q0})S_0=E(\{q_0\}) and the transition rule by Δ(S,a)=E ⁣(qSδ(q,a))\Delta(S,a)=E\!\big(\bigcup_{q\in S}\delta(q,a)\big) — that is, take the ε\varepsilon-closure after every step. The identical induction (now using the ε\varepsilon-aware definition of δ^N\hat\delta_N) gives the same conclusion. \blacksquare

Corollary 14.6. DFAs and NFAs recognise exactly the same class of languages. (Every DFA is trivially an NFA; Theorem 14.5 gives the converse.)

Remark (only reachable subsets matter). Although QD=2QQ_D=2^{Q} can be huge, in practice one builds DD lazily, generating only the subsets actually reached from S0S_0. Often these are few. This is the standard NFA-to-DFA algorithm inside regex tools.

Worked subset construction

Apply the construction to the NFA NN of Example F (states p0,p1,p2p_0,p_1,p_2, no ε\varepsilon-moves). Start from {p0}\{p_0\} and compute reachable subsets:

Δ({p0},0)=δ(p0,0)={p0,p1},Δ({p0},1)={p0},Δ({p0,p1},0)={p0,p1}={p0,p1},Δ({p0,p1},1)={p0}{p2}={p0,p2},Δ({p0,p2},0)={p0,p1}={p0,p1},Δ({p0,p2},1)={p0}={p0}.\begin{aligned} \Delta(\{p_0\},0)&=\delta(p_0,0)=\{p_0,p_1\}, & \Delta(\{p_0\},1)&=\{p_0\},\\ \Delta(\{p_0,p_1\},0)&=\{p_0,p_1\}\cup\varnothing=\{p_0,p_1\}, & \Delta(\{p_0,p_1\},1)&=\{p_0\}\cup\{p_2\}=\{p_0,p_2\},\\ \Delta(\{p_0,p_2\},0)&=\{p_0,p_1\}\cup\varnothing=\{p_0,p_1\}, & \Delta(\{p_0,p_2\},1)&=\{p_0\}\cup\varnothing=\{p_0\}. \end{aligned}

Only three subsets appear. Rename A={p0}A=\{p_0\}, B={p0,p1}B=\{p_0,p_1\}, C={p0,p2}C=\{p_0,p_2\}; accepting subsets are those meeting F={p2}F=\{p_2\}, namely CC alone.

State on 0 on 1
 A\rightarrow\ A BB AA
BB BB CC
 C\ast\ C BB AA

This is character-for-character the hand-built DFA of Example B. The subset construction rediscovered our suffix-tracking machine — a satisfying confirmation that the two designs recognise the same language, “ends in 01”.

Worked subset construction with ε\varepsilon-moves

Let NεN_\varepsilon recognise aba^\ast b^\ast: states q0q_0 (start), q1q_1; δ(q0,a)={q0}\delta(q_0,a)=\{q_0\}, δ(q0,ε)={q1}\delta(q_0,\varepsilon)=\{q_1\}, δ(q1,b)={q1}\delta(q_1,b)=\{q_1\}; accepting F={q1}F=\{q_1\}. First the closures: E({q0})={q0,q1}E(\{q_0\})=\{q_0,q_1\} and E({q1})={q1}E(\{q_1\})=\{q_1\}.

Start S0=E({q0})={q0,q1}=:AS_0=E(\{q_0\})=\{q_0,q_1\}=:A (accepting — contains q1q_1).

Δ(A,a)=E(δ(q0,a)δ(q1,a))=E({q0})={q0,q1}=A,Δ(A,b)=E(δ(q0,b)δ(q1,b))=E({q1})={q1}=:B,Δ(B,a)=E(δ(q1,a))=E()==:,Δ(B,b)=E({q1})={q1}=B,\begin{aligned} \Delta(A,a)&=E(\delta(q_0,a)\cup\delta(q_1,a))=E(\{q_0\})=\{q_0,q_1\}=A,\\ \Delta(A,b)&=E(\delta(q_0,b)\cup\delta(q_1,b))=E(\{q_1\})=\{q_1\}=:B,\\ \Delta(B,a)&=E(\delta(q_1,a))=E(\varnothing)=\varnothing=:\varnothing,\\ \Delta(B,b)&=E(\{q_1\})=\{q_1\}=B, \end{aligned}

and the empty set is an absorbing non-accepting dead state.

State on a on b
  A\rightarrow\ \ast\ A AA BB
 B\ast\ B \varnothing BB
\varnothing \varnothing \varnothing

Traces. aab: AAABA\to A\to A\to B accept (aab=a2b1aab=a^2b^1). aba: AABA\to A\to B\to\varnothing reject (aba is not of the form aba^\ast b^\ast). ba: ABA\to B\to\varnothing reject. All correct.


14.9 Regular expressions and Kleene’s theorem

We have two automaton-based definitions of “regular” (DFA and NFA, now known equal). There is a third, purely syntactic one.

Definition (regular expression). The regular expressions over Σ\Sigma, and the language L(R)L(R) each denotes, are defined by recursion:

  • \varnothing is a regex with L()=L(\varnothing)=\varnothing;
  • ε\varepsilon is a regex with L(ε)={ε}L(\varepsilon)=\{\varepsilon\};
  • each aΣa\in\Sigma is a regex with L(a)={a}L(a)=\{a\};
  • if R,SR,S are regexes, so are (R+S)(R+S), (RS)(RS), and (R)(R^\ast), with

L(R+S)=L(R)L(S),L(RS)=L(R)L(S),L(R)=L(R).L(R+S)=L(R)\cup L(S),\quad L(RS)=L(R)L(S),\quad L(R^\ast)=L(R)^\ast.

Binding tightest to loosest: ^\ast, then concatenation, then ++. So ab+cab^\ast+c means (a(b))+c\big(a(b^\ast)\big)+c.

For instance (0+1)01(0+1)^\ast 01 denotes “ends in 01”; (0+1)001(0+1)(0+1)^\ast 001 (0+1)^\ast denotes “contains 001”; 010^\ast 1^\ast denotes aba^\ast b^\ast (with 0,10,1 for a,ba,b). These are the same languages we built machines for.

Theorem 14.7 (Kleene, 1956 — stated). A language is regular (recognised by some DFA/NFA) if and only if it is denoted by some regular expression.

We do not give the full two-way proof, but the forward-hard direction is short and ties the chapter together, so we sketch it; the converse is noted.

Regex \to NFA (Thompson’s construction, sketch). Build an ε\varepsilon-NFA by recursion on the regex, maintaining the invariant “one start state, one accepting state.” Base cases ,ε,a\varnothing,\varepsilon,a are one- or two-state machines. For R+SR+S, add a new start with ε\varepsilon-edges into the two sub-machines’ starts and a new accept with ε\varepsilon-edges from their accepts. For RSRS, glue RR’s accept to SS’s start with an ε\varepsilon-edge. For RR^\ast, add a new start/accept state with ε\varepsilon-edges to RR’s start, from RR’s accept back to RR’s start, and straight through for the empty string. Each rule adds O(1)O(1) states, so the NFA has size linear in the regex; by the subset construction (Theorem 14.5) it converts to a DFA. This is exactly how regex libraries compile a pattern.

NFA \to regex (converse, noted). Eliminate states one at a time from the automaton, relabelling the remaining edges with regexes that account for the removed state’s paths (the state-elimination / GNFA method), until a single edge labelled by the answer regex remains. \square

Definition (regular language — final form). By Corollary 14.6 and Theorem 14.7, the following are the same class of languages, the regular languages: those recognised by a DFA; by an NFA; and those denoted by a regular expression. “Finite automaton”, “regular language”, “regular expression” are three views of one idea — the languages a finite-memory machine can recognise.


14.10 Closure properties of the regular languages

The regular languages are remarkably robust: combine them with the natural set and string operations and you stay regular. These closure theorems are workhorses — both for building machines compositionally and (via §14.11) for proving non-regularity. Note how they mirror the logical connectives of Lecture 6: union is “or”, intersection is “and”, complement is “not”.

Theorem 14.8 (closure under complement). If LL is regular over Σ\Sigma, so is L=ΣL\overline{L}=\Sigma^\ast\setminus L.

Proof. Take a DFA M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with L(M)=LL(M)=L — crucially a DFA, whose δ\delta is total, so every string has a run ending in a well-defined state. Form M=(Q,Σ,δ,q0,QF)M'=(Q,\Sigma,\delta,q_0,Q\setminus F) by swapping accepting and non-accepting states. Since the run of a string is identical in MM and MM', for every ww,

wL(M)    δ^(q0,w)QF    δ^(q0,w)F    wL(M).w\in L(M') \iff \hat\delta(q_0,w)\in Q\setminus F \iff \hat\delta(q_0,w)\notin F \iff w\notin L(M).

Hence L(M)=LL(M')=\overline{L}. \blacksquare

Common pitfall. This works only for a complete DFA. Swapping the accepting set of an NFA does not complement its language (because “some path accepts” and “some path rejects” can both hold at once). Determinise first, ensure δ\delta is total, then swap.

Theorem 14.9 (product construction: closure under intersection and union). If L1,L2L_1,L_2 are regular, so are L1L2L_1\cap L_2 and L1L2L_1\cup L_2.

Proof. Let Mi=(Qi,Σ,δi,q0,i,Fi)M_i=(Q_i,\Sigma,\delta_i,q_{0,i},F_i) be DFAs with L(Mi)=LiL(M_i)=L_i. Run both in parallel on one input by taking states to be pairs:

M=(Q1×Q2, Σ, δ, (q0,1,q0,2), F),δ((p,q),a)=(δ1(p,a),δ2(q,a)).M=\big(Q_1\times Q_2,\ \Sigma,\ \delta,\ (q_{0,1},q_{0,2}),\ F\big),\qquad \delta\big((p,q),a\big)=\big(\delta_1(p,a),\,\delta_2(q,a)\big).

Choose F=F1×F2F=F_1\times F_2 for intersection, or F={(p,q):pF1 or qF2}F=\{(p,q):p\in F_1\text{ or }q\in F_2\} for union.

Parallel-run lemma. δ^((p,q),w)=(δ^1(p,w),δ^2(q,w))\hat\delta\big((p,q),w\big)=\big(\hat\delta_1(p,w),\, \hat\delta_2(q,w)\big) for all ww. Induct on w|w|: base w=εw=\varepsilon gives (p,q)(p,q) on both sides; step w=xaw=xa,

δ^((p,q),xa)=δ(δ^((p,q),x),a)=IHδ((δ^1(p,x),δ^2(q,x)),a)=(δ^1(p,xa),δ^2(q,xa)).\hat\delta((p,q),xa)=\delta\big(\hat\delta((p,q),x),a\big) \overset{\text{IH}}{=}\delta\big((\hat\delta_1(p,x),\hat\delta_2(q,x)),a\big) =\big(\hat\delta_1(p,xa),\hat\delta_2(q,xa)\big).

Now for intersection, wL(M)w\in L(M) iff (δ^1(q0,1,w),δ^2(q0,2,w))F1×F2\big(\hat\delta_1(q_{0,1},w),\hat\delta_2(q_{0,2},w)\big)\in F_1\times F_2 iff δ^1(q0,1,w)F1\hat\delta_1(q_{0,1},w)\in F_1 and δ^2(q0,2,w)F2\hat\delta_2(q_{0,2},w)\in F_2 iff wL1L2w\in L_1\cap L_2. The union case is identical with “and” replaced by “or”. \blacksquare

Worked use. To accept binary strings with an even number of $0$s AND an odd number of $1$s, take the product of the two 2-state parity DFAs: 2×2=42\times2=4 states {(e0,e1),(e0,o1),(o0,e1),(o0,o1)}\{(\text{e}_0,\text{e}_1),(\text{e}_0,\text{o}_1),(\text{o}_0,\text{e}_1), (\text{o}_0,\text{o}_1)\}, start (e0,e1)(\text{e}_0,\text{e}_1), accepting the single state (e0,o1)(\text{e}_0,\text{o}_1).

Theorem 14.10 (closure under union, concatenation, star). If L1,L2L_1,L_2 are regular, so are L1L2L_1\cup L_2, L1L2L_1L_2, and L1L_1^\ast.

Proof (via NFAs). Take NFAs N1,N2N_1,N_2 for L1,L2L_1,L_2 and use the Thompson gadgets of §14.9. Union: a new start with ε\varepsilon-edges into both starts. Concatenation: ε\varepsilon-edges from N1N_1’s accepting states to N2N_2’s start; start =N1=N_1’s start, accept =N2=N_2’s accepts. Star: a new start/accept state with an ε\varepsilon-edge to N1N_1’s start, ε\varepsilon-edges from N1N_1’s accepts back to its start, and acceptance of ε\varepsilon built in. In each case one checks the ε\varepsilon-NFA accepts exactly the intended language; the subset construction then yields a DFA. (These three operations are precisely the constructors of regular expressions — this theorem is the “\Leftarrow” of Kleene’s theorem in disguise.) \blacksquare

Corollary 14.11 (closure under set difference). If L1,L2L_1,L_2 are regular, so is L1L2=L1L2L_1\setminus L_2=L_1\cap\overline{L_2} — a regular language by Theorems 14.8 and 14.9.

Remark (closure as a proof tool). Closure runs backwards too. If we already know some language KK is not regular, and we can manufacture KK from a suspect language LL using operations that preserve regularity, then LL cannot be regular either — otherwise KK would be. We exploit exactly this in §14.11.


14.11 The limits of finite memory: the pumping lemma

Everything so far builds regular languages. We now prove that some languages are not regular — that finite memory is a genuine limitation. The tool is the pumping lemma, whose engine is the pigeonhole principle (Lecture 8): a run longer than the number of states must repeat a state, and a repeated state is a loop we can traverse any number of times.

Pumping lemma: a long accepted string splits as x·y·z where the middle y is a loop that can be repeated, so xyⁱz is accepted for every i

Theorem 14.12 (pumping lemma for regular languages). If LL is regular, then there is a constant p1p\ge 1 (a pumping length) such that every string sLs\in L with sp|s|\ge p can be split as s=xyzs=xyz with

  1. y1|y|\ge 1 (the middle part is non-empty),
  2. xyp|xy|\le p (the loop occurs within the first pp symbols), and
  3. xyizLxy^{\,i}z\in L for all i0i\ge 0 (the middle part may be “pumped”).

Proof. Since LL is regular, fix a DFA M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with L(M)=LL(M)=L, and set p=Qp=|Q|, the number of states. Let s=a1a2anLs=a_1a_2\cdots a_n\in L with n=spn=|s|\ge p. Consider the run

r0,r1,,rn,r0=q0,ri=δ^(q0,a1ai).r_0,r_1,\dots,r_n,\qquad r_0=q_0,\quad r_i=\hat\delta(q_0,a_1\cdots a_i).

Look at the first p+1p+1 of these states, r0,r1,,rpr_0,r_1,\dots,r_p. They are p+1p+1 states drawn from a set of only pp states, so by the pigeonhole principle two of them coincide: there exist indices j<kj<k with 0j<kp0\le j<k\le p and rj=rkr_j=r_k.

Define the split

x=a1aj,y=aj+1ak,z=ak+1an.x=a_1\cdots a_j,\qquad y=a_{j+1}\cdots a_k,\qquad z=a_{k+1}\cdots a_n.

Then y=kj1|y|=k-j\ge 1 (condition 1) and xy=kp|xy|=k\le p (condition 2). Reading yy from state rjr_j returns to rk=rjr_k=r_j, so δ^(rj,y)=rj\hat\delta(r_j,y)=r_j — a loop. By Lemma 14.1 (concatenation law) and induction on ii, δ^(rj,yi)=rj\hat\delta(r_j,y^{\,i})=r_j for every i0i\ge 0: going around the loop any number of times lands back at rjr_j. Therefore, again by Lemma 14.1,

δ^(q0,xyiz)=δ^(δ^(δ^(q0,x),yi),z)=δ^(δ^(rj,yi),z)=δ^(rj,z)=δ^(rk,z)=rn.\hat\delta(q_0,\,xy^{\,i}z) =\hat\delta\big(\hat\delta(\hat\delta(q_0,x),\,y^{\,i}),\,z\big) =\hat\delta\big(\hat\delta(r_j,y^{\,i}),\,z\big) =\hat\delta(r_j,z) =\hat\delta(r_k,z)=r_n .

Since s=xyzLs=xyz\in L, we have rnFr_n\in F; hence δ^(q0,xyiz)=rnF\hat\delta(q_0,xy^{\,i}z)=r_n\in F, i.e. xyizLxy^{\,i}z\in L for all i0i\ge 0 (condition 3). \blacksquare

How to use it (the adversary game). The lemma is a one-directional implication: regular \Rightarrow pumpable. To prove a language LL is not regular we use the contrapositive as a game against an adversary:

  1. (Adversary) Suppose LL were regular; it hands you some pumping length pp (you do not get to choose pp).
  2. (You) Choose a specific string sLs\in L with sp|s|\ge p, cleverly, usually in terms of pp.
  3. (Adversary) Splits s=xyzs=xyz subject only to y1|y|\ge1 and xyp|xy|\le p (you must handle every legal split).
  4. (You) Exhibit one i0i\ge0 with xyizLxy^{\,i}z\notin L. That contradicts the lemma, so LL is not regular.

Worked non-regularity proof: {0n1n}\{0^n1^n\}

Theorem 14.13. L={0n1n:n0}L=\{0^n1^n : n\ge 0\} is not regular.

Proof. Suppose, for contradiction, that LL is regular, and let pp be a pumping length from Theorem 14.12. Choose

s=0p1pL,s=2pp.s=0^{p}1^{p}\in L,\qquad |s|=2p\ge p.

By the pumping lemma s=xyzs=xyz with y1|y|\ge1 and xyp|xy|\le p. Because the first pp symbols of ss are all $0$s, the prefix xyxy (of length p\le p) consists solely of $0$s; in particular y=0my=0^{m} for some m1m\ge 1. Pump with i=2i=2:

xy2z=0p+m1p.xy^{2}z=0^{\,p+m}1^{\,p}.

Since m1m\ge1 this string has more $0$s than $1$s, so it is not of the form 0n1n0^n1^n and xy2zLxy^2z\notin L — contradicting condition 3. Therefore LL is not regular. \blacksquare

Intuition. LL demands matching an unbounded count of $0$s against an equal count of $1$s. A finite automaton has only p=Qp=|Q| states, so after reading enough $0$s it must reuse a state and thereby “forget” exactly how many it has seen. The pumping lemma turns that forgetting into a concrete counterexample.

A second worked proof: perfect squares of $0$s

Theorem 14.14. L={0n2:n0}L=\{0^{\,n^2} : n\ge 0\} (strings of $0$s whose length is a perfect square) is not regular.

Proof. Suppose LL is regular with pumping length pp. Choose s=0p2Ls=0^{\,p^2}\in L (note s=p2p|s|=p^2\ge p since p1p\ge1). Write s=xyzs=xyz with 1yxyp1\le|y|\le|xy|\le p; say y=m|y|=m, so 1mp1\le m\le p. Pump to i=2i=2: the string xy2z=0p2+mxy^2z=0^{\,p^2+m} has length p2+mp^2+m. Now

p2<p2+mp2+p<p2+2p+1=(p+1)2.p^2 < p^2+m \le p^2+p < p^2+2p+1=(p+1)^2.

So p2+mp^2+m lies strictly between the consecutive squares p2p^2 and (p+1)2(p+1)^2 and is therefore not a perfect square; hence xy2zLxy^2z\notin L, a contradiction. Thus LL is not regular. \blacksquare

Remark (pairing with closure — equal $0$s and $1$s). Let E={w{0,1}:#0(w)=#1(w)}E=\{w\in\{0,1\}^\ast : \#_0(w)=\#_1(w)\}. Is EE regular? Suppose it were. The language 010^\ast1^\ast is regular (regex 010^\ast1^\ast), so by closure under intersection (Theorem 14.9) EL(01)E\cap L(0^\ast1^\ast) would be regular. But

EL(01)={0n1n:n0},E\cap L(0^\ast1^\ast)=\{0^n1^n:n\ge0\},

which is not regular (Theorem 14.13) — contradiction. Hence EE is not regular. This is the “closure as a proof tool” remark of §14.10 in action: we avoided a fresh pumping argument by reducing to one we already had.

Common pitfall. (a) You do not choose pp; you must survive whatever pp the adversary picks. (b) You do not choose the split x,y,zx,y,z; you must defeat every split obeying y1, xyp|y|\ge1,\ |xy|\le p. Choosing s=0p1ps=0^p1^p (rather than, say, 0p10^p1) is what forces yy into the 00-block via xyp|xy|\le p. (c) The lemma is not an “if and only if”: some non-regular languages happen to be pumpable, so a successful pump does not prove regularity. Use the lemma only to prove non-regularity.


14.12 Beyond finite automata (a signpost)

A finite automaton is the weakest standard abstract machine: its only memory is its state. The languages just shown non-regular are not exotic — they are the bread-and-butter of programming languages (balanced brackets, anbna^nb^n-style matching). To recognise them we must add memory to the same tape-reader-control picture of §14.3:

  • Add a stack (last-in-first-out memory) and you get a pushdown automaton, which recognises the context-free languages — enough for {0n1n}\{0^n1^n\}, balanced parentheses, and the grammars of programming languages.
  • Add an unbounded read/write tape and you get a Turing machine, the most general model of computation, recognising the recursively enumerable languages.

These sit at higher rungs of the Chomsky hierarchy (regular \subsetneq context-free \subsetneq context-sensitive \subsetneq recursively enumerable) and are the subject of a full course in the theory of computation. They lie beyond this introduction; we mention them only to place finite automata on the map. Likewise the DFA-minimisation algorithm (there is a unique smallest DFA per regular language, computable by merging indistinguishable states — the algorithmic face of Myhill–Nerode) is noted but not developed here.

The Chomsky hierarchy as nested language classes: regular (finite memory) ⊊ context-free ⊊ context-sensitive ⊊ recursively enumerable


Historical note

The finite-state idea was first written down by Warren McCulloch and Walter Pitts in 1943, who modelled networks of idealised neurons as devices with a finite number of states. Stephen Cole Kleene, analysing those nerve nets in a 1951 RAND memorandum published in the 1956 volume Automata Studies, introduced regular events (our regular expressions) and proved the equivalence now called Kleene’s theorem (Theorem 14.7). The output-producing machines are named for their inventors at Bell Labs: George H. Mealy (“A Method for Synthesizing Sequential Circuits”, 1955) attached output to transitions, while Edward F. Moore (“Gedanken-experiments on Sequential Machines”, 1956) attached it to states. Michael O. Rabin and Dana Scott introduced nondeterministic automata and the subset construction (Theorem 14.5) in their 1959 paper “Finite Automata and Their Decision Problems”, work for which they shared the 1976 Turing Award. The pumping lemma and the algebraic characterisation of regularity (Myhill–Nerode) followed in the late 1950s. It is a striking feature of the subject that a single, small abstraction — the finite automaton — was reached almost simultaneously from neuroscience, logic, and circuit engineering.


CS connections

Finite automata are not a museum piece; they run inside tools you use daily.

  • Lexical analysis (lexers). The first phase of every compiler chops source code into tokens (identifiers, numbers, operators). Each token class is described by a regular expression; a tool like lex/flex compiles the union of them (via Thompson’s construction and the subset construction) into a single DFA that scans the source in linear time. This is §14.8–14.9 turned into a product.
  • Regular-expression engines. grep, RE2, and lexer generators simulate an NFA or its determinised DFA. Ken Thompson’s 1968 grep used exactly the on-the-fly NFA simulation of §14.7; guaranteed-linear engines (RE2) determinise lazily as in the remark after Theorem 14.5, avoiding the exponential blow-up that “backtracking” regex engines suffer.
  • Network protocols. TCP connection management is specified as a finite-state machine (states LISTEN, SYN-SENT, ESTABLISHED, TIME-WAIT, …); the transitions are triggered by packets and timeouts — a Mealy/Moore machine in production.
  • Digital hardware. Controllers, vending machines, and traffic lights are synthesised directly from Mealy/Moore descriptions (§14.6) into flip-flops plus combinational logic (§14.1) — closing the loop back to Lecture 5.
  • String search and validation. The Knuth–Morris–Pratt matcher is essentially the substring-DFA of Example C; input validation for dates, phone numbers, and URLs is DFA membership testing.

Chapter summary

  • Combinational vs sequential. Combinational logic (Lecture 5) computes a fixed Boolean function of the current inputs (adders, decoders, ALU); sequential logic adds state, held in flip-flops (kk flip-flops 2k\Rightarrow 2^k states), so output can depend on the past. State is exactly what automata theory formalises.
  • Vocabulary. An alphabet Σ\Sigma, strings Σ\Sigma^\ast under concatenation (a monoid with unit ε\varepsilon), and languages LΣL\subseteq\Sigma^\ast; distinguish ε\varepsilon, \varnothing, and {ε}\{\varepsilon\}.
  • DFA M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with total δ:Q×ΣQ\delta:Q\times\Sigma\to Q; the extended δ^\hat\delta satisfies the concatenation law (Lemma 14.1); MM accepts ww iff δ^(q0,w)F\hat\delta(q_0,w)\in F, and L(M)={w:δ^(q0,w)F}L(M)=\{w:\hat\delta(q_0,w)\in F\}. Design = pick states recording just-enough finite information (parity, running remainder, suffix progress); always total (add a dead state).
  • Mealy vs Moore transducers attach output to transitions vs states; they are interconvertible and model sequential circuits exactly.
  • NFA allows set-valued transitions and ε\varepsilon-moves; it accepts if some path succeeds. The subset construction (Theorem 14.5) determinises any NFA, so DFAs and NFAs recognise the same class — but NFAs can be exponentially smaller.
  • Regular languages = DFA-recognisable = NFA-recognisable = regular-expression denotable (Kleene, Theorem 14.7). The class is closed under union, intersection (product construction, Theorem 14.9), complement (swap accepting states of a DFA, Theorem 14.8), concatenation, and Kleene star (Theorem 14.10).
  • Pumping lemma (Theorem 14.12): long strings in a regular language contain a pumpable loop. Its contrapositive proves non-regularity: {0n1n}\{0^n1^n\}, {0n2}\{0^{n^2}\}, and equal-count languages are not regular (Theorems 14.13–14.14) — finite memory has real limits.
  • Beyond: adding a stack gives pushdown automata (context-free); an unbounded tape gives Turing machines. Minimisation (Myhill–Nerode) is noted, not developed.

Exercises

Warm-up

1. Let Σ={a,b}\Sigma=\{a,b\}. List all strings of length 2\le 2 over Σ\Sigma, and give ε|\varepsilon|, abba|abba|, and the string (ab)3(ab)^3. Explain why {ε}\varnothing\ne\{\varepsilon\}.

2. For the even-number-of-$1$s DFA (Example A), trace the runs on 11010 and on 010, and state whether each is accepted.

3. Write out the five components of the DFA tuple M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) and say how each corresponds to the Mealy/Moore transducer tuple M=(S,I,O,f,g,s0,F)M=(S,I,O,f,g,s_0,F) of §14.6 — and which two components a transducer adds.

4. For the “ends in 01” DFA (Example B), trace 1101 and 0010 and give the verdicts.

5. For the running-parity transducer (§14.6), write the full Moore and Mealy output strings on input 0110, and explain why they differ in length by exactly one.

6. Give a regular expression for each: (a) strings over {0,1}\{0,1\} that begin with 1; (b) strings that end in 01; (c) strings that contain 001.

Standard

7. Design a DFA (states, transition table, start, accepting set) over {0,1}\{0,1\} for strings whose number of $0$s is divisible by 33. Trace it on 00100.

8. Design a DFA over {0,1}\{0,1\} for “does not contain 11” (no two consecutive $1$s). Then, using Theorem 14.8, describe how to obtain a DFA for the complement (“contains 11”).

9. Use the product construction (Theorem 14.9) to build a DFA for “even number of $0$s and odd number of $1$s”. Give its four states, start, and accepting set, and trace 0110.

10. Apply the subset construction to the NFA NN with states {p0,p1,p2}\{p_0,p_1,p_2\}, start p0p_0, accepting p2p_2, and δ(p0,0)={p0,p1}\delta(p_0,0)=\{p_0,p_1\}, δ(p0,1)={p0}\delta(p_0,1)=\{p_0\}, δ(p1,1)={p2}\delta(p_1,1)=\{p_2\} (all other entries \varnothing). List the reachable DFA states and give its table. Which familiar language is this?

11. Give a Mealy machine over input {0,1}\{0,1\}, output {0,1}\{0,1\}, that outputs 1 on exactly the step whose input symbol completes an occurrence of the pattern 01 (and 0 otherwise). Trace it on 10011.

12. For the divisible-by-33 DFA (Example D), trace 1110 and 10010 and give the values and verdicts. Confirm against ordinary arithmetic.

13. Prove that the class of regular languages is closed under set difference L1L2L_1\setminus L_2, citing the theorems you use.

Challenge

14. Prove that L={anbm:n>m0}L=\{a^nb^m : n>m\ge 0\} is not regular. (Hint: pick s=ap+1bps=a^{p+1}b^{p} and pump down.)

15. Prove that the language of binary palindromes {w{0,1}:w=wR}\{w\in\{0,1\}^\ast : w=w^{R}\} is not regular, where wRw^{R} is ww reversed. (Hint: try s=0p10ps=0^{p}1\,0^{p}.)

16. Prove the regular languages are closed under reversal: if LL is regular then LR={wR:wL}L^{R}=\{w^{R}:w\in L\} is regular. (Hint: reverse every edge of an NFA for LL, swap start and accept roles using an ε\varepsilon-move.)

17. Show that any DFA recognising Lk={w:L_k=\{w: the kk-th symbol from the end is 1}\} needs at least 2k2^k states. (Hint: exhibit 2k2^k length-kk strings that are pairwise distinguishable — for each pair, a suffix that one can complete into LkL_k and the other cannot — and argue they must reach distinct states.)

18. A DFA has pp states and accepts at least one string. Prove it accepts a string of length <p<p. (Hint: a shortest accepted string cannot repeat a state, by the pumping argument.) Conclude an algorithm to test whether L(M)=L(M)=\varnothing.

19. Prove the pumping lemma’s index bound is tight in the following sense: give a regular language and a string in it for which pumping with i=0i=0 (deleting yy) leaves the language, illustrating that condition 3 must hold for all i0i\ge0, including i=0i=0.

20. Using closure properties only (no direct pumping), prove that {0n1m:nm}\{0^n1^m : n\ne m\} is not regular. (Hint: complement, intersect with 010^\ast1^\ast, and use Theorem 14.13.)


Selected answers & hints

2. 11010: qeqoqeqeqoqoq_{\text{e}}\to q_{\text{o}}\to q_{\text{e}}\to q_{\text{e}}\to q_{\text{o}}\to q_{\text{o}}; ends in qoq_{\text{o}}rejected (three $1$s, odd). 010: qeqeqoqoq_{\text{e}}\to q_{\text{e}}\to q_{\text{o}}\to q_{\text{o}}; ends in qoq_{\text{o}}rejected (one 11, odd).

5. The running parity of $1$s after reading ε,0,01,011,0110\varepsilon,0,01,011,0110 is 0,0,1,0,00,0,1,0,0. Moore output on 0110 is 00100 (5 symbols: it visits states E,E,O,E,EE,E,O,E,E and prints their labels 0,0,1,0,00,0,1,0,0); Mealy output is 0100 (4 symbols: one per transition, 0,1,0,00,1,0,0). Moore emits one symbol per state visited — there are n+1=5n+1=5 states for an input of length n=4n=4, including the pre-input output — while Mealy emits one symbol per transition, exactly n=4n=4. Hence the length differs by one, and Moore’s string is Mealy’s with the initial 0 prepended.

6. (a) 1(0+1)1(0+1)^\ast; (b) (0+1)01(0+1)^\ast 01; (c) (0+1)001(0+1)(0+1)^\ast 001(0+1)^\ast.

7. States t0,t1,t2t_0,t_1,t_2 = (number of $0$s) mod3\bmod\,3; reading 1 is a self-loop on every state, reading 0 cycles t0t1t2t0t_0\to t_1\to t_2\to t_0; start and only accepting state t0t_0. On 00100: t0t1t2t2t0t1t_0\to t_1\to t_2\to t_2\to t_0\to t_1; ends t1t_1rejected (four $0$s, 4mod3=14\bmod3=1).

9. States are pairs (parity of $0$s, parity of $1$s): (e,e),(e,o),(o,e),(o,o)(\text{e},\text{e}),(\text{e},\text{o}),(\text{o},\text{e}),(\text{o},\text{o}); start (e,e)(\text{e},\text{e}); accepting the single state (e,o)(\text{e},\text{o}). On 0110: (e,e)0(o,e)1(o,o)1(o,e)0(e,e)(\text{e},\text{e})\xrightarrow{0}(\text{o},\text{e})\xrightarrow{1} (\text{o},\text{o})\xrightarrow{1}(\text{o},\text{e})\xrightarrow{0}(\text{e},\text{e}); ends (e,e)(e,o)(\text{e},\text{e})\ne(\text{e},\text{o})rejected (two $0$s even, two $1$s even, so $1$s not odd).

10. Reachable states A={p0},B={p0,p1},C={p0,p2}A=\{p_0\},B=\{p_0,p_1\},C=\{p_0,p_2\} with the table A0B, A1A, B0B, B1C, C0B, C1AA\xrightarrow{0}B,\ A\xrightarrow{1}A,\ B\xrightarrow{0}B,\ B\xrightarrow{1}C,\ C\xrightarrow{0}B,\ C\xrightarrow{1}A, accepting CC — the language “ends in 01” (identical to Example B), confirming Theorem 14.5.

11. Reuse Example B’s states A,B,CA,B,C as a Mealy machine: on every transition into CC (namely B1B\xrightarrow{1}) output 1, otherwise 0. On 10011: transitions A1A/0, A0B/0, B0B/0, B1C/1, C1A/0A\xrightarrow{1}A/0,\ A\xrightarrow{0}B/0,\ B\xrightarrow{0}B/0,\ B\xrightarrow{1}C/1,\ C\xrightarrow{1}A/0 — output 00010.

13. L1L2=L1L2L_1\setminus L_2=L_1\cap\overline{L_2}. L2\overline{L_2} is regular by complement closure (Theorem 14.8), and the intersection of two regular languages is regular by the product construction (Theorem 14.9); hence L1L2L_1\setminus L_2 is regular.

14. Adversary gives pp. Choose s=ap+1bpLs=a^{p+1}b^{p}\in L (s=2p+1p|s|=2p+1\ge p). Since xyp|xy|\le p, yy lies in the aa-block, y=amy=a^{m}, m1m\ge1. Pump down (i=0i=0): xy0z=ap+1mbpxy^0z=a^{\,p+1-m}b^{\,p}. As m1m\ge1, the number of aa’s is p+1mpp+1-m\le p, which is not >p>p, so xy0zLxy^0z\notin L — contradiction.

16. From an NFA N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F) for LL, build NRN^R: reverse the direction of every transition, make the old accepting states into (nondeterministic) starts — add a fresh start ss with ε\varepsilon-edges to each old accept — and make q0q_0 the sole accepting state. A path spelling ww from q0q_0 to FF in NN becomes a path spelling wRw^R from ss to q0q_0 in NRN^R, so L(NR)=LRL(N^R)=L^{R}; determinise if a DFA is wanted.

18. Let ww be a shortest accepted string and suppose wp=Q|w|\ge p=|Q|. By the pumping lemma w=xyzw=xyz with y1|y|\ge1 and xy0z=xzLxy^0z=xz\in L, but xz<w|xz|<|w| — contradicting minimality. Hence the shortest accepted string has length <p<p. Algorithm: test all strings of length <p<p (or, better, check by graph reachability whether any accepting state is reachable from q0q_0); L(M)=L(M)=\varnothing iff none is accepted.

20. If {0n1m:nm}\{0^n1^m:n\ne m\} were regular, so would be its complement, and intersecting the complement with the regular language L(01)L(0^\ast1^\ast) gives exactly {0n1n:n0}\{0^n1^n:n\ge0\} — non-regular by Theorem 14.13. Contradiction, so {0n1m:nm}\{0^n1^m:n\ne m\} is not regular.


Further reading

  • Michael Sipser, Introduction to the Theory of Computation — Chapter 1 (regular languages, DFAs/NFAs, the subset construction, regular expressions, and the pumping lemma) is the closest match to this chapter.
  • John Hopcroft, Rajeev Motwani, and Jeffrey Ullman, Introduction to Automata Theory, Languages, and Computation — a fuller treatment, including minimisation and the Myhill–Nerode theorem.
  • Kenneth Rosen, Discrete Mathematics and Its Applications — the chapter on modelling computation gives a gentler, discrete-math-oriented introduction consistent with this course.

Self-check questions

  1. State precisely the difference between combinational and sequential logic, and explain why a two-bit counter cannot be built from combinational gates alone.
  2. Give the five components of M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) and define δ^\hat\delta by recursion. Why must δ\delta be total?
  3. Prove that the DFA of Example A recognises exactly the even-#1\#_1 strings.
  4. What does the subset construction show, and why does swapping the accepting set of an NFA fail to complement its language?
  5. State the pumping lemma and use it to prove {0n1n}\{0^n1^n\} is not regular.
  6. Name the three equivalent characterisations of the regular languages, and list the operations under which the class is closed.

Lectures/CDM-L14.md · 63.9 KB · updated 2026-08-01 20:48