DFA minimization
Merge the equivalent states of a deterministic machine: start from accepting versus non-accepting and split the blocks until no more splits happen.
Two states are equivalent if for every input string either both reach acceptance or neither does. Equivalent states can be merged; the result is the minimal machine, which is unique for each regular language (up to naming the states).
Read the full explanation ↓- Time:
O(n²) here (O(n log n) with Hopcroft's method) - Space:
O(n) - Johnsonbaugh, chapter 12
- Grimaldi, chapter 6
A 5-state machine for “ends with ab”; state 3 is equivalent to 0 and state 4 to 1, so the minimal DFA has 3 states. Each row: [after a, after b, accepting].
def minimize(delta):# delta: one row per state: [state after a, state after b, accepting (1) or not (0)].n = len(delta)if any(t >= n for row in delta for t in row[:2]):return Noneblock = [1 if row[2] else 0 for row in delta]table = [[q, delta[q][0], delta[q][1], block[q]] for q in range(n)]while True:# Two states stay together only if their blocks agree and so do the blocks they go to.signature = [(block[q], block[delta[q][0]], block[delta[q][1]]) for q in range(n)]kinds = sorted(set(signature))new = [kinds.index(sig) for sig in signature]for q in range(n):table[q][3] = new[q]if len(kinds) == len(set(block)):breakblock = newreturn len(set(block))
Start
The table is still empty.
Local variables
Orange = changed in this step
delta list
Call stack
- minimize()line 1
Explanation
The idea
Two states are equivalent if for every input string either both reach acceptance or neither does. Equivalent states can be merged; the result is the minimal machine, which is unique for each regular language (up to naming the states).
Partition refinement (Moore's method)
- Split the states into two blocks: accepting and non-accepting (the empty string tells them apart).
- Split each block: two states stay together only if on
athey go to the same block, and onbtoo. - Repeat while some block splits. When none does, each block is one state of the minimal machine.
Cost
At most n rounds of O(n) each: O(n²). Hopcroft's method picks the blocks cleverly and reaches O(n log n).
The input
Each row is a state: [after a, after b, accepting (1) or not (0)]; the start state is 0.