Sub topic 6
Minimizing a DFA
This chapter assumes you can build a DFA, read its transition table, and follow the idea of an equivalence relation. Two DFAs can accept the same language with different numbers of states. Minimization finds the smallest one, and GATE asks about it often, usually as "how many states does the minimal DFA have?"
Why minimize
A DFA built by hand or by subset construction often has redundant states. Some states can never be reached. Others behave identically to each other. Removing them gives a smaller machine with the same language.
The minimal DFA for a regular language is unique up to renaming of states. So two DFAs accept the same language exactly when their minimal forms are identical after renaming. This gives you a way to test language equivalence, covered later under decision problems.
Distinguishable and equivalent states
Take a complete DFA and two of its states p and q. They are distinguishable if some string w exists such that exactly one of the states reached from p and q after reading w is accepting. If no such w exists, p and q are equivalent.
The empty string is a valid w. So an accepting state is always distinguishable from a non-accepting one. This fact starts the whole algorithm.
Equivalent states can be merged into one state without changing the language. Equivalence is an equivalence relation, so it splits the states into blocks, and each block becomes one state of the minimal DFA.
The algorithm: partition refinement
This method is also called Moore's algorithm. It is the table-filling method organized by blocks.
- Delete every state that cannot be reached from the start state.
- Split the remaining states into two blocks: accepting and non-accepting.
- For each state, record which block it belongs to and which block each symbol leads to. Two states in the same block stay together only if these records match. Otherwise, split the block.
- Repeat step 3 until no block splits.
- Each final block is one state of the minimal DFA.
The table-filling version marks pairs instead. Mark every pair (p, q) where exactly one state is accepting. Then repeatedly mark (p, q) if, for some symbol c, the pair (δ(p,c), δ(q,c)) is already marked. When nothing changes, the unmarked pairs are the equivalent ones.
Worked example
Take a DFA over {0, 1} with states 0 to 5, start state 0, and accepting state 4.
| State | on 0 | on 1 |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 3 | 4 |
| 2 | 3 | 4 |
| 3 | 3 | 4 |
| 4 | 3 | 4 |
| 5 | 4 | 3 |
Step 1. No transition from states 0 to 4 leads to state 5, and 5 is not the start state. So state 5 is unreachable and is deleted.
Step 2. The initial blocks are A = {0, 1, 2, 3} and B = {4}.
Step 3, round 1. Look at the block reached on 0 and on 1:
| State | on 0 | on 1 |
|---|---|---|
| 0 | A | A |
| 1 | A | B |
| 2 | A | B |
| 3 | A | B |
State 0 differs from 1, 2, 3, so block A splits into {0} and {1, 2, 3}.
Round 2. State 0 is alone. States 1, 2, 3 all go to {1, 2, 3} on 0 and to {4} on 1. Nothing splits, so the algorithm stops.
The minimal DFA has three states: {0}, {1, 2, 3}, and {4}. It accepts strings of length at least 2 that end in 1.
Implementing it
def minimize(delta, start, accepting, alphabet):
# Step 1: keep only states reachable from the start state.
reachable = {start}
stack = [start]
while stack:
s = stack.pop()
for c in alphabet:
t = delta[(s, c)]
if t not in reachable:
reachable.add(t)
stack.append(t)
# Step 2: initial partition is accepting vs non-accepting.
block = {s: int(s in accepting) for s in reachable}
# Step 3: refine until the number of blocks stops growing.
while True:
sig = {s: (block[s], tuple(block[delta[(s, c)]] for c in alphabet))
for s in reachable}
ids = {}
new = {s: ids.setdefault(sig[s], len(ids)) for s in sorted(reachable)}
if len(ids) == len(set(block.values())):
break
block = new
groups = {}
for s in sorted(reachable):
groups.setdefault(new[s], []).append(s)
return sorted(tuple(g) for g in groups.values())
delta = {
(0, '0'): 1, (0, '1'): 2,
(1, '0'): 3, (1, '1'): 4,
(2, '0'): 3, (2, '1'): 4,
(3, '0'): 3, (3, '1'): 4,
(4, '0'): 3, (4, '1'): 4,
(5, '0'): 4, (5, '1'): 3,
}
groups = minimize(delta, 0, {4}, "01")
print(groups) # [(0,), (1, 2, 3), (4,)]
print(len(groups)) # 3Each round either splits at least one block or stops. A DFA with n states can have at most n blocks, so the loop runs at most n times.
Edge cases and common errors
- The DFA must be complete. If some transitions are missing, add a dead (trap) state that loops to itself on every symbol and receives all missing transitions. The dead state counts as a state in the minimal complete DFA. Many GATE questions say "minimal DFA" and mean the complete one, so include the dead state in your count. If a question asks for a DFA "without dead state," it says so.
- Unreachable states go first. An unreachable state can look distinguishable from reachable ones, and an unreachable state alone can inflate your count. Always delete it before refining.
- Do not compare transition targets by name. Compare the blocks they belong to. Two states going to different states p and q are still equivalent when p and q are in the same block.
- Minimization applies to DFAs only. An NFA with fewer states than the minimal DFA is normal, and no unique minimal NFA exists. Convert to a DFA first, then minimize.
- Complement keeps the state count. To complement a language, make the DFA complete and swap accepting and non-accepting states. A minimal DFA stays minimal, because distinguishability does not depend on which side is accepting.
Counting states for GATE
These counts for minimal complete DFAs come up repeatedly:
| Language over {a, b} or {0, 1} | Minimal states |
|---|---|
| Strings ending in a fixed string of length k, such as abb | k + 1 |
| Strings with at least k occurrences of a symbol | k + 1 |
| Strings whose length is divisible by n | n |
| Binary numbers divisible by n, n odd | n |
| Binary numbers divisible by 4 | 3 (remainders 1 and 3 merge) |
| The k-th symbol from the right is 1 | 2^k |
The last row shows why minimization matters. An NFA for this language has k + 1 states, but any DFA needs 2^k. Subset construction can blow up, and this family shows it is sometimes unavoidable.
A useful check follows from Myhill-Nerode, covered in a later chapter. The number of states in the minimal DFA equals the number of classes of strings that no suffix can tell apart. If you find k strings where every pair is distinguished by some suffix, the minimal DFA needs at least k states.
Checking equivalence with minimization
To test whether two DFAs accept the same language, minimize both and compare them. Alternatively, run partition refinement on the union of their states, and check whether the two start states land in the same block. If they do, the languages are equal.
Exercise
A complete DFA over {a, b} has states 1 to 5, start state 1, and accepting states {4, 5}. Transitions on a: 1→2, 2→3, 3→3, 4→5, 5→4. Transitions on b: 1→4, 2→5, 3→3, 4→4, 5→5. All five states are reachable. How many states does the minimal DFA have?
Show answer
Start with blocks {1, 2, 3} and {4, 5}. In {1, 2, 3}: state 1 goes to (2, 4), state 2 goes to (3, 5), state 3 goes to (3, 3). On a, state 1 goes to block {1,2,3} and so do states 2 and 3. On b, states 1 and 2 go to {4,5} and state 3 goes to {1,2,3}. So state 3 splits off. In {4, 5}: 4 goes to (5, 4) and 5 goes to (4, 5), both inside {4, 5}, so they stay together. Blocks are {1, 2}, {3}, {4, 5}. Next round: state 1 goes to (2, 4) which is ({1,2}, {4,5}); state 2 goes to (3, 5) which is ({3}, {4,5}). These differ, so {1, 2} splits. Final blocks: {1}, {2}, {3}, {4, 5}. The minimal DFA has 4 states.
Exercise
A minimal complete DFA has n states. You swap its accepting and non-accepting states. How many states does the resulting minimal DFA have, and why?
Show answer
It has n states. Two states are distinguishable when some string leads exactly one of them to an accepting state. After swapping, the same string still leads exactly one of them to an accepting state, so the same pairs stay distinguishable. All states remain reachable and pairwise distinguishable, so the machine is still minimal.