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 ) if some continuation is accepted after one and rejected after the other: but . In a DFA, distinguishable strings must lead to different states. If strings are pairwise distinguishable, every DFA for needs at least 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 and the product has states , start and ; it runs both machines in parallel. Choosing the accepting pairs as gives the intersection, the union, the difference.
How it works
The method
- Decide what the machine must remember about the part of the string read so far (a number modulo , the last few symbols, which flags have been seen).
- Give each different memory content one state, and write the transitions by asking "if I remember this and read , what must I remember now?".
- Mark as accepting the states whose memory content makes the string acceptable.
- Check short strings including , 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| language | states of the DFA built | states of the minimal DFA | why |
|---|---|---|---|
| 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.
Counting modulo (one counter). "The number of s is ": states , the symbol moves , other symbols stay. Length : the same with every symbol counting. Needs exactly states.
Two counters. For the number of s modulo and the number of s modulo : the product, states (6 for modulo and modulo , as in the table). Counters on the same symbol combine to states: " even and divisible by " is "divisible by ", states, and "divisible by and by " is "divisible by ": states, not .
Suffix patterns (ends with ). The state remembers the longest suffix of the input that is a prefix of : states (). On a mismatch the machine does not return to but to the longest prefix of that is still a suffix of the new text (the idea of the Knuth-Morris-Pratt algorithm); for and reading it goes to (the new may start another ).
Substring patterns (contains ). The same machine with an absorbing accepting state at the end: states. (Contains : .)
Prefix patterns (starts with ). The machine must also reject once the beginning is wrong, so it needs a dead state: states. (Starts with : : start, "read ", accepting, dead.)
Length conditions. Exactly symbols: states (lengths plus a dead state). At most symbols: also . At least symbols: (no dead state; the last state is absorbing and accepting). Any finite language whose longest string has length needs at least states.
Occurrence counts. Exactly symbols : states (counts and a dead state). At most s: states. At least s: states. "At least one and at least one ": states (neither, only , only , both).
Binary numbers divisible by . Keep the remainder ; reading the bit gives . This gives states, and for odd that is minimal (: ; : ; : ). For even with odd, fewer are needed: the minimal DFA has states (for : ; : ; : ; : ; confirmed by minimisation for up to ).
Combining machines
- Intersection and union (" even and ends with ", "...or ..."): product construction. The number of states is at most 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 .
When a DFA is not enough
A DFA has finitely many states, so it cannot remember an unbounded count () or compare two arbitrarily long pieces (). The languages for which the minimal DFA is much larger than an NFA are those that need to remember a position far back: the -th symbol from the end needs DFA states against NFA states (subset construction).
An example in detail: contains
Four states: (no progress), (last symbol is the that could start ), (have ), (found, accepting, absorbing). From reading stays in (the new may start the pattern); from reading goes to (the text ends in : no progress), reading goes to . The run on :
DFA for the strings that contain the substring aba: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
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.
Worked examples
EX 1Strings ending in ab
EasyOver : three states (nothing useful), (ends with ), (ends with , accepting). , , , , , . Minimum number of states: .
EX 2Counting the states for a length condition
EasyBinary strings of length exactly : states for lengths (the last accepting) and a dead state: . Strings of length at most : the same states with the first four accepting. Strings of length at least : states (no dead state needed).
EX 3Product of two counters
MediumStrings over with an even number of s and an even number of s: product of two -state machines, states , , , ; accepting: . All four are distinguishable (for every pair of the strings , , , some continuation is accepted after one and rejected after the other), so is minimal. For "even number of s or even number of s" the same product with three accepting states has again states.
EX 4Lcm of counters
Medium"The number of s is divisible by and by " (strings over ): the product would have states, and indeed the language equals "divisible by ", which needs . "Divisible by and by " equals "divisible by ": states although the product has . 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 ending in : . (b) Binary numbers divisible by : ; by : (the value modulo is , , or odd, and the remainder and behave alike). (c) Strings over in which every is followed immediately by : (ok so far, just read an , dead). (d) Strings of length divisible by over : . (e) Language of strings that do not contain : (last symbol not , last symbol , dead). (f) The complement of the language in (e): also . (g) Number of states in the product for -counter and -counter on different symbols: .
Cheat box
Cheat box
- Minimum states number of Myhill-Nerode classes. pairwise distinguishable strings at least states.
- Ends with : . Contains : . Starts with : . Mismatch goes back to the longest prefix of that is still a suffix.
- Length exactly or at most : ; at least : . Exactly symbols (or at most): ; at least: .
- Count modulo : states. Counters on different symbols: product . Same symbol: .
- Binary divisible by ( odd): states ( itself for odd ).
- Complement: same number of states (complete DFA). Union, intersection: product, at most .
- The -th symbol from the end: DFA states, NFA states.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
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
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)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 COMPUTATIONConsider 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 _______.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).
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
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 ?
Show answerHide
().
Question 2
How many states does the minimum DFA have for the strings over that start with ?
Show answerHide
: start, accepting (started with ), dead ().
Question 3
Why does the language " divisible by and by " need only states?
Show answerHide
It is the same as "divisible by ": a count that is divisible by is divisible by .
Question 4
How many states does a minimal DFA for the binary numbers divisible by have?
Show answerHide
(, so ): the state counts the trailing s of the string, up to (odd; one; two; three or more).
Revision card
One-screen revision
- Design by deciding what to remember; states = Myhill-Nerode classes.
- ends/contains : ; starts with : ; length exactly/at most: ; at least: .
- Counters: ; product ; same symbol lcm. Binary divisible by : .
- 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
My notes (saved in this browser)