Introduction

A deterministic finite automaton (DFA) is a finite-state machine that reads an input string from left to right, one symbol at a time, and either accepts or rejects. At every step the next state is uniquely determined by the current state and the current symbol. DFAs recognize exactly the regular languages.

The working skills are designing a DFA for a stated language, simulating a given DFA on a string, counting states, and reasoning about what language a transition diagram accepts. This note fixes the formal definition, the extended transition function, design patterns and two fully checked examples: binary integers divisible by 3, and strings ending with 01.

Prerequisites

Core concepts

Formal definition

A DFA is a 5-tuple M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) where:

  • QQ is a finite nonempty set of states.
  • Σ\Sigma is the input alphabet.
  • δ:Q×Σ→Q\delta: Q \times \Sigma \to Q is the transition function (total: defined for every state and symbol).
  • q0∈Qq_0 \in Q is the start state.
  • F⊆QF \subseteq Q is the set of accepting (final) states.

Deterministic and total. From each state, for each symbol, there is exactly one next state. An incomplete diagram is conventionally read as having a missing dead (sink) state that rejects and stays on every symbol; state it when you add one.

How a DFA processes a string

The transition function extends to strings by δ^:Q×Σ∗→Q\hat{\delta}: Q \times \Sigma^* \to Q:

δ^(q,ε)=q,δ^(q,wa)=δ(δ^(q,w),a).\hat{\delta}(q, \varepsilon) = q, \qquad \hat{\delta}(q, wa) = \delta\bigl(\hat{\delta}(q, w), a\bigr).

The DFA accepts ww iff δ^(q0,w)∈F\hat{\delta}(q_0, w) \in F. The language of MM is

L(M)={w∈Σ∗∣δ^(q0,w)∈F}.L(M) = \{ w \in \Sigma^* \mid \hat{\delta}(q_0, w) \in F \}.

Acceptance depends only on the state after reading all of ww. There is no stack and no unbounded memory: the current state is the entire memory.

Transition diagrams and tables

  • States are circles; accepting states have a double circle.
  • The start state has an incoming arrow with no source.
  • An edge p→aqp \xrightarrow{a} q means δ(p,a)=q\delta(p,a) = q. Multiple symbols on one edge mean several transitions drawn together.
  • A transition table has rows = states, columns = symbols, entries = δ(q,a)\delta(q,a). Mark the start and the accepting rows.

Dead states and unreachable states

  • A dead (reject sink) state d∉Fd \notin F satisfies δ(d,a)=d\delta(d,a) = d for every aa. Irreversible rejection can be represented by one dead sink; all hopeless states collapse to that sink on minimization.
  • An unreachable state is never entered from q0q_0 on any string. It does not affect L(M)L(M) and can be removed.

How it works

Design patterns

  1. Track a property of the prefix read so far that is finite-valued: length modulo kk, value modulo kk, progress toward a keyword, parity of a count, last few symbols.
  2. Make one state per possible value of that property.
  3. Mark accepting those values that mean "the string so far is in the language".
  4. Fill every transition; add a dead state if the property can fail permanently.

Keyword / suffix pattern. For "strings ending with ww" where ∣w∣=m|w| = m, keep the longest prefix of ww that is a suffix of the text read so far (the failure function idea from string matching). That needs at most m+1m+1 states.

Modular arithmetic. For binary strings whose integer value is 0 mod k0 \bmod k, states are residues 0,1,…,k−10,1,\ldots,k-1. Reading bit bb sends residue rr to (2r+b) mod k(2r+b) \bmod k.

Algorithms and pseudocode

Pseudocode uses sets and maps; δ[q][a]\delta[q][a] is the next state.

Text
DFA-ACCEPT(M, w)
1  q <- M.q0
2  for each symbol a in w, left to right
3      q <- M.delta[q][a]
4  return q in M.F

Time Θ(∣w∣)\Theta(|w|); space Θ(1)\Theta(1) beyond the machine description.

Text
DFA-LANGUAGE-NONEMPTY(M)
1  reach <- {M.q0};  work <- queue with M.q0
2  while work is not empty
3      p <- dequeue(work)
4      for each a in Sigma
5          r <- M.delta[p][a]
6          if r not in reach
7              add r to reach; enqueue r
8  return reach intersects M.F

Emptiness of a DFA language is decidable in linear time in the size of the machine (reachability).

Mathematical explanation

Myhill-Nerode (statement for design intuition)

Define x≡Lyx \equiv_L y iff for every zz, xz∈L⇔yz∈Lxz \in L \Leftrightarrow yz \in L. The equivalence classes of ≡L\equiv_L are exactly the distinctions a DFA for LL must remember. LL is regular iff ≡L\equiv_L has finitely many classes, and the minimal DFA has one state per class (see DFA Minimization).

Closure teaser

If LL and MM are DFA languages, so are L∪ML \cup M, L∩ML \cap M and L‾\overline{L}, by product constructions and flipping accepting states. Full proofs sit in Closure and Decision Properties of Regular Languages.

Worked examples

Example 1: binary value divisible by 3

Alphabet Σ={0,1}\Sigma = \{0,1\}. Interpret ww as a binary integer (w=εw = \varepsilon has value 0). States q0,q1,q2q_0,q_1,q_2 remember the value of the prefix modulo 3. Start and only accept: q0q_0.

δ(qr,0)=q(2r) mod 3,δ(qr,1)=q(2r+1) mod 3.\delta(q_r, 0) = q_{(2r) \bmod 3}, \qquad \delta(q_r, 1) = q_{(2r+1) \bmod 3}.
Stateon 0on 1
q0q_0q0q_0q1q_1
q1q_1q2q_2q0q_0
q2q_2q1q_1q2q_2

Traces:

  • 0: value 0, stay in q0q_0, accept.
  • 1: value 1, to q1q_1, reject.
  • 11: value 3, q0→1q1→1q0q_0 \xrightarrow{1} q_1 \xrightarrow{1} q_0, accept.
  • 10: value 2, to q2q_2, reject.

Induction on the length shows that after reading ww the machine is in qrq_r with r=val(w) mod 3r = \mathrm{val}(w) \bmod 3, because appending bit bb maps the value vv to 2v+b2v + b.

DFA for binary strings with value divisible by 3
Figure 1. DFA with states q0, q1, q2 for binary value modulo 3; q0 is start and accept; edges labelled 0 and 1 implement δ(qr,b)=q(2r+b) mod 3\delta(q_r,b)=q_{(2r+b)\bmod 3}.

Example 2: strings ending with 01

States remember the current progress toward the suffix 01:

StateMeaningAccept?
ε\varepsilonno progressno
0suffix is 0no
01suffix is 01yes
1suffix is 1 (but not 01)no

Transitions for a redundant four-state refinement of the canonical three-state suffix-progress automaton (the states labelled epsilon and 1 have equivalent future behaviour):

01
ε\varepsilon01
0001
0101
101

Check: 01 accepted; 001 accepted; 010 rejected; 1 rejected; every string of length at most 7 behaves as the predicate "ends with 01".

This DFA is correct but not minimal. States ε\varepsilon and 1 behave identically: both go to 0 on 0 and to 1 on 1, and both reject. Merging them leaves 3 states, which is the minimum: the prefixes ε\varepsilon, 0, 01 are pairwise distinguishable (ε\varepsilon and 0 by the continuation 1; either of them and 01 by the empty continuation). In general "ends with ww", ∣w∣=m|w| = m, needs exactly m+1m+1 states.

Example 3: strings containing the substring aba

Alphabet {a,b}\{a,b\}. State ii means "the longest suffix of the input that is a prefix of aba has length ii"; state 3 means aba has already occurred, and it is an accepting sink.

Stateon aon bAccept?
010no
112no
230no
333yes

The two failure edges decide the question. From state 1 on a the machine stays in 1 (the new a can start the pattern again), and from state 2 on b it returns to 0 (abb has no suffix that begins aba). Traces: abaa ends in 3 and is accepted; abba passes 1, 2, 0, 1 and is rejected; aababb reaches 3 at the fourth symbol and stays there. The four prefixes ε\varepsilon, a, ab, aba are pairwise distinguishable, so 4 states is minimal.

Substring versus suffix. "Contains ww" has an accepting sink; "ends with ww" does not, because later symbols can destroy the match. Both need m+1m+1 states. "Starts with ww" needs m+2m+2: the m+1m+1 progress states plus a dead state for an early mismatch.

Example 4: two counters at once

Let L={w∈{a,b}∗∣#a(w) even and #b(w)≡0(mod3)}L = \{ w \in \{a,b\}^* \mid \#_a(w) \text{ even and } \#_b(w) \equiv 0 \pmod 3 \}. Track the pair (#a mod 2,#b mod 3)(\#_a \bmod 2, \#_b \bmod 3): 6 states, start and only accept (0,0)(0,0); a flips the first component, b adds 1 to the second modulo 3. No two pairs are equivalent (pairs differing in the aa-parity are separated by a suitable string of bbs followed by nothing or by a, and similarly for the bb-count), so the minimal DFA has exactly 2×3=62 \times 3 = 6 states. The same 6 states with accept set "first component 0 or second component 0" recognize the union, which also needs 6 states.

Example 5: counting DFAs

How many DFAs are there with the two states X,YX, Y, start state XX, over {a,b}\{a,b\}? Each of the 2×2=42 \times 2 = 4 table entries has 2 choices and FF is any of the 4 subsets: 24×4=642^4 \times 4 = 64. In general, with nn labelled states, a fixed start state and ∣Σ∣=m|\Sigma| = m,

#DFAs=2n⋅nnm,and n⋅2n⋅nnm if the start state may be any of the n.\#\text{DFAs} = 2^{n} \cdot n^{nm}, \qquad \text{and } n \cdot 2^{n} \cdot n^{nm} \text{ if the start state may be any of the } n.

How many of the 64 accept ∅\emptyset? Exactly those with no reachable accepting state. F=∅F = \emptyset: all 16 tables. F={Y}F = \{Y\}: YY must be unreachable, so both edges of XX loop on XX, while YY's two edges are free: 4 tables. FF containing XX always accepts ε\varepsilon. Total 16+4=2016 + 4 = 20. These 64 machines accept only 26 different languages, a reminder that counting machines is not counting languages.

Important properties and observations

  • A DFA has exactly one run on each string; acceptance is "final state in FF".
  • Adding unreachable states does not change the language; removing a dead state may make δ\delta partial (avoid in formal answers unless the problem allows incomplete DFAs).
  • Number of states is not unique for a language unless the DFA is minimal.
  • L=Σ∗L = \Sigma^* iff every reachable state is accepting; L=∅L = \emptyset iff no accepting state is reachable.
  • DFAs and NFAs define the same class of languages; DFAs can need exponentially more states (see NFA and Subset Construction).

Minimal state counts to remember

Over Σ={a,b}\Sigma = \{a, b\} unless stated. Every count is for a complete minimal DFA, so a dead state is included when one is needed.

LanguageMinimal statesWhy
∣w∣=n\lvert w \rvert = nn+2n+2lengths 0..n0..n, plus dead for "too long"
∣w∣≤n\lvert w \rvert \le nn+2n+2same states, different accept set
∣w∣≥n\lvert w \rvert \ge nn+1n+1lengths 0..n−10..n-1, then an accepting sink
∣w∣ mod k=r\lvert w \rvert \bmod k = rkkresidues of the length
#a(w) mod k=r\#_a(w) \bmod k = rkkresidues of the count
#a mod p=r\#_a \bmod p = r and #b mod q=s\#_b \bmod q = spqpqproduct; the same for "or" when p,q≥2p,q\ge2 and 0≤r<p0\le r<p, 0≤s<q0\le s<q; trivial moduli can collapse the OR language
ends with ww, ∣w∣=m\lvert w \rvert = mm+1m+1prefixes of ww, no sink
contains ww, ∣w∣=m\lvert w \rvert = mm+1m+1prefixes of ww, accepting sink
starts with nonempty ww, ∣w∣=m≥1\lvert w \rvert=m\ge1m+2m+2prefixes of ww, accepting sink, dead state
first and last symbols equal (nonempty)5start, and two states for each first symbol
kk-th symbol from the end is aa2k2^kmust remember the last kk symbols
binary value divisible by k=2tmk = 2^t m, mm oddm+tm + tthe factor 2t2^t only needs the last tt bits

For starts-with, the empty prefix gives the universal language and needs one state.

The last row needs care. Divisibility by 3 needs 3 states, but divisibility by 4 needs only 3 (not 4): after padding with leading zeros, a number is a multiple of 4 iff its last two bits are 00. Thus accept epsilon and 0 as well as every string ending 00. The initial state is accepting; the three state classes are "epsilon, 0, or ends 00", "ends 10", and "ends 1". Divisibility by 6 needs 3+1=43 + 1 = 4, by 8 needs 4, by 12 needs 5, by 16 needs 5. These counts treat ε\varepsilon as the value 0 and allow leading zeros; a question that forbids leading zeros adds states, so read it carefully.

Acceptance of the empty string. ε∈L(M)\varepsilon \in L(M) iff q0∈Fq_0 \in F. Use this as a one-second sanity check on every designed machine.

Complement needs a complete DFA. Swapping FF and Q∖FQ \setminus F recognizes Σ∗∖L\Sigma^* \setminus L only when δ\delta is total. On a partial diagram, first add the dead state; it then becomes accepting in the complement.

Common misconceptions.

  • "Missing transitions mean reject immediately." Formally δ\delta is total; model missing arrows by a dead state.
  • "The machine accepts as soon as it visits an accept state." No: acceptance is checked only after the whole string is read (unless the problem explicitly asks for acceptance on a prefix).
  • "More states always mean a larger language." False; state count is about memory, not language size.

GATE examination perspective

  • SIMULATE CAREFULLY. Write the state after each symbol; do not skip.
  • MODULO MACHINES. Confirm the update rule (2r+b) mod k(2r+b) \bmod k for binary value; for decimal digits use (10r+d) mod k(10r+d) \bmod k.
  • SUFFIX LANGUAGES. Build the progress automaton; follow the particular table given: its redundant refinement sends 01 on 1 to 1, which is equivalent to ε\varepsilon in the canonical minimized automaton.
  • COUNT THE STATES. Minimal state counts use Myhill-Nerode distinguishability: exhibit nn prefixes that are pairwise distinguishable for the lower bound and an nn-state DFA for the upper bound. Use the table of minimal counts above, and recheck the dead state.
  • "MINIMAL" WITH A DEAD STATE. Some questions count the dead state and some say "ignore the trap state". The table counts it; subtract 1 only when the question says so.
  • BINARY DIVISIBILITY. Divisible by 2tm2^t m needs m+tm + t states, not 2tm2^t m. Decimal digits use (10r+d) mod k(10r + d) \bmod k.
  • COUNTING MACHINES. "How many DFAs with nn states" counts tables and accept sets (2nnnm2^n n^{nm} with a fixed start), not languages.
  • CONTAINS VERSUS ENDS WITH. Check whether the accept state is a sink. A sink is right for "contains", wrong for "ends with".
  • DEAD STATE. If the question's diagram omits transitions, say that a dead sink is assumed.

Summary

Sources

  1. J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley: Section 2.2 (deterministic finite automata), Section 2.4 (text search), Section 4.4 (equivalence and minimization).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 1.1 (finite automata, designing finite automata).
  3. P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Section 2.1 (deterministic finite accepters).

Next steps