G27
Self-checkedTier 1 10 min readtoc.dfa

Deterministic finite automata

The DFA as a five-tuple, its transition function and the extended transition function, acceptance and the language of a DFA, transition tables and state diagrams, complete DFAs and the dead state, how a run proceeds step by step, why a DFA needs only constant memory and time linear in the input, how many DFAs there are, and the standard small examples (ends with 01, even number of a's, divisible by 3).

Why it exists

A finite automaton is the simplest model of a computer: a machine with a fixed, finite amount of memory that reads its input once, from left to right, and then says yes or no. It is also the model of a great many real things: the lexical analyser of a compiler (see lexical analysis), a vending machine, the control of a traffic light, a network protocol's state. The deterministic version has no choices: in every state the next symbol decides the next state. Everything in this subject is built on it, and the questions GATE asks are of three kinds: what language does this DFA accept, design a DFA for this language, and how many states are needed.

Prerequisites

You need strings, alphabets and languages (alphabets, strings and languages).

Precise definitions

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

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

Extended transition function. δ∗:Q×Σ∗→Q\delta^* : Q \times \Sigma^* \to Q is defined by δ∗(q,ε)=q\delta^*(q, \varepsilon) = q and δ∗(q,wa)=δ(δ∗(q,w),a)\delta^*(q, wa) = \delta(\delta^*(q, w), a): the state reached from qq after reading the whole string ww.

Acceptance and language. MM accepts ww if δ∗(q0,w)∈F\delta^*(q_0, w) \in F. The language of MM is L(M)={w∈Σ∗:δ∗(q0,w)∈F}L(M) = \{ w \in \Sigma^* : \delta^*(q_0, w) \in F \}. A language is regular if some DFA accepts it.

Representations. A transition table lists δ(q,a)\delta(q, a) for every qq and aa (with marks for the start and accepting states). A state diagram draws states as circles (accepting ones doubled), the start state with an arrow from nowhere, and an edge q→arq \xrightarrow{a} r whenever δ(q,a)=r\delta(q, a) = r (parallel edges are drawn as one edge with several labels).

Complete and partial DFAs. In the definition every state has an edge for every symbol. Diagrams in books often omit the edges that lead to a dead state (trap state) dd, a non-accepting state with δ(d,a)=d\delta(d, a) = d for all aa; a machine with missing edges is a partial DFA, and the missing edges mean "go to the dead state and reject". When counting states, a complete DFA must include the dead state if it is needed.

How it works

A run

Reading a string is a walk through the diagram: start in q0q_0, and for each symbol follow the edge with that label. There is exactly one such walk, with ∣w∣|w| steps; the string is accepted if the walk ends in an accepting state. The animation shows a DFA for the binary strings that end with 0101: the three states mean "the string so far does not end with 00 or with 0101" (q0q_0, which includes the empty string), "ends with 00" (q1q_1), and "ends with 0101" (q2q_2, accepting). On the input 11011101:

DFA for the binary strings that end with 01: start

Simulated, not typed
100110q0q1q2

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

input1101
state{q0}

Step 0

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

0 / 5
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

Another machine, for the strings over {a,b}\{a, b\} with an even number of aas, run on abaababaab (three aas, so rejected). It has two states: one remembers "even so far", one "odd so far".

DFA for the strings over {a, b} with an even number of a's: start

Simulated, not typed
baabEO

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

inputabaab
state{E}

Step 0

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

0 / 6
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

Reading a transition table

0011
→q0\to q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
∗q2* q_2q1q_1q0q_0

(An arrow marks the start state, a star an accepting state.) This is the machine of the first animation. To find δ∗(q0,1101)\delta^*(q_0, 1101): q0→1q0→1q0→0q1→1q2q_0 \xrightarrow{1} q_0 \xrightarrow{1} q_0 \xrightarrow{0} q_1 \xrightarrow{1} q_2, an accepting state.

What a DFA can and cannot do

  • Time and memory. It makes exactly one transition per input symbol: time Θ(∣w∣)\Theta(|w|), and its memory is its current state, a constant amount independent of the input. It can never "count" beyond what its finite number of states can remember: for example no DFA accepts {anbn:n≥0}\{a^n b^n : n \ge 0\} (shown with the pumping lemma later).
  • Remembering information. Each state stands for a set of strings with the same future: after reading uu and after reading vv the machine is in the same state only if every continuation zz gives the same verdict for uzuz and vzvz. Designing a DFA means deciding what must be remembered (design patterns).
  • Examples. Binary numbers divisible by 33: the state is the remainder of the number read so far modulo 33; reading the bit bb turns the value vv into 2v+b2v + b, so the remainder rr becomes (2r+b) mod 3(2r + b) \bmod 3: three states. Strings with an even number of aas: two states. Strings containing abaaba: four states (progress 00 to 33 with an absorbing accepting state).

How many DFAs are there?

For nn states and kk symbols the number of transition functions is nnkn^{nk} (each of the nknk table entries is one of nn states), there are nn choices of start state and 2n2^n choices of accepting set: nnk⋅n⋅2nn^{nk} \cdot n \cdot 2^n labelled DFAs. For n=2n = 2, k=2k = 2: 24⋅2⋅4=1282^4 \cdot 2 \cdot 4 = 128 (many accept the same language). With the start state fixed there are nnk2nn^{nk} 2^n.

Finite languages need dead states

A DFA for the language of all strings of length at most nn needs n+2n + 2 states: one for each length 0,…,n0, \ldots, n and a dead state for longer strings (checked by counting the distinguishable classes for n=1,…,5n = 1, \ldots, 5). In general a complete DFA for a finite language whose longest string has length nn needs at least n+2n + 2 states.

Worked examples

EX 1Which strings are accepted?

Easy

The DFA with Q={q0,q1,q2}Q = \{q_0, q_1, q_2\} of the table above. 00: q0→q1q_0 \to q_1, not accepting. 0101: q1→q2q_1 \to q_2, accepted. 1010: q0→q0→q1q_0 \to q_0 \to q_1, rejected. 01010101: q0→q1→q2→q1→q2q_0 \to q_1 \to q_2 \to q_1 \to q_2, accepted. ε\varepsilon: stays in q0q_0, rejected. The language is {w:w ends with 01}\{w : w \text{ ends with } 01\}.

EX 2Complete the DFA

Easy

A diagram has states s,ts, t with the edges s→ass \xrightarrow{a} s, s→bts \xrightarrow{b} t, t→btt \xrightarrow{b} t (and tt accepting, ss accepting) over {a,b}\{a, b\}. The edge t→at \xrightarrow{a} is missing: reading an aa in tt kills the run. Completing it adds a dead state dd with t→adt \xrightarrow{a} d and d→a,bdd \xrightarrow{a, b} d. The language is a∗b∗a^* b^* and the complete DFA has 33 states.

EX 3Divisible by 3

Medium

State == remainder. Input 1101 1 0 (=6= 6): R0→1R1R_0 \xrightarrow{1} R_1 (2⋅0+12 \cdot 0 + 1), R1→1R0R_1 \xrightarrow{1} R_0 (2⋅1+1=3≡02 \cdot 1 + 1 = 3 \equiv 0), R0→0R0R_0 \xrightarrow{0} R_0: accepted, and 66 is divisible by 33. Input 101101 (=5= 5): R0→R1→R2R_0 \to R_1 \to R_2 (2⋅1+0=22 \cdot 1 + 0 = 2) →R2\to R_2 (2⋅2+1=5≡22 \cdot 2 + 1 = 5 \equiv 2): rejected. The empty string is read as 00 and accepted.

EX 4Counting states and machines

Medium

How many different DFAs (as 5-tuples with states labelled 0,…,n−10, \ldots, n-1) are there with 33 states over {0,1}\{0, 1\}? 33⋅2⋅3⋅23=729⋅24=17 4963^{3 \cdot 2} \cdot 3 \cdot 2^3 = 729 \cdot 24 = 17\,496. With the start state fixed: 58325832. If the question asks for the number of different languages the answer is much smaller, because many machines accept the same language.

EX 5Typical statements

GATE level

(a) A DFA with nn states accepts a string of length ≥n\ge n only if it visits some state twice: true (pigeonhole); this is the idea of the pumping lemma. (b) A DFA for LL can be turned into a DFA for L‾\overline{L} by making the accepting states non-accepting and vice versa: true for a complete DFA (not for a partial one). (c) Every language accepted by a DFA is accepted by a DFA with exactly one accepting state: false (the language {ε,a}\{\varepsilon, a\} over {a}\{a\}: the start state must accept, and a single accepting state would have to loop on aa and so also accept aaaa). (d) A DFA can have no accepting state: yes, it accepts ∅\emptyset. (e) The minimum number of states of a DFA accepting a finite language with longest string of length nn: n+2n + 2. (f) Time to run a DFA on an input of length mm: Θ(m)\Theta(m).

Cheat box

Cheat box

  • M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F), δ:Q×Σ→Q\delta : Q \times \Sigma \to Q total. δ∗(q,wa)=δ(δ∗(q,w),a)\delta^*(q, wa) = \delta(\delta^*(q, w), a). L(M)={w:δ∗(q0,w)∈F}L(M) = \{w : \delta^*(q_0, w) \in F\}.
  • Run: exactly ∣w∣|w| transitions; one path; constant memory. Complete DFA: add a dead state for missing edges.
  • Complement: flip accepting/non-accepting in a complete DFA.
  • Number of DFAs: nnk⋅n⋅2nn^{nk} \cdot n \cdot 2^n (start fixed: nnk2nn^{nk} 2^n).
  • A finite language with longest string of length nn needs ≥n+2\ge n + 2 states (complete DFA).
  • Divisible by 33 (binary): remainder, (2r+b) mod 3(2r + b) \bmod 3, 33 states. Even number of aas: 22 states.
  • Regular language == accepted by some DFA.

Traps and GATE patterns

Connections

DFAs are drawn like the state machines of digital logic (state machines) but they have accepting states and no outputs. The lexical analyser of a compiler is a DFA (lexical analysis). Design methods are in design patterns; allowing choices gives the NFA, which accepts exactly the same languages.

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 2026 · Set 2 · MSQ · 2 marksQ3 · page 197 of the book
Consider the following two finite automata D1  and
D2  (as shown in the figure).

Which of the following statements is/are true?
  • (a). L(D1)=L(D2)
  • (b). L(D1) is a proper subset of L(D2)
  • (c). L(D1)∩L(D2)={ε}
  • (d). (L(D1)∪L(D2))* consists of all strings in {0,1}* whose length is divisible by 3
GATE 2025 · Set 1 · MSQ · 2 marksQ7 · page 197 of the book
Consider the following deterministic finite automaton
(DFA) defined over the alphabet, Σ = {a, b}. Identify which
of the following language(s) is/are accepted by the given
DFA.

THEORY OF COMPUTATION
  • (a)The set of all strings containing an even number of b's.
  • (b)The set of all strings containing the pattern bab.
  • (c)The set of all strings ending with the pattern bab.
  • (d)The set of all strings not containing the pattern aba.
GATE 2009 · MCQ · 1 markQ59 · page 205 of the book
The above DFA accepts the set of all strings over {0,1}
that
  • (a)begin either with 0 or 1 .
  • (b)end with 0.
  • (c)end with 00.
  • (d)contain the substring 00 THEORY OF COMPUTATION
GATE 2008 · MCQ · 2 marksQ63 · page 207 of the book
If the final states and non-final states in the DFA
below are interchanged, then which of the following
languages over the alphabet {a, b} will be accepted
by the new DFA?
  • (a)Set of all strings that do not end with ab
  • (b)Set of all strings that begin with either an a or a b
  • (c)Set of all strings that do not contain the substring ab
  • (d)The set described by the regular expression b*aa*(ba)*b*
GATE 2006 · MCQQ73 · page 210 of the book
In the automaton below,s is the start state and t is the
only final state.

Consider the strings u = abbaba, v = bab, and w = aabb.
Which of the following statements is true?
  • (a)The automaton accepts u and v but not w
  • (b)The automaton accepts each of u, v, and w
  • (c)The automaton rejects each of u, v, and w
  • (d)The automaton accepts u but rejects v and w
GATE 2005 · MCQ · 2 marksQ76 · page 210 of the book
Consider the machine M:

THEORY OF COMPUTATION
The language recognized by M is:
  • (a){ w ∈ {a, b}* | every a in w is followed by exactly two b's }
  • (b){ w ∈ {a, b}* | every a in w is followed by at least two b's }
  • (c){ w ∈ {a, b}* | w contains the substring 'abb' }
  • (d){ w ∈ {a, b}* | w does not contain 'aa' as a substring }

Self-check

Question 1

Give the transition table of a DFA over {a}\{a\} that accepts the strings whose length is a multiple of 33.

Show answer

Three states m0m_0 (start, accepting), m1m_1, m2m_2 with δ(mi,a)=m(i+1) mod 3\delta(m_i, a) = m_{(i+1) \bmod 3}.

Question 2

How many states does a complete DFA need for the language of all binary strings of length exactly 33?

Show answer

55: states for lengths 0,1,2,30, 1, 2, 3 and a dead state for longer strings.

Question 3

If δ∗(q0,x)=δ∗(q0,y)\delta^*(q_0, x) = \delta^*(q_0, y), what can you say about xzxz and yzyz for every zz?

Show answer

They end in the same state, so both are accepted or both rejected.

Question 4

How many DFAs with 22 states over a one-letter alphabet exist (start state not fixed)?

Show answer

22⋅2⋅22=322^{2} \cdot 2 \cdot 2^2 = 32.

Revision card

One-screen revision

  • 5-tuple, δ\delta total, δ∗\delta^*, L(M)L(M); run = one path of ∣w∣|w| steps.
  • Complete DFAs have a dead state; complement = flip FF (complete only).
  • Number of DFAs nnk⋅n⋅2nn^{nk} \cdot n \cdot 2^n; finite language with longest string nn: ≥n+2\ge n + 2 states.
  • Constant memory: cannot count unboundedly.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.1 Finite automata
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 2.2 Deterministic finite automata
  • ·Kozen, Automata and Computability, Lecture 3 Deterministic finite automata

Verification

Every DFA drawn on this page was compared with an independent predicate (ends with 01, even number of a's, contains aba, binary number divisible by 3) on every string up to length 10; the runs of the animations were replayed by a separate simulation. The number of DFAs with n states over k symbols (n^(nk) transition functions, n start choices, 2^n accepting sets) was confirmed by enumeration for small n and k; the statement that a complete DFA for the finite language of all strings of length at most n needs exactly n + 2 states was confirmed for n up to 5 by counting Myhill-Nerode classes. 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