Deterministic Finite Automata: Deciding with Fixed Memory
The formal and graphical definition of a deterministic finite automaton, its language, its run, and trap states.
Last updated:
In this lesson we learn the formal definition of a deterministic finite automaton (DFA), how to follow its run on a word and decide whether the word is accepted, how to describe the language of a given automaton, and how to design an automaton for a simple language, including the use of trap states.
Recognising a binary number without remembering the input
The first component of every compiler is a scanner (lexical analyzer):
it reads the source code character by character and decides which sequences are numbers, names and keywords.
A form field does something similar when it checks what was typed.
We focus on one question for a scanner: is what has been read so far a valid binary number, that is, one or more bits (a bit is a binary digit, or ), followed, if you like, by a point and one or more bits?
and (whose value is ) are valid; , and are not.
The text can be arbitrarily long, so the scanner cannot store it, or even count its characters:
it has only a fixed, small amount of memory.
Think first: the scanner has read , then a point was read and the text became , then the bit arrived and the text became .
What does the scanner need to know at each of these moments in order to continue correctly, and at which of them is what has been read already a valid number?
Hint
At every moment ask: if one more character arrives now, what is allowed, and what makes the text invalid?
Ask also whether the value itself, or , matters for what follows.
The answer, and the idea of an automaton
After another bit continues the number, and a point opens a fractional part; the text is already valid.
After a bit must come, and another point makes the text invalid; the text is not yet valid.
After bits continue the fractional part, and a point makes the text invalid; the text is valid.
The scanner does not need the value, only what it must remember:
nothing has been read yet, a whole number has been read, a point has been read, a fractional part has been read, or the input is invalid.
A machine whose entire memory is “which of finitely many states is it in” is a finite automaton.
What is an automaton
An automaton is a state machine: it reads an input letter by letter, and each letter moves it from one state to another.
In a diagram the states are circles and the transitions are arrows labeled by a letter; the start state has an arrow coming from nowhere; an accepting state is drawn with a double circle.
The scanner is an automaton. We go through it by the five parts of an automaton, one by one.
The diagram and the highlighted parts need JavaScript. This is the transition table of the scanner (the arrow marks the start state and the asterisk an accepting state):
| state | on | on | on |
|---|---|---|---|
The formal definition
Try before the definition: write the scanner as a list of the five parts we saw in the walkthrough:
the set of states, the alphabet, the start state, the set of accepting states, and a way to write down all the transitions together.
Beside each part, write what kind of object it is: a set, an element of a set, or something else.
Answer
The states (a set); the alphabet (a set); the start state (an element of the set of states); the accepting states (a subset of the set of states); the transitions:
for each pair of a state and a letter, the next state, as in the transition table.
A rule that assigns one value to each pair is a function.
A deterministic finite automaton (DFA, “אס״ד”) is an ordered quintuple
where is a finite set of states, is the alphabet, is the start state, is the set of accepting states, and
is the transition function: is the single state reached from on reading the letter .
“Deterministic” means that for every state and every letter there is exactly one next state.
We return to the same automaton, and this time we see, beside each part, the piece of the definition that describes it.
Let us build the run on a whole word together. The function describes one step, on one letter.
To describe a run on a whole word we want a function that returns the state the automaton is in after reading the word starting from the state .
We denote it . Its domain is the pairs of a state and a word, and its range is the set of states:
. Some examples first, in the scanner, starting from :
| the word | |||||
|---|---|---|---|---|---|
Think: every nonempty word has the form (a word followed by one last letter ).
Suppose we already know the state the automaton is in after reading , that is .
What is ? What must be? Write a definition in your own words before opening anything below.
Only after you have written a definition of your own, open the candidate definitions and choose the one you think is right:
Candidate definitions
In every option is a word and is a letter.
A:
The thinking: one step on the last letter.
The mistakes: this rule forgets : it starts from and not from the state we reached after reading .
Check it on : , but the real run reaches .
B:
The thinking: the step rule is right, only the base value is different.
The mistakes: reading nothing leaves the automaton where it is, and does not send it back to the start.
With the base value we would get , but an automaton that has read nothing stays in .
C:
The thinking: with no reading we stay where we are, and to read we read first and then take one step on .
This is the definition we want. Check it on : .
D:
The thinking: take a step on and keep reading .
The mistakes: this reads the last letter before , that is, in the opposite order.
On we would get instead of .
Hint 1
Compare with , and with . What changed in the word, and what changed in the state the automaton is in?
Hint 2
A recursive definition needs two parts: a value for the base word , and a rule that gets the value for from the value for using .
The definition, and how to run it on a word
With the domain and range we saw, written as cases, as in a formula sheet (here and below, is a word and a letter):
The first case says that reading nothing changes nothing. The second case says that to read we read first and then take one step on .
For example .
The language of the automaton. Write for the set of words the automaton accepts.
Think: how do we write it using , the start state and the accepting states?
Hint
A word is accepted when its run ends in an accepting state. From which state does the run start, and which set must its result belong to?
The definition
A DFA recognizes a language if its language is exactly .
A language that some DFA recognizes is called a regular language.
We run the scanner on the text : the run is
that is , and the text is accepted. On the run is :
the second point sends it to , which is not accepting.
Connection to later courses: a run that always stops
A run of a DFA on a word of length ends after exactly steps, so the question “is ?” is decidable:
there is an algorithm that always stops and answers yes or no, and it simply runs the automaton.
In stronger models, such as the Turing machine (a machine with an unbounded memory tape), a run may never end, and the distinction between a decidable language and one that can only be accepted becomes essential.
Memory is what separates the languages that a DFA can recognize from the rest.
To decide whether a word has an even number of ‘s, one bit is enough while reading:
is the count so far even? To decide the language , one must remember how many ‘s were read, and that number has no bound.
A machine with a fixed, finite memory cannot do that, so no DFA recognizes this language; the unit on the limits of finite machines shows why.
Question 1. A DFA has states and the alphabet .
Answer each part before you open its answer.
(a) How many values must its transition function specify?
Answer
is defined on , which has pairs, so six values:
one next state for each state and letter.
(b) Suppose that in every state the transition on leads back to , that is .
Compute from the definition of , and then explain why for every (the word is zeros).
Answer
. In general, by induction on : for this is , and if then .
Reading only zeros leaves the automaton where it was, whatever state it started in.
Question 2. A DFA over has the states and this transition table (the start state and the accepting states are not needed here):
| state | on | on |
|---|---|---|
Compute for each of the three states . What do the three results have in common?
Answer
By the definition, . In the table for every state , and , so .
Whatever state we start in, reading brings the automaton to .
From the definition to the language (four tiny automata)
Over consider four automata, each with the start state and a transition function that sends every letter to the state shown:
| Automaton | States | ||
|---|---|---|---|
| on both letters; on both | |||
| on both letters; on both |
Question 3. What is for each ? Answer each part before you open its answer.
(a)
Answer
: no state is accepting, so no run is accepted.
(b)
Answer
: every run ends in the accepting state.
(c)
Answer
: the empty run stays in , which is not accepting, and any letter leads to the accepting state , which it can never leave.
(d)
Answer
: only the empty run ends in the accepting ; after any letter the run moves to the non-accepting .
Lab: predict, then follow the run
Before you run anything: which of the four automata in the lab (an even number of ‘s, contains , starts with , and a binary number divisible by ) should accept the empty word, and why?
Then choose one of them, read its diagram and table, type a word over its alphabet, predict whether it is accepted, and run it.
The highlighted circle shows the current state. Then change one letter and see whether the verdict changes, and why.
The lab needs JavaScript. Use the fixed example and the transition tables below instead.
Alphabet:
The transition tables of the lab machines
The arrow marks the start state and the asterisk an accepting state.
These are the tables of three of the machines; the table of the fourth, a binary number divisible by , is in the lesson Designing a Deterministic Finite Automaton.
An even number of ‘s (over ):
| state | on | on |
|---|---|---|
Contains the substring :
| state | on | on |
|---|---|---|
Starts with (the state is a state that cannot be left once it is entered):
| state | on | on |
|---|---|---|
Trap states
Think first: in the scanner, what happens to the run after a second point?
Where can it go from , and what does that say about every continuation of the text?
The answer
The second point sends the run to , and every letter read there, , or a point, leads back to .
The run stays in until the end of the text, and is not accepting, so no continuation makes the text a valid number.
A state is a trap state if every transition out of it leads back to itself:
once the run enters it, it never leaves. Other sources also call it an absorbing state, and a dead state when it is not accepting; in this course we use one name, trap state.
State in the scanner is a trap state that is not accepting, and so is state in the automaton for “starts with ” (after a first letter no continuation helps).
Such a state records “this word can no longer be accepted”.
Because a DFA needs a transition for every state and every letter, a trap state is how we say “this letter is forbidden here”:
send the transition to a trap state that is not accepting, never leave it missing.
Question 4. Can a trap state be accepting? Give an example from this lesson.
Answer
Yes: in the automaton for “contains ”, the state is accepting and every letter leads back to it.
An accepting trap state records “this word is already accepted, whatever follows”:
once has appeared, nothing can change the verdict.
Connection: absorbing states in Markov chains
The name comes from Markov chains: a state is absorbing when the probability of going from it to itself is , that is, there is no way out of it.
The two notions connect: if every letter of the input is chosen at random, with the same probability, the sequence of states of a DFA is a Markov chain, and the trap states of the automaton are exactly the absorbing states of the chain.
In the automaton for “contains ” over the state is a trap, so the probability that a random word of length contains tends to as grows.
Connection to later courses: questions about an automaton
The question whether is answered by an algorithm on the diagram:
the language is empty exactly when no accepting state can be reached from the start state by following the arrows (a graph search).
For general programs, that is Turing machines, the parallel question (“does the machine ever accept a word?”) is undecidable.
Practice
1. Let and the transition function is:
Describe and write it as a set.
Hint
Draw the automaton. What happens after the first ? Which words keep the run inside , and in which of them does it end?
Answer
is a rejecting trap entered by any , and alternate on ; hence .
Back to Languages: Operations and Equalities. Next: Designing a Deterministic Finite Automaton, or see the glossary.