Sub topic 2
Minimizing a DFA
This chapter assumes you know sets, union and concatenation of languages, and how a DFA (deterministic finite automaton) accepts a string. You should also be comfortable with equivalence relations.
Two states of a DFA are equivalent when they accept exactly the same set of continuations. A minimal DFA has the fewest states possible for its language, and it is unique up to renaming.
Minimizing a DFA
Use partition refinement. Start with two blocks, accepting and non-accepting states. Split a block when two of its states send some symbol into different blocks. Stop when no block splits.
Take a DFA over {a, b} with start state 0 and accepting states {2, 3}. Its transitions are 0 -a-> 1, 0 -b-> 0, 1 -a-> 1, 1 -b-> 2, 2 -a-> 1, 2 -b-> 0, 3 -a-> 1, 3 -b-> 0.
def minimize(states, alphabet, delta, accepting):
partition = [set(accepting), set(states) - set(accepting)]
partition = [b for b in partition if b]
changed = True
while changed:
changed = False
block_of = {s: i for i, b in enumerate(partition) for s in b}
new = []
for block in partition:
groups = {}
for s in block:
key = tuple(block_of[delta[(s, c)]] for c in alphabet)
groups.setdefault(key, set()).add(s)
if len(groups) > 1:
changed = True
new.extend(groups.values())
partition = new
return partition
states = {0, 1, 2, 3}
delta = {(0,'a'):1, (0,'b'):0, (1,'a'):1, (1,'b'):2,
(2,'a'):1, (2,'b'):0, (3,'a'):1, (3,'b'):0}
print(sorted(sorted(b) for b in minimize(states, 'ab', delta, {2, 3})))
# [[0], [1], [2, 3]]States 2 and 3 merge into one state, giving a 3-state minimal DFA.
Regular expressions and the languages they denote
A regular expression builds a language from symbols using three operations: union (|), concatenation (juxtaposition), and Kleene star (*, zero or more repetitions). The expression (a|b)*ab denotes all strings over {a, b} that end in ab.
import re
print(bool(re.fullmatch(r"(a|b)*ab", "aab"))) # True
print(bool(re.fullmatch(r"(a|b)*ab", "aba"))) # FalseFrom regular expression to automaton
Thompson's construction turns each regular expression into an NFA with epsilon moves. It builds a small piece for each symbol and joins the pieces with epsilon transitions. The diagram shows the piece for a|b.
From automaton to regular expression
State elimination removes one state at a time. Each removed state rewires its incoming and outgoing paths into a labeled edge that combines their expressions. When two states remain, the label on the edge from start to accept is the regular expression.
Closure properties of regular languages
Regular languages are closed under union, concatenation, star, complement, intersection, and reversal. Complement swaps accepting and non-accepting states of a complete DFA. Intersection uses a product automaton.
| Operation | Construction |
|---|---|
| Union | NFA with a new start and epsilon moves to both machines |
| Complement | Swap accepting and non-accepting states of a complete DFA |
| Intersection | Product DFA that tracks both machines at once |
Exercise
Does the 4-state DFA in the minimization example reduce to 3 states?
Show answer
Yes. States 2 and 3 have identical transitions and both accept, so they merge.