Recall warm-up
- When does a DFA accept ε?
Stuck? Peek at 1.1.1 States and the DFA
- DFA = (Q, Σ, δ, q₀, F), δ : Q × Σ → Q total. One step per symbol; always halts.
- Accept iff the last state ∈ F. ε accepted ⇔ q₀ ∈ F.
- Dead state: non-accepting, all self-loops. GATE counts it in minimal-DFA questions.
- Exactly one string of length k: k + 2 states. #DFAs (n states, m symbols, fixed start) = nⁿᵐ · 2ⁿ.
- How many DFAs with 2 states over {a,b} with fixed start?
Stuck? Peek at 1.1.1 States and the DFA
- DFA = (Q, Σ, δ, q₀, F), δ : Q × Σ → Q total. One step per symbol; always halts.
- Accept iff the last state ∈ F. ε accepted ⇔ q₀ ∈ F.
- Dead state: non-accepting, all self-loops. GATE counts it in minimal-DFA questions.
- Exactly one string of length k: k + 2 states. #DFAs (n states, m symbols, fixed start) = nⁿᵐ · 2ⁿ.
- If 11 numbers are divided by 5, must two share a remainder?
Stuck? Peek at 0.2.4 Pigeonhole principle
- n + 1 objects in n boxes ⇒ some box has ≥ 2.
- N objects in k boxes ⇒ some box has ≥ ⌈N/k⌉. To force m in a box: (m−1)k + 1 objects.
- Machine with n modes, input length ≥ n ⇒ some mode repeats ⇒ a loop in the input.
The Hook
GATE rarely asks you to invent a strange DFA. It asks: “how many states does the minimal DFA for this kind of language have?” — starts with, ends with, contains, length conditions, counts mod n, divisibility. Each kind has a standard shape and a state-count formula. Learn the shapes once and these questions take under a minute.
Predict First
Over {a,b}: how many states does the minimal DFA need for “strings that contain aba”? And for “strings that start with aba”?
Core Concept
Prediction check
Contains aba: 4 (progress 0,1,2,3 matched letters; the last state accepts forever — no dead state needed). Starts with aba: 5 (progress 0..3 plus a dead state for a wrong start). The dead state is the difference.
Pattern 1 — starts with w (|w| = k)
States: progress 0, 1, …, k (k is accepting and loops on everything) plus a dead state for any mismatch. k + 2 states.
States: q0, q1, q2, q3, D
Transitions: q0>q1: a; q0>D: b; q1>q2: b; q1>D: a; q2>q3: a; q2>D: b; q3>q3: a,b; D>D: a,b
Pattern 2 — contains w as a substring
States: “longest prefix of w matched so far” 0, …, k. On a mismatch you don’t restart from 0 blindly: you move to the longest prefix of w that is a suffix of what you just read (contains aba: in state 2 = “ab”, reading b gives “abb”, whose longest suffix that is a prefix of aba is ε → state 0; reading a goes to 3). State k accepts and loops. k + 1 states. No dead state.
| state (matched so far) | on a | on b |
|---|---|---|
| q0 (ε) | q1 | q0 |
| q1 (a) | q1 (aa → keep “a”) | q2 |
| q2 (ab) | q3 ✓ | q0 (abb → nothing) |
| q3 (aba found) | q3 | q3 |
Pattern 3 — ends with w
Same “longest prefix of w that is a suffix of the input” states 0, …, k, but state k does not loop — further symbols can leave it. k + 1 states. (Ends with 01: 3 states, as in 1.1.2.)
States: q0, q1, q2, q3
Transitions: q0>q0: b; q0>q1: a; q1>q1: a; q1>q2: b; q2>q3: a; q2>q0: b; q3>q1: a; q3>q2: b
Pattern 4 — length conditions
- |w| ≡ r (mod n): a cycle of n states, one per remainder.
- |w| = k exactly: k + 2 (counter 0..k, plus “too long” dead).
- |w| ≤ k: k + 2 (0..k accepting, then dead).
- |w| ≥ k: k + 1 (0..k, last one accepting with self-loop).
States: r0, r1, r2
Transitions: r0>r1: a,b; r1>r2: a,b; r2>r0: a,b
Pattern 5 — counting symbols mod m and n
“#a ≡ i (mod m) and #b ≡ j (mod n)”: a grid of states (#a mod m, #b mod n) → m · n states. The same grid works for “or”, “not”, any combination — only F changes. Minimal for most conditions (a condition that ignores one coordinate collapses it).
| #b ≡ 0 | #b ≡ 1 | #b ≡ 2 | |
|---|---|---|---|
| #a even | e0 ✓ | e1 | e2 |
| #a odd | o0 | o1 | o2 |
Pattern 6 — divisibility of numbers
Read a binary number left to right. If the value so far is v, reading bit b gives 2v + b. Track only r = v mod k: new r = (2r + b) mod k. So k states, start and accept at r = 0. For decimal: new r = (10r + d) mod k.
k states always suffice; the minimal number can be smaller when k shares factors with the base. Binary, divisible by 2: 2 states; by 4: 3 states (it is just “ends with 00”); by 8: 4 states; by 6: 4 states. For odd k in binary, all k remainder states are needed.
| read | 1 | 1 | 0 | 1 |
|---|---|---|---|---|
| value so far | 1 | 3 | 6 | 13 |
| remainder mod 3 | 1 | 0 | 0 | 1 |
Pattern 7 — the k-th symbol from the start
“3rd symbol is 1”: skip 2 symbols (states 0,1,2), then 1 → accept-forever, 0 → dead. k + 2 states. (The k-th symbol from the end is a very different story — Chapter 1.2.)
States: q0, q1, q2, A, D
Transitions: q0>q1: 0,1; q1>q2: 0,1; q2>A: 1; q2>D: 0; A>A: 0,1; D>D: 0,1
Putting it together
- Starts-with: k + 2. Contains / ends-with: k + 1, with fall-back moves.
- Length mod n: n states. Two counters mod m and n: an m × n grid.
- Binary ÷ k: r ← (2r + b) mod k; minimal m + j for k = 2ʲ·m.
Visual
States: q0, q1, q2, q3
Transitions: q0>q0: b; q0>q1: a; q1>q1: a; q1>q2: b; q2>q3: a; q2>q0: b; q3>q3: a,b
contains aba — failure moves explained
state = longest prefix of "aba" that is a suffix of what was read
q0 "" a→q1 "a" b→q0
q1 "a" a→q1 "a" (aa: suffix "a") b→q2 "ab"
q2 "ab" a→q3 "aba" ✓ b→q0 (abb: no suffix is a prefix of aba)
q3 a,b→q3 (already found)
traces: "babab": q0 b q0 a q1 b q2 a q3 b q3 ACCEPT
"abba" : q0 a q1 b q2 b q0 a q1 REJECT
States: r0, r1, r2
Transitions: r0>r0: 0; r0>r1: 1; r1>r0: 1; r1>r2: 0; r2>r1: 0; r2>r2: 1
divisible by 3: r' = (2r + b) mod 3 trace 110 (=6): r0 ─1→ r1 ─1→ r0 ─0→ r0 ACCEPT
0 1 trace 111 (=7): r0 ─1→ r1 ─1→ r0 ─1→ r1 REJECT
→*r0 r0 r1
r1 r2 r0
r2 r1 r2 (ε stays in r0 and is accepted — by convention ε has value 0)
States: e0, e1, e2, o0, o1, o2
Transitions: e0>o0: a; o0>e0: a; e1>o1: a; o1>e1: a; e2>o2: a; o2>e2: a; e0>e1: b; e1>e2: b; e2>e0: b; o0>o1: b; o1>o2: b; o2>o0: b
Worked Examples
Minimal DFA states for strings over {a,b} whose length is divisible by 4?
Show the full solution
A 4-cycle of remainders: 4. All four are needed (remainder r needs 4−r more symbols to reach acceptance — different for each r).
Minimal DFA for strings over {0,1} that end with 00. Give the table and trace one accepted and one rejected string.
Show the full solution
States = longest suffix matching a prefix of 00: A (none), B (“0”), C (“00”, accept). A: 0→B, 1→A. B: 0→C, 1→A. C: 0→C, 1→A. 3 states. Trace 1100: A→A→A→B→C accept. 0010: A→B→C→A→B reject. (Note C on 0 stays C — “000” still ends with 00.)
Minimal DFA states over {a,b} for: (i) #a ≡ 0 (mod 3) and #b ≡ 0 (mod 2); (ii) #a ≡ 0 (mod 3) or #b ≡ 0 (mod 2); (iii) the third symbol from the start is b; (iv) |w| ≥ 3 and w starts with a.
Show the full solution
(i) grid 3·2 = 6. (ii) same grid, different F: still 6 (all cells distinguishable: from each cell, the set of “b-counts and a-counts to add” that lead to acceptance differs). (iii) states 0,1,2 skipping, accept-forever, dead: 5. (iv) start; after “a”: need ≥ 2 more (counter states “a”, “a?”, then accept-forever “a??+”), dead for b first: start, 1, 2, 3(accept loop), dead = 5.
GATE Traps
- “Starts with” needs a dead state (k+2); “contains” and “ends with” do not (k+1).
- In “ends with” and “contains”, a mismatch does not always send you back to the start — use the longest suffix that is still a prefix.
- Binary divisibility by k: k states is an upper bound; for even k the minimal DFA is smaller (÷4 → 3 states).
- “Exactly k” and “at most k” length conditions both need a dead state.
Memory Hook
Starts-with locks the door behind a wrong first step (dead state). Contains and ends-with just slide back to the best partial match.
Connect the Dots
We now design single DFAs fast. But GATE often combines conditions: “even a’s AND ends with b”, “starts with a OR has odd length”. Next lesson builds DFAs for combinations automatically, by running two DFAs side by side — and flips a DFA to get its complement.
Self-Test
- NAT
Minimal DFA states over {a,b} for strings that start with
abb?Write your answer before opening the key.
- NAT
Minimal DFA states over {a,b} for strings that end with
abb?Write your answer before opening the key.
- NAT
Minimal DFA states for binary numbers divisible by 5?
Write your answer before opening the key.
- MSQ
Which languages over {a,b} have a minimal DFA with exactly 3 states?
- |w| ≡ 0 (mod 3)
- w ends with ab
- |w| = 1
- w contains bb
- MCQ
Minimal DFA states for “#a ≡ 1 (mod 4) and #b ≡ 2 (mod 3)” over {a,b}:
- 7
- 12
- 4
- 3
Rule Card
| Language (|w| = k pattern) | Minimal states |
|---|---|
| starts with w | k + 2 |
| contains w / ends with w | k + 1 |
| |x| ≡ r mod n | n |
| |x| = k, |x| ≤ k | k + 2 |
| |x| ≥ k | k + 1 |
| #a mod m and/or #b mod n | m·n (typical) |
| binary value ≡ 0 mod k, k odd | k |
| binary ÷ 2ʲ | j + 1 |
| k-th symbol from start fixed | k + 2 |
Divisibility update: r ← (base·r + digit) mod k.
Concept Ledger
- DFA design patterns
- failure transition (longest suffix that is a prefix)
- remainder states (mod n)
- grid / counter DFA (m·n states)
- divisibility DFA r ← (2r+b) mod k
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- When q₀ ∈ F.
- 64.
- Yes — 5 remainders, 11 numbers.
Self-test answers
- 5 — k + 2 = 3 + 2.
- 4 — k + 1.
- 5 — 5 is odd, so all 5 remainders are distinguishable.
- ABCD — A: 3 remainders. B: k+1 = 3. C: k+2 = 3. D: k+1 = 3.
- B — Grid 4 × 3.
- A: Adding instead of multiplying.
- C: Ignores b.
- D: Ignores a.
Flashcards
- Minimal states: strings starting with a fixed w, |w| = k?
Reveal answer
k + 2 - Minimal states: strings containing w (|w| = k)?
Reveal answer
k + 1 - Minimal states: binary numbers divisible by 4?
Reveal answer
3 (it is “ends with 00”) - Divisibility-by-k DFA transition for binary?
Reveal answer
r ← (2r + b) mod k - #a mod m and #b mod n — states?
Reveal answer
m · n
Exam shortcuts
- Binary divisible by k = 2ʲ·m (m odd): the minimal DFA has m + j states. ÷6 → 3 + 1 = 4, ÷12 → 3 + 2 = 5, ÷8 → 1 + 3 = 4.
- For “contains w” the accept state is a sink; for “ends with w” it is not — that single difference is everything.