Back to the 2021 paper

Module 1: Introduction & Regular Languages

20217m

Construct an NFA accepting strings over {0,1,2}\{0,1,2\} beginning with 0, followed by an odd number of 1s, ending with any number of 2s.

Worked SolutionAI Assisted

Answer

Language: L={0 12k+1 2∗∣k≥0}={01 2∗, 0111 2∗, 011111 2∗,… }L = \{ 0\, 1^{2k+1}\, 2^* \mid k \ge 0 \} = \{ 01\, 2^*,\, 0111\, 2^*,\, 011111\, 2^*, \dots \}

Structure: starts with 0 → odd number of 1's → any number of 2's.


NFA Design

NFA: N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F)

  • Σ={0,1,2}\Sigma = \{0,1,2\}
  • Q={q0,q1,q2,q3}Q = \{q_0, q_1, q_2, q_3\}
  • q0q_0 = start
  • F={q3}F = \{q_3\} (final)

States meaning:

  • q0q_0: initial, expecting leading 0
  • q1q_1: saw 0, read first 1 (odd count = 1)
  • q2q_2: in even number of 1's (after 2nd, 4th, ...)
  • q3q_3: in odd number of 1's (after 1st, 3rd, ...) — can transition to 2's

Transition Function δ\delta

State Input Next State(s)
q0q_0 00 {q1}\{q_1\}
q1q_1 11 {q2}\{q_2\}
q2q_2 11 {q1}\{q_1\}
q1q_1 22 {q3}\{q_3\}
q3q_3 22 {q3}\{q_3\}

No other transitions (missing transitions = dead/implicit reject).


State Diagram

       0
   ┌───────┐
   │       ▼
 (q0)──►(q1)──1──►(q2)
   │    ▲   │    │
   │    │   └────1
   │    2     │
   ▼    │     │
  (q3)◄──2─────┘
    2

How It Works

Input Path Accept?
01 q0→0q1→1q2q_0 \xrightarrow{0} q_1 \xrightarrow{1} q_2 → no (ends in q2q_2, even 1's)
011 q0→q1→1q2→1q1q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 → yes (q1→2q3∈Fq_1 \xrightarrow{2} q_3 \in F)
0111 q0→q1→1q2→1q1→1q2q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 \xrightarrow{1} q_2 → no (even)
012 q0→q1→1q2→2q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{2} → no (q2q_2 has no 2-transition)
0112 q0→q1→1q2→1q1→2q3q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 \xrightarrow{2} q_3 → yes
01112 q0→q1→1q2→1q1→1q2→2q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 \xrightarrow{1} q_2 \xrightarrow{2} → no
01122 q0→q1→1q2→1q1→2q3→2q3q_0 \to q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 \xrightarrow{2} q_3 \xrightarrow{2} q_3 → yes
0 q0→0q1q_0 \xrightarrow{0} q_1 → no (no 1's, count=0 even)

Equivalent DFA (via Subset Construction)

States = subsets of NFA states:

  • A={q0}A = \{q_0\}
  • B={q1}B = \{q_1\}
  • C={q2}C = \{q_2\}
  • D={q1,q3}D = \{q_1, q_3\}
  • E={q3}E = \{q_3\} (accepting)
  • Dead = ∅\emptyset

But NFA is simpler and sufficient.


Regular Expression

0(11)∗1 2∗=0 (11)∗ 1 2∗0(11)^*1\,2^* = 0\,(11)^*\,1\,2^*

Verification:

  • 01 = 0⋅(11)0⋅1⋅200 \cdot (11)^0 \cdot 1 \cdot 2^0 ✓
  • 0111 = 0⋅(11)1⋅10 \cdot (11)^1 \cdot 1 ✓
  • 011122 = 0⋅(11)1⋅1⋅220 \cdot (11)^1 \cdot 1 \cdot 2^2 ✓

Final Answer

NFA with 4 states (q0,q1,q2,q3q_0,q_1,q_2,q_3):

  • q0→0q1q_0 \xrightarrow{0} q_1 (leading 0)
  • q1→1q2→1q1q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_1 (toggle odd/even 1's)
  • q1→2q3q_1 \xrightarrow{2} q_3 (odd 1's → can start 2's)
  • q3→2q3q_3 \xrightarrow{2} q_3 (any number of 2's)
  • Final = {q3}\{q_3\}
  • Accepts 0(11)∗12∗0(11)^*1 2^* exactly.

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.