Skip to content
Part IA Michaelmas, Lent Term

Decompiling NFA to Regular Expressions

Goal

Given an NFA MM, construct a regular expression RR with L(R)=L(M)L(R) = L(M).

The State Elimination Method

Setup

Given NFA M=(Q,Σ,Δ,s,F)M = (Q, \Sigma, \Delta, s, F), we compute a regex for paths through MM.

Normalisation

First, transform MM to have:

  1. A single start state (with no incoming edges)
  2. A single accepting state (with no outgoing edges)

Add new start snews_{\text{new}} with ε\varepsilon-edge to ss.

Add new accept fnewf_{\text{new}} with ε\varepsilon-edges from all states in FF.

Regional Languages

Definition

A region (p,S,q)(p, S, q) specifies:

  • Source state pp
  • Target state qq
  • Set SS of states allowed as intermediate

The regional language R(p,S,q)R(p, S, q) is the set of strings labelling paths from pp to qq using only states in SS as intermediate nodes.

Base Case: S=S = \emptyset

R(p,,q)={εa1akp=q and paipa1akpaiqno direct pathR(p, \emptyset, q) = \begin{cases} \varepsilon \cup a_1 | \cdots | a_k & p = q \text{ and } p \xrightarrow{a_i} p \\ a_1 | \cdots | a_k & p \xrightarrow{a_i} q \\ \emptyset & \text{no direct path} \end{cases}

Where a1,,aka_1, \ldots, a_k label the direct edges from pp to qq.

Inductive Step

For S=S{r}S = S' \cup \{r\} where rSr \notin S':

R(p,S,q)=R(p,S,q)R(p,S,r)R(r,S,r)R(r,S,q)R(p, S, q) = R(p, S', q) \cup R(p, S', r) \cdot R(r, S', r)^* \cdot R(r, S', q)

Interpretation

Paths from pp to qq through SS either:

  1. Don’t visit rr at all (stay in SS'): R(p,S,q)R(p, S', q)
  2. Visit rr at least once:
    • Go prp \to r staying in SS': R(p,S,r)R(p, S', r)
    • Loop at rr zero or more times: R(r,S,r)R(r, S', r)^*
    • Go rqr \to q staying in SS': R,S,q)R, S', q)

Algorithm

  1. Normalise: single start ss, single accept ff
  2. Compute R(s,Q{s,f},f)R(s, Q \setminus \{s, f\}, f) by eliminating states one by one
  3. The result is L(M)L(M)

Order of Elimination

The order in which states are eliminated does not affect correctness, but can affect the size of the resulting regex.

Common strategies:

  • Eliminate states with few transitions first
  • Eliminate “middle” states before endpoints

Example

Automaton

Two-state NFA: q0aq1bq0q_0 \xrightarrow{a} q_1 \xrightarrow{b} q_0, with q1q_1 accepting.

Two state NFA example

Computation

We want R(q0,{q0,q1},q1)R(q_0, \{q_0, q_1\}, q_1).

Let S={q0,q1}S = \{q_0, q_1\}.

Eliminate in some order. Suppose we eliminate q0q_0 first.

Actually, with normalisation, we have start ss and accept ff. The internal states are {q0,q1}\{q_0, q_1\}.

Compute R(s,{q0,q1},f)R(s, \{q_0, q_1\}, f):

  • Paths: sεq0aq1εfs \xrightarrow{\varepsilon} q_0 \xrightarrow{a} q_1 \xrightarrow{\varepsilon} f, or
  • q0aq1bq0aq1εfq_0 \xrightarrow{a} q_1 \xrightarrow{b} q_0 \xrightarrow{a} q_1 \xrightarrow{\varepsilon} f, etc.

After elimination: the language is a(ba)a(ba)^* plus accounting for ε\varepsilon-transitions.

Size Considerations

Worst Case

The regex may grow exponentially as states are eliminated due to the union and concatenation operations.

Optimisation

  • Factor common subexpressions
  • Use algebraic simplifications (e.g., R=\emptyset \cdot R = \emptyset, εR=R\varepsilon \cdot R = R)
  • Choose good elimination order

Closure under Complement

Using automaton-to-regex conversion:

  1. Convert regex to DFA (via NFA)
  2. Swap accepting and non-accepting states
  3. Convert back to regex

This shows regular languages are closed under complement.


Summary

  • Compute regex R(p,S,q)R(p, S, q) for paths through region
  • Base case: direct transitions
  • Inductive step: R(p,S,q)=R(p,S,q)R(p,S,r)R(r,S,r)R(r,S,q)R(p, S, q) = R(p, S', q) \cup R(p, S', r) \cdot R(r, S', r)^* \cdot R(r, S', q)
  • Eliminate all internal states to get the full language