Skip to content
GATEiit

GATE CSE · Theory of Computation

Regular expressions and finite automata

Deterministic and nondeterministic finite automata, subset construction, regular expressions and their identities, conversions between them (Arden's theorem), and DFA minimisation. GATE asks minimal DFA state counts, the language of a regex and equivalences.

Before you start

  • Sets, relations and equivalence classes →

    Automata are defined with sets and functions; minimisation merges equivalence classes of states.

  • Directed graphs →

    A finite automaton is a labelled directed graph; reachability decides which states matter.

  • Strings and alphabets

    Concatenation, prefixes, suffixes and Σ∗ are the basic objects of every question.

This topic comes before: Context-free grammars and push-down automata, Lexical analysis, parsing and syntax-directed translation, Regular and context-free languages; pumping lemma

Concept notes

1. Deterministic finite automata (DFA)

A DFA is a 5-tuple (Q,Σ,δ,q0,F): finite states Q, input alphabet Σ, transition function δ:Q×Σ→Q, start state q0 and accepting states F⊆Q. Each state has exactly one move on each symbol. The extended function δ^(q,w) follows the string symbol by symbol; w is accepted if δ^(q0,w)∈F. The set of accepted strings is L(M), and a language is regular if some DFA accepts it.

Design by meaning of states. Decide what each state must remember about the input read so far.

Language over {a,b} What a state remembers Minimal DFA states
number of a's ≡0(modn) count mod n n
ends in ab longest suffix that is a prefix of ab 3
contains aba progress towards aba 4
length ≡0(mod3) and even number of a's (length mod 3, a-count mod 2) 6
binary numbers divisible by n value mod n at most n

For the value of a binary string mod n: reading bit c in state r moves to (2r+c) mod n.

A dead (trap) state is a non-accepting state with all moves to itself. Questions that ask for a DFA usually expect a complete DFA, so count the dead state if the language needs one (for example, strings that start with ab).

ba q0q1q2 abab
Minimal DFA over {a, b} for strings ending in ab. q0: no progress, q1: just read a, q2 (accepting): just read ab

2. NFA, epsilon moves and the subset construction

An NFA may have zero, one or many moves on a symbol: δ:Q×Σ→2Q. An ε-NFA can also move without reading input. A string is accepted if some path of choices ends in an accepting state.

NFAs, ε-NFAs and DFAs accept exactly the same class: the regular languages. The subset construction converts an NFA to a DFA:

  1. The DFA start state is ε-closure(q0).
  2. From a set S on symbol a, move to ε-closure(⋃q∈Sδ(q,a)).
  3. A DFA state is accepting if it contains any NFA accepting state.

Only reachable subsets need to be built. An n-state NFA can need up to 2n DFA states. The standard witness: strings whose k-th symbol from the end is a have an NFA with k+1 states but a minimal DFA with 2k states, because the DFA must remember the last k symbols.

For the NFA in the figure (k=2), the reachable subsets are {p0},{p0,p1},{p0,p2},{p0,p1,p2}: 4 DFA states, the last two accepting.

Closure properties of regular languages: union, intersection, complement, difference, concatenation, Kleene star, reversal and homomorphism. Product construction runs two DFAs in parallel on pairs of states (for intersection or union). Complement swaps accepting and non-accepting states of a complete DFA; doing it on an NFA is wrong.

a, b p0p1p2 aa, b
NFA for strings whose second-last symbol is a: it guesses which a is second from the end

3. Regular expressions and their identities

Regular expressions over Σ: ∅, ε and each symbol a are regular expressions; if r,s are, so are r+s (union), rs (concatenation) and r∗ (zero or more). Precedence: star, then concatenation, then union. So ab∗+c means (a(b∗))+c.

Kleene's theorem: a language is regular if and only if some regular expression denotes it.

Identity Note
∅+r=r,  ∅r=∅ ∅ kills concatenation
εr=r,  ∅∗=ε,  ε∗=ε
r∗r∗=r∗,  (r∗)∗=r∗
r+=rr∗=r∗r one or more
(a+b)∗=(a∗b∗)∗=(a∗+b∗)∗=a∗(ba∗)∗ all strings
(rs)∗r=r(sr)∗ shifting rule
(a+b)∗≠a∗+b∗ ab is in the left only

To check a claimed equality, test short strings, especially ε and strings like ab, ba, aab. One string in one language but not the other settles it.

Useful patterns: strings with no two consecutive a's are (b+ab)∗(ε+a); strings with an even number of a's are (b∗ab∗a)∗b∗; strings whose second-last symbol is a are (a+b)∗a(a+b).

4. Converting automata to expressions and minimising DFAs

Arden's theorem. If P does not contain ε, the equation R=Q+RP has the unique solution R=QP∗. To get a regular expression from a DFA, write one equation per state: a state equals the sum over its incoming edges (source state times symbol), plus ε for the start state. Solve by substitution and Arden's rule, then add the expressions of the accepting states. (State elimination gives the same answer by removing states one at a time and relabelling edges with expressions.)

RE to NFA (Thompson's construction) builds an ε-NFA with small pieces for union, concatenation and star.

Minimisation. Two states p,q are equivalent if for every string w, δ^(p,w)∈F  ⟺  δ^(q,w)∈F. To minimise:

  1. Remove states unreachable from the start.
  2. Split into accepting and non-accepting groups.
  3. Repeatedly split a group if two of its states go, on some symbol, to different groups.
  4. When nothing splits, each group is one state of the minimal DFA.

The minimal DFA is unique up to renaming states. By the Myhill–Nerode theorem, its number of states equals the number of classes of the relation x≡y iff, for all z, xz∈L  ⟺  yz∈L. To prove a DFA needs at least k states, find k strings that are pairwise distinguishable by some suffix.

Two regular expressions or automata are equivalent exactly when their minimal DFAs are the same.

Formulas to remember

DFA
M=(Q,Σ,δ,q0,F),δ:Q×Σ→Q
NFA transition
δ:Q×(Σ∪{ε})→2Q
Subset construction bound
∣QDFA∣≤2∣QNFA∣
Arden's theoremWhen ε∉L(P)
R=Q+RP  ⇒  R=QP∗
Binary value mod n
δ(r,c)=(2r+c) mod n
Product DFA sizeFor intersection or union of two DFAs
∣Q1∣×∣Q2∣

Common mistakes

  • Forgetting the dead state when the question asks for a complete DFA, for example for strings that start with a fixed prefix.
  • Complementing an NFA by swapping its accepting states; that works only on a complete DFA.
  • Reading ab∗ as (ab)∗; star binds tighter than concatenation.
  • Assuming (a+b)∗=a∗+b∗; the right side has no string with both letters.
  • Applying Arden's rule R=QP∗ when P contains ε; the solution is then not unique.
  • Merging states in minimisation only because they have the same accept status; their moves must also lead to the same groups.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Minimal DFA for strings ending in ab

numerical

Over {a,b}, design the minimal DFA for strings that end in ab and count its states.

  1. Step 1: Decide what each state remembers.

    Try it on paper first.

Example 2: Subset construction

numerical

Convert the NFA in the figure (second-last symbol is a) into a DFA. How many reachable DFA states are there, and which accept?

  1. Step 1: Start from the start set.

    Try it on paper first.

Example 3: Regular expression from a DFA by Arden's theorem

numerical

Use Arden's theorem on the DFA of the figure to find a regular expression for its language.

  1. Step 1: Write one equation per state.

    Try it on paper first.

Example 4: Minimising a DFA

numerical

A DFA over {0,1} has start q0, accepting {q2,q3}, and moves (on 0, on 1): q0: (q1,q2), q1: (q1,q3), q2: (q4,q2), q3: (q4,q3), q4: (q1,q2). Minimise it.

  1. Step 1: Make the first split.

    Try it on paper first.

Previous year questions

25 official GATE CSE questions on this topic, newest first.

Practise these 25 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.