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
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 :
- : finite set of states
- : finite input alphabet
- : transition function, exactly one move per state and symbol
- : start state
- : accepting states
Extend to strings: and . The language is . 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 for binary numbers divisible by 3, read most significant bit first. State means "value so far leaves remainder ", and reading bit moves remainder to . Start and accept at .
| State | 0 | 1 |
|---|---|---|
The same idea gives a -state remainder DFA for any modulus , but the minimal DFA can be smaller. For divisibility by 6 the remainders and 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 : zero, one or many moves per symbol. It accepts if some run ends in an accepting state. An -NFA also allows moves on that consume no input. The -closure is the set of states reachable from using only -moves.
Subset construction
Every NFA (with or without -moves) has an equivalent DFA:
- The DFA start state is .
- From a DFA state on symbol , go to .
- A DFA state is accepting if it contains an NFA accepting state.
With NFA states there are at most DFA states (including , which acts as the dead state). The bound is tight: "the -th symbol from the right is " has an -state NFA but needs DFA states.
So DFAs, NFAs and -NFAs all accept exactly the regular languages. Nondeterminism buys succinctness, not power.
Regular expressions
Regular expressions over are built from , , and each 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 |
|---|---|
| definition of star | |
| starring twice adds nothing | |
| all strings | |
| shifting brackets | |
| star of the empty set | |
| concatenating with nothing | |
| in general | is in the left side only |
Arden's rule
If does not contain , then the equation (or for the right-linear form) has the unique solution (respectively ). 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 , call strings and equivalent () if for every suffix , . The theorem:
- is regular if and only if has finitely many classes.
- The number of classes equals the number of states in the minimal complete DFA for .
This gives the cleanest proofs in both directions. To show a lower bound of states, find prefixes that are pairwise distinguishable (each pair has a separating suffix). To show non-regularity, find infinitely many pairwise distinguishable prefixes. For , the prefixes work: and are separated by .
DFA minimization
The minimal DFA is unique up to renaming of states. Two standard procedures produce it.
Moore (partition refinement).
- Remove states unreachable from the start.
- Start with two blocks: accepting and non-accepting.
- Split a block whenever two of its states go, on some symbol, to different blocks.
- 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 if for some symbol the pair 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 of length | |
| strings containing a fixed substring of length | |
| strings starting with a fixed string of length | (includes dead) |
| -th symbol from the left is fixed | (includes dead) |
| -th symbol from the right is fixed (binary) | |
| length | |
| and together | (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, "" 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 were regular, then would be regular; if that intersection is , 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 equals number of ", 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 for strings ending in .
Track the longest suffix of the input that is a prefix of : none, , or .
| State | a | b |
|---|---|---|
| (no progress) | ||
| (seen ) | ||
| (seen ) |
Notice on stays in : after , the last still starts a possible . Distinguishability: is the only accepting state; and are separated by the suffix . Answer: 3 states, no dead state needed.
Worked example 2: Subset construction
NFA over for "second-last symbol is ": states , start , accept . Moves: , , .
| DFA state | a | b |
|---|---|---|
Four reachable subsets, out of a possible . The subsets correspond to the last two symbols read: , , , . All four are distinguishable, so the minimal DFA has 4 states. In general, "-th from the right" gives .
Worked example 3: Minimizing a table
DFA over , start , accepting :
| State | 0 | 1 |
|---|---|---|
Round 0: , .
Round 1: on symbol 1, goes to while stay in the big block. Split: , , .
Round 2: on symbol 1, goes to , while and go to . Split: , , , .
Round 3: and have identical rows (, ), so no further split.
Result: 4 states. The language is binary strings ending in , which matches the "ending with a string of length 3" rule ().
Worked example 4: Counting strings
How many strings of length 5 over contain ?
Count the complement. Let be the number of strings with no . A valid string ends in (anything valid before it) or in (anything valid before that), so , , . Then , , , .
Strings containing : 19.
The recurrence comes from the DFA: the states "last symbol was " and "last symbol was not " 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 as the start and only accepting state. Write equations for how each state is entered:
From the third, by Arden: . Substitute: , so . Substitute into the first: , so
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 " 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 needs only 3.
- Regex equivalence by guess. Test small strings against both sides: , single symbols, and versus catch most wrong options.
- Counting strings vs counting splits. has 22 strings of length 6, not 28, because has many splits.
- Finite means regular. is regular.
- Subsets of regular languages are not automatically regular. .
- NFA state bound. Subset construction gives at most , not or .
Quick revision
- DFA, NFA and -NFA accept the same class: the regular languages.
- Subset construction: at most DFA states from NFA states; tight.
- Kleene: regex finite automaton.
- Arden: , gives .
- 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: states for intersection, union or difference.
- "-th from right" needs states; "-th from left" needs with dead state.
- To prove non-regular: distinguishable prefixes, pumping lemma, or intersect with a regular language to reach a known non-regular one.