Construct a DFA for the Regular Expression r=0∗1∗2∗r = 0^*1^*2^*

Construct a DFA for the Regular Expression r=0∗1∗2∗r = 0^*1^*2^*

Verified Sources
Nov 3, 2026

We want a DFA for the language denoted by the regular expression r=0∗1∗2∗r = 0^*1^*2^*, i.e., all strings over Σ={0,1,2}\Sigma=\{0,1,2\} 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 L=L(0∗1∗2∗)L = \mathcal{L}(0^*1^*2^*). Then

L={ 0i1j2k∣i,j,k≥0 }.L=\{\,0^i1^j2^k \mid i,j,k\ge 0\,\}.

Equivalently:

  • If you ever see a 11 after a 22, the string must be rejected (order violation).
  • If you ever see a 00 after a 11 or 22, the string must be rejected (order violation).
  • If you’re still in the “allowed region” (only 00s, then only 11s, then only 22s), 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 0∗1∗2∗0^*1^*2^*.

Footnotes

  1. 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 0∗0^*”
  • Phase 1: “now consuming 1∗1^*”
  • Phase 2: “now consuming 2∗2^*”
  • 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^*$

  1. 1
    Step 1

    Take Σ={0,1,2} \Sigma = \{0,1,2\} (if your course assumes a different alphabet, add any missing symbols to the dead-state transitions).

  2. 2
    Step 2

    Let Q={q0,q1,q2,qd}Q = \{q_0,q_1,q_2,q_d\} where: q0q_0 = phase for 0∗0^*, q1q_1 = phase for 1∗1^*, q2q_2 = phase for 2∗2^*, and qdq_d = dead/sink state.

  3. 3
    Step 3

    Set the start state s=q0s=q_0 (before reading anything, we are in the 0∗0^* block).

  4. 4
    Step 4

    The regex allows any i,j,k≥0i,j,k\ge 0, so strings can end after finishing any prefix phase: accept in q0,q1,q2q_0,q_1,q_2. Thus A={q0,q1,q2}A=\{q_0,q_1,q_2\}.

  5. 5
    Step 5

    From q0q_0: on input 00 stay in q0q_0; on input 11 move to q1q_1; on input 22 move to q2q_2 (because 1∗1^* can be empty).

  6. 6
    Step 6

    From q1q_1: on 11 stay in q1q_1; on 22 move to q2q_2 (since the 0∗0^* block is over); on 00 go to dead qdq_d (order violation).

  7. 7
    Step 7

    From q2q_2: on 22 stay in q2q_2; on input 00 or 11 go to dead qdq_d (since no symbols other than 22 are allowed after the 2∗2^* block starts).

  8. 8
    Step 8

    From dead qdq_d: on any input in Σ \Sigma stay in qdq_d.

  9. 9
    Step 9

    Report D=(Q,Σ,δ,s,A)D=(Q,\Sigma,\delta,s,A) with the transition table in the next block.

DFA definition (transition table)

Let D=(Q,Σ,δ,s,A)D=(Q,\Sigma,\delta,s,A) where:

  • Q={q0,q1,q2,qd}Q=\{q_0,q_1,q_2,q_d\}
  • Σ={0,1,2}\Sigma=\{0,1,2\}
  • s=q0s=q_0
  • A={q0,q1,q2}A=\{q_0,q_1,q_2\}
  • The transition function δ\delta is:
Stateon 0on 1on 2
q0q_0q0q_0q1q_1q2q_2
q1q_1qdq_dq1q_1q2q_2
q2q_2qdq_dqdq_dq2q_2
qdq_dqdq_dqdq_dqdq_d

This satisfies the DFA requirement that there is exactly one transition for each state-symbol pair.

Footnotes

  1. Definition of Deterministic Finite Automata (DFA definitions) - Defines DFA components and transition tables/diagrams. ↩

Visual state diagram (Mermaid)

Acceptance: inputs ending in q0,q1,q2q_0,q_1,q_2 are accepted; those ending in qdq_d are rejected.

type="tip" title="Pro Tip: interpret 0∗1∗2∗0^*1^*2^* as ordered blocks" content="Treat each regex factor (0∗0^*, then 1∗1^*, then 2∗2^*) 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 {0,1,2}\{0,1,2\}, 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 ww:

  • If the DFA is in q0q_0, then ww is of the form 0i0^i.
  • If the DFA is in q1q_1, then ww is of the form 0i1j0^i1^j.
  • If the DFA is in q2q_2, then ww is of the form 0i1j2k0^i1^j2^k.
  • If the DFA is in qdq_d, then ww contains an order violation (a 00 after a 11 or 22, or a 11 after a 22).

The transition rules preserve this invariant by construction:

  • Moving from q0q_0 to q1q_1 on 11 matches starting the 1∗1^* block.
  • Moving from q1q_1 to q2q_2 on 22 matches starting the 2∗2^* block.
  • Any backwards symbol forces qdq_d, 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

1

Recognize the pattern 0∗1∗2∗0^*1^*2^* as three consecutive phases."

Create one state per phase

2

Use q0,q1,q2q_0,q_1,q_2 for 0∗,1∗,2∗0^*,1^*,2^*."

Add a dead state for violations

3

All out-of-order inputs go to qdq_d."

Mark accepting phases

4

Accept 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

Question 1 of 4
Q1Single choice

In the DFA construction for 0∗1∗2∗0^*1^*2^*, which set of states should be accepting?