Designing a Deterministic Finite Automaton
How to build a deterministic finite automaton for a given language: ask what the machine must remember, discover five design strategies (one fact, progress in a pattern, counting modulo, stages, a trap state) and practise each at once, on validators from real life.
Last updated:
In this lesson we learn to build a deterministic finite automaton (DFA) for a language described in words, guided by the question “what must the machine remember?”.
We meet five design strategies: remember one fact, follow progress in a pattern, count modulo, read the stages of the word, and send invalid input to a trap state; and we test the automaton we built on edge-case words.
We practise the skill on validators: components that check whether an input satisfies a validity rule.
Designing a validator: from specification to automaton
A validator is a component that reads input character by character and says whether it satisfies a rule:
an amount field in a form, a PIN code, a file name. When the rule can be checked with finite memory, the validator can be built as a DFA, correct by construction and open to testing.
The specification says what is valid; the question is how to get from it to an automaton.
The question that leads the design is what must the machine remember? In every topic we start with an attempt:
we build an example validator and discover what must be remembered, then we name the approach, and we practise it at once, before moving to the next topic.
Remember one fact
Think first: a validator accepts words over that end with .
What must it remember at every moment, and how many states does that mean?
Hint
What matters about the input that has already passed, apart from the last letter that was read?
Answer
Only the last letter: is it . Two states: , not yet (start), and , just read (accepting).
A state’s name is the letter with a subscript that says what the state remembers:
in the last letter is , and the subscript marks a state with no progress yet, the start state, to which every returns.
Every leads to and every to .
Transitions of the diagram:
- δ(q_0, a) = q_0
- δ(q_0, b) = q_b
- δ(q_b, a) = q_0
- δ(q_b, b) = q_b
This is the first strategy: remember one fact. When the rule depends on one detail of what was read (the last letter, the first letter, whether something appeared), use one state for each value of the detail.
1. Build a DFA over that accepts even binary numbers, that is, nonempty words that end with .
Hint
Which fact about the word decides whether the number is even?
Answer
Two states: (“not even”: start, or the last letter is not ) and (the last letter is , so the number is even; accepting).
The subscript of each state is a property of the number read so far:
even, and (not even; is the symbol for “not”).
The empty word is not an even number, so the start state is .
leads to and to from every state.
The empty word is rejected, because is not accepting.
Follow progress in a pattern
A validator of a pattern (for example a file name that ends with a given extension) must know how much of the end of the input matches the pattern.
We start with an attempt:
2. Build a DFA with at most states over for the words that end with .
Give it as a diagram and as a formal tuple with a transition table.
Hint
What must be remembered about the part of the word already read?
Think about how many letters of the end of the word matches so far.
Answer
States named by the part of that the end of the word already matches:
(matches nothing), (the word ends with ) and (ends with , accepting).
From : , . From : , . From :
, .
Transitions of the diagram:
- δ(q_0, a) = q_a
- δ(q_0, b) = q_0
- δ(q_a, a) = q_a
- δ(q_a, b) = q_ab
- δ(q_ab, a) = q_a
- δ(q_ab, b) = q_0
For a pattern of length , states suffice: the machine remembers how many letters of the pattern have matched so far, and that number is one of the values , that is, values.
This is the second strategy: follow progress in a pattern. For “contains ” the states are the same as for “ends with ”, but the last state is a trap:
after we found it, nothing changes the answer; for “ends with ” a new goes back to and a to .
3. Explain your machine. Take a DFA for the words that end with .
In one sentence per state, say what the state remembers, and name a word that reaches it.
Then explain why no state is needed for “the last two letters are ”.
Hint
For each state of the automaton of exercise 2, finish the sentence “the run is here exactly when the word so far …”.
After reading from or , does it matter whether the letter before was a ?
Answer
: the word does not end with or (for example or ).
: the word ends with (for example or ).
: the word ends with (for example ). A state for “ends with ” is not needed because a matters only right after an ; whether the letter before was a changes nothing about what the word still needs in order to end with .
4. A base (a nitrogenous base) is one of the four letters of DNA:
. A start codon is the sequence , where a stretch that codes for a protein begins.
Build a DFA over that accepts every sequence containing a start codon, and test it on and on .
Hint
Track the progress in the pattern . How many of its letters have just been read at the end of the input, and what must be remembered when an comes after ?
It can start a new .
Answer
Four states, each named by the part of that the end of the input already matches:
(no progress), (the sequence ends with ), (ends with ), (found , accepting and a trap: every letter stays in ).
From : , any other letter stays. From : , , .
From : , , . The run on is (accepted); the run on is (rejected: after came an , not a ).
Transitions of the diagram:
- δ(q_0, A) = q_A
- δ(q_0, C) = q_0
- δ(q_0, G) = q_0
- δ(q_0, T) = q_0
- δ(q_A, A) = q_A
- δ(q_A, C) = q_0
- δ(q_A, G) = q_0
- δ(q_A, T) = q_AT
- δ(q_AT, A) = q_A
- δ(q_AT, C) = q_0
- δ(q_AT, G) = q_ATG
- δ(q_AT, T) = q_0
- δ(q_ATG, A) = q_ATG
- δ(q_ATG, C) = q_ATG
- δ(q_ATG, G) = q_ATG
- δ(q_ATG, T) = q_ATG
Meaningful state names. In the machines we built so far, the name of a state says what the state remembers:
a letter and a subscript, and the subscript is the information the state keeps.
In the start-codon recognizer, means “the input so far ends with ”.
Names like these make the diagram easier to read, and they also make the formal description easier, because it is clear how to fill in the transition table:
from , the letter completes the pattern and leads to , the letter starts a new match and leads to , and the letters and lead back to .
When one question involves two automata, we keep the same form and tell them apart by the letter:
for the states of the first automaton and for the states of the second.
Count modulo
Think first: a validator accepts words over in which the number of ‘s is even.
What must it remember? And what if the condition is that the number of ‘s is divisible by ?
Answer
Not the count, only its remainder: one state for each remainder.
Modulo there are two states ( swaps them, keeps them), and modulo three states ( adds to the remainder modulo ).
Transitions of the diagram:
- δ(q_0, a) = q_1
- δ(q_0, b) = q_0
- δ(q_1, a) = q_2
- δ(q_1, b) = q_1
- δ(q_2, a) = q_0
- δ(q_2, b) = q_2
This is the third strategy: count modulo . When the condition depends on a count only through its remainder on division by a fixed number (an even number of ‘s, a length divisible by , the binary value of the input), use one state for each remainder, that is, states.
First find the update rule, then the table. The next example is the hardest of the three, because the update rule needs a computation:
An example from hardware: bits arrive one at a time on a wire, most significant bit first, and the device must decide whether the number received so far is divisible by (one way to verify a computation in hardware is to compare remainders modulo : the method was studied as early as Avizienis, 1971, and a patent on fault detection in an arithmetic unit shows it in use).
The most significant bit is the one that contributes the most to the number’s value, and it is the leftmost.
For example, is written in binary: the bits contribute , , and (since ), and the leftmost bit, which contributes , arrives first.
The number may have millions of bits, so here too it cannot be stored, or even have its bits counted.
The automaton for “binary number divisible by ” has states (the remainder so far), start and accepting state , and for every bit , .
Why this formula: appending a bit to a binary number of value gives the value , and if then , so the new remainder depends only on the old remainder and on :
In the transition table below, the arrow marks the start state and the asterisk an accepting state:
| State | on 0 | on 1 |
|---|---|---|
Transitions of the diagram:
- δ(q_0, 0) = q_0
- δ(q_0, 1) = q_1
- δ(q_1, 0) = q_2
- δ(q_1, 1) = q_0
- δ(q_2, 0) = q_1
- δ(q_2, 1) = q_2
On the word the run is
that is , so is accepted, and indeed . The state never held the number, only its remainder.
The empty word is accepted here, because is accepting:
exactly when . Here the empty word is read as the number ; to reject it we would add a new non-accepting start state that moves exactly like .
5. A communication line sends frames of bits, and the sender adds a bit that makes the number of ones even.
A parity validator checks that a received frame has an even number of ones.
Build a DFA over for this validator.
Hint
What must be remembered from the count of ones, and what does do?
Answer
Two states: (an even number of ones so far, start and accepting) and (odd).
keeps the state and swaps it. The empty word is accepted:
zero ones is an even number.
Stages of a word
When the input has a structure, for example a beginning followed by an end, or a sign followed by digits, it passes through stages.
We start with an attempt:
6. Build a DFA with at most states over for the words that start with and end with .
Hint
Use a state for each phase of the word. What must be known about the start, and what about the last letter, to decide whether to accept?
Answer
States (start), (started with , last letter ), (started with , last letter , accepting) and a rejecting trap .
After a correct start the subscript is the last letter. From :
, . From : , . From : , .
From : both letters to .
Transitions of the diagram:
- δ(q_0, a) = q_a
- δ(q_0, b) = q_trap
- δ(q_a, a) = q_a
- δ(q_a, b) = q_b
- δ(q_b, a) = q_a
- δ(q_b, b) = q_b
- δ(q_trap, a) = q_trap
- δ(q_trap, b) = q_trap
This is the fourth strategy: read the stages of the word. One state for each stage.
For “starts with and ends with ”: nothing has been read yet; we started correctly and the last letter is not ; we started correctly and the last letter is ; we failed at the start.
7. A form field for an amount accepts an integer with an optional sign, such as , or .
Input validation is the check that the typed text fits the format.
The input is a word over : is a sign (plus or minus), a digit and any other character.
Build a DFA that accepts exactly the words that look like an optional sign followed by at least one digit, and include a rejecting trap state for everything invalid.
Hint
Use a state for each phase of the word. Which phases lie between the start of the text and the first digit, and which phase is accepting?
What happens to a sign that comes after a digit?
Answer
Four states: (start), (a sign was read), (at least one digit was read, accepting) and (invalid, a rejecting trap).
From : , , . From : , .
From : , . From every letter stays in .
For example and are accepted, while , , and are rejected.
Transitions of the diagram:
- δ(q_0, s) = q_sign
- δ(q_0, d) = q_digits
- δ(q_0, x) = q_trap
- δ(q_sign, s) = q_trap
- δ(q_sign, d) = q_digits
- δ(q_sign, x) = q_trap
- δ(q_digits, s) = q_trap
- δ(q_digits, d) = q_digits
- δ(q_digits, x) = q_trap
- δ(q_trap, s) = q_trap
- δ(q_trap, d) = q_trap
- δ(q_trap, x) = q_trap
8. The binary-number scanner of the lesson Deterministic Finite Automata: Deciding with Fixed Memory accepts texts such as and (the alphabet is ).
Extend it to accept an optional minus sign at the start as well, as in .
The new alphabet also contains the minus sign. How many states must be added, and what are the new transitions?
Hint
The minus sign is allowed only at the start of the text. What must be remembered after a minus sign was read and before a bit was read?
And what happens to any minus sign that arrives later?
Answer
Add one state, (a minus sign was read and no bit yet), which is not accepting.
From on the minus sign go to . From on a bit go to (from there continue as before), and on the point and on the minus sign go to .
In each of the states the transition on the minus sign goes to .
In total six states. For example is accepted, while and are rejected.
9. The scanner must now recognize a decimal number: the alphabet is the digits to and the point.
(a) Build it and write its five parts. What changes compared with the binary scanner of the lesson Deterministic Finite Automata: Deciding with Fixed Memory?
Hint
Which arrows of the binary scanner were labelled ? What happens to them when there are ten digits?
Answer
The same five states and the same accepting states, . The alphabet is .
Every arrow that was labelled is now labelled with the ten digits:
, and for every digit . The transitions on the point do not change.
The number of states stays, and only the number of letters on each arrow grows.
(b) The lesson Languages: Operations and Equalities defines the language of the natural numbers written in decimal, with no leading zero (except for the number zero itself).
Build a DFA that accepts exactly that language, over the digits only.
Hint
What must be remembered after a was read as the first digit, and what after any other first digit?
What happens to a digit that arrives after a first ?
Answer
Four states: (nothing read yet), (only a was read, accepting), (a first digit other than and digits after it were read, accepting) and (invalid, a trap).
, for every , , and for every digit .
The set of accepting states is . For example and are accepted, while is rejected.
Forbidden is a trap state
A validator rejects whatever is forbidden, and once the input is invalid no continuation repairs it.
We start with an attempt:
10. A validator accepts words over that do not contain two consecutive ‘s.
Build a DFA.
Hint
What must be remembered about the last letter, and what happens when an follows an ?
Answer
Three states: (start, or the last letter is ), (the last letter is ) and (invalid, a rejecting trap state).
From : , . From : , . From :
both letters to . The states are accepting.
Transitions of the diagram:
- δ(q_0, a) = q_a
- δ(q_0, b) = q_0
- δ(q_a, a) = q_trap
- δ(q_a, b) = q_0
- δ(q_trap, a) = q_trap
- δ(q_trap, b) = q_trap
This is the fifth strategy: forbidden is a trap state. Send every invalid input to a rejecting trap state, like in the scanner; and after we found what we were looking for, the accepting state itself is a trap.
11. A validator of a short code accepts exactly the words over of length (four bits, no fewer and no more).
Build a DFA.
Hint
What must be remembered, and what happens when the word is too long?
Answer
Six states: by the number of letters read, and a rejecting trap state for input that is too long.
on both letters for every , and . Only is accepting.
Transitions of the diagram:
- δ(q_0, 0) = q_1
- δ(q_0, 1) = q_1
- δ(q_1, 0) = q_2
- δ(q_1, 1) = q_2
- δ(q_2, 0) = q_3
- δ(q_2, 1) = q_3
- δ(q_3, 0) = q_4
- δ(q_3, 1) = q_4
- δ(q_4, 0) = q_trap
- δ(q_4, 1) = q_trap
- δ(q_trap, 0) = q_trap
- δ(q_trap, 1) = q_trap
Summary: what must the machine remember?
Fill in the middle column before you click the question mark in a row:
for each strategy, what the machine remembers.
| The strategy | What the machine remembers | Examples from the lesson |
|---|---|---|
| Remember one fact | ?the value of one detail of the word (for example the last letter) | ends with ; an even binary number |
| Follow progress in a pattern | ?how many letters of the pattern have matched, from to | ends with ; a start codon |
| Count modulo | ?the remainder of the count | an even number of ‘s; a binary number divisible by ; parity |
| Read the stages of the word | ?at which stage of the structure we are | starts with and ends with ; a number with an optional sign |
| Forbidden is a trap state | ?whether the input has already been rejected (or already accepted for good) | contains no ; exactly four bits |
How many states are needed? Two prefixes must lead to different states if some continuation separates them (one of them continues into the language and the other does not):
had they reached the same state, every continuation would be handled the same way.
This gives a lower bound on the number of states.
Every time, test. Check the empty word; check each state has an arrow for each letter; run the two or three shortest words that are in the language and the shortest ones that are not.
When these strategies fail. If the machine would have to remember an unbounded quantity (for example, how many ‘s came before the ‘s in ), no finite number of states is enough.
Common mistakes
- A missing transition. In a DFA every state needs an arrow for every letter. Send forbidden letters to a trap state.
- Confusing the start state with an accepting state. Whether is accepted depends only on whether .
- Testing too few words. A few accepted words do not show that the automaton recognizes the intended language. Check border words, and justify the design by the meaning of each state.
Back to the lesson Deterministic Finite Automata: Deciding with Fixed Memory.
Next: combining automata, or see the glossary.