Lesson

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, 00 or 11⁠), followed, if you like, by a point and one or more bits?
101101 and 101.01101.01 (whose value is 5.255.25⁠) are valid; .1.1⁠, 1.1. and 1.0.11.0.1 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 101101⁠, then a point was read and the text became 101.101.⁠, then the bit 11 arrived and the text became 101.1101.1⁠.
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, 101101 or 101.1101.1⁠, matters for what follows.

The answer, and the idea of an automaton

After 101101 another bit continues the number, and a point opens a fractional part; the text is already valid.
After 101.101. a bit must come, and another point makes the text invalid; the text is not yet valid.
After 101.1101.1 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):

stateon 00on 11on .\texttt{.}
→q0\to q_0q1q_1q1q_1q4q_4
∗ q1*\,q_1q1q_1q1q_1q2q_2
q2q_2q3q_3q3q_3q4q_4
∗ q3*\,q_3q3q_3q3q_3q4q_4
q4q_4q4q_4q4q_4q4q_4

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 {q0,q1,q2,q3,q4}\{q_0,q_1,q_2,q_3,q_4\} (a set); the alphabet {0,1,.}\{0,1,\texttt{.}\} (a set); the start state q0q_0 (an element of the set of states); the accepting states {q1,q3}\{q_1,q_3\} (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

A=(Q, Σ, δ, q0, F)A=(Q,\ \Sigma,\ \delta,\ q_0,\ F)

where QQ is a finite set of states, Σ\Sigma is the alphabet, q0∈Qq_0\in Q is the start state, F⊆QF\subseteq Q is the set of accepting states, and

δ:Q×Σ→Q\delta:Q\times\Sigma\to Q

is the transition function: δ(q,a)\delta(q,a) is the single state reached from qq on reading the letter aa⁠.
“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 δ\delta 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 ww starting from the state qq⁠.
We denote it δ∗(q,w)\delta^*(q,w)⁠. Its domain is the pairs of a state and a word, and its range is the set of states:
δ∗:Q×Σ∗→Q\delta^*:Q\times\Sigma^*\to Q⁠. Some examples first, in the scanner, starting from q0q_0⁠:

the word wwε\varepsilon111.1.1.01.01..1..
δ∗(q0,w)\delta^*(q_0,w)q0q_0q1q_1q2q_2q3q_3q4q_4

Think: every nonempty word has the form w=uaw=ua (a word u∈Σ∗u\in\Sigma^* followed by one last letter a∈Σa\in\Sigma⁠).
Suppose we already know the state the automaton is in after reading uu⁠, that is δ∗(q,u)\delta^*(q,u)⁠.
What is δ∗(q,w)\delta^*(q,w)⁠? What must δ∗(q,ε)\delta^*(q,\varepsilon) 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 u∈Σ∗u\in\Sigma^* is a word and a∈Σa\in\Sigma is a letter.

A: δ∗(q,w)={qw=εδ(q,a)w=ua\delta^*(q,w)=\begin{cases}q & w=\varepsilon\\ \delta(q,a) & w=ua\end{cases}

The thinking: one step on the last letter.

The mistakes: this rule forgets uu⁠: it starts from qq and not from the state we reached after reading uu⁠.
Check it on 1.1.⁠: δ(q0,.)=q4\delta(q_0,\texttt{.})=q_4⁠, but the real run reaches q2q_2⁠.

B: δ∗(q,w)={q0w=εδ(δ∗(q,u),a)w=ua\delta^*(q,w)=\begin{cases}q_0 & w=\varepsilon\\ \delta\bigl(\delta^*(q,u),a\bigr) & w=ua\end{cases}

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 q0q_0 we would get δ∗(q1,ε)=q0\delta^*(q_1,\varepsilon)=q_0⁠, but an automaton that has read nothing stays in q1q_1⁠.

C: δ∗(q,w)={qw=εδ(δ∗(q,u),a)w=ua\delta^*(q,w)=\begin{cases}q & w=\varepsilon\\ \delta\bigl(\delta^*(q,u),a\bigr) & w=ua\end{cases}

The thinking: with no reading we stay where we are, and to read uaua we read uu first and then take one step on aa⁠.

This is the definition we want. Check it on 1.=1⋅.1.=1\cdot\texttt{.}⁠: δ∗(q0,1.)=δ(δ∗(q0,1),.)=δ(q1,.)=q2\delta^*(q_0,1.)=\delta\bigl(\delta^*(q_0,1),\texttt{.}\bigr)=\delta(q_1,\texttt{.})=q_2⁠.

D: δ∗(q,w)={qw=εδ∗(δ(q,a),u)w=ua\delta^*(q,w)=\begin{cases}q & w=\varepsilon\\ \delta^*\bigl(\delta(q,a),u\bigr) & w=ua\end{cases}

The thinking: take a step on aa and keep reading uu⁠.

The mistakes: this reads the last letter before uu⁠, that is, in the opposite order.
On 1.1. we would get δ∗(δ(q0,.),1)=δ∗(q4,1)=q4\delta^*\bigl(\delta(q_0,\texttt{.}),1\bigr)=\delta^*(q_4,1)=q_4 instead of q2q_2⁠.

Hint 1

Compare 1.1. with 11⁠, and 1.01.0 with 1.1.⁠. 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 ε\varepsilon⁠, and a rule that gets the value for uaua from the value for uu using δ\delta⁠.

The definition, and how to run it on a word

With the domain and range δ∗:Q×Σ∗→Q\delta^*:Q\times\Sigma^*\to Q we saw, written as cases, as in a formula sheet (here and below, uu is a word and aa a letter):

δ∗(q,w)={qw=εδ(δ∗(q,u),a)w=u⋅a∧u∈Σ∗∧a∈Σ\delta^*(q,w)= \begin{cases} q & w=\varepsilon\\ \delta\bigl(\delta^*(q,u),a\bigr) & w=u\cdot a\wedge u\in\Sigma^*\wedge a\in\Sigma \end{cases}

The first case says that reading nothing changes nothing. The second case says that to read uaua we read uu first and then take one step on aa⁠.
For example δ∗(q0,1.0)=δ(δ∗(q0,1.),0)=δ(q2,0)=q3\delta^*(q_0,1.0)=\delta\bigl(\delta^*(q_0,1.),0\bigr)=\delta(q_2,0)=q_3⁠.

The language of the automaton. Write L(A)L(A) for the set of words the automaton AA accepts.
Think: how do we write it using δ∗\delta^*⁠, 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 L(A)={ w∈Σ∗∣δ∗(q0,w)∈F }L(A)=\{\,w\in\Sigma^*\mid\delta^*(q_0,w)\in F\,\}

A DFA recognizes a language LL if its language L(A)L(A) is exactly LL⁠.
A language that some DFA recognizes is called a regular language.

We run the scanner on the text 101.1101.1⁠: the run is

q0→1q1→0q1→1q1→.q2→1q3,q_0\xrightarrow{1}q_1\xrightarrow{0}q_1\xrightarrow{1}q_1\xrightarrow{\texttt{.}}q_2\xrightarrow{1}q_3 ,

that is δ∗(q0,101.1)=q3∈F\delta^*(q_0,101.1)=q_3\in F⁠, and the text is accepted. On 1.0.11.0.1 the run is q0,q1,q2,q3,q4,q4q_0,q_1,q_2,q_3,q_4,q_4⁠:
the second point sends it to q4q_4⁠, which is not accepting.

Connection to later courses: a run that always stops

A run of a DFA on a word of length nn ends after exactly nn steps, so the question “is w∈L(A)w\in L(A)⁠?” 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 aa‘s, one bit is enough while reading:
is the count so far even? To decide the language {anbn∣n∈N}\{a^nb^n\mid n\in\mathbb{N}\}⁠, one must remember how many aa‘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 33 states and the alphabet {0,1}\{0,1\}⁠.
Answer each part before you open its answer.

(a) How many values must its transition function δ\delta specify?

Answer

δ\delta is defined on Q×ΣQ\times\Sigma⁠, which has 3⋅2=63\cdot2=6 pairs, so six values:
one next state for each state and letter.

(b) Suppose that in every state qq the transition on 00 leads back to qq⁠, that is δ(q,0)=q\delta(q,0)=q⁠.
Compute δ∗(q,00)\delta^*(q,00) from the definition of δ∗\delta^*⁠, and then explain why δ∗(q,0n)=q\delta^*(q,0^n)=q for every n∈Nn\in\mathbb{N} (the word 0n0^n is nn zeros).

Answer

δ∗(q,00)=δ(δ∗(q,0),0)=δ(δ(δ∗(q,ε),0),0)=δ(δ(q,0),0)=δ(q,0)=q\delta^*(q,00)=\delta\bigl(\delta^*(q,0),0\bigr)=\delta\bigl(\delta(\delta^*(q,\varepsilon),0),0\bigr)=\delta\bigl(\delta(q,0),0\bigr)=\delta(q,0)=q⁠. In general, by induction on nn⁠: for n=0n=0 this is δ∗(q,ε)=q\delta^*(q,\varepsilon)=q⁠, and if δ∗(q,0n)=q\delta^*(q,0^n)=q then δ∗(q,0n+1)=δ(δ∗(q,0n),0)=δ(q,0)=q\delta^*(q,0^{n+1})=\delta\bigl(\delta^*(q,0^n),0\bigr)=\delta(q,0)=q⁠.
Reading only zeros leaves the automaton where it was, whatever state it started in.

Question 2. A DFA over {a,b}\{a,b\} has the states q0,q1,q2q_0,q_1,q_2 and this transition table (the start state and the accepting states are not needed here):

stateon aaon bb
q0q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
q2q_2q1q_1q0q_0

Compute δ∗(q,ab)\delta^*(q,ab) for each of the three states qq⁠. What do the three results have in common?

Answer

By the definition, δ∗(q,ab)=δ(δ∗(q,a),b)=δ(δ(q,a),b)\delta^*(q,ab)=\delta\bigl(\delta^*(q,a),b\bigr)=\delta\bigl(\delta(q,a),b\bigr)⁠. In the table δ(q,a)=q1\delta(q,a)=q_1 for every state qq⁠, and δ(q1,b)=q2\delta(q_1,b)=q_2⁠, so δ∗(q0,ab)=δ∗(q1,ab)=δ∗(q2,ab)=q2\delta^*(q_0,ab)=\delta^*(q_1,ab)=\delta^*(q_2,ab)=q_2⁠.
Whatever state we start in, reading abab brings the automaton to q2q_2⁠.

From the definition to the language (four tiny automata)

Over Σ={0,1}\Sigma=\{0,1\} consider four automata, each with the start state q0q_0 and a transition function that sends every letter to the state shown:

AutomatonStatesδ\deltaFF
A1A_1{q0}\{q_0\}δ(q0,0)=δ(q0,1)=q0\delta(q_0,0)=\delta(q_0,1)=q_0∅\varnothing
A2A_2{q0}\{q_0\}δ(q0,0)=δ(q0,1)=q0\delta(q_0,0)=\delta(q_0,1)=q_0{q0}\{q_0\}
A3A_3{q0,q1}\{q_0,q_1\}q0→q1q_0\to q_1 on both letters; q1→q1q_1\to q_1 on both{q1}\{q_1\}
A4A_4{q0,q1}\{q_0,q_1\}q0→q1q_0\to q_1 on both letters; q1→q1q_1\to q_1 on both{q0}\{q_0\}

Question 3. What is L(Ai)L(A_i) for each ii⁠? Answer each part before you open its answer.

(a) L(A1)L(A_1)

Answer

L(A1)=∅L(A_1)=\varnothing⁠: no state is accepting, so no run is accepted.

(b) L(A2)L(A_2)

Answer

L(A2)=Σ∗L(A_2)=\Sigma^*⁠: every run ends in the accepting state.

(c) L(A3)L(A_3)

Answer

L(A3)=Σ+L(A_3)=\Sigma^+⁠: the empty run stays in q0q_0⁠, which is not accepting, and any letter leads to the accepting state q1q_1⁠, which it can never leave.

(d) L(A4)L(A_4)

Answer

L(A4)={ε}L(A_4)=\{\varepsilon\}⁠: only the empty run ends in the accepting q0q_0⁠; after any letter the run moves to the non-accepting q1q_1⁠.

Lab: predict, then follow the run

Before you run anything: which of the four automata in the lab (an even number of aa‘s, contains abab⁠, starts with aa⁠, and a binary number divisible by 33⁠) 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.

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 33⁠, is in the lesson Designing a Deterministic Finite Automaton.

An even number of aa‘s (over {a,b}\{a,b\}⁠):

stateon aaon bb
→∗ q0\to{*}\,q_0q1q_1q0q_0
q1q_1q0q_0q1q_1

Contains the substring abab⁠:

stateon aaon bb
→q0\to q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
∗ q2*\,q_2q2q_2q2q_2

Starts with aa (the state dd is a state that cannot be left once it is entered):

stateon aaon bb
→s\to sttdd
∗ t*\,ttttt
dddddd

Trap states

Think first: in the scanner, what happens to the run after a second point?
Where can it go from q4q_4⁠, and what does that say about every continuation of the text?

The answer

The second point sends the run to q4q_4⁠, and every letter read there, 00⁠, 11 or a point, leads back to q4q_4⁠.
The run stays in q4q_4 until the end of the text, and q4q_4 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 q4q_4 in the scanner is a trap state that is not accepting, and so is state dd in the automaton for “starts with aa” (after a first letter bb 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 abab”, the state q2q_2 is accepting and every letter leads back to it.
An accepting trap state records “this word is already accepted, whatever follows”:
once abab has appeared, nothing can change the verdict.

Connection: absorbing states in Markov chains

The name comes from Markov chains: a state ii is absorbing when the probability of going from it to itself is Pii=1P_{ii}=1⁠, 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 abab” over {a,b}\{a,b\} the state q2q_2 is a trap, so the probability that a random word of length nn contains abab tends to 11 as nn grows.

Connection to later courses: questions about an automaton

The question whether L(A)=∅L(A)=\varnothing 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 A=({q0,q1,q2},{a,b},δ,q0,{q0})A=(\{q_0,q_1,q_2\},\{a,b\},\delta,q_0,\{q_0\}) and the transition function is:

δ(q0,a)=q1,δ(q1,a)=q0,δ(q0,b)=δ(q1,b)=δ(q2,a)=δ(q2,b)=q2\begin{aligned} &\delta(q_0,a)=q_1,\qquad \delta(q_1,a)=q_0,\\ &\delta(q_0,b)=\delta(q_1,b)=\delta(q_2,a)=\delta(q_2,b)=q_2 \end{aligned}

Describe L(A)L(A) and write it as a set.

Hint

Draw the automaton. What happens after the first bb⁠? Which words keep the run inside q0,q1q_0,q_1⁠, and in which of them does it end?

Answer

q2q_2 is a rejecting trap entered by any bb⁠, and q0,q1q_0,q_1 alternate on aa⁠; hence L(A)={a2k∣k∈N}L(A)=\{a^{2k}\mid k\in\mathbb{N}\}⁠.

Back to Languages: Operations and Equalities. Next: Designing a Deterministic Finite Automaton, or see the glossary.