Section 6 · 11 min read

Regular expressions and finite automata

DFAs, NFAs, epsilon-NFAs, regular expressions, Myhill-Nerode and DFA minimization, with the counting and state-count questions GATE asks most.

On this page
  1. Why this topic matters
  2. Deterministic finite automata
  3. Nondeterministic finite automata
  4. Regular expressions
  5. Myhill-Nerode theorem
  6. DFA minimization
  7. Building DFAs for common patterns
  8. Showing a language is regular or not
  9. Worked examples
  10. Common traps
  11. Quick revision

Why this topic matters

Regular languages give GATE its most reliable marks in Theory of Computation. Almost every paper has a minimal-DFA state count, a "which regex is equal to which" question, or a "which of these is regular" question. All three come down to one skill: knowing exactly what finite memory can and cannot remember.

Deterministic finite automata

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

  • QQ: finite set of states
  • Σ\Sigma: finite input alphabet
  • δ:Q×Σ→Q\delta : Q \times \Sigma \to Q: transition function, exactly one move per state and symbol
  • q0∈Qq_0 \in Q: start state
  • F⊆QF \subseteq Q: accepting states

Extend δ\delta to strings: δ^(q,ϵ)=q\hat\delta(q, \epsilon) = q and δ^(q,wa)=δ(δ^(q,w),a)\hat\delta(q, wa) = \delta(\hat\delta(q, w), a). The language is L(M)={w:δ^(q0,w)∈F}L(M) = \{w : \hat\delta(q_0, w) \in F\}. A language is regular if some DFA accepts it.

A DFA is complete if every state has a move on every symbol. Many textbooks and GATE keys draw partial DFAs and leave the dead (trap) state implicit. Always read whether a question counts it.

Transition tables

GATE often gives a DFA as a table. Here is a DFA over {0,1}\{0,1\} for binary numbers divisible by 3, read most significant bit first. State qrq_r means "value so far leaves remainder rr", and reading bit bb moves remainder rr to (2r+b) mod 3(2r + b) \bmod 3. Start and accept at q0q_0.

State 0 1
→∗q0\to * q_0 q0q_0 q1q_1
q1q_1 q2q_2 q0q_0
q2q_2 q1q_1 q2q_2

The same idea gives a kk-state remainder DFA for any modulus kk, but the minimal DFA can be smaller. For divisibility by 6 the remainders rr and r+3r + 3 always move to the same state, so only the accepting status keeps them apart, and the minimal DFA has 4 states, not 6.

Nondeterministic finite automata

An NFA allows δ:Q×Σ→2Q\delta : Q \times \Sigma \to 2^Q: zero, one or many moves per symbol. It accepts if some run ends in an accepting state. An ϵ\epsilon-NFA also allows moves on ϵ\epsilon that consume no input. The ϵ\epsilon-closure ECLOSE(q)ECLOSE(q) is the set of states reachable from qq using only ϵ\epsilon-moves.

Subset construction

Every NFA (with or without ϵ\epsilon-moves) has an equivalent DFA:

  1. The DFA start state is ECLOSE(q0)ECLOSE(q_0).
  2. From a DFA state SS on symbol aa, go to ECLOSE(⋃q∈Sδ(q,a))ECLOSE\left(\bigcup_{q \in S} \delta(q, a)\right).
  3. A DFA state is accepting if it contains an NFA accepting state.

With nn NFA states there are at most 2n2^n DFA states (including ∅\emptyset, which acts as the dead state). The bound is tight: "the nn-th symbol from the right is aa" has an (n+1)(n+1)-state NFA but needs 2n2^n DFA states.

So DFAs, NFAs and ϵ\epsilon-NFAs all accept exactly the regular languages. Nondeterminism buys succinctness, not power.

Regular expressions

Regular expressions over Σ\Sigma are built from ∅\emptyset, ϵ\epsilon, and each a∈Σa \in \Sigma using union (++), concatenation and Kleene star. Kleene's theorem says a language is regular if and only if some regular expression denotes it.

Useful identities (check each by asking "which strings are in it?"):

Identity Comment
ϵ+RR∗=R∗\epsilon + RR^* = R^* definition of star
(R∗)∗=R∗(R^*)^* = R^* starring twice adds nothing
(a+b)∗=(a∗b∗)∗=(a∗+b∗)∗(a + b)^* = (a^* b^*)^* = (a^* + b^*)^* all strings
(ab)∗a=a(ba)∗(ab)^* a = a(ba)^* shifting brackets
∅∗=ϵ\emptyset^* = \epsilon star of the empty set
R∅=∅R\emptyset = \emptyset concatenating with nothing
(R+S)∗≠R∗+S∗(R + S)^* \ne R^* + S^* in general abab is in the left side only

Arden's rule

If PP does not contain ϵ\epsilon, then the equation X=PX+QX = PX + Q (or X=Q+XPX = Q + XP for the right-linear form) has the unique solution X=P∗QX = P^* Q (respectively X=QP∗X = QP^*). This turns a DFA into a regular expression: write one equation per state describing how it can be reached, then eliminate variables.

Myhill-Nerode theorem

For a language LL, call strings xx and yy equivalent (x≡Lyx \equiv_L y) if for every suffix zz, xz∈L  ⟺  yz∈Lxz \in L \iff yz \in L. The theorem:

  • LL is regular if and only if ≡L\equiv_L has finitely many classes.
  • The number of classes equals the number of states in the minimal complete DFA for LL.

This gives the cleanest proofs in both directions. To show a lower bound of kk states, find kk prefixes that are pairwise distinguishable (each pair has a separating suffix). To show non-regularity, find infinitely many pairwise distinguishable prefixes. For {anbn}\{a^n b^n\}, the prefixes a0,a1,a2,…a^0, a^1, a^2, \dots work: aia^i and aja^j are separated by bib^i.

DFA minimization

The minimal DFA is unique up to renaming of states. Two standard procedures produce it.

Moore (partition refinement).

  1. Remove states unreachable from the start.
  2. Start with two blocks: accepting and non-accepting.
  3. Split a block whenever two of its states go, on some symbol, to different blocks.
  4. Repeat until no block splits. Each block is one state of the minimal DFA.

Table filling (Myhill-Nerode). Mark every pair (accepting, non-accepting). Then repeatedly mark a pair (p,q)(p, q) if for some symbol aa the pair (δ(p,a),δ(q,a))(\delta(p,a), \delta(q,a)) is already marked. Unmarked pairs are equivalent.

Never skip step 1. An unreachable state can look distinct from everything and inflate your count.

Building DFAs for common patterns

Pattern Minimal complete DFA states
strings ending with a fixed string ww of length kk k+1k + 1
strings containing a fixed substring ww of length kk k+1k + 1
strings starting with a fixed string ww of length kk k+2k + 2 (includes dead)
kk-th symbol from the left is fixed k+2k + 2 (includes dead)
kk-th symbol from the right is fixed (binary) 2k2^k
length ≡r(modk)\equiv r \pmod k kk
na(w) mod pn_a(w) \bmod p and nb(w) mod qn_b(w) \bmod q together p⋅qp \cdot q (if accepting set does not collapse)

The "ending with" and "containing" counts can be smaller only in degenerate cases; check by Moore refinement whenever the accepting set is unusual. For a product of two counters, the accepting set may let classes merge. For example, "na≡nb(mod3)n_a \equiv n_b \pmod 3" needs only 3 states, because only the difference matters.

Showing a language is regular or not

To show regular: give a DFA, NFA or regex; or build it from known regular languages with closure (union, intersection, complement, concatenation, star, reversal, homomorphism, inverse homomorphism). Any finite language is regular.

To show not regular: use Myhill-Nerode (infinitely many distinguishable prefixes), the pumping lemma, or closure. The closure trick: if LL were regular, then L∩a∗b∗L \cap a^* b^* would be regular; if that intersection is {anbn}\{a^n b^n\}, LL is not regular.

Quick intuition test: does recognizing the language need an unbounded counter or memory of an arbitrary prefix? If yes, it is not regular. But beware languages that look like counting yet are not, such as "number of abab equals number of baba", which only depends on whether the first and last symbols agree.

Worked examples

Worked example 1: Strings ending in ab

Build the minimal complete DFA over {a,b}\{a, b\} for strings ending in abab.

Track the longest suffix of the input that is a prefix of abab: none, aa, or abab.

State a b
→p0\to p_0 (no progress) p1p_1 p0p_0
p1p_1 (seen aa) p1p_1 p2p_2
∗p2* p_2 (seen abab) p1p_1 p0p_0

Notice p1p_1 on aa stays in p1p_1: after aaaa, the last aa still starts a possible abab. Distinguishability: p2p_2 is the only accepting state; p0p_0 and p1p_1 are separated by the suffix bb. Answer: 3 states, no dead state needed.

Worked example 2: Subset construction

NFA over {a,b}\{a, b\} for "second-last symbol is aa": states 1,2,31, 2, 3, start 11, accept 33. Moves: 1→a,b11 \xrightarrow{a,b} 1, 1→a21 \xrightarrow{a} 2, 2→a,b32 \xrightarrow{a,b} 3.

DFA state a b
→{1}\to \{1\} {1,2}\{1,2\} {1}\{1\}
{1,2}\{1,2\} {1,2,3}\{1,2,3\} {1,3}\{1,3\}
∗{1,2,3}* \{1,2,3\} {1,2,3}\{1,2,3\} {1,3}\{1,3\}
∗{1,3}* \{1,3\} {1,2}\{1,2\} {1}\{1\}

Four reachable subsets, out of a possible 23=82^3 = 8. The subsets correspond to the last two symbols read: bbbb, baba, aaaa, abab. All four are distinguishable, so the minimal DFA has 4 states. In general, "kk-th from the right" gives 2k2^k.

Worked example 3: Minimizing a table

DFA over {0,1}\{0,1\}, start AA, accepting {E}\{E\}:

State 0 1
→A\to A BB CC
BB BB DD
CC BB CC
DD BB EE
∗E* E BB CC

Round 0: {A,B,C,D}\{A,B,C,D\}, {E}\{E\}.

Round 1: on symbol 1, DD goes to EE while A,B,CA, B, C stay in the big block. Split: {A,B,C}\{A,B,C\}, {D}\{D\}, {E}\{E\}.

Round 2: on symbol 1, BB goes to DD, while AA and CC go to CC. Split: {A,C}\{A,C\}, {B}\{B\}, {D}\{D\}, {E}\{E\}.

Round 3: AA and CC have identical rows (0→B0 \to B, 1→C1 \to C), so no further split.

Result: 4 states. The language is binary strings ending in 011011, which matches the "ending with a string of length 3" rule (3+1=43 + 1 = 4).

Worked example 4: Counting strings

How many strings of length 5 over {a,b}\{a, b\} contain aaaa?

Count the complement. Let f(n)f(n) be the number of strings with no aaaa. A valid string ends in bb (anything valid before it) or in baba (anything valid before that), so f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2), f(0)=1f(0) = 1, f(1)=2f(1) = 2. Then f(2)=3f(2) = 3, f(3)=5f(3) = 5, f(4)=8f(4) = 8, f(5)=13f(5) = 13.

Strings containing aaaa: 25−13=2^5 - 13 = 19.

The recurrence comes from the DFA: the states "last symbol was aa" and "last symbol was not aa" are exactly the two terms.

Worked example 5: DFA to regular expression with Arden

Take the remainder-mod-3 DFA from the transition table section, with q0q_0 as the start and only accepting state. Write equations for how each state is entered:

  • q0=ϵ+q00+q11q_0 = \epsilon + q_0 0 + q_1 1
  • q1=q01+q20q_1 = q_0 1 + q_2 0
  • q2=q10+q21q_2 = q_1 0 + q_2 1

From the third, by Arden: q2=q101∗q_2 = q_1 0 1^*. Substitute: q1=q01+q101∗0q_1 = q_0 1 + q_1 0 1^* 0, so q1=q01(01∗0)∗q_1 = q_0 1 (01^*0)^*. Substitute into the first: q0=ϵ+q0(0+1(01∗0)∗1)q_0 = \epsilon + q_0 (0 + 1(01^*0)^*1), so

q0=(0+1(01∗0)∗1)∗q_0 = (0 + 1(01^*0)^*1)^*

That is the regular expression for binary multiples of 3.

Common traps

  • Dead state convention. "Minimal DFA" in a question that draws partial DFAs often means without the dead state. If the question says complete, include it. Languages like "starts with abab" differ by exactly one between the two conventions.
  • Unreachable states. Remove them before minimizing.
  • Merging by accepting pattern. Modular counters can collapse: length divisible by 2 or 3 needs 6 states, but n mod 6∈{0,3}n \bmod 6 \in \{0, 3\} needs only 3.
  • Regex equivalence by guess. Test small strings against both sides: ϵ\epsilon, single symbols, and abab versus baba catch most wrong options.
  • Counting strings vs counting splits. a∗b∗a∗a^* b^* a^* has 22 strings of length 6, not 28, because a6a^6 has many splits.
  • Finite means regular. {anbn:n≤1000}\{a^n b^n : n \le 1000\} is regular.
  • Subsets of regular languages are not automatically regular. {anbn}⊆a∗b∗\{a^n b^n\} \subseteq a^* b^*.
  • NFA state bound. Subset construction gives at most 2n2^n, not n2n^2 or 2n2n.

Quick revision

  • DFA, NFA and ϵ\epsilon-NFA accept the same class: the regular languages.
  • Subset construction: at most 2n2^n DFA states from nn NFA states; tight.
  • Kleene: regex   ⟺  \iff finite automaton.
  • Arden: X=PX+QX = PX + Q, ϵ∉P\epsilon \notin P gives X=P∗QX = P^*Q.
  • Myhill-Nerode: number of classes = minimal complete DFA size; infinite classes means not regular.
  • Minimize: drop unreachable, split by acceptance, refine until stable.
  • Product construction: m×nm \times n states for intersection, union or difference.
  • "kk-th from right" needs 2k2^k states; "kk-th from left" needs k+2k + 2 with dead state.
  • To prove non-regular: distinguishable prefixes, pumping lemma, or intersect with a regular language to reach a known non-regular one.

Further reading

Test this section