The Timpson Lyceum seal

The Timpson Lyceum · Computation Theory

Lecture 5 (Computation Theory) Nondeterministic Finite Automata

Nondeterministic Finite Automata – Lecture 5

Overview

This lecture introduces the nondeterministic finite automaton (NFA), the second model of computation in the study of regular languages. Where a deterministic finite automaton (DFA) follows exactly one forced path through its states, an NFA is allowed to branch: from a single state on a single symbol it may move to several states at once, none, or even change state without reading any input at all (an ε\varepsilon-transition). We build intuition for how an NFA accepts a string, give the formal five-tuple definition, and then prove the central result of the section: every NFA can be converted into an equivalent DFA, so the two models recognize exactly the same class of languages (the regular languages). The payoff is practical — NFAs are often dramatically smaller and clearer to design than the equivalent DFA, even though they are no more powerful.

Key Concepts

Deterministic vs. nondeterministic. In a DFA, the current state plus the input symbol completely determine the next state — exactly one transition out of every state for every alphabet symbol. An NFA relaxes this: for a given state and symbol there may be zero, one, or many possible next states, and the machine may also take ε\varepsilon-transitions (move without consuming any input).

How an NFA accepts. An NFA accepts a string if there exists at least one path through its states (a sequence of valid transitions) that consumes the whole string and ends in an accept state. Equivalently:

ε\varepsilon-transitions. An edge labeled ε\varepsilon lets the machine change state without reading a symbol. Reading ε\varepsilon does not change the underlying string: the string 1 ε 11\,\varepsilon\,1 is just the string 1111. So ε\varepsilon never enters the alphabet of the language — it only appears in the transition rules. These transitions are what make operations like union easy to wire together.

Why NFAs are useful. NFAs frequently express a language far more transparently than a DFA. Example: "the string contains 1111 as a substring" is a tiny NFA, but as a DFA the same idea, or "the third-from-last symbol is a 11," blows up — tracking the last three symbols needs 23=82^3 = 8 states. The DFA and NFA are logically equivalent; the NFA is just easier for a human to read.

Formal definition. An NFA is a five-tuple N=(Q, Σ, δ, q0, F)N = (Q,\ \Sigma,\ \delta,\ q_0,\ F) where QQ is a finite set of states, Σ\Sigma is a finite alphabet, q0∈Qq_0 \in Q is the start state, and F⊆QF \subseteq Q is the set of accept states. The only piece that differs from a DFA is the transition function: δ:Q×Σε→P(Q),\delta : Q \times \Sigma_\varepsilon \to \mathcal{P}(Q), where Σε=Σ∪{ε}\Sigma_\varepsilon = \Sigma \cup \{\varepsilon\} and P(Q)\mathcal{P}(Q) is the power set of QQ (the set of all subsets of QQ). So δ\delta takes a state and either a symbol or ε\varepsilon, and returns a set of states — possibly empty. For example δ(q1,1)={q1,q2,q3}\delta(q_1, 1) = \{q_1, q_2, q_3\}, while a dead end gives δ(q3,0)=∅\delta(q_3, 0) = \varnothing.

Power set. P(S)\mathcal{P}(S) is the set of all subsets of SS. If ∣S∣=n|S| = n then ∣P(S)∣=2n|\mathcal{P}(S)| = 2^n. E.g. P({1,2,3})\mathcal{P}(\{1,2,3\}) has 23=82^3 = 8 elements, always including ∅\varnothing. This is exactly the supply of "states" we need when simulating an NFA, because after reading some input the NFA could be in any subset of its states at once.

Formal acceptance. NN accepts w=w1w2⋯wnw = w_1 w_2 \cdots w_n (each wi∈Σεw_i \in \Sigma_\varepsilon) if there is a sequence of states r0,r1,…,rnr_0, r_1, \dots, r_n such that:

  1. r0=q0r_0 = q_0 (start in the start state),
  2. ri+1∈δ(ri,wi+1)r_{i+1} \in \delta(r_i, w_{i+1}) for i=0,…,n−1i = 0, \dots, n-1 (every step is a legal transition), and
  3. rn∈Fr_n \in F (end in an accept state).

Note there is one more state (n+1n+1 of them) than there are input symbols (nn).

Equivalence of NFAs and DFAs (the subset construction). Every NFA has an equivalent DFA. Given N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F), build a DFA M=(Q′,Σ,δ′,q0′,F′)M = (Q', \Sigma, \delta', q_0', F') whose states are subsets of NN's states — each DFA state records exactly which set of NFA states the machine could currently be in:

To handle ε\varepsilon-moves, define the ε\varepsilon-closure E(R)E(R) = the set of all states reachable from any state in RR using zero or more ε\varepsilon-transitions (including the states of RR themselves). Then:

Since every DFA is already (trivially) an NFA, the two models recognize the same languages. Corollary: a language AA is regular if and only if some NFA recognizes it.

Worked Example

Convert the NFA NN over Σ={a,b}\Sigma = \{a, b\} with states {1,2,3}\{1, 2, 3\}, start state 11, accept state F={1}F = \{1\}, and the single ε\varepsilon-edge 1→ε31 \xrightarrow{\varepsilon} 3, whose transitions are:

δ(1,a)=∅,δ(1,b)={2},δ(1,ε)={3}δ(2,a)={2,3},δ(2,b)={3}δ(3,a)={1},δ(3,b)=∅\begin{aligned} &\delta(1,a) = \varnothing, \quad \delta(1,b) = \{2\}, \quad \delta(1,\varepsilon) = \{3\} \\ &\delta(2,a) = \{2,3\}, \quad \delta(2,b) = \{3\} \\ &\delta(3,a) = \{1\}, \quad \delta(3,b) = \varnothing \end{aligned}

Start state. Take the ε\varepsilon-closure of the NFA start state. The general rule, then the substitution:

q0′=E({q0})=E({1})={1,3}(since 1→ε3).\begin{aligned} q_0' &= E(\{q_0\}) \\ &= E(\{1\}) = \{1, 3\} \quad(\text{since } 1 \xrightarrow{\varepsilon} 3). \end{aligned}

A transition. Compute δ′({1,3},a)\delta'(\{1,3\}, a) using the subset rule, then substitute:

δ′(R,a)=E ⁣(⋃r∈Rδ(r,a))δ′({1,3},a)=E(δ(1,a)∪δ(3,a))=E(∅∪{1})=E({1})={1,3}.\begin{aligned} \delta'(R, a) &= E\!\left( \bigcup_{r \in R} \delta(r, a) \right) \\ \delta'(\{1,3\}, a) &= E\big( \delta(1,a) \cup \delta(3,a) \big) \\ &= E(\varnothing \cup \{1\}) = E(\{1\}) = \{1, 3\}. \end{aligned}

So from the DFA state {1,3}\{1,3\}, reading aa returns to {1,3}\{1,3\}. Because {1,3}\{1,3\} contains the NFA accept state 11, it is an accept state of the DFA ({1,3}∩{1}={1}≠∅\{1,3\} \cap \{1\} = \{1\} \neq \varnothing). Continuing this process for every reachable subset and both symbols produces a full DFA over the power set of {1,2,3}\{1,2,3\} whose accept states are every subset meeting {1}\{1\} — and it recognizes exactly the same language as NN.

Summary