Sub topic 4
Epsilon transitions and epsilon-closure
Prerequisites
This chapter assumes you know alphabets, strings, and languages, DFAs, and NFAs as covered in earlier chapters. You should be comfortable with the transition function of an NFA returning a set of states, and with basic set notation.
What an epsilon transition is
An epsilon transition (ε-transition) is a move that an automaton makes without reading any input symbol. An NFA with ε-transitions, usually written ε-NFA, has the transition function
δ : Q × (Σ ∪ {ε}) → 2^Q
The machine may be in state p and jump to state q for free whenever q ∈ δ(p, ε). The input head does not move.
ε-transitions add no power. Every ε-NFA has an equivalent NFA without them, and so an equivalent DFA. They make construction easier, especially when combining machines for union, concatenation, and Kleene star. The next chapters on regular expressions rely on them.
Epsilon-closure
The ε-closure of a state q, written ECLOSE(q), is the set of all states reachable from q by following zero or more ε-transitions. Two points matter:
- q is always in ECLOSE(q), because zero ε-moves are allowed.
- Closure is transitive. If r ∈ ECLOSE(q) and s ∈ ECLOSE(r), then s ∈ ECLOSE(q).
For a set of states S, ECLOSE(S) is the union of ECLOSE(q) over all q ∈ S.
You compute it by graph search. Treat only the ε-edges as a graph and find everything reachable from q.
def eclose(states, eps):
# eps maps a state to the set of states reachable by one epsilon move.
result = set(states)
stack = list(states)
while stack:
s = stack.pop()
for t in eps.get(s, set()):
if t not in result:
result.add(t)
stack.append(t)
return result
eps = {0: {1}, 1: {2}, 3: {0}}
print(sorted(eclose({0}, eps))) # [0, 1, 2]
print(sorted(eclose({3}, eps))) # [0, 1, 2, 3]
print(sorted(eclose({2}, eps))) # [2]Note that the closure follows edges in one direction only. State 3 reaches 0, but 0 does not reach 3.
Extended transition function
For an ε-NFA, the extended function δ̂ is defined by:
- δ̂(q, ε) = ECLOSE(q)
- For a string w and symbol a, let P = δ̂(q, w). Then δ̂(q, wa) = ECLOSE( ∪ over p ∈ P of δ(p, a) ).
The second rule has three steps in a fixed order. Take the current set, move on the symbol a, then close the result under ε. The initial closure of the start state happens once, before any input is read.
A string w is accepted if δ̂(q0, w) contains at least one accepting state.
Simulating an ε-NFA
The following program simulates an ε-NFA for the language of strings of the form abc*. The three loops are joined by ε-transitions.
eps = {0: {1}, 1: {2}}
delta = {(0, 'a'): {0}, (1, 'b'): {1}, (2, 'c'): {2}}
accepting = {2}
def eclose(states):
result = set(states)
stack = list(states)
while stack:
s = stack.pop()
for t in eps.get(s, set()):
if t not in result:
result.add(t)
stack.append(t)
return result
def accepts(w):
current = eclose({0})
for ch in w:
moved = {q for s in current for q in delta.get((s, ch), set())}
current = eclose(moved)
return bool(current & accepting)
for w in ["", "aabbcc", "abc", "ba", "cb"]:
print(repr(w), accepts(w))
# '' True
# 'aabbcc' True
# 'abc' True
# 'ba' False
# 'cb' FalseThe empty string is accepted because ECLOSE(0) = {0, 1, 2} already contains the accepting state 2.
Removing epsilon transitions
To convert an ε-NFA into an NFA with the same states and no ε-edges, build a new transition function δ' and a new set of accepting states F'.
- For each state q and symbol a ∈ Σ, set δ'(q, a) = ECLOSE( ∪ over p ∈ ECLOSE(q) of δ(p, a) ).
- Set F' = F ∪ { q : ECLOSE(q) ∩ F ≠ ∅ }. A state becomes accepting if it can reach an accepting state by ε-moves alone.
- Keep the same start state and the same state set.
Many students forget the closure on the outside in step 1. Without the outer ECLOSE, the new NFA would fail to follow ε-moves that occur after the symbol is read.
Worked example
Use the abc* machine above. The closures are ECLOSE(0) = {0, 1, 2}, ECLOSE(1) = {1, 2}, and ECLOSE(2) = {2}.
| State | on a | on b | on c |
|---|---|---|---|
| 0 | {0, 1, 2} | {1, 2} | {2} |
| 1 | ∅ | {1, 2} | {2} |
| 2 | ∅ | ∅ | {2} |
For state 0 on a: ECLOSE(0) = {0, 1, 2}. Only state 0 has an a-move, and it goes to {0}. Then ECLOSE({0}) = {0, 1, 2}.
For the accepting set, ECLOSE(0) and ECLOSE(1) both contain 2, so F' = {0, 1, 2}. All three states accept. This matches the language, since any prefix of the form a, ab, or abc can end in any of these states.
The new NFA has no ε-edges and the same number of states. The transitions may have grown denser, which is the usual cost.
Conversion directly to a DFA
You can skip the intermediate NFA. In subset construction for an ε-NFA, the start state is ECLOSE(q0), and the transition from a DFA state S on symbol a is
ECLOSE( ∪ over p ∈ S of δ(p, a) )
A DFA state is accepting if the set contains any accepting NFA state. The next chapter develops this fully.
Common errors
- Forgetting that q ∈ ECLOSE(q). Zero ε-moves count.
- Closing only once. Following one ε-edge is not enough. You must follow chains of any length, including cycles. Use a visited set so that a cycle such as 0 → 1 → 0 does not loop forever.
- Reading ε-edges backwards. ECLOSE follows edge direction.
- Applying closure before moving on a symbol, but not after. The move on a symbol uses the current set, which is already closed. The result must then be closed again.
- Not updating accepting states. After removing ε-edges, states that reach an accepting state by ε-moves must become accepting. This matters most for the empty string.
- Treating ε as a symbol in Σ. ε is not part of the alphabet, and a string never contains it as a character. ε is the empty string, with length 0.
Edge cases
- ε-cycles. The edges 0 → 1 and 1 → 0 give ECLOSE(0) = ECLOSE(1) = {0, 1}. The two states behave identically for ε-purposes, though they may differ on real symbols.
- An ε-only machine. An ε-NFA with only ε-edges and no symbol edges accepts either ∅ or {ε}, depending on whether an accepting state is in ECLOSE(q0).
- Empty language versus empty string. The language ∅ has no strings. The language {ε} has exactly one string. An ε-NFA accepting {ε} has an accepting start state, or a start state with an ε-path to one, and no symbol transitions that lead to acceptance.
- State count. Removing ε-edges never changes the number of states. The subset construction may increase it, up to 2^n.
Why ε-moves help
They make closure constructions straightforward. For two machines M1 and M2:
- Union: add a new start state with ε-edges to both start states.
- Concatenation: add ε-edges from each accepting state of M1 to the start state of M2, and make only the accepting states of M2 accepting.
- Star: add a new start state that is accepting, with an ε-edge to the old start state, and add ε-edges from old accepting states back to the old start state.
The regular-expression chapter uses exactly these patterns.
Exercises
Exercise
An ε-NFA has states {0, 1, 2, 3}, with ε-edges 0→1, 1→2, 3→0 and no other ε-edges. Give ECLOSE(3), ECLOSE(1), and ECLOSE({0, 3}).
Show answer
ECLOSE(3) = {3, 0, 1, 2}. ECLOSE(1) = {1, 2}. ECLOSE({0, 3}) = ECLOSE(0) ∪ ECLOSE(3) = {0, 1, 2} ∪ {0, 1, 2, 3} = {0, 1, 2, 3}.
Exercise
An ε-NFA has states {p, q, r} with start p and accepting set {r}. Its edges are p –ε→ q, q –a→ r, and r –ε→ p. Give the set of states after reading the string "a", then say whether "a" is accepted, and which states become accepting after ε-removal.
Show answer
Start: ECLOSE(p) = {p, q}. On a, only q moves, to r, so the moved set is {r}. Then ECLOSE(r) = {r, p, q}. This contains r, so "a" is accepted. For ε-removal, F' = F plus every state whose closure contains r. ECLOSE(p) = {p, q} does not contain r, ECLOSE(q) = {q} does not, and ECLOSE(r) = {r, p, q} does. So F' = {r}. The empty string is not accepted because ECLOSE(p) has no accepting state.
Exercise
Why is the outer ECLOSE needed in δ'(q, a) = ECLOSE( ∪ δ(p, a) over p ∈ ECLOSE(q) )?
Show answer
After reading a, the machine may take further ε-moves before the next symbol. Without the outer closure, the new NFA would miss paths that read a and then follow ε-edges, so it would reject strings that the ε-NFA accepts.