Computer Science and IT‹›⌂ 0:00 studied

Module 7 · 17 min read · Free chapter

Regular expressions and finite automata

In short

A DFA reads a string one symbol at a time and has exactly one move per state and symbol. NFAs and ε-NFAs may guess, but they accept the same regular languages. The subset construction turns an n-state NFA into at most 2ⁿ DFA states, and minimisation merges equivalent states.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: a shopkeeper with a tiny memory

Imagine the shopkeeper at a ration shop who follows one strange rule this month. A family may collect sugar only if its card has an even number of stamps. The shopkeeper does not count the stamps as one, two, three and so on. He keeps just one thought in his head, "even so far" or "odd so far", and flips it each time his finger passes a stamp.

When his finger reaches the end of the card, he looks at the thought he is holding. If it says "even", the family gets sugar, and if it says "odd", it does not. However long the card is, he never needs more than these two thoughts.

That shopkeeper is a finite automaton. It reads its input one symbol at a time, from left to right, and it remembers only which of a fixed, finite number of states it is in. A set of strings that such a machine can check is called a regular language, and a regular expression is a short formula that names the same set.

Why it matters. Every search box that matches a pattern, every compiler that splits code into words and every vending machine that waits for the right coins is a finite automaton at heart. GATE regularly asks about it, very often in the form "how many states does the minimal DFA have?".

Alphabets, strings and languages

Before building machines, we need exact words for what they read. An alphabet Σ\Sigma is a finite, non-empty set of symbols, such as Σ={0,1}\Sigma = \{0, 1\} or Σ={a,b}\Sigma = \{a, b\}. A string over Σ\Sigma is a finite sequence of its symbols, like abbaabba, and its length ∣w∣\lvert w \rvert is the number of symbols in it, so ∣abba∣=4\lvert abba \rvert = 4.

The empty string ε\varepsilon has no symbols, so ∣ε∣=0\lvert \varepsilon \rvert = 0. Joining two strings end to end is concatenation: if x=abx = ab and y=bay = ba, then xy=abbaxy = abba, and εw=wε=w\varepsilon w = w\varepsilon = w for every string ww. We write wRw^R for the reverse of ww and ana^n for nn copies of aa, so a3=aaaa^3 = aaa and a0=εa^0 = \varepsilon.

The set of all strings over Σ\Sigma, including ε\varepsilon, is Σ∗\Sigma^*, read "sigma star". The set Σ+=Σ∗−{ε}\Sigma^+ = \Sigma^* - \{\varepsilon\} holds all the non-empty strings. A language is any subset of Σ∗\Sigma^*, whether finite like {ab,ba}\{ab, ba\} or infinite like {anbn∣n≥0}\{a^n b^n \mid n \ge 0\}.

Counting questions come straight from these definitions. Over an alphabet of kk symbols there are knk^n strings of length exactly nn, and kn+1−1k−1\dfrac{k^{n+1} - 1}{k - 1} strings of length at most nn when k≥2k \ge 2. A string of length nn has n+1n + 1 prefixes and n+1n + 1 suffixes, counting ε\varepsilon and the string itself. If all its symbols are different, it has n(n+1)2+1\dfrac{n(n + 1)}{2} + 1 distinct substrings, including ε\varepsilon.

For example, over {0,1}\{0, 1\} there are 23=82^3 = 8 strings of length 3 and 1+2+4+8=151 + 2 + 4 + 8 = 15 strings of length at most 3. The string abcdabcd has 5 prefixes and 4×52+1=11\dfrac{4 \times 5}{2} + 1 = 11 substrings.

Why it matters. Every later idea, from automata to grammars to Turing machines, is a way of describing a language. If you mix up ε\varepsilon, ∅\emptyset and {ε}\{\varepsilon\} here, you will lose marks in every chapter that follows.

Deterministic finite automata

A deterministic finite automaton (DFA) is the shopkeeper written in mathematics. It is a 5-tuple M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F), where QQ is a finite set of states, Σ\Sigma is the input alphabet, q0∈Qq_0 \in Q is the start state and F⊆QF \subseteq Q is the set of accepting states. The transition function δ:Q×Σ→Q\delta : Q \times \Sigma \to Q says where to go next, so δ(q0,a)=q1\delta(q_0, a) = q_1 means that in state q0q_0, on reading aa, the machine moves to q1q_1.

Deterministic means that for each state and each symbol there is exactly one next state. We extend δ\delta to whole strings and call it δ^\hat{\delta}: start in q0q_0, follow one arrow per symbol and see where you end. The DFA accepts ww if δ^(q0,w)∈F\hat{\delta}(q_0, w) \in F, and its language is L(M)={w∈Σ∗∣δ^(q0,w)∈F}L(M) = \{w \in \Sigma^* \mid \hat{\delta}(q_0, w) \in F\}.

Let us build a DFA for all strings over {a,b}\{a, b\} that end in abab. We only need to remember how much of abab we have just seen. That is nothing useful (q0q_0), a final aa (q1q_1), or a final abab (q2q_2, accepting).

State Meaning On aa On bb
q0q_0 Start; nothing useful seen. q1q_1 q0q_0
q1q_1 The last symbol was aa. q1q_1 q2q_2
q2q_2 Accepting; the last two were abab. q1q_1 q0q_0

Run it on aabaab: the path is q0→aq1→aq1→bq2q_0 \xrightarrow{a} q_1 \xrightarrow{a} q_1 \xrightarrow{b} q_2, which ends in an accepting state, so aabaab is accepted. On abbabb the path is q0→q1→q2→q0q_0 \to q_1 \to q_2 \to q_0, which ends outside FF, so abbabb is rejected.

A DFA must have an arrow for every symbol from every state. When a string can never be accepted once it goes wrong, we send it to a dead state, a non-accepting state that loops to itself on every symbol. For example, the DFA for strings that start with abab needs states for "nothing yet", "seen aa", "seen abab" and a dead state, which makes 4.

Why it matters. A DFA does a single table look-up per symbol, so it checks a string of length nn in O(n)O(n) time with memory that never grows. That is why the lexical analyser of a compiler and the pattern matcher in a network device are built as DFAs.

NFA, ε-NFA and the subset construction

A nondeterministic finite automaton (NFA) relaxes the rule of exactly one arrow. From a state, a symbol may lead to several states or to none, so δ:Q×Σ→2Q\delta : Q \times \Sigma \to 2^Q, where 2Q2^Q is the set of all subsets of QQ. An NFA accepts ww if at least one path labelled ww ends in an accepting state, and paths that get stuck simply die.

Think of an NFA as a machine that is allowed to guess. For strings ending in abab, the NFA stays in q0q_0 on every symbol, but on an aa it may also guess that this is the aa of the final abab and jump to q1q_1. From q1q_1, a bb leads to the accepting state q2q_2, which has no outgoing arrows at all.

An ε\varepsilon-NFA may also move on ε\varepsilon, without reading anything. The ε\varepsilon-closure of a state qq is the set of states reachable from qq using only ε\varepsilon-moves, including qq itself. To process a symbol, you take the ε\varepsilon-closure, follow the symbol, and take the ε\varepsilon-closure again.

These extras add convenience, not power. DFAs, NFAs and ε\varepsilon-NFAs accept exactly the same class of languages, the regular languages. The proof is the subset construction: each DFA state is a set of NFA states, namely all the states the NFA could be in after the input read so far.

Apply it to the NFA above. Start with {q0}\{q_0\}, which goes to {q0,q1}\{q_0, q_1\} on aa and stays at {q0}\{q_0\} on bb. From {q0,q1}\{q_0, q_1\}, the symbol aa gives {q0,q1}\{q_0, q_1\} and bb gives {q0,q2}\{q_0, q_2\}. From {q0,q2}\{q_0, q_2\}, the symbol aa gives {q0,q1}\{q_0, q_1\} and bb gives {q0}\{q_0\}.

Only three subsets are reachable, and {q0,q2}\{q_0, q_2\} is accepting because it contains q2q_2. This is exactly the three-state DFA we built by hand, with new names.

In general, an NFA with nn states can need up to 2n2^n DFA states, and this blow-up really happens. The language of strings over {0,1}\{0, 1\} whose kk-th symbol from the end is 1 has an NFA with k+1k + 1 states, but its minimal DFA has 2k2^k states. The reason is that the DFA must remember all of the last kk symbols, since any of them may turn out to be the one that counts.

Why it matters. NFAs are easy to design and easy to build from regular expressions, while DFAs are fast to run. The subset construction is the bridge between them, and GATE often asks how many states the resulting DFA has.

Minimising a DFA and counting states

Two DFAs can accept the same language with different numbers of states. Minimisation finds the smallest one, and the result is unique up to renaming the states. It works in two steps: delete every state that cannot be reached from the start state, then merge states that behave the same.

Two states pp and qq are equivalent if, for every string ww, the states δ^(p,w)\hat{\delta}(p, w) and δ^(q,w)\hat{\delta}(q, w) are both accepting or both non-accepting. The partition method finds them. Start with two groups, FF and Q−FQ - F, then split any group whose members go to different groups on some symbol, and repeat until nothing splits.

Take this DFA over {0,1}\{0, 1\} with start state AA and accepting state EE. Its moves are A→0BA \xrightarrow{0} B, A→1CA \xrightarrow{1} C, B→0BB \xrightarrow{0} B, B→1DB \xrightarrow{1} D, C→0BC \xrightarrow{0} B, C→1CC \xrightarrow{1} C, D→0BD \xrightarrow{0} B, D→1ED \xrightarrow{1} E, E→0BE \xrightarrow{0} B and E→1CE \xrightarrow{1} C. The partition changes like this:

{A,B,C,D},{E}  ⇒  {A,B,C},{D},{E}  ⇒  {A,C},{B},{D},{E}\{A, B, C, D\}, \{E\} \;\Rightarrow\; \{A, B, C\}, \{D\}, \{E\} \;\Rightarrow\; \{A, C\}, \{B\}, \{D\}, \{E\}

In the first round DD leaves its group, because on 1 it goes to EE while the others stay inside. In the second round BB leaves, because on 1 it goes to DD, which is now a group of its own. AA and CC never split, since both go to BB on 0 and to CC on 1, so the minimal DFA has 4 states. It accepts the strings that end in 011011, and a fixed ending of length 3 always needs 3+1=43 + 1 = 4 states.

GATE asks the same counting question in many forms, so it pays to know the common answers. The table below gives the number of states in the minimal complete DFA over {a,b}\{a, b\}.

Language States
Length divisible by nn nn
Number of aa divisible by mm and number of bb divisible by nn mnmn
Contains a fixed substring of length kk, such as abbabb k+1k + 1
Ends with a fixed string of length kk k+1k + 1
Starts with a fixed string of length kk k+2k + 2
Length exactly kk, or length at most kk k+2k + 2
Length at least kk k+1k + 1
Binary numbers divisible by an odd nn, such as 3 or 5 nn
The kk-th symbol from the end is fixed 2k2^k

For example, binary strings read from the most significant bit and divisible by 3 need 3 states, one for each remainder 0, 1 and 2. Reading bit bb in remainder rr moves to remainder (2r+b) mod 3(2r + b) \bmod 3. So from remainder 1, a 0 leads to remainder 2 and a 1 leads to remainder 0.

Why it matters. Fewer states mean a smaller table and cheaper hardware, which matters in lexers and in circuits. In GATE, a question that asks for a number of states almost always means the minimal complete DFA, so knowing these patterns saves real time.

Regular expressions and conversion both ways

A regular expression is a formula that names a regular language. The basic expressions are ∅\emptyset (no strings), ε\varepsilon (only the empty string) and a single symbol aa (only the string aa). Bigger expressions are built with three operations: union r+sr + s, concatenation rsrs and Kleene star r∗r^*, which means zero or more copies.

Star binds tightest, then concatenation, then union. So ab∗+cab^* + c means (a(b∗))+c(a(b^*)) + c, and not (ab)∗+c(ab)^* + c or a(b∗+c)a(b^* + c). You should be able to read these expressions at sight:

  • (a+b)∗(a + b)^* is every string over {a,b}\{a, b\}, including ε\varepsilon.
  • (a+b)∗abb(a + b)^* abb is every string that ends in abbabb.
  • (a+b)∗a(a+b)∗(a + b)^* a (a + b)^* is every string with at least one aa.
  • ((a+b)(a+b))∗((a + b)(a + b))^* is every string of even length.
  • (b+ab)∗(ε+a)(b + ab)^* (\varepsilon + a) is every string in which no two aa's are next to each other.

Some identities let you simplify or compare expressions quickly:

∅∗=ε,ε∗=ε,(r∗)∗=r∗,r+∅=r,rε=r,r∅=∅\emptyset^* = \varepsilon, \quad \varepsilon^* = \varepsilon, \quad (r^*)^* = r^*, \quad r + \emptyset = r, \quad r\varepsilon = r, \quad r\emptyset = \emptyset
(a+b)∗=(a∗b∗)∗=(a∗+b∗)∗,(ab)∗a=a(ba)∗(a + b)^* = (a^* b^*)^* = (a^* + b^*)^*, \qquad (ab)^* a = a(ba)^*

Kleene's theorem says that regular expressions and finite automata describe exactly the same languages. To go from an expression to an automaton, Thompson's construction builds a tiny ε\varepsilon-NFA for each symbol and glues the pieces together. Union adds a new start state with ε\varepsilon-arrows into both parts, concatenation links the accepting state of the first part to the start of the second, and star adds ε\varepsilon-arrows that allow skipping or repeating.

To go back from an automaton to an expression, use state elimination or Arden's theorem. In state elimination you remove one state at a time and relabel the arrows that passed through it. If a removed state has a self-loop ss, an incoming arrow labelled r1r_1 and an outgoing arrow labelled r2r_2, the new direct arrow gets the label r1s∗r2r_1 s^* r_2.

Arden's theorem says that if ε∉P\varepsilon \notin P, the equation below has exactly one solution:

R=Q+RP  ⇒  R=QP∗R = Q + RP \;\Rightarrow\; R = QP^*

Take the two-state DFA with start state q0q_0 and accepting state q1q_1, where aa always leads to q1q_1 and bb always leads to q0q_0. Write one equation per state, listing the ways to arrive there:

q0=ε+q0b+q1bq1=q0a+q1a\begin{aligned} q_0 &= \varepsilon + q_0 b + q_1 b \\ q_1 &= q_0 a + q_1 a \end{aligned}

The second equation has the form R=Q+RPR = Q + RP with Q=q0aQ = q_0 a and P=aP = a, so q1=q0aa∗q_1 = q_0 a a^*. Putting this into the first gives q0=ε+q0(b+aa∗b)q_0 = \varepsilon + q_0(b + aa^*b), so q0=(b+aa∗b)∗q_0 = (b + aa^*b)^*. Hence the language is q1=(b+aa∗b)∗aa∗q_1 = (b + aa^*b)^* aa^*, which simplifies to (a+b)∗a(a + b)^* a, the strings that end in aa.

Why it matters. Programmers write search patterns as regular expressions, and tools such as grep and lexer generators turn them into automata behind the scenes. GATE tests both directions, and it also checks whether two expressions name the same language.

Machines with output: Moore and Mealy

So far our machines only say yes or no at the end. A transducer gives an output as it goes, and there are two kinds. A Moore machine attaches an output to each state, λ:Q→Δ\lambda : Q \to \Delta, while a Mealy machine attaches an output to each transition, λ:Q×Σ→Δ\lambda : Q \times \Sigma \to \Delta, where Δ\Delta is the output alphabet.

A Moore machine gives the output of its start state before reading anything, so an input of length nn produces n+1n + 1 outputs. A Mealy machine produces exactly nn outputs, one per symbol read. For an input of length 4, such as 01100110, a Moore machine prints 5 symbols and a Mealy machine prints 4.

The two models are equally powerful. A Moore machine converts to a Mealy machine with the same number of states, by moving each state's output onto the arrows that enter it. The other way, each Mealy state is split according to the output on its incoming arrows, so the Moore machine may need up to ∣Q∣×∣Δ∣\lvert Q \rvert \times \lvert \Delta \rvert states.

Why it matters. Moore and Mealy machines are the same models used to design sequential circuits such as sequence detectors and traffic-light controllers. Exam questions on them usually ask for the number of outputs or the number of states after a conversion.

Descriptive lens: how written papers ask this

Written papers for scientist posts, state Assistant Programmer exams and university tests ask about finite automata in four ways:

  1. Define a DFA, an NFA and an ε\varepsilon-NFA formally, as tuples, and explain how acceptance differs.
  2. Construct an automaton for a language given in words, and show its transition table.
  3. Convert between models: NFA to DFA by the subset construction, DFA to minimal DFA, and expression to automaton or back.
  4. Prove a general result, such as the equivalence of NFAs and DFAs or Arden's theorem.

Practice questions (our own, not previous-year questions).

  1. Define a DFA and an NFA formally. Prove that for every NFA there is a DFA that accepts the same language. (10 marks)
  2. Construct the minimal DFA that accepts binary numbers divisible by 5, read from the most significant bit, and give its transition table. (10 marks)
  3. Convert the regular expression (a+b)∗abb(a + b)^* abb into an NFA, then into a DFA by the subset construction, and minimise the result. (15 marks)

Answer plan for question 3.

  • Draw an NFA with four states q0q_0 to q3q_3, in which q0q_0 loops on aa and bb and the path q0→aq1→bq2→bq3q_0 \xrightarrow{a} q_1 \xrightarrow{b} q_2 \xrightarrow{b} q_3 spells abbabb.
  • Start the subset construction from {q0}\{q_0\} and list each new subset with its moves on aa and bb in a table.
  • Show that the reachable subsets are {q0}\{q_0\}, {q0,q1}\{q_0, q_1\}, {q0,q2}\{q_0, q_2\} and {q0,q3}\{q_0, q_3\}, and mark {q0,q3}\{q_0, q_3\} as the only accepting state.
  • Apply the partition method from the groups of accepting and non-accepting states, and show that every group finally splits into single states.
  • Conclude that the minimal DFA has 4 states, which matches the rule that a fixed ending of length 3 needs 4 states.

Quick revision

  • An alphabet is finite, a string is a finite sequence, and a language is any subset of Σ∗\Sigma^*.
  • ∅\emptyset has no strings, {ε}\{\varepsilon\} has one, and ∅∗={ε}\emptyset^* = \{\varepsilon\}.
  • A DFA is (Q,Σ,δ,q0,F)(Q, \Sigma, \delta, q_0, F) with exactly one move per state and symbol, and it runs in O(n)O(n) time.
  • An NFA may have many moves or none, and an ε\varepsilon-NFA may also move without reading.
  • DFA, NFA and ε\varepsilon-NFA are equal in power, and the subset construction gives at most 2n2^n DFA states.
  • The kk-th symbol from the end needs 2k2^k DFA states but only k+1k + 1 NFA states.
  • Minimisation removes unreachable states and then splits the partition {F,Q−F}\{F, Q - F\} until it is stable.
  • A fixed ending or substring of length kk needs k+1k + 1 states, and a fixed beginning needs k+2k + 2.
  • Star binds before concatenation, and concatenation binds before union.
  • Arden's theorem solves R=Q+RPR = Q + RP as R=QP∗R = QP^* when ε∉P\varepsilon \notin P.
  • A Moore machine prints n+1n + 1 symbols for nn inputs, and a Mealy machine prints nn.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. How many strings of length exactly 3 are there over Σ={0,1}\Sigma = \{0, 1\}?

  2. Which statement is TRUE?

  3. In a DFA, the transition function has the form:

  4. Which statement about finite automata is TRUE?

  5. An NFA has 4 states. The DFA from the subset construction has at most:

  6. The minimal DFA for binary strings whose 3rd symbol from the end is 1 has:

  7. In DFA minimisation, the first partition of the states is:

  8. How many states does the minimal complete DFA have for strings over {a,b}\{a, b\} that start with abab?

  9. The minimal DFA for strings over {a,b}\{a, b\} in which the number of aa is divisible by 2 and the number of bb is divisible by 3 has:

  10. The minimal DFA that accepts binary numbers divisible by 3 has:

  11. Which pair of regular expressions describes the SAME language?

  12. Which expression describes all strings over {a,b}\{a, b\} with no two consecutive aa?

  13. By Arden's theorem, if ε∉P\varepsilon \notin P, the solution of R=Q+RPR = Q + RP is:

  14. For an input string of length 6, a Moore machine produces how many output symbols?

  15. Which topic is listed under Theory of Computation in the GATE CS syllabus?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Are NFAs more powerful than DFAs?

No. Every NFA can be turned into a DFA by the subset construction, so both accept exactly the regular languages.

How many states can the subset construction produce?

At most 2ⁿ for an NFA with n states, and this bound is reached for some languages.

Is the dead state counted in a minimal DFA?

Yes. GATE questions on minimal DFAs mean the complete DFA, so the dead state is counted when it is needed.

What does Arden's theorem say?

If P does not contain ε, the equation R = Q + RP has the unique solution R = QP*.

More in this module: Theory of computation

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Theory of Computation (IIT Kanpur, 106104148): finite automata, nondeterminism and regular expressions