Back to the 2024 paper

Module 1: Introduction & Regular Languages

20247m

Construct DFA for all strings over {0,1}\{0,1\} interpreted as binary numbers divisible by 44.

Worked SolutionAI Assisted

Answer

Language: L={w∈{0,1}∗∣value2(w)≡0(mod4)}L = \{ w \in \{0,1\}^* \mid \text{value}_2(w) \equiv 0 \pmod 4 \}

Interpret string as binary number (most significant bit first), check divisibility by 4.


Key Observation

A binary number is divisible by 4 iff its last two bits are 00 (or the string is just "0" or empty, treated as 0).

  • Binary x≡0(mod4)  ⟺  xx \equiv 0 \pmod 4 \iff x ends in 00.
  • Special case: empty string ϵ\epsilon and "0" represent 0, divisible by 4.

DFA Construction (4 states)

Track value modulo 4. States = residues {0,1,2,3}\{0,1,2,3\}.

DFA: M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F)

  • Q={q0,q1,q2,q3}Q = \{q_0, q_1, q_2, q_3\} where qrq_r = value ≡r(mod4)\equiv r \pmod 4
  • Σ={0,1}\Sigma = \{0,1\}
  • q0q_0 = start (value 0)
  • F={q0}F = \{q_0\} (residue 0)

Transition: On input bit b∈{0,1}b \in \{0,1\}:

δ(qr,b)=q(2r+b) mod 4\delta(q_r, b) = q_{(2r + b) \bmod 4}

Transition Table:

State \ Input 0 1
q0q_0 (0) q0q_0 q1q_1
q1q_1 (1) q2q_2 q3q_3
q2q_2 (2) q0q_0 q1q_1
q3q_3 (3) q2q_2 q3q_3

Derivation:

  • 2×0+0=0→q02 \times 0 + 0 = 0 \to q_0
  • 2×0+1=1→q12 \times 0 + 1 = 1 \to q_1
  • 2×1+0=2→q22 \times 1 + 0 = 2 \to q_2
  • 2×1+1=3→q32 \times 1 + 1 = 3 \to q_3
  • 2×2+0=4≡0→q02 \times 2 + 0 = 4 \equiv 0 \to q_0
  • 2×2+1=5≡1→q12 \times 2 + 1 = 5 \equiv 1 \to q_1
  • 2×3+0=6≡2→q22 \times 3 + 0 = 6 \equiv 2 \to q_2
  • 2×3+1=7≡3→q32 \times 3 + 1 = 7 \equiv 3 \to q_3

State Diagram

MermaidDiagram
Rendering diagram…

Simplified 3-State DFA (Using Last-Two-Bits Insight)

Since divisibility by 4 depends only on last 2 bits = 00:

DFA with 3 states:

  • q0q_0: start, also accept (0 or empty or ...00)
  • q1q_1: last bit = 1
  • q2q_2: last bit = 0, previous ≠ 0 (i.e., ends in 10)
State \ Input 0 1
q0q_0 (accept) q0q_0 q1q_1
q1q_1 q2q_2 q1q_1
q2q_2 q0q_0 q1q_1

F={q0}F = \{q_0\}. q0q_0 = last two bits 00 (or empty/"0").

Why 3 states minimal: Myhill-Nerode: ϵ,1,10\epsilon, 1, 10 are pairwise distinguishable mod 4.


Test Cases

Input Value Path Accept?
ϵ\epsilon 0 q0q_0 Yes
0 0 q0→0q0q_0 \xrightarrow{0} q_0 Yes
100 4 q0→q1→0q2→0q0q_0 \to q_1 \xrightarrow{0} q_2 \xrightarrow{0} q_0 Yes
1000 8 q0→q1→q2→q0→q0q_0 \to q_1 \to q_2 \to q_0 \to q_0 Yes
10 2 q0→q1→0q2q_0 \to q_1 \xrightarrow{0} q_2 No ($q_2
otin F$)
101 5 q0→q1→0q2→1q1q_0 \to q_1 \xrightarrow{0} q_2 \xrightarrow{1} q_1 No
1100 12 ends in q0q_0 Yes

Regular Expression

(0+1)∗00+0(0+1)^*00 + 0

(All strings ending in 00 plus the single 0; ϵ\epsilon can be included as (0+1)*00 | 0 | \epsilon)


Final Answer

DFA with 4 states (mod 4 tracking) or minimal 3 states:

  • States: residues 0,1,2,3 (or 3-state: 00, ends-in-1, ends-in-10)
  • Transitions: δ(qr,b)=q(2r+b) mod 4\delta(q_r, b) = q_{(2r+b)\bmod 4}
  • Accept: q0q_0 only
  • Binary divisible by 4 ⇔ ends in 00 (or is 0/ε)
DFA(3 states):
q0 --0--> q0
q0 --1--> q1
q1 --0--> q2
q1 --1--> q1
q2 --0--> q0
q2 --1--> q1
Accept: q0

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.