G27
Self-checkedTier 1 10 min readtoc.nfa

NFA and epsilon-NFA

Nondeterministic finite automata: the transition function that returns a set of states, epsilon moves and epsilon closure, acceptance as the existence of an accepting computation, simulating an NFA by tracking the set of active states, design by guessing, the epsilon-NFA, how NFAs compare in size with DFAs, and what nondeterminism does and does not add.

Why it exists

A DFA must commit at every step. Many languages are easier to describe if the machine may guess: "the third symbol from the end is a 11" is simple for a machine that guesses that this 11 is the third from the end and then checks that exactly two symbols follow. A nondeterministic finite automaton (NFA) has that freedom. Nondeterminism does not let finite automata recognise more languages (the subset construction turns every NFA into a DFA) but it makes machines smaller and constructions simpler, and the idea returns for pushdown automata, where it does add power.

Prerequisites

You need the DFA (DFA).

Precise definitions

NFA. N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F) as for a DFA, except that δ:Q×(Σ∪{ε})→P(Q)\delta : Q \times (\Sigma \cup \{\varepsilon\}) \to \mathcal{P}(Q) returns a set of states (possibly empty). A move on ε\varepsilon consumes no input symbol. If no ε\varepsilon move exists the machine is an NFA without ε\varepsilon; the general machine is sometimes called an ε\varepsilon-NFA.

Computation. For an input w=a1a2⋯anw = a_1 a_2 \cdots a_n, a computation is a sequence of states r0=q0,r1,…,rmr_0 = q_0, r_1, \ldots, r_m in which each step either follows an ε\varepsilon edge or reads the next input symbol, and all nn symbols are read. It is accepting if it ends in an accepting state.

Acceptance. NN accepts ww if at least one computation on ww is accepting; it rejects ww if every computation fails (ends in a non-accepting state or gets stuck). L(N)L(N) is the set of accepted strings.

Epsilon closure. ECLOSE⁡(S)\operatorname{ECLOSE}(S) is the set of states reachable from the states of SS by following ε\varepsilon edges only (zero or more of them); it contains SS itself.

Extended transition function. δ^(q,ε)=ECLOSE⁡({q})\hat\delta(q, \varepsilon) = \operatorname{ECLOSE}(\{q\}) and δ^(q,wa)=ECLOSE⁡(⋃p∈δ^(q,w)δ(p,a))\hat\delta(q, wa) = \operatorname{ECLOSE}\bigl(\bigcup_{p \in \hat\delta(q, w)} \delta(p, a)\bigr). Then w∈L(N)w \in L(N) iff δ^(q0,w)∩F≠∅\hat\delta(q_0, w) \cap F \ne \emptyset.

How it works

Nondeterminism is parallel exploration

There is no magic and no probability: think of the NFA as exploring all choices at once, or as a machine that can duplicate itself at every branching, each copy following one choice. Copies die when they have no move. The string is accepted if some copy is in an accepting state when the input ends. Equivalently, one can simulate the NFA by keeping the set of active states: start with ECLOSE⁡({q0})\operatorname{ECLOSE}(\{q_0\}), and for each symbol replace the set SS by ECLOSE⁡(⋃p∈Sδ(p,a))\operatorname{ECLOSE}(\bigcup_{p \in S} \delta(p, a)). This simulation takes time O(∣w∣⋅∣Q∣2)O(|w| \cdot |Q|^2) and memory O(∣Q∣)O(|Q|) and is exactly what the animations show.

Example 1: ends with 01

Three states: pp loops on 00 and 11 (waiting), on 00 it may also guess "this 00 is the second-last symbol" and move to qq; qq moves to the accepting rr on 11. On the input 10011001 the active sets are {p}\{p\}, {p}\{p\}, {p,q}\{p, q\}, {p,q}\{p, q\}, {p,r}\{p, r\}: the last set contains the accepting state rr, so the string is accepted.

NFA for the binary strings that end with 01: start

Simulated, not typed
0,101pqr

Double circle = accepting state. Arrow from nowhere = start state.

input1001
active states{p}

Step 0

Start in the start state p. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.

0 / 5
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Example 2: the third symbol from the end is 1

Four states: pp loops on everything; on 11 it may move to s1s_1 (guess: this is the third-last symbol); s1s_1 and s2s_2 move on either symbol; s3s_3 is accepting. The machine can only accept if exactly two symbols follow the guessed 11. A DFA for this language needs 88 states (it must remember the last three symbols); the NFA needs 44.

NFA for 'the third symbol from the end is 1': start

Simulated, not typed
0,110,10,1ps1s2s3

Double circle = accepting state. Arrow from nowhere = start state.

input01101
active states{p}

Step 0

Start in the start state p. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Example 3: union by guessing, contains 00 or 11

The NFA starts in ss, which loops forever; on 00 it can guess "a 0000 starts here" and go to aa, on 11 to bb; then it checks the next symbol and falls into the accepting, looping state tt. Two simple machines are glued by letting the start state choose between them. On 0101101011: {s}\{s\}, {a,s}\{a, s\}, {s,b}\{s, b\}, {a,s}\{a, s\}, {s,b}\{s, b\}, {b,s,t}\{b, s, t\}.

NFA for the binary strings that contain 00 or 11: start

Simulated, not typed
0,101010,1sabt

Double circle = accepting state. Arrow from nowhere = start state.

input01011
active states{s}

Step 0

Start in the start state s. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Epsilon moves

An ε\varepsilon edge lets the machine change state without reading anything. It is most useful for gluing: to accept L1∪L2L_1 \cup L_2 make a new start state with ε\varepsilon edges to the starts of the two machines; for L1L2L_1 L_2 add ε\varepsilon edges from the accepting states of the first to the start of the second; for L∗L^* add ε\varepsilon edges back to the start. Following ε\varepsilon edges repeatedly gives the closure: in the machine below the closure of ii is {i,a1,b1}\{i, a_1, b_1\}, of a2a_2 it is {a2,c1,f}\{a_2, c_1, f\} and of c2c_2 it is {c1,c2,f}\{c_1, c_2, f\}. The machine recognises (a∣b) c∗(a \mid b)\, c^*; on bccbcc the sets are first the closure of ii, then the bb-move and its closure, and so on.

epsilon-NFA for (a | b) c*: start

Simulated, not typed
εεaεbεcεεεia1a2b1b2c1c2f

Double circle = accepting state. Arrow from nowhere = start state.

inputbcc
active states{i, a1, b1}

Step 0

Start in the start state and everything reachable from it by epsilon moves: {i, a1, b1}. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

The smaller machine for a∗b∗a^* b^* shows a single ε\varepsilon edge from the loop on aa to the loop on bb:

epsilon-NFA for a*b*: start

Simulated, not typed
aεbs0s1

Double circle = accepting state. Arrow from nowhere = start state.

inputaabb
active states{s0, s1}

Step 0

Start in the start state and everything reachable from it by epsilon moves: {s0, s1}. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

What nondeterminism does and does not change

  • Same languages. Every NFA (with or without ε\varepsilon) has an equivalent DFA, so the languages accepted are exactly the regular languages. Nondeterminism saves states: the nn-th symbol from the end is 11 needs n+1n + 1 NFA states and 2n2^n DFA states.
  • Removing ε\varepsilon moves without adding states: let δ′(q,a)=ECLOSE⁡(move⁡(ECLOSE⁡({q}),a))\delta'(q, a) = \operatorname{ECLOSE}(\operatorname{move}(\operatorname{ECLOSE}(\{q\}), a)) and make a state accepting if its closure contains an accepting state. The result accepts the same language (checked on random machines).
  • One accepting state can be arranged for an ε\varepsilon-NFA (add a new accepting state with ε\varepsilon edges from the old ones) but not always for an NFA without ε\varepsilon (the language {ε,a}\{\varepsilon, a\} needs two).
  • Complement is not easy: swapping accepting and non-accepting states of an NFA does not give the complement, because a string is accepted if some computation accepts, so after the swap it is accepted if some computation rejects. Determinise first.
  • Reversal is easy: reverse every edge, swap start and accepting states (adding ε\varepsilon edges if there are several accepting states).
  • Testing acceptance of a given string is cheap (the set simulation above), but the number of computations can be exponential.

Worked examples

EX 1Simulate by sets

Easy

NFA of example 1, input 01010101. Start {p}\{p\}. Read 00: p→{p,q}p \to \{p, q\}. Read 11: from pp: pp; from qq: rr; so {p,r}\{p, r\}. Read 00: from pp: {p,q}\{p, q\}; rr has no 00-edge: {p,q}\{p, q\}. Read 11: {p,r}\{p, r\}. The final set contains rr: accepted (and indeed 01010101 ends with 0101).

EX 2Epsilon closure

Easy

States 1,2,3,41, 2, 3, 4 with ε\varepsilon edges 1→21 \to 2, 2→32 \to 3, 4→14 \to 1 and nothing else on ε\varepsilon. ECLOSE⁡({1})={1,2,3}\operatorname{ECLOSE}(\{1\}) = \{1, 2, 3\}; ECLOSE⁡({4})={4,1,2,3}\operatorname{ECLOSE}(\{4\}) = \{4, 1, 2, 3\}; ECLOSE⁡({3})={3}\operatorname{ECLOSE}(\{3\}) = \{3\}. The closure follows paths of any length, but not backwards.

EX 3Design an NFA by guessing

Medium

Binary strings whose fifth symbol from the end is 00. States p,s1,…,s5p, s_1, \ldots, s_5. pp loops on 00 and on 11; on 00 it may guess "this 00 is the fifth from the end" and move to s1s_1; then s1→s2→s3→s4→s5s_1 \to s_2 \to s_3 \to s_4 \to s_5 on any symbol (four more symbols), and s5s_5 accepts: 66 states. The DFA for the same language needs 25=322^5 = 32 states (it must remember the last five symbols). Guessing the position replaces remembering.

EX 4When is an NFA's answer 'no'?

Medium

The NFA of example 3 on 01010101: sets {s}\{s\}, {a,s}\{a, s\}, {s,b}\{s, b\}, {a,s}\{a, s\}, {s,b}\{s, b\}: the accepting state tt is never reached; every computation fails: rejected. 01010101 contains neither 0000 nor 1111. The answer "no" needs all computations to fail, while "yes" needs just one: that asymmetry is why complementing an NFA is hard.

EX 5Typical statements

GATE level

(a) Every NFA has an equivalent DFA: true. (b) An NFA can accept a language that no DFA accepts: false. (c) The number of states of the smallest DFA is at most 2n2^n for an nn-state NFA: true, and the bound can be reached. (d) Replacing accepting states by non-accepting ones and vice versa gives an NFA for the complement: false. (e) The language accepted by an ε\varepsilon-NFA changes if all ε\varepsilon edges are removed: yes in general: the closures must be taken into account (see the elimination above). (f) An NFA with nn states accepting a non-empty language accepts a string of length less than nn: true (a shortest accepted string visits no state twice along an accepting computation).

Cheat box

Cheat box

  • NFA: δ:Q×(Σ∪{ε})→P(Q)\delta : Q \times (\Sigma \cup \{\varepsilon\}) \to \mathcal{P}(Q). Accept iff some computation ends in FF with the whole input read; reject iff all fail.
  • Simulation: track the set of active states; S↦ECLOSE⁡(⋃p∈Sδ(p,a))S \mapsto \operatorname{ECLOSE}(\bigcup_{p \in S} \delta(p, a)). Accept iff the final set meets FF.
  • ECLOSE⁡\operatorname{ECLOSE}: states reachable by ε\varepsilon edges (including the start). ε\varepsilon edges glue union, concatenation and star.
  • Same power as DFAs (regular languages); NFA can be exponentially smaller (n+1n + 1 against 2n2^n).
  • Complement of an NFA: determinise first. ε\varepsilon-removal keeps the number of states.
  • Nondeterminism adds no power for finite automata (but does for pushdown automata).

Traps and GATE patterns

Connections

The NFA is the intermediate form in converting regular expressions to automata; it is turned into a DFA by the subset construction. The same "accept if some computation accepts" idea defines the pushdown automaton, where nondeterminism really adds power.

PYQs

10 previous year questions from your book. I re-solved each one myself (9 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 2 · MSQ · 2 marksQ3 · page 197 of the book
Consider the following two finite automata D1  and
D2  (as shown in the figure).

Which of the following statements is/are true?
  • (a). L(D1)=L(D2)
  • (b). L(D1) is a proper subset of L(D2)
  • (c). L(D1)∩L(D2)={ε}
  • (d). (L(D1)∪L(D2))* consists of all strings in {0,1}* whose length is divisible by 3
GATE 2024 · Set 2 · MCQ · 2 marksQ16 · page 199 of the book
Let M be the 5-state NFA with ε-transitions shown in the
diagram below

Which one of the following regular expressions
represents the language accepted by M ?
  • (a). (00)* + 1(11)*
  • (b). 0* + (1 + 0(00)* )(11)*
  • (c). (00)* + (1 + (00)* )(11)*
  • (d). 0+ + 1(11)* + 0(11)*
GATE 2017 · Set 2 · MCQ · 2 marksQ31 · page 201 of the book
Let
δ
denote
the
transition
function
and
δ̂ denote the extended transition function of the
ε-NFA whose transition table is given below:

δ
ε
a
b
→ q0 {q2} {q1} {q0}
q1
{q2} {q2} {q3}
q2
{q0} ∅
∅
q3
∅
∅
{q2}

Then δ̂ (q2 , aba) is:
  • (a)∅
  • (b){q0,q1,q3}
  • (c){q0,q1,q2}
  • (d){q0,q2,q3}
GATE 2016 · Set 2 · MCQ · 2 marksQ37 · page 202 of the book
Consider
the
following
two
statements:
I. If all states of an NFA are accepting states then the
language
accepted
by
the
NFA
is
Σ*.
II. There exists a regular language A such that for all
languages B, A ∩B is regular.
Which one of the following is CORRECT?
  • (a)Only I is true
  • (b)Only II is true
  • (c)Both I and II are true
  • (d)Both I and II are false
GATE 2014 · Set 1 · MCQ · 1 markQ45 · page 203 of the book
Consider the finite automaton in the following figure.
What is the set of reachable states for the input string
0011?
  • (a){q0,q1,q2}
  • (b){q0,q1}
  • (c){q0,q1,q2, q3}
  • (d){ q3}
GATE 2012 · Set 1 · MCQ · 1 markQ50 · page 203 of the book
What is the complement of the language accepted by the
NFA shown below?
Assume Σ = {a} and ε is the empty string.
  • (a)∅
  • (b){ε}
  • (c)a*
  • (d){a ,ε}
GATE 2010 · MCQ · 2 marksQ57 · page 205 of the book
Let w be any string of length n is {0, 1}*. Let L be the set
of all substrings of w. What is the minimum number of
states in a non-deterministic finite automaton that accepts
L?
  • (a)n-1
  • (b)n
  • (c)n+1
  • (d)2n-1
GATE 2008 · MSQ · 2 marksQ64 · page 207 of the book
Consider the following two finite automata. M1 accepts L1
and M2 accepts L2

Which one of the following is TRUE?
  • (a). L1 = L2
  • (b). L1 ⊂ L2
  • (c). L1 ∩ L2c = ∅
  • (d). L1 ∪ L2 ≠ L1
GATE 2007 · MCQ · 2 marksQ69 · page 209 of the book
Consider the regular expression
 R = (a + b)*(aa + bb)(a + b)*
Which of the following non-deterministic finite
automata recognizes the language defined by the
regular expression R?
Edges labelled λ denote transitions on the empty
string.
  • (a).
  • (b)
  • (c).
  • (d).
GATE 2005 · MCQ · 2 marksQ78 · page 211 of the book
Consider the non-deterministic finite automaton
(NFA) shown in the figure.

State X is the starting state of the automaton. Let the
language accepted by the NFA with Y as the only
accepting state be L1. Similarly, let the language
accepted by the NFA with Z as the only accepting state
be L2. Which of the following statements about L1 and
L2 is TRUE?
  • (a)L1 = L2
  • (b)L1 ⊂ L2
  • (c)L2 ⊂ L1
  • (d)None of the above

Self-check

Question 1

An NFA has δ(q0,a)={q0,q1}\delta(q_0, a) = \{q_0, q_1\}, δ(q1,b)={q2}\delta(q_1, b) = \{q_2\}, q2q_2 accepting. Is aabaab accepted?

Show answer

Yes: after aaaa the active set is {q0,q1}\{q_0, q_1\}; reading bb moves q1q_1 to q2q_2 (and q0q_0 dies): {q2}\{q_2\}, accepting. Check: the computation q0→aq0→aq1→bq2q_0 \xrightarrow{a} q_0 \xrightarrow{a} q_1 \xrightarrow{b} q_2 exists.

Question 2

What is ECLOSE⁡({q})\operatorname{ECLOSE}(\{q\}) if qq has no ε\varepsilon edges?

Show answer

{q}\{q\}.

Question 3

Why is complementing an NFA by swapping accepting states wrong?

Show answer

A string is accepted if some computation accepts; after swapping it is accepted if some computation rejects, which is not the same as all computations rejecting.

Question 4

How many DFA states may be needed for an NFA with 55 states?

Show answer

Up to 25=322^5 = 32.

Revision card

One-screen revision

  • δ\delta gives a set; ε\varepsilon moves read nothing; accept iff some computation accepts.
  • Simulate with the set of active states and ECLOSE⁡\operatorname{ECLOSE}.
  • Same languages as DFAs, up to 2n2^n times fewer states; ε\varepsilon edges glue machines.
  • Do not complement by flipping; determinise first.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.2 Nondeterminism
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Sections 2.3 and 2.5 (nondeterministic finite automata, epsilon transitions)
  • ·Kozen, Automata and Computability, Lecture 5 Nondeterministic finite automata

Verification

On 400 random NFAs (half with epsilon moves) acceptance computed by tracking sets of states was compared with acceptance by trying every computation path, on every string up to length 6: they agree. Epsilon removal (delta'(q, a) = closure(move(closure(q), a))) preserved the language on every string up to length 7. The named NFAs (ends with 01, third symbol from the end, contains 00 or 11, a*b*, (a|b)c*) were compared with independent predicates on every string up to length 10, and the epsilon closures quoted on this page were recomputed. The animations replay the run of the machine by an independent simulation. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never