Module 7 · 17 min read · Free chapter
Regular expressions and finite automata
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.
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 is a finite, non-empty set of symbols, such as or . A string over is a finite sequence of its symbols, like , and its length is the number of symbols in it, so .
The empty string has no symbols, so . Joining two strings end to end is concatenation: if and , then , and for every string . We write for the reverse of and for copies of , so and .
The set of all strings over , including , is , read "sigma star". The set holds all the non-empty strings. A language is any subset of , whether finite like or infinite like .
Counting questions come straight from these definitions. Over an alphabet of symbols there are strings of length exactly , and strings of length at most when . A string of length has prefixes and suffixes, counting and the string itself. If all its symbols are different, it has distinct substrings, including .
For example, over there are strings of length 3 and strings of length at most 3. The string has 5 prefixes and 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 , and 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 , where is a finite set of states, is the input alphabet, is the start state and is the set of accepting states. The transition function says where to go next, so means that in state , on reading , the machine moves to .
Deterministic means that for each state and each symbol there is exactly one next state. We extend to whole strings and call it : start in , follow one arrow per symbol and see where you end. The DFA accepts if , and its language is .
Let us build a DFA for all strings over that end in . We only need to remember how much of we have just seen. That is nothing useful (), a final (), or a final (, accepting).
| State | Meaning | On | On |
|---|---|---|---|
| Start; nothing useful seen. | |||
| The last symbol was . | |||
| Accepting; the last two were . |
Run it on : the path is , which ends in an accepting state, so is accepted. On the path is , which ends outside , so 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 needs states for "nothing yet", "seen ", "seen " 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 in 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 , where is the set of all subsets of . An NFA accepts if at least one path labelled 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 , the NFA stays in on every symbol, but on an it may also guess that this is the of the final and jump to . From , a leads to the accepting state , which has no outgoing arrows at all.
An -NFA may also move on , without reading anything. The -closure of a state is the set of states reachable from using only -moves, including itself. To process a symbol, you take the -closure, follow the symbol, and take the -closure again.
These extras add convenience, not power. DFAs, NFAs and -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 , which goes to on and stays at on . From , the symbol gives and gives . From , the symbol gives and gives .
Only three subsets are reachable, and is accepting because it contains . This is exactly the three-state DFA we built by hand, with new names.
In general, an NFA with states can need up to DFA states, and this blow-up really happens. The language of strings over whose -th symbol from the end is 1 has an NFA with states, but its minimal DFA has states. The reason is that the DFA must remember all of the last 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 and are equivalent if, for every string , the states and are both accepting or both non-accepting. The partition method finds them. Start with two groups, and , then split any group whose members go to different groups on some symbol, and repeat until nothing splits.
Take this DFA over with start state and accepting state . Its moves are , , , , , , , , and . The partition changes like this:
In the first round leaves its group, because on 1 it goes to while the others stay inside. In the second round leaves, because on 1 it goes to , which is now a group of its own. and never split, since both go to on 0 and to on 1, so the minimal DFA has 4 states. It accepts the strings that end in , and a fixed ending of length 3 always needs 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 .
| Language | States |
|---|---|
| Length divisible by | |
| Number of divisible by and number of divisible by | |
| Contains a fixed substring of length , such as | |
| Ends with a fixed string of length | |
| Starts with a fixed string of length | |
| Length exactly , or length at most | |
| Length at least | |
| Binary numbers divisible by an odd , such as 3 or 5 | |
| The -th symbol from the end is fixed |
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 in remainder moves to remainder . 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 (no strings), (only the empty string) and a single symbol (only the string ). Bigger expressions are built with three operations: union , concatenation and Kleene star , which means zero or more copies.
Star binds tightest, then concatenation, then union. So means , and not or . You should be able to read these expressions at sight:
- is every string over , including .
- is every string that ends in .
- is every string with at least one .
- is every string of even length.
- is every string in which no two 's are next to each other.
Some identities let you simplify or compare expressions quickly:
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 -NFA for each symbol and glues the pieces together. Union adds a new start state with -arrows into both parts, concatenation links the accepting state of the first part to the start of the second, and star adds -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 , an incoming arrow labelled and an outgoing arrow labelled , the new direct arrow gets the label .
Arden's theorem says that if , the equation below has exactly one solution:
Take the two-state DFA with start state and accepting state , where always leads to and always leads to . Write one equation per state, listing the ways to arrive there:
The second equation has the form with and , so . Putting this into the first gives , so . Hence the language is , which simplifies to , the strings that end in .
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, , while a Mealy machine attaches an output to each transition, , where is the output alphabet.
A Moore machine gives the output of its start state before reading anything, so an input of length produces outputs. A Mealy machine produces exactly outputs, one per symbol read. For an input of length 4, such as , 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 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:
- Define a DFA, an NFA and an -NFA formally, as tuples, and explain how acceptance differs.
- Construct an automaton for a language given in words, and show its transition table.
- Convert between models: NFA to DFA by the subset construction, DFA to minimal DFA, and expression to automaton or back.
- Prove a general result, such as the equivalence of NFAs and DFAs or Arden's theorem.
Practice questions (our own, not previous-year questions).
- Define a DFA and an NFA formally. Prove that for every NFA there is a DFA that accepts the same language. (10 marks)
- Construct the minimal DFA that accepts binary numbers divisible by 5, read from the most significant bit, and give its transition table. (10 marks)
- Convert the regular expression 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 to , in which loops on and and the path spells .
- Start the subset construction from and list each new subset with its moves on and in a table.
- Show that the reachable subsets are , , and , and mark 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 .
- has no strings, has one, and .
- A DFA is with exactly one move per state and symbol, and it runs in time.
- An NFA may have many moves or none, and an -NFA may also move without reading.
- DFA, NFA and -NFA are equal in power, and the subset construction gives at most DFA states.
- The -th symbol from the end needs DFA states but only NFA states.
- Minimisation removes unreachable states and then splits the partition until it is stable.
- A fixed ending or substring of length needs states, and a fixed beginning needs .
- Star binds before concatenation, and concatenation binds before union.
- Arden's theorem solves as when .
- A Moore machine prints symbols for inputs, and a Mealy machine prints .