Sub topic 9
From automaton to regular expression
This chapter assumes you can read a DFA or NFA transition table and know the three regular-expression operators: union, concatenation and Kleene star. It also assumes you know the earlier result that every regular expression has an equivalent automaton. Here we prove the other direction. Every finite automaton accepts a language that some regular expression denotes. Together the two directions show that the models are equivalent.
The problem and the two standard methods
Given a DFA or NFA, we want a regular expression for the set of strings that lead from the start state to an accepting state. Two methods cover almost every GATE question.
- State elimination removes states one at a time and relabels edges with regular expressions.
- Arden's theorem writes one equation per state and solves the system by substitution.
A third method, Kleene's recurrence R<sub>ij</sub><sup>k</sup>, is the textbook proof. It is rarely used by hand, because it builds a table of expressions that grows quickly.
| Method | Best for | Main risk |
|---|---|---|
| State elimination | Seeing the structure, programming it | Forgetting the new start or final state |
| Arden's theorem | Small automata on paper, GATE answers | Writing the equations in the wrong direction |
| Kleene's R<sub>ij</sub><sup>k</sup> | Proofs | Huge expressions |
The running example
Take the DFA over {a, b} that accepts strings with an odd number of a's. State q0 means an even count so far, and q1 means odd. State q1 is accepting.
Method 1: state elimination
State elimination works on a generalized automaton, where each edge is labeled with a regular expression instead of a single symbol. The procedure has four steps.
- Add a new start state S with an ε-edge to the old start state.
- Add a new final state F. Give every old accepting state an ε-edge to F, and make F the only accepting state.
- Remove the other states one at a time. To remove state q, take every incoming edge p → q labeled X and every outgoing edge q → r labeled Z. Let Y be the label of the self-loop on q. If q has no self-loop, Y is ∅, so Y is ε. Add the path expression X Y Z to the edge p → r, using union if an edge p → r already exists.
- When only S and F remain, the label on S → F is the answer. No edge means the language is ∅.
Apply this to the example. Remove q1 first. Its self-loop is b, so Y = b. The incoming edge q0 → q1 is a. There are two outgoing edges, q1 → q0 labeled a and q1 → F labeled ε. This adds two new edges.
- q0 → q0 gets a b a, which is unioned with the existing loop b. The new loop label is b | aba.
- q0 → F gets a b*.
Now remove q0. Its loop is (b | ab\a), and S → q0 is ε, so S → F becomes ε (b | ab\a)\ ab\. The result is:
(b | ab\a)\ ab\*
Read it as follows. The block (b | ab\a)\ reads any run of even-a-count stretches, and then a b\* finishes with the one unpaired a.
Method 2: Arden's theorem
Arden's theorem says that if P does not contain ε, the equation R = Q + RP has the unique solution R = QP\. The mirror image, R = Q + PR, gives R = P\Q.
To use it, let Q<sub>i</sub> be the set of strings that take the machine from the start state to state i. Each equation lists every way to arrive at state i, which means one term for each incoming edge. The start state gets an extra ε term.
For the example:
- Q0 = ε + Q0 b + Q1 a
- Q1 = Q0 a + Q1 b
Solve the Q1 equation first. It has the form R = Q + RP with P = b, so Q1 = Q0 a b\*. Substitute this into the Q0 equation:
- Q0 = ε + Q0 b + Q0 a b\ a = ε + Q0 (b | ab\a)
- So Q0 = (b | ab\a)\
The answer is the union of Q<sub>i</sub> over accepting states. Here that is Q1 = (b | ab\a)\ a b\*, which matches state elimination.
Implementing state elimination
This program runs state elimination on the example DFA. It uses None for ∅ and the empty string for ε. It then checks the resulting regex against the DFA on every string up to length 8.
import re
from itertools import product
def alt(r, s):
if r is None:
return s
if s is None:
return r
if r == s:
return r
return f"(?:{r}|{s})"
def cat(r, s):
if r is None or s is None:
return None
return r + s
def star(r):
if r is None or r == "":
return ""
if len(r) == 1:
return r + "*"
return f"(?:{r})*"
def dfa_to_regex(states, start, accepting, delta, order):
edges = {}
for (q, c), t in delta.items():
edges[(q, t)] = alt(edges.get((q, t)), c)
edges[("S", start)] = ""
for q in accepting:
edges[(q, "F")] = alt(edges.get((q, "F")), "")
for q in order:
loop = star(edges.get((q, q)))
nodes = set(states) | {"S", "F"}
ins = [p for p in nodes if p != q and (p, q) in edges]
outs = [r for r in nodes if r != q and (q, r) in edges]
for p in ins:
for r in outs:
path = cat(cat(edges[(p, q)], loop), edges[(q, r)])
edges[(p, r)] = alt(edges.get((p, r)), path)
edges = {k: v for k, v in edges.items() if q not in k}
return edges.get(("S", "F"))
delta = {(0, "a"): 1, (0, "b"): 0, (1, "a"): 0, (1, "b"): 1}
accepting = {1}
rx = dfa_to_regex([0, 1], 0, accepting, delta, order=[1, 0])
print(rx)
def dfa_accepts(w):
state = 0
for ch in w:
state = delta[(state, ch)]
return state in accepting
for n in range(9):
for t in product("ab", repeat=n):
w = "".join(t)
assert bool(re.fullmatch(rx, w)) == dfa_accepts(w), w
print("regex agrees with the DFA on all strings up to length 8")The printed regex is (?:(?:b|ab*a))*ab*. The doubled group comes from star wrapping an expression that is already grouped. It is correct, only less tidy than the hand-derived form.
Edge cases and common errors
- Start state with incoming edges. Without the new state S, you can lose or double-count the ε that starts every path. Always add S.
- Several accepting states. Send each to F with an ε-edge. Do not eliminate accepting states as if they were ordinary states.
- Dead states. A trap state can never reach an accepting state, so delete it before you begin. It contributes ∅ and only adds work.
- Parallel edges. Merge edges between the same pair of states into one union label before eliminating. The code does this with
alt. - NFAs. Both methods work unchanged, since several edges with the same symbol are just a union. ε-edges are labeled ε. With Arden, a term where P contains ε makes the solution non-unique. QP\* is still the smallest solution, and it is the one we want.
- Order matters. Different elimination orders give different but equivalent expressions. Their sizes can differ greatly. In the worst case the expression grows exponentially with the number of states, and no method avoids this in general.
Where this fits
The chapter on regex to automaton gave one direction, and this chapter gives the other. Together they show that DFAs, NFAs, ε-NFAs and regular expressions all describe exactly the regular languages. Closure properties and decision problems rely on this: you can switch to whichever representation makes the proof easiest.
Exercise
A DFA over {0, 1} has start state q0 and accepting state q1. Its transitions are: q0 on 0 goes to q0, q0 on 1 goes to q1, q1 on 0 goes to q0, q1 on 1 goes to q1. Use Arden's theorem to find a regular expression for its language, then simplify it in words.
Show answer
Write the equations by incoming edges. Q0 = ε + Q0 0 + Q1 0, and Q1 = Q0 1 + Q1 1. From the second, Q1 = Q0 1 1. Substitute into the first: Q0 = ε + Q0 (0 | 1 1 0), so Q0 = (0 | 110). The accepting state is q1, so the answer is Q1 = (0 | 110) 11. It denotes the same language as (0|1)1, which is all binary strings ending in 1.
Exercise
You run state elimination on a DFA and, when only S and F remain, there is no edge from S to F. What language does the DFA accept, and what does it mean if you forgot to add S and F?
Show answer
No edge from S to F means the language is ∅, so no string reaches an accepting state. This happens, for example, when the accepting states are unreachable from the start. If you skipped S or F, you can miss the ε that starts each path, or mishandle accepting states that have outgoing edges or a start state with incoming edges. The extra states make the procedure uniform.