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

  1. Colour all Final states one colour; all non-Final states another.
  2. 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).
  3. Until no new colour is added in a full pass.
  4. 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:

Statecolour β†’ β†’
redredred
redredred
redbluered
bluebluered
bluebluered

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
Start1 (red)12
2 (green)31
Final3 (blue)31

πŸ–₯️ Implementing a finite automaton

Once minimised, the machine is trivial to run from its table:

  • Keep a currentState variable, initialised to the Start state.
  • Loop while input letters remain: read the next letter and set currentState to the table entry for (current state, that letter).
  • Finish by reporting a match if currentState is 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

⚠️ 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