DFA Minimisation (Colouring)
Context: FIT2014_MOC Β· step 3 of the pipeline regex β NFA β DFA β simplify Β· shrinks the subset-construction blow-up back down Problem it solves: given a DFA, produce an equivalent DFA with the fewest possible states.
Quick Revision
- π― Trigger: a DFA (usually one that came out of the subset construction with redundant states) β colour states, then repeatedly split colours whose rows disagree.
- β‘ Key Constraint: different colours βΉ definitely different states; same colour βΉ only possibly mergeable. Sameness is provisional and must survive every refinement round.
π¨ The colouring principle
- Seed β a Final state and a non-Final state are fundamentally different and can never be combined. Give all Final states one colour and all non-Final states a different colour.
- Different colours different states β they cannot be combined.
- Same colour same states β they may or may not be combinable; you have merely not yet ruled it out.
- Refinement rule β if two states share a colour but their transition rows lead to different colour patterns, they are distinguishable, so the colour must split.
βοΈ The algorithm
- Colour all Final states one colour; all non-Final states another.
- Repeat:
- For each colour used so far, consider all states of that colour.
- Rewrite each stateβs row in terms of the colours its transitions lead to.
- If those rows do not all share the same colour pattern, split: give each distinct row-pattern a new colour (states with the same row pattern keep a common colour).
- Until no new colour is added in a full pass.
- Number the colours and read off the transition table β each colour is one state of the minimal DFA.
π Worked trace
Starting DFA from the subset construction for :
| State | |||
|---|---|---|---|
| Start | |||
| Final | |||
| Final |
Round 1 β colour Finals blue, non-Finals red. Rows become:
| State | colour | β | β |
|---|---|---|---|
| red | red | red | |
| red | red | red | |
| red | blue | red | |
| blue | blue | red | |
| blue | blue | red |
Round 2 β among the reds, has row (blue, red) while the other two have (red, red) βΉ split: becomes green. The blues agree, so they stay merged.
Result β three colours βΉ a 3-state minimal DFA (down from 5):
| State | |||
|---|---|---|---|
| Start | 1 (red) | 1 | 2 |
| 2 (green) | 3 | 1 | |
| Final | 3 (blue) | 3 | 1 |
π₯οΈ Implementing a finite automaton
Once minimised, the machine is trivial to run from its table:
- Keep a
currentStatevariable, initialised to the Start state. - Loop while input letters remain: read the next letter and set
currentStateto the table entry for (current state, that letter). - Finish by reporting a match if
currentStateis a Final state, otherwise no match. - Note β better algorithms exist: some build a minimum-state DFA directly from a regular expression without an intermediate NFA, and there are more compact table encodings than the plain two-dimensional array.
βοΈ Practice
Practice: A 4-state DFA has Start ; , ; , ; Final with , ; Final, unreachable, with , . Minimise.
Reference solution
- Colour β Finals blue; non-Finals red.
- Rows by colour β : (red, red) Β· : (red, blue) Β· : (red, red) Β· : (red, red).
- Split the reds β is (red, red) but is (red, blue) βΉ gets its own colour. The blues agree βΉ stay merged.
- Result β 3 states: , , .
- Key move: and have identical rows and the same Final status, so they merge β this is exactly how unreachable or duplicated states are absorbed.
β οΈ Common Mistakes
- π‘ Compare colour patterns, not destination names β two rows count as matching when they lead to the same colours, even if the named target states differ. This is what lets distinct states merge.
- π‘ Never merge a Final with a non-Final β that distinction is the seed of the whole algorithm and is preserved by every refinement.
- π‘ Re-examine every colour each round β a split can make a previously-agreeing colour disagree; keep iterating until a full pass adds nothing.
- π‘ Same colour is provisional β states sharing a colour are only βnot yet shown differentβ; only the final partition licenses merging.
π§ Active Recall
Why does the algorithm start by separating Final from non-Final states?
Answer
- Short answer: they are behaviourally distinguishable by the empty string: from a Final state is accepted, from a non-Final state it is not. Merging them would change the language, so they must carry different colours from the outset.
- Why: Seeding the partition β every later split is derived from this one: if two states send some letter into differently-coloured states, they too are distinguishable, and refinement propagates that difference backwards until the partition stabilises.
Two states have the same colour after the algorithm terminates. What does that license, and why?
Answer
- Short answer: they can be merged into a single state of the minimal DFA β the algorithm has stabilised, so no string distinguishes them: every input drives them through identically-coloured states and they agree on acceptance.
- Why: Fixpoint = indistinguishability β termination means no row-pattern disagreement remains, so the colours are exactly the equivalence classes of the βsame behaviour on all inputsβ relation, and merging each class yields the fewest possible states.