Sub topic 4
Epsilon Transitions and Epsilon-Closure
Prerequisites and the epsilon transition
This chapter assumes that you know the basic parts of a nondeterministic finite automaton (NFA): states, an input alphabet, transitions, a start state, and accepting states.
An epsilon transition is a transition labeled ε. It changes the current state without reading an input symbol. An automaton that allows these transitions is called an epsilon-NFA or ε-NFA.
For example, consider this automaton:
The input a is accepted. The machine first moves from q0 to q1 without consuming input, then reads a and reaches q2.
Epsilon transitions are useful when combining smaller automata. However, they require careful tracking because the machine may be in several states before reading the next symbol.
Epsilon-closure
The epsilon-closure of a state q, written ε-closure(q), is the set of states reachable from q using zero or more epsilon transitions.
“Zero or more” means that q itself is always included. Epsilon transitions can also be followed repeatedly.
| Starting state | Epsilon-reachable states | Epsilon-closure |
|---|---|---|
| q0 | q0, q1 | {q0, q1} |
| q1 | q1 | {q1} |
| q2 | q2, q3 | {q2, q3} |
If a set of states is given, its epsilon-closure is the union of the closures of all states in that set.
To compute a closure:
- Put the starting state in a set.
- Follow every available ε transition.
- Add each newly reached state.
- Repeat until no new state appears.
The starting state is accepted for the empty string if its epsilon-closure contains an accepting state.
Simulating an epsilon-NFA
During simulation, maintain a set of possible states. First take its epsilon-closure. For each input symbol, move along that symbol from every current state, then take epsilon-closure again.
EPSILON = None
transitions = {
(0, EPSILON): {1},
(1, "a"): {2},
(2, EPSILON): {3},
}
accepting = {3}
def epsilon_closure(states):
closure = set(states)
stack = list(states)
while stack:
state = stack.pop()
for target in transitions.get((state, EPSILON), set()):
if target not in closure:
closure.add(target)
stack.append(target)
return closure
def accepts(word):
current = epsilon_closure({0})
for symbol in word:
next_states = set()
for state in current:
next_states.update(transitions.get((state, symbol), set()))
current = epsilon_closure(next_states)
return bool(current & accepting)
print(accepts("a")) # True
print(accepts("")) # FalseA common error is to calculate epsilon-closure only at the beginning. It must also be calculated after every symbol is consumed. Another error is to treat ε as an input character. It is a state-changing move, not part of the input string.
Removing epsilon transitions
An ε-NFA can be converted to an equivalent NFA without epsilon transitions. For state q and symbol a, use:
- Compute ε-closure(q).
- From every state in that closure, follow an
atransition. - Compute the epsilon-closure of all reached states.
- Use that final set as the destination for
a.
A state is accepting in the new NFA if its epsilon-closure contains an accepting state. This preserves exactly the same language.
Exercise
In the automaton above, what is ε-closure({q0}), and does the automaton accept the empty string?
Show answer
ε-closure({q0}) = {q0, q1}. It does not accept the empty string because this set does not contain the accepting state q2.