4.6.5Theory of Computation

Regular expressions — equivalence with finite automata

2,186 words10 min readdifficulty · medium

What are we proving?

This is an iff, so we need two directions:

  1. Regex → NFA (every pattern is buildable as a machine) — Thompson's construction.
  2. FA → Regex (every machine's behavior is expressible as a pattern) — state elimination.

Because NFA ≡ DFA (subset construction, proven elsewhere), proving regex ↔ NFA is enough.


Regex syntax (first, so we know what we're converting)


Direction 1: Regex → NFA (Thompson's construction)

Figure — Regular expressions — equivalence with finite automata

Direction 2: FA → Regex (state elimination / GNFA)


Putting both directions together

Recall Feynman: explain to a 12-year-old

Imagine a regex is a recipe written in words ("first an apple, then any number of bananas"). A finite automaton is a little robot that walks through rooms as it eats each fruit and lights up green if the meal matches. Kleene's theorem says: any recipe you can write, a robot can be built to check it — and any robot's behavior can be written back as a recipe. They're two languages for the same idea, so you can always translate one into the other.


Active recall

Kleene's Theorem states
A language is regular (regex-describable) iff it is accepted by a finite automaton (DFA/NFA).
The two constructions proving the equivalence
Regex→NFA via Thompson's construction; FA→Regex via state elimination (GNFA).
Thompson gadget for union RSR\mid S
New start ε-branches into NRN_R and NSN_S; new accept reached by ε from both old accepts.
Thompson gadget for concatenation RSRS
ε-edge from NRN_R's accept to NSN_S's start; accept = NSN_S's accept.
Thompson gadget for star RR^*
New start ε to new accept (zero) and to NRN_R's start; NRN_R's accept ε back to its start (loop) and to new accept (stop).
State-elimination relabeling rule
Rijnew=RijRiqRqqRqjR_{ij}^{new} = R_{ij} \mid R_{iq} R_{qq}^{*} R_{qj}.
Meaning of RqqR_{qq}^{*} in elimination
Loop on the state being deleted zero or more times before leaving.
Why add fresh start/accept before elimination
So start has no incoming and accept no outgoing edges, making the rip rule always applicable.
Value of \varnothing^{*}
ε\varepsilon — "zero or more of nothing" is the empty string.
Why NFA proof suffices (not DFA directly)
NFA ≡ DFA by subset construction, so regex↔NFA gives regex↔DFA for free.
Does elimination order change the language
No — different orders give equivalent regexes (same language), only differing in form.
Regex for FA: 1a21\xrightarrow{a}2, 2b22\xrightarrow{b}2 loop, accept 2
abab^{*}.

Connections

Concept Map

connects

connects

describes

accepts

needs direction

needs direction

method

method

glues gadgets via

mirror

built from

so regex vs NFA suffices

Kleene Theorem iff

Regular expression pattern

Finite automaton machine

Regular languages

Regex to NFA

FA to Regex

Thompson construction

State elimination

Epsilon transitions

Operators union concat star

NFA equiv DFA

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, regular expression ek pattern hai jo tum likhte ho — jaise (a|b)*a, matlab "kitne bhi a ya b, last me ek a". Aur finite automaton ek machine hai states ke saath jo string padh kar batati hai accept ya reject. Dono bilkul alag lagte hain, par Kleene's Theorem kehta hai ki dono exactly same languages describe karte hain — yahi regular languages hain. Isi wajah se grep, search bars, aur compiler ke lexers kaam karte hain.

Do directions prove karni padti hain. Pehli: regex se NFA banao — ye hai Thompson's construction. Har chhote pattern ka ek mini-machine banao jisme ek hi start aur ek hi accept ho, phir unhe epsilon-transitions (khali move, bina input padhe) se jod do. Union ke liye dono machine me branch karo, concatenation me ek ke baad doosri chalao, star me ek loop lagao aur "zero copies" ka skip edge bhi.

Doosri direction: FA se regex — ye hai state elimination. Ek-ek karke states hatao, aur jo edges bachte hain unpe pura regex likh do, formula se: Rijnew=RijRiqRqqRqjR_{ij}^{new}=R_{ij}\mid R_{iq}R_{qq}^{*}R_{qj}. Yahan RqqR_{qq}^{*} ka matlab hai "delete hone wali state pe jitni baar chahe ghoom lo". Jab sirf start aur accept bache, beech ka label hi tumhara answer hai. Yaad rakhna: elimination ka order badalne se language nahi badalti, sirf regex ka likhne ka tareeka badalta hai — 2+32+3 aur 3+23+2 jaisa. Dono directions milke "iff" complete karti hain, isliye regex, NFA, DFA — teeno same regular languages hain.

Go deeper — visual, from zero

Test yourself — Theory of Computation

Connections