Construct a DFA for the Regular Expression
We want a DFA for the language denoted by the regular expression , i.e., all strings over that consist of some number of 0s, followed by some number of 1s, followed by some number of 2s, in that order (and with any of the three blocks possibly empty). This is a standard DFA-design pattern: keep track of which “phase” of the input you’re currently in, and once you move to a later phase you can never go back.
Key DFA notions used below: DFA , Transition function , Accepting state , Sink (dead) state.
RE to DFA (Thompson + Subset Construction) — background video
Language characterization
Let . Then
Equivalently:
- If you ever see a after a , the string must be rejected (order violation).
- If you ever see a after a or , the string must be rejected (order violation).
- If you’re still in the “allowed region” (only s, then only s, then only s), you accept at the end of the input.
This matches the general idea behind building automata from regular languages: regular expressions correspond to finite automata, and conversion techniques often go via NFA + subset construction, but here a direct DFA is simpler and minimal for the structure .
Footnotes
-
Regular Expressions (CS103 lecture PDF) - Explains conversions between regex, NFA, DFA; highlights equivalence. ↩
Direct DFA construction intuition (3 phases + dead state)
Use one state per phase:
- Phase 0: “still consuming ”
- Phase 1: “now consuming ”
- Phase 2: “now consuming ”
- Dead state: “order already violated”
A DFA must be deterministic, so for each phase state and each input symbol, define exactly one next state.
Build the DFA for $0^*1^*2^*$
- 1Step 1
Take (if your course assumes a different alphabet, add any missing symbols to the dead-state transitions).
- 2Step 2
Let where: = phase for , = phase for , = phase for , and = dead/sink state.
- 3Step 3
Set the start state (before reading anything, we are in the block).
- 4Step 4
The regex allows any , so strings can end after finishing any prefix phase: accept in . Thus .
- 5Step 5
From : on input stay in ; on input move to ; on input move to (because can be empty).
- 6Step 6
From : on stay in ; on move to (since the block is over); on go to dead (order violation).
- 7Step 7
From : on stay in ; on input or go to dead (since no symbols other than are allowed after the block starts).
- 8Step 8
From dead : on any input in stay in .
- 9Step 9
Report with the transition table in the next block.
DFA definition (transition table)
Let where:
- The transition function is:
| State | on 0 | on 1 | on 2 |
|---|---|---|---|
This satisfies the DFA requirement that there is exactly one transition for each state-symbol pair.
Footnotes
-
Definition of Deterministic Finite Automata (DFA definitions) - Defines DFA components and transition tables/diagrams. ↩
Visual state diagram (Mermaid)
Acceptance: inputs ending in are accepted; those ending in are rejected.
type="tip" title="Pro Tip: interpret as ordered blocks" content="Treat each regex factor (, then , then ) as a phase. Your DFA’s state should encode “which phase we are in,” and any symbol that would move you backwards in phase must go to the dead state."
type="warning" title="Beware the alphabet assumption" content="If your DFA’s alphabet is larger than , then every extra symbol must also transition to the dead state, since the regex restricts the string to only 0s, 1s, and 2s."
Correctness sketch (why this DFA matches the regex)
Define the invariant: after reading any prefix :
- If the DFA is in , then is of the form .
- If the DFA is in , then is of the form .
- If the DFA is in , then is of the form .
- If the DFA is in , then contains an order violation (a after a or , or a after a ).
The transition rules preserve this invariant by construction:
- Moving from to on matches starting the block.
- Moving from to on matches starting the block.
- Any backwards symbol forces , matching that the regex does not allow reordering. Finally, accepting states are exactly those phases where the input can legally end.
Conceptual workflow for this DFA
Parse the ordered blocks
1Recognize the pattern as three consecutive phases."
Create one state per phase
2Use for ."
Add a dead state for violations
3All out-of-order inputs go to ."
Mark accepting phases
4Accept in any phase where the input may legally end."
Phase transitions summary
Allowed next symbols from each phase state (others go to dead).
FAQ: common edge cases
Knowledge Check
In the DFA construction for , which set of states should be accepting?