G27
Self-checkedTier 1 11 min readtoc.dfa-design-patterns

DFA design patterns and counting states

How to design a DFA by deciding what must be remembered, and how many states the result needs: counting modulo k, remembering the last symbols (ends with, contains, starts with), length conditions, remainders of binary numbers, combining machines by product (intersection, union), and complement; with the exact state counts GATE asks for, each computed by minimisation and confirmed by counting Myhill-Nerode classes.

Why it exists

Designing a DFA is the creative half of the subject and the half GATE tests most: give a DFA for this language, or how many states does the minimum DFA for this language have? There is no algorithm for the first (you must see what the machine has to remember), but a handful of patterns cover almost every exam question. This page lists them, with the number of states each needs, and says how to confirm that the number cannot be lowered.

Prerequisites

You need the DFA and its extended transition function (DFA).

Precise definitions

Two strings are distinguishable (with respect to a language LL) if some continuation zz is accepted after one and rejected after the other: xz∈Lxz \in L but yz∉Lyz \notin L. In a DFA, distinguishable strings must lead to different states. If nn strings are pairwise distinguishable, every DFA for LL needs at least nn states. Conversely, if every state can be matched to a set of indistinguishable strings, the DFA is minimal. The classes of indistinguishable strings are the Myhill-Nerode classes; their number is the number of states of the minimal complete DFA.

Minimal DFA. A complete DFA with the fewest states among all complete DFAs for the language (it is unique up to renaming; see minimisation).

Product of two DFAs. For DFAs M1=(Q1,Σ,δ1,s1,F1)M_1 = (Q_1, \Sigma, \delta_1, s_1, F_1) and M2=(Q2,Σ,δ2,s2,F2)M_2 = (Q_2, \Sigma, \delta_2, s_2, F_2) the product has states Q1×Q2Q_1 \times Q_2, start (s1,s2)(s_1, s_2) and δ((p,q),a)=(δ1(p,a),δ2(q,a))\delta((p, q), a) = (\delta_1(p, a), \delta_2(q, a)); it runs both machines in parallel. Choosing the accepting pairs as F1×F2F_1 \times F_2 gives the intersection, (F1×Q2)∪(Q1×F2)(F_1 \times Q_2) \cup (Q_1 \times F_2) the union, F1×(Q2∖F2)F_1 \times (Q_2 \setminus F_2) the difference.

How it works

The method

  1. Decide what the machine must remember about the part of the string read so far (a number modulo kk, the last few symbols, which flags have been seen).
  2. Give each different memory content one state, and write the transitions by asking "if I remember this and read aa, what must I remember now?".
  3. Mark as accepting the states whose memory content makes the string acceptable.
  4. Check short strings including ε\varepsilon, and ask whether two states could be merged (no string distinguishes them).

The patterns and their sizes

The table lists the machines of the patterns below. "States of the DFA built" is the size of the machine written down (complete, only reachable states); "states of the minimal DFA" is what minimisation leaves; here they agree because each machine was designed to remember exactly what is needed.

How many states does a DFA for the language need?: start

Computed by a script, not typed
languagestates of the DFA builtstates of the minimal DFAwhy
ends with 01 (over 0, 1)
ends with 010 (over 0, 1)
contains aba
contains aaa
even number of a's
number of a's divisible by 3
length divisible by 3
exactly two a's
at least two a's
binary number divisible by 3
odd number of a's AND number of b's divisible by 3
a*b* (all a's before all b's)

Step 0

Each language over {a, b} (or {0, 1} where stated) with a DFA that remembers exactly what is needed, and the number of states of the smallest complete DFA, found by minimising the machine.

0 / 12
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Counting modulo kk (one counter). "The number of aas is ≡r(modk)\equiv r \pmod k": kk states c0,…,ck−1c_0, \ldots, c_{k-1}, the symbol aa moves ci→ci+1 mod kc_i \to c_{i+1 \bmod k}, other symbols stay. Length ≡r(modk)\equiv r \pmod k: the same with every symbol counting. Needs exactly kk states.

Two counters. For the number of aas modulo kk and the number of bbs modulo mm: the product, k⋅mk \cdot m states (6 for modulo 22 and modulo 33, as in the table). Counters on the same symbol combine to lcm⁡(k,m)\operatorname{lcm}(k, m) states: "∣w∣a|w|_a even and ∣w∣a|w|_a divisible by 33" is "divisible by 66", 66 states, and "divisible by 22 and by 44" is "divisible by 44": 44 states, not 88.

Suffix patterns (ends with ww). The state remembers the longest suffix of the input that is a prefix of ww: ∣w∣+1|w| + 1 states (q0,…,q∣w∣q_0, \ldots, q_{|w|}). On a mismatch the machine does not return to q0q_0 but to the longest prefix of ww that is still a suffix of the new text (the idea of the Knuth-Morris-Pratt algorithm); for w=01w = 01 and q2q_2 reading 00 it goes to q1q_1 (the new 00 may start another 0101).

Substring patterns (contains ww). The same machine with an absorbing accepting state at the end: ∣w∣+1|w| + 1 states. (Contains abaaba: 44.)

Prefix patterns (starts with ww). The machine must also reject once the beginning is wrong, so it needs a dead state: ∣w∣+2|w| + 2 states. (Starts with abab: 44: start, "read aa", accepting, dead.)

Length conditions. Exactly nn symbols: n+2n + 2 states (lengths 0..n0..n plus a dead state). At most nn symbols: also n+2n + 2. At least nn symbols: n+1n + 1 (no dead state; the last state is absorbing and accepting). Any finite language whose longest string has length nn needs at least n+2n + 2 states.

Occurrence counts. Exactly kk symbols aa: k+2k + 2 states (counts 0..k0..k and a dead state). At most kk aas: k+2k + 2 states. At least kk aas: k+1k + 1 states. "At least one aa and at least one bb": 44 states (neither, only aa, only bb, both).

Binary numbers divisible by kk. Keep the remainder rr; reading the bit bb gives (2r+b) mod k(2r + b) \bmod k. This gives kk states, and for odd kk that is minimal (k=3k = 3: 33; k=5k = 5: 55; k=7k = 7: 77). For even k=2jmk = 2^j m with mm odd, fewer are needed: the minimal DFA has m+jm + j states (for k=4k = 4: 33; k=6k = 6: 44; k=8k = 8: 44; k=10k = 10: 66; confirmed by minimisation for kk up to 1616).

Combining machines

  • Intersection and union ("∣w∣a|w|_a even and ends with bb", "...or ..."): product construction. The number of states is at most ∣Q1∣⋅∣Q2∣|Q_1| \cdot |Q_2| and may be smaller after minimisation.
  • Complement: swap accepting and non-accepting states of a complete DFA; the number of states stays the same. (A language and its complement need the same number of DFA states, confirmed for nine of the languages above.)
  • Difference L1∖L2=L1∩L2‾L_1 \setminus L_2 = L_1 \cap \overline{L_2}.

When a DFA is not enough

A DFA has finitely many states, so it cannot remember an unbounded count (anbna^n b^n) or compare two arbitrarily long pieces (wwww). The languages for which the minimal DFA is much larger than an NFA are those that need to remember a position far back: the nn-th symbol from the end needs 2n2^n DFA states against n+1n + 1 NFA states (subset construction).

An example in detail: contains abaaba

Four states: s0s_0 (no progress), s1s_1 (last symbol is the aa that could start abaaba), s2s_2 (have abab), s3s_3 (found, accepting, absorbing). From s1s_1 reading aa stays in s1s_1 (the new aa may start the pattern); from s2s_2 reading bb goes to s0s_0 (the text ends in abbabb: no progress), reading aa goes to s3s_3. The run on bbababbbabab:

DFA for the strings that contain the substring aba: start

Simulated, not typed
baabbaa,bs0s1s2s3

Double circle = accepting state. Arrow from nowhere = start state.

inputbbabab
state{s0}

Step 0

Start in the start state s0. The machine reads the input from left to right, one symbol at a time, and every symbol moves it along exactly one edge.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

Worked examples

EX 1Strings ending in ab

Easy

Over {a,b}\{a, b\}: three states q0q_0 (nothing useful), q1q_1 (ends with aa), q2q_2 (ends with abab, accepting). δ(q0,a)=q1\delta(q_0, a) = q_1, δ(q0,b)=q0\delta(q_0, b) = q_0, δ(q1,a)=q1\delta(q_1, a) = q_1, δ(q1,b)=q2\delta(q_1, b) = q_2, δ(q2,a)=q1\delta(q_2, a) = q_1, δ(q2,b)=q0\delta(q_2, b) = q_0. Minimum number of states: 3=∣ab∣+13 = |ab| + 1.

EX 2Counting the states for a length condition

Easy

Binary strings of length exactly 33: states for lengths 0,1,2,30, 1, 2, 3 (the last accepting) and a dead state: 55. Strings of length at most 33: the same 55 states with the first four accepting. Strings of length at least 33: 44 states (no dead state needed).

EX 3Product of two counters

Medium

Strings over {a,b}\{a, b\} with an even number of aas and an even number of bbs: product of two 22-state machines, 44 states (even,even)(\text{even}, \text{even}), (odd,even)(\text{odd}, \text{even}), (even,odd)(\text{even}, \text{odd}), (odd,odd)(\text{odd}, \text{odd}); accepting: (even,even)(\text{even}, \text{even}). All four are distinguishable (for every pair of the strings ε\varepsilon, aa, bb, abab some continuation is accepted after one and rejected after the other), so 44 is minimal. For "even number of aas or even number of bbs" the same product with three accepting states has again 44 states.

EX 4Lcm of counters

Medium

"The number of aas is divisible by 22 and by 33" (strings over {a,b}\{a, b\}): the product would have 66 states, and indeed the language equals "divisible by 66", which needs 66. "Divisible by 22 and by 44" equals "divisible by 44": 44 states although the product has 88. A question asking for the minimum number of states wants the minimised size, not the size of the product.

EX 5Typical questions

GATE level

(a) Minimum states of a DFA for strings over {0,1}\{0, 1\} ending in 0000: 33. (b) Binary numbers divisible by 55: 55; by 44: 33 (the value modulo 44 is 00, 22, or odd, and the remainder 11 and 33 behave alike). (c) Strings over {a,b}\{a, b\} in which every aa is followed immediately by bb: 33 (ok so far, just read an aa, dead). (d) Strings of length divisible by 33 over {a,b}\{a, b\}: 33. (e) Language of strings that do not contain aaaa: 33 (last symbol not aa, last symbol aa, dead). (f) The complement of the language in (e): also 33. (g) Number of states in the product for kk-counter and mm-counter on different symbols: kmkm.

Cheat box

Cheat box

  • Minimum states == number of Myhill-Nerode classes. nn pairwise distinguishable strings ⇒\Rightarrow at least nn states.
  • Ends with ww: ∣w∣+1|w| + 1. Contains ww: ∣w∣+1|w| + 1. Starts with ww: ∣w∣+2|w| + 2. Mismatch goes back to the longest prefix of ww that is still a suffix.
  • Length exactly or at most nn: n+2n + 2; at least nn: n+1n + 1. Exactly kk symbols aa (or at most): k+2k + 2; at least: k+1k + 1.
  • Count modulo kk: kk states. Counters on different symbols: product kmk m. Same symbol: lcm⁡(k,m)\operatorname{lcm}(k, m).
  • Binary divisible by k=2jmk = 2^j m (mm odd): m+jm + j states (kk itself for odd kk).
  • Complement: same number of states (complete DFA). Union, intersection: product, at most ∣Q1∣∣Q2∣|Q_1||Q_2|.
  • The nn-th symbol from the end: 2n2^n DFA states, n+1n + 1 NFA states.

Traps and GATE patterns

Connections

The machines here are DFAs (DFA); the question "which states can be merged" is answered by minimisation; the product construction proves that the regular languages are closed under union and intersection (closure properties); languages that need exponentially more states than an NFA are the subject of the subset construction.

PYQs

6 previous year questions from your book. I re-solved each one myself (6 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2025 · Set 2 · NATQ11 · page 198 of the book
Let Σ = {1,2,3,4}. For x ∈ Σ* , let prod(x) be the product of
symbols in x modulo 7. We take prod(ε) =1,where ε is the
null string .
For example, prod(124) = (1 × 2 × 4) mod 7 = 1.
Define L = {x ∈ Σ* | prod(x) = 2}.
The number of states in a minimum state DFA for L is
___________. (Answer in integer)
GATE 2023 · NAT · 2 marksQ19 · page 199 of the book
Consider the language L over the alphabet {0, 1}, given
below: L = {w ∈ {0, 1}* | w does not contain three or more
consecutive 1's}. The minimum number of states in a
Deterministic Finite-State Automaton (DFA) for L is _____.

THEORY OF COMPUTATION
GATE 2020 · NAT · 2 marksQ22 · page 200 of the book
Consider the following language.
L = {x ε {a,b}* | number of a's in x is divisible by 2 but
not divisible by 3}. The minimum number of states in a
DFA that accepts L is _______.
GATE 2012 · Set 1 · MCQ · 2 marksQ51 · page 203 of the book
Consider the set of strings on {0,1} in which, every
substring of 3 symbols has at most two zeros. For example,
001110 and 011001 are in the language, but 100010 is not.
All strings of length less than 3 are also in the language. A
partially completed DFA that accepts this language is
shown below.

THEORY OF COMPUTATION

The missing arcs in the DFA are:
  • (a).
  • (b).
  • (c).
  • (d).
GATE 2008 · MCQQ61 · page 206 of the book
Given below are two finite state automata (-> indicates
the start state and F indicates a final state)
Automaton Y

a
b
→1
1
2
2(F)
2
1

Automaton Z

a
b
→1
2
2
2(F)
1
1
Which of the following represents the product
automaton Z × Y?
  • (a). a b →p S R Q R S R(F) Q P S Q P
  • (b)a b →p S Q Q R S R(F) Q P S P Q
  • (c)a b →p Q S Q R S R(F) Q P S Q P
  • (d)a b →p S Q Q S R R(F) Q P S Q P THEORY OF COMPUTATION
GATE 2007 · MCQ · 2 marksQ67 · page 208 of the book
Consider the following DFA in which S0 is the start
state and S1,S3 are the final states.

What language does this DFA recognize?
  • (a)All strings of x and y
  • (b)All strings of x and y which have either even number of x and even number of y or odd number of x and odd number of y
  • (c)All strings of x and y which have equal number of x and y
  • (d)All strings of x and y with either even number of x and odd number of y or odd number of x and even number of y

Self-check

Question 1

How many states does the minimum DFA have for the binary strings that contain 101101?

Show answer

44 (∣101∣+1|101| + 1).

Question 2

How many states does the minimum DFA have for the strings over {a,b}\{a, b\} that start with aa?

Show answer

33: start, accepting (started with aa), dead (∣a∣+2|a| + 2).

Question 3

Why does the language "∣w∣a|w|_a divisible by 22 and by 44" need only 44 states?

Show answer

It is the same as "divisible by 44": a count that is divisible by 44 is divisible by 22.

Question 4

How many states does a minimal DFA for the binary numbers divisible by 88 have?

Show answer

44 (8=23⋅18 = 2^3 \cdot 1, so m+j=1+3m + j = 1 + 3): the state counts the trailing 00s of the string, up to 33 (odd; one; two; three or more).

Revision card

One-screen revision

  • Design by deciding what to remember; states = Myhill-Nerode classes.
  • ends/contains ww: ∣w∣+1|w| + 1; starts with ww: ∣w∣+2|w| + 2; length nn exactly/at most: n+2n + 2; at least: n+1n + 1.
  • Counters: kk; product kmkm; same symbol lcm. Binary divisible by 2jm2^j m: m+jm + j.
  • Complement same size; union/intersection by product.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.1 (examples of finite automata) and Section 1.2 (closure under union)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 2.2 and Section 4.2.1 (product construction)
  • ·Kozen, Automata and Computability, Lectures 3 and 4

Verification

Every state count on this page was computed twice: by minimising an explicit DFA, and by counting Myhill-Nerode classes (distinct sets of continuations) of the language on all strings up to length 7 or 8 over both prefixes and suffixes; the two agree for every language. In addition, for the languages with at most 4 states, all 5898 complete DFAs with up to 3 states over {a, b} were enumerated and none accepts the same language (so the stated minimum cannot be improved). The formula m + j for the number of states of a DFA for binary numbers divisible by k = 2^j m (m odd) was confirmed for k up to 16. The product of counters (lcm for counters on the same symbol) was confirmed. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never