knowledge

Theory of Computation: Lecture 2

Nondeterminism, NFAs and their acceptance, the subset construction with its equivalence proof, epsilon closure, and closure under concatenation.

From Concatenation to Nondeterminism

Complement, union, and intersection all came with DFA constructions. Concatenation did not: to recognize L1L2L_1\circ L_2, a machine reading w=xyw=xy must know where the first part xx ends and the second part yy begins, and the input does not mark that split. A deterministic run cannot try all splits, so the machine is given a new ability: it may guess.

Nondeterminism. A machine is nondeterministic when it may have several possible next moves. A string is accepted when at least one possible computation accepts; all other computations are simply ignored.

Nondeterministic Finite Automata

Nondeterministic finite automaton (NFA). A finite automaton whose transition rule may offer any number of next states, including none, and may move without reading input. The differences from a DFA:

  • Branching. On a given state and symbol, there may be more than one next state.
  • Missing transition. There may be no next state at all; that computation branch dies.
  • Epsilon transition. A transition labeled ϵ\epsilon moves to another state without consuming an input symbol.

Example 1. Strings containing 11 or 101.

This NFA waits in q1q_1 until it guesses that a matching substring has started. If the guess succeeds, it reaches q4q_4 and stays there.

0,110, ε10,1q1q2q3q4
An NFA for strings containing 11 or 101.
L(N)={w{0,1}w contains 11 or 101}L(N)=\{w\in\{0,1\}^*\mid w\text{ contains }11\text{ or }101\}

The transition q2q3q_2\to q_3 has two labels. Reading 0 helps match 101; taking ϵ\epsilon helps match 11.

Accepted: 1111, 101101, 010110010110. Rejected: ϵ\epsilon, 00, 10001000.

Formal NFA. An NFA is a 5-tuple with the same parts as a DFA except for the transition function:

N=(Q,Σ,δ,q0,F)N=(Q,\Sigma,\delta,q_0,F)
SymbolRole
QQstates
Σ\Sigmaalphabet
δ:Q×(Σ{ϵ})2Q\delta:Q\times(\Sigma\cup\{\epsilon\})\to 2^Qtransition function
q0Qq_0\in Qstart state
FQF\subseteq Qaccepting states

Power set. The power set 2Q2^Q is the set of all subsets of QQ. The transition function returns an element of 2Q2^Q because there may be zero, one, or many next states.

If Q={q1,q2}Q=\{q_1,q_2\}, then 2Q={,{q1},{q2},{q1,q2}}2^Q=\{\varnothing,\{q_1\},\{q_2\},\{q_1,q_2\}\}.

For Example 1, the diagram is equivalent to this transition table, where each cell is a set and an impossible move is \varnothing, not an error:

State01ϵ\epsilon
q1q_1{q1}\{q_1\}{q1,q2}\{q_1,q_2\}\varnothing
q2q_2{q3}\{q_3\}\varnothing{q3}\{q_3\}
q3q_3\varnothing{q4}\{q_4\}\varnothing
q4q_4{q4}\{q_4\}{q4}\{q_4\}\varnothing

NFA acceptance. An NFA accepts ww if there exists a path that starts at q0q_0, reads exactly ww, and ends in a state of FF when the input ends. Epsilon transitions may be taken anywhere along the path because they read nothing. Visiting an accepting state in the middle of the input proves nothing; only the state where the input runs out counts.

Computation of an NFA. Two equivalent ways to read the same definition.

  • Parallel view. The machine forks into one copy per possible move and runs all copies at once. A copy with no valid move dies. The input is accepted if any surviving copy ends in an accepting state.
  • Guess view. The machine magically guesses a single lucky path and accepts if that path ends in an accepting state.

Guess and verify: design an NFA so that a guess marks the interesting position, and the rest of the machine merely checks it.

Example 2. Every possibility at once on 010110.

Running Example 1's NFA in the parallel view, the set of live states after each symbol (including states reached by free ϵ\epsilon-moves) evolves as follows.

Input readPossible states
start{q1}\{q_1\}
0{q1}\{q_1\}
1{q1,q2,q3}\{q_1,q_2,q_3\}
0{q1,q3}\{q_1,q_3\}
1{q1,q2,q3,q4}\{q_1,q_2,q_3,q_4\}
1{q1,q2,q3,q4}\{q_1,q_2,q_3,q_4\}
0{q1,q3,q4}\{q_1,q_3,q_4\}

Reading 1 in q1q_1 opens three branches: stay in q1q_1, move to q2q_2, or move to q2q_2 and slide on to q3q_3 by the ϵ\epsilon-transition. Branches die quietly, as q3q_3 does on 0. The final set contains the accepting state q4q_4, so 010110 is accepted: in the guess view, one lucky path guessed the 101 in the middle.

Designing NFAs

Nondeterminism has no physical analog, yet it earns its place: NFAs are often easier to build and much smaller than DFAs for the same language, and the equivalence theorem below converts any NFA into a DFA mechanically. Two patterns cover most designs.

Guess-and-check pattern. The NFA stays in a broad waiting state, guesses the important position, then checks the remaining symbols.

Example 3. A 1 in the third position from the end.

The machine may leave q1q_1 whenever it reads a 1. After that guess, exactly two more symbols must be read before the input ends.

0,110,10,1q1q2q3q4
An NFA that guesses the third symbol from the end.
L={w{0,1}w has 1 in the third position from the end}L=\{w\in\{0,1\}^*\mid w\text{ has }1\text{ in the third position from the end}\}

The accepting state has no outgoing transition, so a branch that guesses too early dies before the input ends. The string 10001000 visits q4q_4 after three symbols but is rejected: the path is not at q4q_4 when the input ends.

Accepted: 100100, 000100000100, 1101111011. Rejected: 1111, 00110011, 10001000.

Epsilon-branching pattern. An NFA can choose between several submachines before reading any input, which is exactly a union of their languages.

Example 4. Unary strings whose length is divisible by 2 or 3.

The start state chooses, by ϵ\epsilon-transition, whether to test divisibility by 2 or divisibility by 3.

εε00000sr0r1t0t1t2
An NFA for lengths divisible by 2 or 3.
L={0kk divisible by 2 or 3}L=\{0^k\mid k\text{ divisible by }2\text{ or }3\}

The top branch cycles with period 2. The bottom branch cycles with period 3. The empty string is accepted because 00 is divisible by both 2 and 3.

Accepted: ϵ\epsilon, 0000, 000000, 00000000, 000000000000. Rejected: 00, 0000000000.

The Subset Construction

The parallel view already contains the determinization idea. In Example 2, the set of possible states after each prefix evolved deterministically: each row of the table was computed from the previous row and one input symbol, with no guessing anywhere. A DFA can therefore simulate an NFA by storing that whole set as its own single state.

Possible-states set. After reading a prefix, the set of all NFA states reachable by some path reading that prefix. The empty set \varnothing means every branch has died, and a set is accepting exactly when it contains at least one accepting NFA state.

One-symbol set transition. For an NFA without ϵ\epsilon-transitions, the next possible-states set after reading aa from any state of AA:

δ~(A,a)=qAδ(q,a)\widetilde{\delta}(A,a)=\bigcup_{q\in A}\delta(q,a)

Example 5. Computing set transitions.

0,110,1pqr
An NFA used for the subset construction.
AAδ~(A,0)\widetilde{\delta}(A,0)δ~(A,1)\widetilde{\delta}(A,1)
\varnothing\varnothing\varnothing
{p}\{p\}{p}\{p\}{p,q}\{p,q\}
{q}\{q\}{r}\{r\}{r}\{r\}
{r}\{r\}\varnothing\varnothing
{p,q}\{p,q\}{p,r}\{p,r\}{p,q,r}\{p,q,r\}
{p,r}\{p,r\}{p}\{p\}{p,q}\{p,q\}
{q,r}\{q,r\}{r}\{r\}{r}\{r\}
{p,q,r}\{p,q,r\}{p,r}\{p,r\}{p,q,r}\{p,q,r\}

Reading the table as a DFA, with one state per subset and start state {p}\{p\}, gives the determinized machine. Only four of the 23=82^3=8 subsets are reachable from {p}\{p\}, and they already recognize the language:

01011001{p}{p,q}{p,q,r}{p,r}
The reachable part of the subset DFA; accepting subsets are those containing r.

Subset construction. Given an NFA without ϵ\epsilon-transitions N=(QN,Σ,δN,q0,FN)N=(Q_N,\Sigma,\delta_N,q_0,F_N), build the DFA M=(QM,Σ,δM,{q0},FM)M=(Q_M,\Sigma,\delta_M,\{q_0\},F_M):

DFA partDefinition
QMQ_M2QN2^{Q_N}
δM(A,a)\delta_M(A,a)qAδN(q,a)\displaystyle\bigcup_{q\in A}\delta_N(q,a)
start state{q0}\{q_0\}
FMF_M{AQNAFN}\{A\subseteq Q_N\mid A\cap F_N\ne\varnothing\}

The construction does not shrink the branching; it stores it. One DFA state is one complete set of live NFA branches.

The construction lists all 2QN2^{|Q_N|} subsets, but only the subsets reachable from {q0}\{q_0\} matter, as in Example 5. The exponential blow-up is sometimes unavoidable: for Example 3's language, a DFA must remember the last three symbols read, which is 232^3 possibilities, while the NFA gets away with four states. NFAs can be exponentially smaller than any equivalent DFA, which is one reason they are worth designing with.

Extended transition functions. To compare the two machines on whole strings, extend both transition functions by the same last-symbol recursion used for DFAs. For the DFA MM, whose states are subsets AA:

ΔM(A,ϵ)=A,ΔM(A,xa)=δM(ΔM(A,x),a)\Delta_M(A,\epsilon)=A, \qquad \Delta_M(A,xa)=\delta_M(\Delta_M(A,x),a)

For the NFA NN, acting on sets of states:

ΔN(A,ϵ)=A,ΔN(A,xa)=qΔN(A,x)δN(q,a)\Delta_N(A,\epsilon)=A, \qquad \Delta_N(A,xa)=\bigcup_{q\in\Delta_N(A,x)}\delta_N(q,a)

So ΔN({q0},w)\Delta_N(\{q_0\},w) is exactly the possible-states set after reading ww. Both functions also split over concatenation, Δ(A,xy)=Δ(Δ(A,x),y)\Delta(A,xy)=\Delta(\Delta(A,x),y), by the same kind of induction.

Theorem: NFA-DFA equivalence. Every nondeterministic finite automaton has an equivalent deterministic finite automaton. Consequently, a language is regular if and only if some NFA recognizes it.

The proof is stated for NFAs without ϵ\epsilon-transitions; the closure paragraph below removes that assumption.

1. Lemma. For every subset AQNA\subseteq Q_N and every string xx: ΔM(A,x)=ΔN(A,x)\Delta_M(A,x)=\Delta_N(A,x). The DFA's state after xx is exactly the NFA's possible-states set after xx.

2. Base case. For x=ϵx=\epsilon, both sides are AA by definition.

3. Inductive step. Write x=yax=ya with aΣa\in\Sigma and assume the lemma for yy:

ΔM(A,ya)=δM(ΔM(A,y),a)definition of ΔM=δM(ΔN(A,y),a)induction hypothesis=ΔN(ΔN(A,y),a)definition of δM=ΔN(A,ya)definition of ΔN\begin{aligned} \Delta_M(A,ya) &=\delta_M(\Delta_M(A,y),a) && \text{definition of }\Delta_M\\ &=\delta_M(\Delta_N(A,y),a) && \text{induction hypothesis}\\ &=\Delta_N(\Delta_N(A,y),a) && \text{definition of }\delta_M\\ &=\Delta_N(A,ya) && \text{definition of }\Delta_N \end{aligned}

4. Conclusion. The machines agree on every string:

xL(M)    ΔM({q0},x)FM    ΔM({q0},x)FN    ΔN({q0},x)FN    xL(N)\begin{aligned} x\in L(M) &\iff \Delta_M(\{q_0\},x)\in F_M \iff \Delta_M(\{q_0\},x)\cap F_N\ne\varnothing\\ &\iff \Delta_N(\{q_0\},x)\cap F_N\ne\varnothing \iff x\in L(N) \end{aligned}

The first two steps unfold the construction's accepting set, the third is the lemma, and the last is the definition of NFA acceptance. Hence L(M)=L(N)L(M)=L(N).

Epsilon closure. For a set of states AA, the set E(A)E(A) of all states reachable from AA by zero or more ϵ\epsilon-transitions. To determinize an NFA with ϵ\epsilon-transitions, charge the free moves to the bookkeeping: start the DFA in E({q0})E(\{q_0\}) and set

δM(A,a)=E(qAδN(q,a)).\delta_M(A,a)=E\Big(\bigcup_{q\in A}\delta_N(q,a)\Big).

The same induction then goes through unchanged, which is how Example 2's table already behaved: every row included the states reached by sliding along ϵ\epsilon-arrows.

Nondeterminism adds convenience, not power.

Concatenation with NFAs

Nondeterminism resolves the split problem that opened the lecture. The NFA for L1L2L_1\circ L_2 runs the first machine, guesses when the first part is finished, and jumps into the second machine by an ϵ\epsilon-transition.

Concatenation construction. Let N1=(Q1,Σ,δ1,q1,F1)N_1=(Q_1,\Sigma,\delta_1,q_1,F_1) recognize L1L_1 and N2=(Q2,Σ,δ2,q2,F2)N_2=(Q_2,\Sigma,\delta_2,q_2,F_2) recognize L2L_2, with disjoint state sets. Build NN for L1L2L_1\circ L_2:

N1εN2q1F1q2F2
Construction sketch for concatenation.
Part of NNDefinition
StatesQ1Q2Q_1\cup Q_2
Start stateq1q_1
Accepting statesF2F_2
Old transitionsKeep all transitions of N1N_1 and N2N_2
New transitionsAdd ϵ\epsilon-transitions from each state in F1F_1 to q2q_2

Written as one transition function:

δ(q,a)={δ1(q,a)if qQ1F1,δ1(q,a)if qF1 and aϵ,δ1(q,a){q2}if qF1 and a=ϵ,δ2(q,a)if qQ2.\delta(q,a)= \begin{cases} \delta_1(q,a) & \text{if } q\in Q_1\setminus F_1,\\ \delta_1(q,a) & \text{if } q\in F_1 \text{ and } a\ne\epsilon,\\ \delta_1(q,a)\cup\{q_2\} & \text{if } q\in F_1 \text{ and } a=\epsilon,\\ \delta_2(q,a) & \text{if } q\in Q_2. \end{cases}

Only accepting states of N1N_1 may jump, and the jump reads nothing. Old accepting states of N1N_1 are no longer accepting in NN, so finishing a string of L1L_1 proves nothing by itself; the rest of the input must still drive N2N_2 to F2F_2.

Recipe: proving an NFA construction correct. L(N)=L1L2L(N)=L_1\circ L_2 needs two directions, and both are exchanges of witnesses.

  1. Path to split. Take any accepting path of NN. It must use exactly one ϵ\epsilon-jump, since the jump is the only way from Q1Q_1 to the accepting states in Q2Q_2. The jump marks a split w=xyw=xy where xx drove N1N_1 to F1F_1, so xL1x\in L_1, and yy drove N2N_2 to F2F_2, so yL2y\in L_2.
  2. Split to path. Take any w=xyw=xy with xL1x\in L_1 and yL2y\in L_2. Glue the accepting run of N1N_1 on xx, the ϵ\epsilon-jump, and the accepting run of N2N_2 on yy into one accepting path of NN.

Closure under concatenation. If L1L_1 and L2L_2 are regular, then L1L2L_1\circ L_2 is regular: the construction gives an NFA for it, and every NFA has an equivalent DFA.

This is the general workflow the equivalence theorem unlocks: build with nondeterminism, conclude regularity for free. The remaining question is whether the whole approach has a ceiling: is every language regular, or does finite memory have a real limit?