4.6.4Theory of Computation

NFA to DFA conversion — subset construction

1,926 words9 min readdifficulty · medium

WHAT is the problem?

WHY is this possible at all? Because both NFA and DFA recognise exactly the regular languages — they have equal power. The NFA is just more convenient to write; the DFA is more mechanical to run.


The two helper operations


HOW — the algorithm

Figure — NFA to DFA conversion — subset construction

Worked Example 1 (no ε)

NFA over Σ={a,b}\Sigma=\{a,b\}, states {q0,q1}\{q_0,q_1\}, start q0q_0, final {q1}\{q_1\}: δN(q0,a)={q0,q1}\delta_N(q_0,a)=\{q_0,q_1\}, δN(q0,b)={q0}\delta_N(q_0,b)=\{q_0\}, δN(q1,b)={q1}\delta_N(q_1,b)=\{q_1\}. (This accepts strings containing an aa followed eventually by a bb… let's just build it.)

DFA state on aa on bb
A={q0}A=\{q_0\} (start) {q0,q1}=B\{q_0,q_1\}=B {q0}=A\{q_0\}=A
B={q0,q1}B=\{q_0,q_1\} (accept) {q0,q1}=B\{q_0,q_1\}=B {q0,q1}=B\{q_0,q_1\}=B

Why AaBA \xrightarrow{a} B? MOVE({q0},a)={q0,q1}(\{q_0\},a)=\{q_0,q_1\}, no ε so ECLOSE leaves it. New state → call it BB. Why is BB accepting? BF={q0,q1}{q1}={q1}B \cap F = \{q_0,q_1\}\cap\{q_1\}=\{q_1\}\neq\varnothing. ✔ Why BbBB\xrightarrow{b}B? MOVE({q0,q1},b)={q0}{q1}={q0,q1}=B(\{q_0,q_1\},b)=\{q_0\}\cup\{q_1\}=\{q_0,q_1\}=B.

Only 2 of the possible 22=42^2=4 subsets are reachable — we never list unreachable ones.


Worked Example 2 (with ε)

NFA: states {0,1,2}\{0,1,2\}, start 00, final {2}\{2\}, Σ={a}\Sigma=\{a\}. δ(0,ε)={1}\delta(0,\varepsilon)=\{1\}, δ(1,a)={2}\delta(1,a)=\{2\}, δ(2,ε)={1}\delta(2,\varepsilon)=\{1\}.

Step 1 — start: q0=ECLOSE({0})={0,1}q_0' = \text{ECLOSE}(\{0\}) = \{0,1\}. Why? From 00 an ε-edge goes to 11; from 11 none. Call this A={0,1}A=\{0,1\}.

Step 2 — AA on aa: MOVE({0,1},a)={2}(\{0,1\},a)=\{2\} (only 11 has an aa-edge). Then ECLOSE({2})={2,1}={1,2}(\{2\})=\{2,1\}=\{1,2\} (the ε-edge 212\to1). Call B={1,2}B=\{1,2\}.

Step 3 — BB on aa: MOVE({1,2},a)={2}(\{1,2\},a)=\{2\}, ECLOSE → {1,2}=B\{1,2\}=B. Self-loop.

DFA state on aa accept?
A={0,1}A=\{0,1\} (start) BB {0,1}{2}=\{0,1\}\cap\{2\}=\varnothing → no
B={1,2}B=\{1,2\} BB contains 22yes

So the DFA accepts a,aa,aaa,a, aa, aaa, \dots i.e. a+a^+. Trace "aa": AaBaBA\xrightarrow{a}B\xrightarrow{a}B (accept). ✔ Trace "" : stays at AA, reject. ✔



Recall Feynman: explain to a 12-year-old

Imagine a maze where, at some forks, a magic robot can split into copies and try every path at once. That's the NFA. A normal robot (DFA) can't split — it walks one path. So instead, the normal robot carries a notebook listing "all the rooms a magic robot could be standing in right now." Each time it reads a letter, it updates the whole list at once. If any room on the list is an exit, it shouts "Accepted!" The notebook's contents = one DFA state. Since there are only finitely many possible lists of rooms, the robot can never get lost forever.


Flashcards

What is one state of the DFA in subset construction?
A set (subset) of NFA states — the cloud of states the NFA could currently be in.
Why is it called the powerset/subset construction?
DFA states are subsets of QQ, drawn from the power set P(Q)\mathcal{P}(Q).
Formula for the DFA start state?
q0=ECLOSE({q0})q_0' = \text{ECLOSE}(\{q_0\}).
Formula for the DFA transition?
δD(S,a)=ECLOSE(MOVE(S,a))\delta_D(S,a)=\text{ECLOSE}(\text{MOVE}(S,a)).
When is a DFA subset-state accepting?
When it contains at least one NFA final state: SFS\cap F\neq\varnothing.
What is ECLOSE(S)?
All states reachable from S using only ε-transitions (S included).
What is MOVE(S,a)?
Union over qSq\in S of δN(q,a)\delta_N(q,a) — states reachable on reading aa.
Maximum number of DFA states from an nn-state NFA?
2n2^n (the number of subsets); usually fewer are reachable.
Why does subset construction always terminate?
Only 2n2^n possible subsets exist, so finitely many DFA states can be created.
What role does the empty set \varnothing play?
The dead/trap state — no NFA states alive, so it rejects and loops to itself.
Correct order of operations each step?
MOVE first, then ECLOSE: ECLOSE(MOVE(S,a)).
Do NFA and DFA recognise the same languages?
Yes — both recognise exactly the regular languages.

Connections

Concept Map

equal power

has

enables

converted via

produces

handles

then apply

combined into

combined into

gives start

builds edges in

seeds

accept if

NFA many states at once

DFA one state at a time

epsilon-moves

DFA state = SET of NFA states

Subset Construction

ECLOSE S: epsilon-reachable states

MOVE S,a: a-step union

Delta_D S,a = ECLOSE of MOVE

q0' = ECLOSE of q0

S intersect F not empty

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, NFA ka kaam hota hai "guess" karna — ek hi waqt mein woh kai states mein ho sakta hai, jaise koi robot maze mein khud ki copies bana ke saare raste ek saath try kare. Par DFA bechara ek hi state mein reh sakta hai, koi splitting nahi. Toh sawaal yeh hai: DFA NFA ko copy kaise kare? Jugaad simple hai — DFA ki ek state ko banao NFA ki states ka ek poora set. Matlab agar NFA abhi {q1,q3}\{q_1,q_3\} mein ho sakta hai, toh DFA ki ek single state ka naam hi {q1,q3}\{q_1,q_3\} rakh do. Isi liye ise subset (powerset) construction kehte hain.

Do tools yaad rakho. ECLOSE matlab epsilon-closure — bina koi letter padhe, sirf ε\varepsilon-edges se jahan-jahan pahunch sakte ho. MOVE(S,a) matlab set SS ke har state se letter aa padh ke jahan jaate ho. Har step ka formula hai δD(S,a)=ECLOSE(MOVE(S,a))\delta_D(S,a)=\text{ECLOSE}(\text{MOVE}(S,a)) — pehle MOVE, phir ECLOSE, har baar. Start state hota hai ECLOSE({q0})\text{ECLOSE}(\{q_0\}) (kyunki shuru mein hi NFA chupke se ε-edges se khisak sakta hai). Aur koi DFA-state accept tab hoti hai jab uske andar koi bhi NFA final state ho, yaani SFS\cap F\neq\varnothing.

Yeh kyun important hai? Kyunki NFA likhna aasan hota hai par chalane mein "guessing" lagti hai. DFA mechanical aur fast hota hai — har step pe seedha ek hi state. Subset construction proof hai ki NFA aur DFA dono same regular languages pehchante hain. Ek dhyaan ki baat: kabhi bhi shuru mein 2n2^n saare subsets mat banao — sirf woh subsets banao jo start se reachable hain, warna time waste. Aur agar MOVE khaali set de de, toh woh dead state (\varnothing) ban jaata hai jo apne aap pe loop kare aur reject kare. Mantra yaad rakho: "START pe CLOSE, phir MOVE-then-CLOSE, final ko TOUCH karo toh accept."

Go deeper — visual, from zero

Test yourself — Theory of Computation

Connections