Sub topic 6
Minimizing a DFA
Assume you know basic set operations, functions, equivalence relations, and how a deterministic finite automaton (DFA) processes a string. A DFA is minimal when no equivalent DFA for the same language has fewer states.
Minimization matters because fewer states mean less memory, simpler proofs, and faster implementations. The goal is to merge states that accept exactly the same remaining strings.
What makes two states equivalent?
Two states \(p\) and \(q\) are equivalent if every possible suffix produces the same result from both states:
- starting at \(p\), the suffix is accepted exactly when it is accepted from \(q\);
- starting at \(p\), the suffix is rejected exactly when it is rejected from \(q\).
A pair is distinguishable if some suffix is accepted from one state and rejected from the other.
The first distinction is immediate:
- Every accepting state differs from every rejecting state, because the empty suffix distinguishes them.
- States in the same group may still differ after reading another symbol.
Partition-refinement method
Use these steps:
- Remove unreachable states. They cannot affect the language recognized from the start state.
- Create an initial partition containing accepting states and non-accepting states.
- Refine each group. Split states when some input symbol sends them to different groups.
- Repeat until no group changes.
- Make one new DFA state for each final group.
The transition of a group is determined by the group containing the destination state.
Consider this DFA over \(\{0,1\}\):
| State | On `0` | On `1` | Accepting |
|---|---|---|---|
| A | B | C | No |
| B | B | D | No |
| C | B | C | Yes |
| D | B | C | No |
The initial partition is:
- \(\{C\}\), the accepting states
- \(\{A,B,D\}\), the non-accepting states
Now compare transitions within \(\{A,B,D\}\):
- A goes to groups \((\{B\}, \{C\})\), which is \((N, F)\).
- B goes to \((N, N)\).
- D goes to \((N, F)\).
Therefore B must split from A and D. The final partition is:
- \(\{A,D\}\)
- \(\{B\}\)
- \(\{C\}\)
The minimized DFA has three states. Its start state is \(\{A,D\}\), and its accepting state is \(\{C\}\).
A small implementation
The following program repeatedly splits state groups according to their transition signatures.
def minimize(states, alphabet, transition, start, accepting):
groups = [set(accepting), set(states) - set(accepting)]
groups = [group for group in groups if group]
changed = True
while changed:
changed = False
group_of = {
state: index
for index, group in enumerate(groups)
for state in group
}
new_groups = []
for group in groups:
buckets = {}
for state in group:
signature = tuple(
group_of[transition[(state, symbol)]]
for symbol in alphabet
)
buckets.setdefault(signature, set()).add(state)
new_groups.extend(buckets.values())
changed |= len(buckets) > 1
groups = new_groups
return groups
states = {"A", "B", "C", "D"}
alphabet = {"0", "1"}
transition = {
("A", "0"): "B", ("A", "1"): "C",
("B", "0"): "B", ("B", "1"): "D",
("C", "0"): "B", ("C", "1"): "C",
("D", "0"): "B", ("D", "1"): "C",
}
print(minimize(states, alphabet, transition, "A", {"C"}))Exercise
Why must an accepting state and a non-accepting state remain in different groups?
Show answer
The empty string distinguishes them: it is accepted from the accepting state and rejected from the non-accepting state.