2it70 Chap2 DFA
2it70 Chap2 DFA
Finite Automata
and Regular Languages
b a a, b
a
a b
0 1 2 3
b
7
8 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
next section, for a DFA in each state q ∈ Q and for every symbol a ∈ Σ the next state,
which is the state δ(q, a), is determined by the transition function δ.
Example 2.2. Figure 2.1 gives a visual representation of a deterministic finite automa-
ton, D say. The set of states is {q0 , q1 , q2 , q3 } with q0 the initial state as indicated by the
small incoming arrow. The alphabet Σ consists of the symbols a and b. The δ-function
is indicated by the arrows between states. E.g., for q0 there is an arrow labeled a to q1 ,
thus δ(q0 , a) = q1 . There is also an arrow labeled b from q0 to itself, so δ(q0 , b) = q0 .
The self-loop of q3 labeled a, b represents two transitions, one for a and one for b. Thus
δ(q3 , a) = q3 and δ(q3 , b) = q3 . There is one final state, viz. q3 , as indicated by the
double boundary of the state. Thus, the set of final states is {q3 }.
Formally, D = (Q, Σ, δ, q0 , F ) where Q = {q0 , q1 , q2 , q3 }, Σ = {a, b}, F = {q3 } and
δ : Q × Σ → Q is given by the table below.
a b
q0 q1 q0
q1 q2 q0
q2 q2 q3
q3 q3 q3
Note, since δ : Q × Σ → Q is a function, for each state q ∈ Q there is exactly one state,
viz. the state δ(q, a) ∈ Q, for each symbol a ∈ Σ.
In the above situation we say that (q, w) yields (q ′ , w′ ) with respect to D, or that D
derives configuration (q ′ , w′ ) from configuration (q, w) in a number –zero, one, or more–
steps.
2.1. DETERMINISTIC FINITE AUTOMATA 9
Example 2.3. Continuing Example 2.2 involving the DFA D given by Figure 2.1,
we have for the relation ⊢D that (q1 , aaaba) ⊢D (q2 , aaba), (q2 , aaba) ⊢D (q2 , aba),
(q2 , aba) ⊢D (q2 , ba), (q2 , ba) ⊢D (q3 , a), and, (q3 , a) ⊢D (q3 , ε). For the relation ⊢∗D
we have (q1 , aaaba) ⊢∗D (q3 , ε), but also (q1 , aaaba) ⊢∗D (q1 , aaaba), (q2 , aaba) ⊢∗D
(q2 , aba), (q2 , aaba) ⊢∗D (q3 , ε), and (q2 , aaba) ⊢∗D (q3 , a).
A special case of the lemma above is when w′ = ε. Since |ε| = 0, we have n = |w| and
|w|
obtain (q, w) ⊢∗D (q ′ , ε) ⇐⇒ (q, w) ⊢ D (q ′ , ε).
For a DFA, given a state q and an symbol a on input, the next state is determined.
It is the state δ(q, a). Also, if it takes the string w to be read off from input to get from
state q to some state q ′ while leaving a string w′ on input, then extending the input with
a string v doesn’t influence this derivation of the DFA from q to q ′ . Thus, we have the
following two properties for the ‘derives’ relation ⊢∗D .
Proof. For the proof we first prove a stronger property. Claim: for all q, q ′ ∈ Q and
w, w′ ∈ Σ∗ it holds that
(q, w) ⊢∗D (q ′ , w′ ) iff (2.1)
∃n > 0 ∃q0 , . . . , qn ∈ Q ∃a1 , . . . , an ∈ Σ :
q0 = q, δ(qi−1 , ai ) = qi for 1 6 i 6 n, qn = q ′
and w = a1 · · · an w′
Proof of the claim. (⇒) If (q, w) ⊢∗D (q ′ , w′ ), then exist n > 0, q0 , . . . , qn ∈ Q,
w0 , . . . , wn ∈ Σ∗ such that (q, w) = (w0 , q0 ), (qi−1 , wi−1 ) ⊢D (qi , wi ) for 1 6 i 6 n,
and (qn , wn ) = (q ′ , w′ ). Since (qi−1 , wi−1 ) ⊢D (qi , wi ) we can pick ai ∈ Σ such that
wi−1 = ai wi and δ(qi−1 , ai ) = qi for 1 6 i 6 n, by definition of ⊢D . It follows that
w = a1 · · · an w′ , which proves the implication.
(⇐) Suppose q0 , . . . , qn ∈ Q, a1 , . . . , an ∈ Σ are such that q0 = q, δ(qi−1 , ai ) = qi for
1 6 i 6 n, and qn = q ′ . Put wi = ai+1 ai+2 · · · an w′ for 0 6 i 6 n. Note wi−1 = ai wi for
1 6 i 6 n. Then (q0 , w0 ) = (q, w), (qi−1 , wi−1 ) = (qi−1 , ai wi ) ⊢D (wi , qi ) for 1 6 i 6 n,
since δ(qi−1 , ai ) = qi , and (qn , wn ) = (q ′ , w′ ). But this means (q, w) ⊢∗D (q ′ , w′ ), and
proves the other implication.
As to prove item (a) of the lemma, suppose (q, w) ⊢∗D (q ′ , w′ ) and (q, w) ⊢∗D (q ′′ , w′ ).
By the claim, we can find n > 0, q0 , . . . , qn ∈ Q, a1 , . . . , an ∈ Σ such that q0 = q,
δ(qi−1 , ai ) = qi for 1 6 i 6 n, qn = q ′ and w = a1 · · · an w′ . We can also find m > 0,
′ ∈ Q, a′ , . . . , a′ ∈ Σ such that q ′ = q, δ(q ′ , a′ ) = q ′ for 1 6 i 6 m, q ′ = q ′′
q0′ , . . . , qm 1 m 0 i−1 i i m
and w = a′1 · · · a′m w′ . Since a1 · · · an w′ = w = a′1 · · · a′m w′ , we have n = m and ai = a′i
for 1 6 i 6 n. Since q0 = q = q0′ it follows that qi = qi′ for 0 6 i 6 n. In particular
q ′ = qn = qm ′ = q ′′ since n = m.
Item (b) follows from the claim too. If (q, w) ⊢∗D (q ′ , w′ ) then applying Equa-
tion (2.1) from left to right, we can pick n > 0, q0 , . . . , qn ∈ Q, a1 , . . . , an ∈ Σ such
that q0 = q, δ(qi−1 , ai ) = qi for 1 6 i 6 n, qn = q ′ and w = a1 · · · an w′ . Since
w = a1 · · · an w′ implies wv = a1 · · · an w′ v, we conclude, applying Equation (2.1) right
to left, that (q, wv) ⊢∗D (q ′ , w′ v).
The first item expresses the determinacy of the DFA D: the state q and the input
string w (together with the number of symbols to be processed) determine the resulting
state. The second item expresses that a computation (q, w) ⊢∗D (q ′ , w′ ) of a DFA is only
influenced by the input that is read, i.e. the prefix u of w such that w = uw′ .
We next introduce the important concept of the language accepted by a DFA.
Definition 2.6 (Language accepted by DFA). Let D = (Q, Σ, δ, q0 , F ) be a determin-
istic finite automaton. The set L(D) ⊆ Σ∗ , called the language accepted by D, is defined
by
∗
L(D) = { w ∈ Σ∗ | ∃q ∈ F : (q0 , w) ⊢D (q, ε) }
2.1. DETERMINISTIC FINITE AUTOMATA 11
Thus, a string w ∈ Σ∗ is in L(D) when starting from the initial state q0 the DFA D
reaches a final state when all of w is processed as input.
In view of the proof of Lemma 2.5, we can reformulate the condition for a string w
′ , a ) for
being included in L(D). Suppose w = a1 · · · an . Put q0′ = q0 and qi′ = δ(qi−1 i
1 6 i 6 n. Then it holds that
w ∈ L(D) ⇐⇒ qn′ ∈ F
The point is that for the DFA D, the string w = a1 · · · an is accepted if a final state is
reached from the start state q0 while processing a1 , . . . , an successively.
Example 2.7. For a DFA D we always have ∅ ⊆ L(D) ⊆ Σ∗ . Extreme cases occur
when D has no (reachable) final states at all, or when each (reachable) state of D is a
final state. In these situation we have L(D) = ∅, and L(D) = Σ∗ , respectively.
For a DFA D, a state q is called reachable in D if (q0 , w) ⊢∗D (q, ε) for some string
w ∈ Σ∗ . Thus, when suitable input w is provided, D will move from the initial state q0
to the state q when processing the string w. Note, non-reachable states do not contribute
to the language of the DFA.
In most other cases than those of Example 2.7 we need to look more closely to
determine L(D). A useful notion when analyzing a DFA is that of a path set. We say
that a set L ⊆ Σ∗ is the path set of q with respect to D when L consists of all words w
that bring, when starting from the initial state, the automaton to state q, notation
pathset D (q). Formally,
∗
pathset D (q) = { w ∈ Σ∗ | (q0 , w) ⊢D (q, ε) }
Comparing the definition of a pathset and the definition of the language accepted by a
DFA, we observe, in the context of a DFA D = (Q, Σ, δ, q0 , F ),
S
L(D) = q∈F pathset D (q)
D1 a
ee oe
a D2 a, b
b b b b e o
a a, b
eo oo
a
Since q3 is the only final state of D it follows that a string w ∈ {a, b}∗ is accepted by D
iff w has a substring aab, as claimed above.
Example 2.9. Consider the deterministic finite automaton D1 depicted in the left part
of Figure 2.2. We argue that the language L(D1 ) of D1 is the set
L(D1 ) = { w ∈ {a, b}∗ | #a (w) odd, or #b (w) odd, but not both }
For D1 we have the following characterization of the path sets:
Only qoe and qeo are final states, thus w is accepted iff #a (w) is odd and #b (w) is even
for reaching qoe , or #a (w) is even and #b (w) is odd for reaching qeo .
Now consider the deterministic finite automaton D2 given by the right part of Fig-
ure 2.2. The state qe is the initial state, the state qo the only final state. We claim that
the language L(D2 ) of strings that are accepted by D2 is the set of strings of odd length,
i.e.
L(D2 ) = { w ∈ {a, b}∗ | |w| odd }
as can be seen by computing the path sets for qe and qo :
Exercise 2.1.1.
(a) Construct a DFA D1 with alphabet {a, b} (with no more than three states) for
the language L1 = { an b | n > 0 } and establish with the help of path sets that
L(D1 ) = L1 .
(b) Also construct a DFA D1′ over {a, b} (now with no more than four states) for the
language L′1 = { an b | n > 0 } and again establish with the help of path sets that
L(D1′ ) = L′1 .
(a) D1′
q0 an n>0
a a,b
q1 an b n>0
b a,b q2 an bw n > 0, w 6= ε string an b followed
0 1 2 by extra symbols
D1′ a
(b) a b
0 1 2
q0 ε forced to leave, no return
b 3 a,b q1 an n>0
q2 an b n>0
a,b q3 b, an bw n > 0, w 6= ε string b, and strings an b
with trailing symbols
Exercise 2.1.2. (a) Construct a DFA D2 with alphabet {0,1} (with no more than
four states) for the language L2 = { w ∈ {0,1}∗ | the second element of w is 0 }
and establish with the help of path sets that L(D2 ) = L2 . For example, 10111 ∈ L2
while 01000 ∈/ L2 .
(b) Construct a DFA D2′ with alphabet {0,1} (with no more than seven states) for the
language L′2 = {w ∈ {0, 1}∗ | the second last element of w is 0} and establish with
the help of path sets that L(D2′ ) = L′2 . For example, 11101 ∈ L′2 while 00010 ∈
/ L′2 .
(a) D2
0 2 0,1 q0 ε forced to leave, no return
0,1 q1 0, 1
0 1
q2 00w, 01w w ∈ {0,1}∗ second symbol 0
1 3 0,1 q3 10w, 11w w ∈ {0,1}∗ second symbol 1
14 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
(b) D2′
0
0 1
q0 ε
q1 0
1 2 q2 1
q3 w00 w ∈ {0,1}∗ string with suffix 00
0 1 0 1
0 q4 w01 w ∈ {0,1}∗ string with suffix 01
1 q5 w10 w ∈ {0,1}∗ string with suffix 10
0 3 4 5 6 1
1 0 q6 w11 w ∈ {0,1}∗ string with suffix 11
0 1
Exercise 2.1.3.
(a) Construct a DFA D3 for the finite language L3 = {aba, abc, bc, b} over the alpha-
bet {a, b, c}.
(b) If a language L ⊆ {a, b, c}∗ is finite, does there exists a DFA D such that L(D) = L?
(a) b D
1 2 0
D3 a a,b,c a,c
a,c b
a b c
c a,b,c
0 5 3
a,b 1 2 3
b c
4 a b c a b c
4 5 6 7 8 9
x x x
x a b c x x
10 11 12 x X
x a,b,c
x
x short for a, b, c
(b) Yes. If Σ has α symbols and the maximal length P of accepted strings in L is ℓ,
then a tree-like finite automaton with at most 1 + ℓk=1 αk can accept the finite
language L. The extra summand 1 is for the sink state that catching strings longer
than ℓ and, if applicable, strings of length at most ℓ not in L.
2.1. DETERMINISTIC FINITE AUTOMATA 15
Exercise 2.1.4.
(a) Construct a DFA D4 with alphabet {0, 1} (with no more than five states) for the
language L4 = { w ∈ {0, 1}∗ | w has a substring 00 or a substring 11 (or both) }
and verify that L(D4 ) = L4 .
(b) Construct a DFA D4′ with alphabet {0, 1} (with no more than eight states) for
the language L′4 = { w ∈ {0, 1}∗ | w has a substring 00 and a substring 11 }. (No
verification with path sets asked.)
Answer to Exercise 2.1.4
D4 D4′ 0
(a) 0 0 (b) 0 0
0 1 2 0,1 0 1 2
0 0
1 1 0 1
1 1
3 3 4
1 1 1
0
4 5 6 7
0
1
1 0,1
0,1
q0 ε q0 ε
q1 0(10)n , 10(10)n n>0 q1 (0 + 10)(10)∗
q2 0(10)n 0w, 10(10)n 0w n > 0, w ∈ {0,1}∗ q2 (0 + 10)(10)∗ 0+
q3 1(01)n , 01(01)n n>0 q3 (1 + 01)(01)∗
q4 1(01)n 1w, 01(01)n w n > 0, w ∈ {0,1}∗ q4 (0 + 10)(10)∗ 0+ 1(0+ 1)∗
q5 (1 + 01)(01)∗ 1+
q6 (1 + 01)(01)∗ 1+ 0(1+ 0)∗
q7 (1 + 01)(01)∗ 1+ 0(1+ 0)∗ 0(0 + 1)∗ +
(0 + 10)(10)∗ 0+ 1)(0+ 1)∗ 1(0 + 1)∗
For item (a) we use that, for n > 0, 0(10)n 0 = (01)n 00, 10(10)n 0 = 1(01)n 00, and
1(01)n 1 = (10)n 11, 01(01)n 1 = 0(10)n 11. From this it follows that accepted strings
contain the substring 00, if accepted by state q2 , or contain the substring 11, if accepted
by state q4 . For item (b), for which verification with the help of path sets was not asked,
we have employed so-called regular expressions (discussed in Section 2.3) to describe the
path sets. Here we use
and
(0 + 10)(10)∗ 0+ = (ε + 1)0(10)∗ 0+ = (ε + 1)(01)∗ 000∗
1(0+ 1)∗ 1(1 + 0)∗ = (10+ )∗ 11(1 + 0)∗
16 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
to see that strings accepted by the DFA D4′ have both a substring 00 and a substring 11.
In particular, qn′ is a final state of D. Since qn′ ∈ / Q \ F , qn′ is not a final state of DC .
Therefore, w ∈/ L(D ).C
Reversely, if w = a1 · · · an ∈ ′ ,a )
/ L and q0′ , . . . , qn′ are such that q0′ = q0 and qi′ = δ(qi−1 i
′
for 1 6 i 6 n, then qn ∈ ′
/ F . But then qn ∈ Q \ F , and therefore w ∈ L(D ). C
(a) Give a DFA D with set of states Q1 × Q2 and alphabet Σ such that L(D) =
L(D1 ) ∩ L(D2 ).
a a
a
0 2 b
τ
may exibit non-determinism as they can have any number of outgoing transitions for
a given symbol, including no transitions. Additionally, transitions are allowed that do
not consume input. These are silent referred to as silent steps. So, we consider non-
deterministic finite automata with silent steps, NFA for short.
Definition 2.10 (Non-deterministic finite automaton with silent steps). A non-deter-
ministic finite automaton with silent steps, or NFA, is a quintuple N = (Q, Σ, →N , q0 , F )
with Q a finite set of states, Σ a finite alphabet, →N ⊆ Q × Στ × Q the transition rela-
tion, q0 ∈ Q the initial state, and F ⊆ Q the set of final states.
Instead of a function δ : Q × Σ → Q as we have for a DFA, we consider for an NFA
a relation →N ⊆ Q × Στ × Q. This relation may include triples (q, a, q ′ ) for states
q, q ′ ∈ Q and a symbol a ∈ Σ, but also triples (q, τ, q ′ ) for states q, q ′ ∈ Q and the
special symbol τ . The symbol τ denotes a so-called silent step. We write Στ for Σ ∪ {τ }.
α
It is assumed that τ ∈ / Σ. We often use α to range over Στ . We write q − →N q ′ if
τ
(q, α, q ′ ) ∈ →N , thus with α ∈ Στ . In case q −→N q ′ we say that there is a silent step
or τ -transition in N from q to q ′ . As we will make explicit below, a silent step does not
affect the input of an NFA. We often omit the subcript N when clear from the context.
The concepts of an NFA and of a DFA are very similar, but differ in three aspects:
• in an NFA states can have τ -transitions;
• in an NFA states can have multiple transitions for the same symbol;
• in an NFA states can have no transitions for a symbol.
Figure 2.3 gives a visual representation of a non-deterministic finite automaton, N say.
Initial state q0 has a τ -transition to state q2 , has two transitions on symbol a (one going
to q1 , and one going to q2 ) and has no transition on symbol b. The transition relation →N
of N contains the triples
(q0 , τ, q2 ) (q0 , a, q1 ) (q0 , a, q2 ) (q1 , a, q2 ) (q2 , b, q2 )
Still we can interpret →N as a functionδ̂N , but now δ̂N : Q × Στ → P(Q), with P(Q)
the powerset of Q, the collection of all the subsets of Q. Here we have
δ̂N (q0 , a) = {q1 , q2 } δ̂N (q1 , a) = {q2 } δ̂N (q2 , a) = ∅
δ̂N (q0 , b) = {q2 } δ̂N (q1 , b) = ∅ δ̂N (q2 , b) = {q2 }
δ̂N (q0 , τ ) = {q2 } δ̂N (q1 , τ ) = ∅ δ̂N (q2 , τ ) = ∅
2.2. FINITE AUTOMATA 19
τ
Note δ̂N (q0 , b) = {q2 }. The idea is to combine the τ -transition q0 −
→N q2 with the
b
b-transition q2 −
→N q2 . We come back to this idea for the general situation at a later
stage.
As for deterministic finite automata we have the notion of a configuration (q, w) for
q ∈ Q and w ∈ Σ∗ for an NFA N = (Q, Σ, →N , q0 , F ). Also here we have the yield or
derives relation among configurations. We put
a τ
(q, w) ⊢N (q ′ , w′ ) iff →N q ′ ∧ w = aw′ or q −
∃a ∈ Σ : q − →N q ′ ∧ w = w′
τ
Note, if q − →N q ′ then we have (q, w) ⊢N (q ′ , w) without change of the input w.
With ⊢∗N we denote the reflexive-transitive closure of ⊢N . More precisely, we put
(q, w) ⊢ 0N (q ′ , w′ ) if q = q ′ and w = w′ , and (q, w) ⊢ n+1
N (q ′ , w′ ) if (q, w) ⊢ nN (q̄, w̄),
for some q̄ ∈ Q, w̄ ∈ Σ∗ , and (q̄, w̄) ⊢N (q ′ , w′ ). Then (q, w) ⊢∗N (q ′ , w′ ) if (q, w) ⊢ nN
(q ′ , w′ ) for some n > 0.
Example 2.11. With respect to finite automaton of Figure 2.3 we have, e.g., (q0 , abb) ⊢∗N
(q2 , ε) and (q0 , abb) ⊢∗N (q1 , bb). Also (q0 , abb) ⊢∗N (q2 , bb), (q2 , bb) ⊢∗N (q2 , b), and
(q2 , b) ⊢∗N (q2 , ε). In configuration (q1 , bb) the automaton N cannot process the (first)
input b; the automaton N is stuck in that configuration. In contrast (q0 , bb) ⊢∗N (q2 , ε),
τ
since (q0 , bb) ⊢N (q2 , bb) via the silent step q0 −
→N q2 , and (q2 , bb) ⊢N (q2 , b), (q2 , b) ⊢N
(q2 , ε).
For NFA we have the followng counterpart to Lemma 2.5 for DFA.
2
a
b b
τ τ
0 1 3
a,τ 4 b
In the non-deterministic setting of NFAs there is no counterpart of Lemma 2.5, part (a).
E.g., for the NFA of Figure 2.3 we have (q0 , abb) ⊢∗N (q1 , bb) and (q0 , abb) ⊢∗N (q2 , bb)
while q1 6= q2 .
Note the similarity of Definition 2.13 for the language of an NFA and the corresponding
definition, Definition 2.6, for a DFA. Also note that L(N ) for an NFA N is considered a
language over the alphabet Σ, rather than the alphabet Στ . Thus τ -transitions do not
contribute (directly) to the word that is accepted.
Strings of the format an b, for n > 0, are accepted via a path of N from the initial state q0
to the final state q3 via state q4 . We have
0, . . . , 9 0, . . . , 9
τ, +, − . 0, . . . , 9 τ
0 1 2 3 5
0, . . . , 9 . τ
4
a
0 1
b
τ
τ τ
2
m times, yielding the substring bb for each time the loop is taken, before moving to q3
via the second τ -transition. More formally,
Example 2.15. Figure 2.5 provides an NFA that accepts decimal numbers. The three
transitions from q0 to q1 , with labels +, − and τ , express that the sign is optional; it can
be a +-sign, a −-sign, or it can be omitted. In state q1 , on reading a digit from input,
the automaton can move to state q4 or stay in state q1 . In state q4 the τ -transition to
state q5 can always be taken, leading to acceptance. Clearly, this latter transition is
redundant, we could have left it out and could have made state q4 final, instead. For
the τ -transition leaving state q0 it may not be obvious if and how it can be eliminated.
However, we shall prove that if a language is accepted by an NFA, then there is a DFA
accepting it too.
22 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
Example 2.16. Consider the NFA N depicted in Figure 2.6. The language L(N ), the
language accepted by N , is given by
as can be seen as follows: a string w is accepted if it brings N from the initial state q0
back to q0 again, since q0 is the only final state. Such a string w has the form w1 · · · wn
for some n > 0, where each wi is the labeling of a path from q0 to itself but not through q0
in between. So, we need to identify the loops of q0 , going through q1 and/or q2 , as this
determines what strings wi are allowed.
A loop of q0 may leave via the a-transition to q1 and return via the b-transition
from q1 to q0 . In between N may pass through q1 and q2 any number of times. This
explains the first of the possible string formats wi = avi,1 · · · vi,ni b. The paths through
q1 and q2 , leaving from and returning to q1 , are relatively simple. Since the b-transition
to q0 is excluded, N can only move from q1 to q2 via a silent step, iterate through the
c-loop of q2 , and return via the τ -transition of q2 back to q1 . This explains the format
for the strings vi,j , viz. vi,j = c mi,j for some mi,j > 0.
Alternatively, a loop of q0 may leave via the τ -transition to q2 , but will also now
return via the b-transition from q1 to q0 . This is why the second of the two string
formats looks like wi = vi,1 · · · vi,ni b. Getting to q1 may take a number of c’s, via the
loop of q2 , followed by a silent step from q2 to q1 , i.e. a string c m in total, where m > 0.
From there the analysis above for the loops from q1 to itself applies. This gives strings
of the same format, i.e. c m , as we have seen. Thus also here vi,j = c mi,j .
In Section 2.3 we introduce so-called regular expressions, which allow to describe the
∗
language of Example 2.16 more concisely, namely by L(N ) = ac∗ b + c∗ b . However,
it is clear that in the case of NFA behaviour of the automaton may be more capricious
than for DFA, and therefore it may become more difficult to determine the language that
is accepted. Below we will present a technique of going from an NFA to an ‘equivalent’
DFA.
Two NFAs N1 and N2 are called language equivalent if they accept the same language,
i.e. L(N1 ) = L(N2 ). Likewise for two DFAs. We also call an NFA and a DFA language
equivalent if they accept the same language.
Example 2.17. The two finite automata depicted in Figure 2.7 are language equivalent,
i.e. they accept the same language. This is the language
However, the right automaton N2 allows a τ -transition. The left automaton N1 can be
seen as both as a DFA and as an NFA. The transition relation →1 of N1 is in fact a
transition function δ1 . The right automaton N2 can be seen as an NFA only.
Proof. Given a DFA D = (Q, Σ, δ, q0 , F ) such that L(D) = L, define the NFA N =
a
→N q ′ iff δ(q, a) = q ′ , for q, q ′ ∈ Q, a ∈ Σ. Then it holds that
(Q, Σ, →N , q0 , F ) by q −
(q, w) ⊢D (q , w ) iff (q, w) ⊢N (q ′ , w′ ). Therefore we have, by Definition 2.6 and
′ ′
Note, the NFA N constructed in the proof above does not involve any silent step. The
more interesting reverse of Theorem 2.18 holds as well: for each NFA exists an language
equivalent DFA.
Thus q ′ ∈ E(q̄) if there is a sequence of zero, one ore more τ -transitions from q̄ to q ′ .
We construct a DFA D = (QD , Σ, δ, QD 0 , F ) such that L(D) = L(N ) as follows.
D
24 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
• FD = { Q ⊆ QN | Q ∩ FN 6= ∅ }
Put differently, δ(Q, a) = { q ′ ∈ QN | ∃q ∈ Q : (q, a) ⊢∗N (q ′ , ε) }, and QD
0 = { q′ ∈ Q |
N
0 , ε) ⊢ ∗ ′
(qN N (q , ε) }. We claim
∗ ∗
(q, w) ⊢N (q ′ , ε) iff ∃Q′ ⊆ QN : (E(q), w) ⊢D (Q′ , ε) and q ′ ∈ Q′
Proof of the claim: (⇒) By induction on |w|. Basis, |w| = 0: Then we have w = ε.
Thus (q, ε) ⊢∗N (q ′ , ε), hence q ′ ∈ E(q) by definition of E(q). So, for Q′ = E(q), we have
(E(q), w) ⊢∗D (Q′ , ε) and q ′ ∈ Q′ . Induction step, |w| > 0: Then we have w = va for
suitable v and a. Thus, for some q̄, q̄ ′ ∈ QN ,
∗ ∗
(q, w) ⊢N (q̄, a) ⊢N (q̄ ′ , ε) ⊢N (q ′ , ε)
By Lemma 2.12, (q, v) ⊢N (q̄, ε). Hence, by induction hypothesis, we can find Q̄ ⊆ QN
such that (E(q), v) ⊢∗D (Q̄, ε) and q̄ ∈ Q̄. By Lemma 2.5 we obtain (E(q), w) ⊢∗D (q̄, a).
a
Since (q̄ ′ , ε) ⊢∗N (q ′ , ε), we have q ′ ∈ E(q̄ ′ ). Put Q′ = { E(q̂ ′ ) | q̂ ∈ Q̄, q̂ −
→N q̂ ′ }.
S
a
Then Q̄ − →D Q′ . Note q̄ ∈ Q̄ and q ∈ Q′ . Combination of all this yields
∗
(E(q), w) ⊢D (Q̄, a) ⊢D (Q′ , ε) and q ′ ∈ Q′
Hence, (E(q), w) ⊢∗D (Q′ , ε) and q ′ ∈ Q′ as was to be shown.
(⇐) By induction on |w|. Basis, |w| = 0: Then we have w = ε and Q′ = E(q), since
D has no τ -transitions. By definition, if q ′ ∈ E(q), then (q, ε) ⊢∗N (q ′ , ε). Induction
step, |w| > 0: Then w = va for suitable v and a. It holds that
∗
(E(q), w) ⊢D (Q̄, a) ⊢D (Q′ , ε)
for some Q̄ ⊆ QN . Thus, again by Lemma 2.5, (E(q), v) ⊢∗D (Q̄, ε). Since (Q̄, a) ⊢D
a
(Q′ , ε) and q ′ ∈ Q′ , we have q̄ − →N q̄ ′ for some q̄ ∈ Q̄ and q̄ ′ ∈ QN such that q ′ ∈
E(q̄ ′ ). By induction hypothesis, (q, v) ⊢∗N (q̄, ε) and (q, w) ⊢∗N (q̄, a) by Lemma 2.12.
Moverover, (q̄, a) ⊢N (q̄ ′ , ε) and (q̄ ′ , ε) ⊢∗N (q ′ , ε). Combining all this yields
∗ ∗
(q, w) ⊢N (q̄, a) ⊢N (q̄ ′ , ε) ⊢N (q ′ , ε)
Thus (q, w) ⊢∗N (q ′ , ε) which proves the claim.
Now, to show that L(N ) = L(D) we reason as follows:
w ∈ L(N )
0 , w) ⊢∗ (q ′ , ε)
⇐⇒ ∃q ′ ∈ FN : (qN N
0 ), w) ⊢∗ (Q′ , ε) ∧ q ′ ∈ Q′
⇐⇒ ∃q ′ ∈ FN ∃Q′ ⊆ QN : (E(qN D
(by the claim)
⇐⇒ ∃Q′ ∈ FD : 0 ), w)
(E(qN ⊢∗D (Q′ , ε) (if Q′ ∈ FD , then Q′ 6= ∅)
⇐⇒ w ∈ L(D)
2.2. FINITE AUTOMATA 25
b b a, b, c
1 {1, 2} ∅
τ τ b c b
a a
a a
c
0 2 c {0, 1, 2} {2} c
a a
The construction above, in the proof of Theorem 2.19, takes for D the complete power-
set P(QN ) as set of states. Usually this leads to superfluous states, unreachable from
the inital state. To end up with a smaller number of states in D, one can do this more
cautiously.
Example 2.20. Consider the automaton N depicted in Figure 2.8 for which we will
construct a DFA D accepting the same language. The set of states of D will be built up
lazily. Following the construction of Theorem 2.19 we need to include the starting state
E(q0 ) = {q0 , q1 , q2 }, the ε-closure of the starting state q0 of N .
We calculate possible transitions for {q0 , q1 , q2 } for all symbols a, b, and c.
a
q0 −
→N q0 E(q0 ) = {q0 , q1 , q2 }
a
q2 −
→N q0 E(q0 ) = {q0 , q1 , q2 }
a
q2 −
→N q1 E(q1 ) = {q1 , q2 }
a
thus {q0 , q1 , q2 } −
→D {q0 , q1 , q2 } i.e. δ({q0 , q1 , q2 }, a) = {q0 , q1 , q2 }
b
q1 −
→ N q1 E(q1 ) = {q1 , q2 }
b
thus {q0 , q1 , q2 } −
→D {q1 , q2 } i.e. δ({q0 , q1 , q2 }, b) = {q1 , q2 }
c
q2 −
→N q2 E(q2 ) = {q2 }
c
thus {q0 , q1 , q2 } −
→D {q2 } i.e. δ({q0 , q1 , q2 }, c) = {q2 }
Note, apart from the initial state {q0 , q1 , q2 } we have encountered two other states, viz.
26 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
{q1 , q2 } and {q2 }. We will first calculate the transitions for {q1 , q2 }.
a
q2 −
→N q0 E(q0 ) = {q0 , q1 , q2 }
a
q2 −
→N q1 E(q1 ) = {q1 , q2 }
a
thus {q1 , q2 } −
→D {q0 , q1 , q2 } i.e. δ({q1 , q2 }, a) = {q0 , q1 , q2 }
b
q1 −
→N q1 E(q1 ) = {q1 , q2 }
b
thus {q1 , q2 } −
→D {q1 , q2 } i.e. δ({q1 , q2 }, b) = {q1 , q2 }
c
q2 −
→N q2 E(q2 ) = {q2 }
c
thus {q1 , q2 } −
→D {q2 } i.e. δ({q1 , q2 }, c) = {q2 }
No new states have been introduced; we continue with calculating the transitions for
state {q2 }.
a
q2 −
→N q0 E(q0 ) = {q0 , q1 , q2 }
a
q2 −
→N q1 E(q1 ) = {q1 , q2 }
a
→D {q0 , q1 , q2 } i.e. δ({q2 }, a) = {q0 , q1 , q2 }
thus {q2 } −
c
q2 −
→N q2 E(q2 ) = {q2 }
c
thus {q2 } −
→D {q2 } i.e. δ({q2 }, c) = {q2 }
q2 has no outgoing b-transition in N
b
thus {q2 } −
→D ∅ i.e. δ({q2 }, b) = ∅
We choose as set of states QD the states that have been introduced up to here. Thus,
This state is needed to obtain a complete transition function for D. Although the non-
determinism of transitions has been resolved now, state {q2 } is lacking a transition for b.
b a b c
So, we add {q2 } −→D ∅ together with ∅ − →D ∅, ∅ − →D ∅, and ∅ − →D ∅. Put differently, we
have δ({q2 }, b) = ∅, δ(∅, a) = ∅, δ(∅, b) = ∅, and δ(∅, c) = ∅. The state ∅ is called a trap
state. Once in, the automaton can’t get out from there.
The final states of D are those states in QD that, as subsets of QN , contain q1 , the
single final state of QN . These are {q0 , q1 , q2 } and {q1 , q2 }. The resulting DFA D is
depicted at the right of Figure 2.8.
Example 2.21. We illustrate the construction of Theorem 2.19 of a DFA D for the
NFA N given in Figure 2.4 with accepted language
E(q0 ) = {q0 , q1 , q3 , q4 }, since each state, except q2 can be reached from q0 by a sequence
of τ -transitions. The alphabet Σ is the same for N and for D.
The theorem proposes to take the complete powerset of N ’s set of states Q. However,
here we do not decide on the set of states yet. Instead, we construct a table encoding the
transition function, including states when needed. We start off with state {q0 , q1 , q3 , q4 }.
The a-transitions of N reach q0 and q4 from q0 , yielding {q0 , q4 }. The ε-closure of {q0 , q4 }
is { q0 , q1 , q3 , q4 } itself. Thus, δ({ q0 , q1 , q3 , q4 }, a) = { q0 , q1 , q3 , q4 }. Starting from
{ q0 , q1 , q3 , q4 } the b-transitions of N reach q2 from q1 and q3 from q4 , yielding { q2 , q3 }.
The ε-closure of { q2 , q3 } is { q2 , q3 }, adding no states. Thus δ({ q0 , q1 , q3 , q4 }, b) =
{ q2 , q3 }.
Starting with the inital state { q0 , q1 , q3 , q4 }, we have need of a new state, viz.
{ q2 , q3 }. We compute the a-transitions and b-transitions for this state. Both q2 and q3
have no a-transition in N . Thus, δ({ q2 , q3 }, a) = ∅, which introduces the empty set as
a new state of D. Note, the ε-closure of ∅ is ∅. The b-transitions for q2 and q3 get to q1
and q3 , respectively, yielding { q1 , q3 } with ε-closure { q1 , q3 }. Thus δ({ q2 , q3 }, a) = ∅
and δ({ q2 , q3 }, b) = { q1 , q3 }.
The empty set will have a transition to itself, for each symbol in the alphabet. Thus,
δ(∅, a) = ∅, and δ(∅, b) = ∅. Therefore, we first consider the newly introduced state
{ q1 , q3 } of D. Both q1 and q3 have no a-transition in N , thus δ({ q1 , q3 }, a) = ∅. As to
b-transitions, q1 has one, to q2 , q3 has no. This yields {q2 }. This set of states is ε closed.
Thus, we have a new state {q2 } of D, and δ({ q1 , q3 }, b) = {q2 }.
For state q2 of N there is no a-transition, thus the state {q2 } of D has an a-transition
to ∅. The state q2 has a b-transition in N , though. We have b-transition from q2 to q1
which can be extended by the τ -transition to q3 . This gives in D a b-transition to {q1 , q3 .
Hence δ({q2 }, b) = { q1 , q3 }.
Summarizing the above, we have for D the set QD of states consisting of the subsets
{ q0 , q1 , q3 , q4 }, { q2 , q3 }, { q1 , q3 }, {q2 } and ∅, and the transition function δ : QD ×
{a, b} → QD such that
a b
{q0 , q1 , q3 , q4 } {q0 , q1 , q3 , q4 } {q2 , q3 }
{q2 , q3 } ∅ {q1 , q3 }
{q1 , q3 } ∅ {q2 , q3 }
{q2 } ∅ {q1 , q3 }
∅ ∅ ∅
Finally, we have to define the set of final states FD of D. This are all states
in QD containing at least one final state of N , i.e. all states containing q3 . Thus,
FD = { {q0 , q1 , q3 , q4 }, {q2 , q3 }, {q1 , q3 } }.
The resulting graphical representation of D is given in Figure 2.9. Now, it is more
easy to read off the language accepted, viz.
which equals L(N ) = { an b, an (bb)m | n > 0, m > 0 }, with NFA N given by Figure 2.4.
28 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
a
b
b b
{0, 1, 3, 4} {2, 3} {1, 3} {1, 2}
b
a a a
a,b ∅
L = { an bm cℓ | n, m, ℓ > 0 }
(a) a b c
τ τ
0 1 2
N1
(b) The ε-closure of q0 is { q0 , q1 , q2 }, which is the initial state of the DFA. The
alphabet is the same as for N1 , hence the alphabet of D1 is {a, b, c}. We construct
a table of for the transition function of D1 , introducing new states when needed
(and writing sequences of numbers to abbreviate sets of states of N1 ):
a b c
0, 1, 2 0, 1, 2 1, 2 2
1, 2 ∅ 1, 2 2
2 ∅ ∅ 2
∅ ∅ ∅ ∅
The final states of D1 are the sets of states containing a final state of N1 , i.e. the
sets of states containing q2 . These are {q0 , q1 , q2 }, {q1 , q2 } and {q2 }. In summary,
the DFA D1 looks as follows.
2.2. FINITE AUTOMATA 29
D1
a b c
b c
0 1 2
c
a a,b
∅
(a) c
b
1 2
a
b
N2
0 c
a 3 4
b
5 c
30 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
(b)
a b c a b c D2
0 1, 3 5 ∅ 3 ∅ 3 4 c
1, 3 ∅ 2, 3 1, 4 4 ∅ ∅ ∅
b
5 ∅ ∅ 5 2 ∅ ∅ ∅ a 5 0
2, 3 ∅ 3 4 1 ∅ 2 1 a
a,b
1, 4 ∅ 2 1 ∅ ∅ ∅ ∅
1,3
c
a c b
a
1,4 2,3
b c
c b
b
b
a,b,c ∅ 2 1 3 4
a,b,c c
c
a,b
a,b,c
Exercise 2.2.9. Give an automaton over the alphabet {a, b, c}, with no more than
4 states, that accepts all strings in which at least one symbol of the alphabet does not
occur.
N3
0
2 a,c
τ
τ
3 a,b
Exercise 2.2.10.
{ cn , cn am b, cn bm a, cn | n, m > 0 }
b
N4 τ 1 2
c 0
τ 3 4
a
{ cn , cn am b, cn bm a | n, m > 0 }
N̂4 τ 1
a 0 τ
τ 2
Note ε ∈ N̂4 . Modify N̂4 into an NFA N̂4′ such that L(N̂4′ ) = L(N̂4 ) \ {ε}.
(c) Suppose the language L ⊆ Σ∗ is regular and ε ∈ L. Show that L \ {ε} is regular
too.
(a) Introduce a new state q0′ . This state has the same transitions as q0 and is reachable
from q0 by a c-transition, since q0 has a c-loop. The new state q0′ is accepting, but
state q0 isn’t anymore.
a
τ b
N4′ τ 1 2
c
c 0′ 0
τ 3 4
τ a
(b) We introduce a new final state q0′ , while the initial state q0 is not accepting anymore.
Transitions for q0′ are the same as for q0 in N̂4 . Transitions for q0′ are the non-τ
32 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
c
2
τ
(c) It is easier to do this for a DFA. The language L is regular. So, by definition,
there exists an NFA N accepting L. By Theorem 2.19 we can find a language-
equivalent DFA D. Thus, L(D) = L. Since ε ∈ L the initial state, q0 say, of D
is accepting. Define the DFA D′ = (Q′ , Σ, δ ′ , q0 , F ′ ) by (i) Q′ = Q ∪ {q0′ } for
a new state q0′ ; (ii) δ ′ is the same as δ, except δ ′ (q, a) = q0′ if δ(q, a) = q0 and
δ ′ (q0′ , a) = δ(q0 , a) for q ∈ Q, a ∈ Σ; (iii) F ′ = (F \ {q0 }) ∪ {q0′ }. Then it holds that
L(D′ ) = L(D) \ {ε} = L \ {ε}. By Theorem 2.18 there exists a language-equivalent
NFA N ′ for D′ . It follows that L \ {ε} = L(D′ ) = L(N ′ ) is accepted by an NFA,
viz. N ′ , and, hence, that L \ {ε} is regular.
Exercise 2.2.11.
(c)6∗ Prove that every finite language over some alphabet Σ is regular.
(a) The language L is regular if it is accepted by an NFA. Consider the NFA N5 below.
N5
a b c
0 1 2 3
c 6 b 4 5
c
a,c
a b b a,b
0 1 2 3 ∅ a,b,c
N5
c
c
4
c a,b,c
5
a,b,c
6
a,b,c
expressions and regular languages correspond, we show how to obtain an equivalent NFA
from a given regular expression.
We start off by introducing the class of regular expressions over a given alphabet. It is
built from the constants 0 and 1 as well as constants for every letter from the alphabet
under consideration, and it is closed under sum or union, under concatenation and under
an operator called Kleene’s star or iteration.
(iii) if r1 , r2 and r are regular expressions, then (r1 + r2 ), (r1 · r2 ) and (r∗ ) are regular
expressions.
When clear from the context, the alphabet Σ may be omitted as subscript of RE Σ .
For reasons to become clear in a minute, the regular expression (r1 + r2 ) called sum,
is also called the union of r1 and r2 , and the regular expression (r1 · r2 ) is called the
concatenation of r1 and r2 . The regular expression (r∗ ) is called the Kleene-closure or
iteration of r.
To reduce the number of parentheses we assume the ∗-construction to bind the most,
followed by concatenation ·, and with sum + having lowest priority. Typically, outermost
parentheses will be suppressed too.
In clause (iii), L(r1 )L(r2 ) denotes the concatenation of L(r1 ) and L(r2 ), while L(r)∗
denotes the Kleene-closure of L(r). Recall
L1 L2 = { w 1 w 2 | w 1 ∈ L1 , w 2 ∈ L2 }
L∗ = { w1 · · · wk | k > 0, w1 , . . . , wk ∈ L }
Example 2.24. Consider, with respect to the alphabet {a, b}, the regular expression
(a + b)∗ · a. We have
L((a + b)∗ · a)
= L((a + b)∗ )L(a)
= (L(a + b))∗ {a}
= (L(a) ∪ L(b))∗ {a}
= ({a} ∪ {b})∗ {a}
= {a, b}∗ {a}
= { w ∈ {a, b}∗ | w ends with a }
Example 2.25. The set of strings over the alphabet {a, b, c} that contains at least one a
and at most one b can be represented by the regular expression
The left part of the regular expression covers the situation where at least on symbol a
preceeds a possible appearance of b. Note, L(b + 1) = {b} + {ε} = {b, ε}. The right part
represents strings with a single occurrence of b followed by minimally one occurrence
of a, interspersed with occurrences of c.
The operators of sum and concatenation are associative with respect to their language
interpretation: L(r1 + (r2 + r3 )) = L((r1 + r2 ) + r3 )) and L(r1 · (r2 · r3 )) = L((r1 · r2 ) · r3 )).
This allows to write, e.g. r1 + r2 + r3 , instead of r1 + (r2 + r3 ) or (r1 + r2 ) + r3 , and
r1 · r2 · r3 , since order of bracketing does not matter for the language that is denoted.
Often, for notational convenience too, we write a1 a2 · · · an for the regular expression
a1 · a2 · · · · · an using juxtaposition rather than concatenation.
It turns out that for every regular expression r there exists a DFA Dr such that L(r) =
L(Dr ). We will show, see Theorem 2.33 below, that for a regular expression r a language-
equivalent NFA Nr exists. The result then follows by Theorem 2.19. The reverse is valid
as well: for every DFA D there exists a regular expression rD such that L(D) = L(rD ).
There are various ways to prove this. We first give a conceptually elegant proof based
k .
on specific regular expressions Ri,j
k , for i, j = 1, . . . , n, k = 0, . . . , n, by
More precisely, we inductively define Ri,j
0 s(i,i)
Ri,i = a1i,i + · · · + ai,i +1
0 s(i,j)
Ri,j = a1i,j + · · · + ai,j for i 6= j
k+1 k + Rk k ∗ k
Ri,j = Ri,j i,k+1 · ( Rk+1,k+1 ) · Rk+1,j
s(i,j) s(i,j)
where a1i,j , . . . , ai,j are all symbols in Σ such that δ(qi , a1i,j ) = qj , . . . , δ(qi , ai,j ) = qj ,
for i, j = 1, . . . , n.
We verify that the regular expressions Ri,j k are as intended: If k = 0 then the path
from qi to qi has no intermediate states. So, either the path has length 1 if δ(qi , a) = qi
yielding the subexpression a with meaning {a}, or the path has length 0, yielding the
subexpression 1 with meaning {ε}. This explains Ri,i 0 . The explanation of the regular
0
expression Ri,j , for i 6= j is similar. However, in this case there is no empty path from qi
to qj , since qi and qj are different states, so no subexpression 1 either.
k+1
With respect to the regular expression Ri,j , a path from qi to qj which may pass
through intermediate states q1 , . . . , qk+1 , may or may not pass through state qk+1 . If not,
the path is already represented by Ri,j k . If the path passes through q
k+1 , there is an initial
part from qi to qk+1 and a final part from qk+1 to qj that both avoid qk+1 . In between
the path can be split up in subpaths from qk+1 to qk+1 that do not pass through qk+1 as
intermediate state; it may pass through intermediate states q1 to qk only. The initial part
and final part are captured by the regular expressions Ri,k+1 k and Rk+1,jk , respectively.
The subpaths from qk+1 to qk+1 with intermediate states from q1 to qk are included
k
in Rk+1,k+1 . Any repetition of such a path is allowed, which explains the Kleene star.
Since the DFA D has n-states, the regular expression Ri,j n represents the labels of all
path through D from state qi to state qj . We have, for a string w over the alphabet Σ,
w ∈ L(D) iff there exists a path labeled w from the initial state q1 of D to a final
state qf ∈ F iff w ∈ L(R1,f n ) for a final state q . Thus, if F = {q , . . . , q }, then
f f1 fm
n
w ∈ L(D) iff w ∈ L( R1,qf + · · · R1,q n ). This proves the theorem, the language L, i.e.
1 fm
n
L(D), is the language of the regular expression R1,qf + · · · R1,qfm . n
1
a a a a+b
b b b
0 1 2 3
1 1
e
Note, since δ(qe , q ′ ) = 0 for each q ′ ∈ Q, we have that q 6= qe if w ∈ δ(q, q ′ ) for some
string w.
Example 2.28. An example GFA G is given in Figure 2.10. Here G = (Q, Σ, δ, q0 , qe )
where set of states Q = {q0 , q1 , q2 , q3 , qe }, alphabet Σ = {a, b}, initial state q0 and
final state qe marked as usual, and with the transition function δ : Q × Q → RE Σ given
by
q0 q1 q2 q3 qe
q0 a b 0 0 0
q1 0 a b 0 1
q2 0 0 a b 0
q3 0 0 0 a+b 1
qe 0 0 0 0 0
In the figure we do not draw edges labeled 0. Note the edge from q3 to itself labeled
with the regular expression a + b, and the edges from q1 and q3 to qe labeled with the
regular expression 0. The language L(G) of G is the set of all strings over {a, b} with
one, three or more occurrences of the symbol b.
With the notion of a GFA in place, we describe our procedure of constructing a language-
equivalent regular expression rD for a DFA D. As a first step for D = (Q, Σ, δ, q0 , F ),
we transform D into a language-equivalent GFA GD as follows: Let qe be a new state
not occurring in Q. Put Q′ = Q ∪ {qe }. We define δ ′ : Q′ × Q′ → RE Σ such that
δ ′ (q, q ′ ) = δ(q, q ′ ) if q, q ′ 6= qe
δ ′ (q, qe ) = 1 if q ∈ F
δ ′ (q, qe ) = 0 if q ∈
/F
δ ′ (q e , q) = 0 for all q ∈ Q′
a a a a,b
b b b
0 1 2 3
for n > 0, a1 , . . . , an ∈ Σ. Then we can choose q0′ , . . . , qn′ ∈ Q such that q0′ = q0 ,
δ(qi−1′ , a ) = q ′ for 1 6 i 6 n, and q ′ ∈ F . Put q ′
i i n n+1 = qe . Put wi = ai for 1 6 i 6 n and
wn+1 = ε. Then we have, for q0′ , . . . , qn+1
′ ∈ Q′ that q0′ = q0 , δ ′ (qi−1
′ , q′ ) = a ∋ a = w
i i i i
for 1 6 i 6 n and δ ′ (qn′ , qn+1′ ) = 1 ∋ ε = wn+1 . Thus w = (a1 · · · an )ε = w1 · · · wn+1 ∈
L(GD ). For the inclusion L(GD ) ⊆ L(D), pick w ∈ L(GD ). Then w = (a1 · · · an )ε,
since all edges toward qe are labeled 1. In particular, there are q0′ , . . . , qn′ ∈ Q such that
′ , q ′ ) = a , for 1 6 i 6 n. Moreover, q ′ = q and q ′ ∈ F . By construction it follows
δ ′ (qi−1 i i 0 0 n
that δ(qi−1 ′ , a ) = q , for 1 6 i 6 n, q ′ = 0 and q ′ ∈ F . Thus w = a · · · a ∈ L(D), as
i i 0 n 1 n
was to be shown.
Example 2.29. Consider the DFA D as given by Figure 2.11 accepting the language
{ w ∈ {a, b}∗ | #a (w) = 1 ∨ #a (w) > 3 }. The GFA GD , the GFA induced by D, is
the GFA of Example 2.28 depicted in Figure 2.10, repeated in the upper-left part of
Figure 2.12. The GFA GD \p3 , depicted as upper-right part of Figure 2.12, is obtained
from GD by eliminating state q3 . Since state q3 of GD has one incoming transition
labeled b, a loop labeled a + b, and one outgoing transition labeled 1, the path from q2
to qe via q3 is replaced by a transition labeled b · (a + b)∗ · 1 = b · (a + b)∗ . Then, removing
state q2 from GD \q3 yields (GD \q3 )\q2 at the bottom-left part of Figure 2.12. The path
from q1 via q2 to qe is combined with the transition from q1 to qe in GD \p3 giving a
single transtion from q1 to qe labeled 1 + b · a∗ · b · (a + b)∗ . Finally, eliminating state q1
gives the GFA ((GD \q3 )\q2 )\q1 , at the bottom-right of Figure 2.12. The path from q0
to qe via q1 is combined into a single transition from q0 to qe labeled by the regular
expression b · a∗ · (1 + b · a∗ · b · (a+b)∗ ). Because of its simple form, we can read off from
((GD \q3 )\q2 )\q1 the accepted language, viz. a∗ ·b·b·a∗ ·(1+b·a∗ ·b·(a+b)∗ ) = a∗ · b · a∗ +
a∗ · b · a∗ · b · a∗ · b · (a+b)∗ , i.e. the language of strings over {a, b} with one, three or
more b’s. The claim is that the DFA D of Figure 2.29 and the four GFA GD , GD \q3 ,
(GD \q3 )\q2 , and ((GD \q3 )\q2 )\q1 of Figure 2.12 are all language equivalent. Hence, the
regular expression a∗ · b · a∗ + a∗ · b · a∗ · b · a∗ · b · (a+b)∗ represents the language of the
DFA D too.
2.3. REGULAR EXPRESSIONS 39
a a a a+b a a a
b b b b b
0 1 2 3 0 1 2
1 1 1 b·(a+b)∗
e e
GD GD \q3
a a a
b
0 1 0
1 + b·a∗ ·b·(a+b)∗ b·a∗ ·(1 + b·a∗ ·b·(a+b)∗ )
e e
(GD \q3 )\q2 ((GD \q3 )\q2 )\q1
Figure 2.12: GFA sequence GD , GD \q3 , (GD \q3 )\q2 , ((GD \q3 )\q2 )\q1 of Example 2.28
The approach of successive elimination of states from a GFA, that is language equivalent
to a given DFA, to obtain a regular expression for the language of the DFA is justified
by the following theorem. Assuming the removal of the state p from a GFA G gives the
GFA G\p, the language of the two automata is the same.
Proof. (L(G) ⊆ L(G\p)) Pick w = w1 · · · wn ∈ L(G). Choose q0′ , . . . , qn′ ∈ Q such that
q0′ = q0 , qn′ = qe , and wi ∈ δ(qi−1 , qi ) for i = 1 . . . n. Define strings v1 , . . . , vm ∈ Σ∗ and
′′ as follows: m = n − #{ k | q ′ = p }, q ′′ = q ′ iff q ′ 6= p and j = i − #{ k 6
states q0′′ , . . . , qm k j i i
i | qk′ = p }. Thus, m is the number of states among q0′ , . . . , qn′ different from p, taking
multiplicities into account. Similarly, qj′′ is the j-th state in the sequence q0′ , . . . , qn′ when
skipping states qk′ that are equal to p. Define ℓ(j), low of j, such that qj−1 ′′ = qℓ′ and h(j),
high of j, such that qj′′ = qh′ , for j = 1 . . . m. Note, qk′ = p for ℓ(j) < k < h(j), ℓ(1) = 0
and h(m) = n. Now, define vj = wℓ(j)+1 · · · wh(j) . Since h(j−1) = ℓ(j), for j = 2 . . . m,
we have w = w1 · · · wn = (wℓ(1)+1 · · · wh(1) ) (wℓ(2)+1 · · · wh(2) ) · · · (wℓ(m)+1 · · · wh(m) ) =
v1 v2 · · · vm . Thus for v1 , . . . , vm and q0′′ , . . . ,qm′′ it holds that w = v · · · v , q ′′ = q ′ = q ,
1 m 0 0 0
∗
qm = qn = qe , and vj ∈ δ(qℓ(j) , p) · δ(p, p) · δ(p, qh(j) ) ⊆ δ ′ (qj−1
′′ ′ ′′ , q ′′ ), for j = 1 . . . m.
j
Thus, w = v1 · · · vm ∈ L(G\p).
(L(G\p) ⊆ L(G)) Suppose v ∈ L(G\p). Pick q0′′ , . . . , qm ′′ ∈ Q\{p} and v , . . . , v ∈ Σ∗
1 m
such that q0′′ = q0 , qm ′′ = q , v = v · · · v , and v ∈ δ ′ (q ′′ , q ′′ ), for j = 1 . . . m. Since
e 1 m j j−1 j
40 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
a 1 c
a
b,c
0 2 b b
b,c
a
a 3 c
∗
′′ , q ′′ ) = δ(q ′′ , q ′′ ) + δ ′ (q ′′ , p) · δ(p, p)
δ ′ (qj−1 j j−1 j j−1 · δ(p, qj′′ ) it follows that we can pick
m(j) m(j)
m(j) > 0, and wj0 , . . . , wj ∈ Σ∗ such that vj = wj0 · · · wj where wj0 ∈ δ(qj−1
′′ , q ′′ ) if
j
m(j) = 0, and wj0 ∈ δ(qj−1 ′′ , p), w k ∈ δ(p, p), for 1 6 k < m(j), and w m(j) ∈ δ(p, q ′′ ) if
j j j
m(j) > 0. It follows, leaving the precise details to the industrious reader, that we can
choose q0′ , . . . qn′ ∈ Q and w1 , . . . , wn ∈ Σ∗ such that q0′ = q0 , qn′ = qe , v = w1 . . . wn , and
′ , q ′ ). Thus, v ∈ L(G).
wi ∈ δ(qi−1 i
From the theorem we obtain again the result that a language represented by a DFA can
also be represented by a regular expression. However, by now we also have a procedure
as how to construct the regular expression rD for the DFA D.
Proof. We first claim that for an arbitrary GFA G there exists a regular expression r
such that L(r) = L(G). We prove the claim by induction on the number of states n of
Note, the GFA G has at least two states, viz. the inital state q0 and the end state qe .
Basis, n = 2: We have L(G) = δ(q0 , q0 )∗ · δ(q0 , qe ), or more precisely, L(GD ) =
L δ(q0 , q0 )∗ · δ(q0 , qe ) . So, put r = δ(q0 , q0 )∗ · δ(q0 , qe ).
Induction step, n > 2: Choose a state p different from q0 and qe . Consider the GFA
G\p with n − 1 states. By induction hypothesis, we can choose a regular expression r
such that L(r) = L(G\p). By Theorem 2.30, we have L(G) = L(G\p). Therefore,
L(r) = L(G).
Now, let GD be the GFA obtained from the DFA D. Then L(GD ) = L(D). By the
claim we can find a regular expression that we call rD such that L(rD ) = L(GD ). From
this we obtain L(rD ) = L(D), which proves the theorem.
In general, there are several ways to reduce the GFA GD to a two-state GFA. Therefore,
there may be several regular expressions that represent the same language.
c
G3 G2
a 1 c + b · c∗ · b
a 1
b+c b · c∗ · a a 1
0 2 b b e 1 + b · c∗
b+c a
a·c ·b
∗
a b+c
0 e
1
a 3 2
b+c + a·c∗ ·a a·c∗
G1 (b+c)·(b+c+a·c∗ ·a) + G0
(a + (b+c)·a·c∗ ·b) ·
b·c∗ ·a + c + b·c∗ ·b + a·a·c∗ ·b ∗
c + b·c∗ ·b + a·a·c∗ ·b ·
a·(b+c+a·c∗ ·a) (b·c∗ ·a + a·(b+c+a·c∗ ·a))
1
1 + b·c∗ +
0 e
a + (b+c)·a·c ·b ∗
a·a·c∗
0 e (b+c)·a·c∗ +
(a + (b+c)·a·c∗ ·b) · ∗
(b+c)·a·c∗ c + b·c∗ ·b + a·a·c∗ ·b ·
(b+c)·(b+c+a·c∗ ·a) (1 + b·c∗ + a·a·c∗ )
Figure 2.14. GFA GD has a single final state, viz. qe , with incoming edges with label 1
from the final states of D. Multiple transitions in D, e.g. from q0 to q2 , combined into
a single edge labeled with a composed regular expression.
We start by eliminating state q3 from the GFA G3 . In line with the proof of Theo-
rem 2.30, we connect incoming edges of state q3 with outgoing edges, combined with the
self loop of q3 . E.g., since δ3 (q1 , q3 ) = b, δ3 (q3 , q3 ) = c, and δ3 (q3 , q0 ) = a we create an
edge from state q1 to state q0 labeled with the regular expression b · c∗ · a in the GFA G2 ,
i.e. δ2 (q1 , q0 ) = b · c∗ · a. The label of the edge from q1 to qe gets strengthened. Since
δ3 (q1 , q3 ) = b, δ3 (q3 , q3 ) = c, and δ3 (q3 , qe ) = 1 and δ3 (q1 , qe ) = 1, we relabel the edge
from q1 to qe for G2 with 1 + b · c∗ · 1 = 1 + b · c∗ . Likewise, the edge from q1 to q3 ,
combined with the self loop of q3 , and the edge from q3 to q1 add the regular expression
b · c∗ · b to the self loop of q1 , obtaining c + b · c∗ · b for G2 . Furthermore, the label of the
edge from q2 to q0 is updated to (b + c) + a · c∗ · a, a new edge from q2 to q1 is added
labeled a · c∗ · b, as well as another edge from q2 to qe labeled a · c∗ · 1 = a · c∗ .
The reverse of Theorem 2.31 also holds true: for every regular expression r there exists
a DFA accepting the language of r. We will establish this result directly. Instead we
show that for every regular expression a corresonding NFA exists. Since for every NFA
42 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
q0 q1 q0 τ q1 q0 a q1
N0 N1 Na
there exists a language-equivalent DFA, Theorem 2.19, the counterpart of Theorem 2.31
follows.
Theorem 2.33. If a language L equals L(r) for some regular expression r, then L equals
L(N ) for some NFA N .
(ii) the inital state of Nr has only outgoing transitions (if any);
N1
q10 qf1
τ τ
Nr1 +r2 Nr1 ·r2 τ
q0 qf q10 qf1 q02 qf2
N1 N2
τ τ
q02 qf2
N2
τ
Nr ∗ τ τ
q0 q00 qf0 qf
N0
a a
τ τ τ τ
τ τ τ b τ
τ τ τ τ
b b
Example 2.34. Figure 2.17 depicts the NFA obtained by the construction described in
the proof of Theorem 2.33 for the regular expression (a + b)∗ · b · (a + b). The resulting
NFA consists of three sequential components, viz. for the iteration (a+b)∗ on the left, for
the basic regular expression b in the middle, and for the alternative composition (a + b)
on the right.
Answer to Exercise 2.3.12 Possible answers include: (i) b and ab in, ba and aba out;
(ii) aa and aba in, ε and ab out; (iii) ε and ab in, bab and aab out; (iv) ε and abba in, a
44 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
and aa out; (v) ε and a in, ab and ba out; (vi) a and b in, ε the ony string out; (vii) a
and b in, ε and aab out; (viii) ε and a in, aba and bab out.
Exercise 2.3.13. Provide a regular expression for each of the following languages.
(i) { w ∈ {a, b}∗ | w starts with a and ends in b }
(ii) { w ∈ {a, b, c}∗ | w contains at most two a’s and at least one b }
(iii) { w ∈ {a, b}∗ | |w| 6 3 }
Exercise 2.3.14. Let the GFA G have two states, q0 and qe . Show that it holds that
L(G) = δ(q0 , q0 )∗ · δ(q0 , qe ).
Answer to Exercise 2.3.14 (L(G) ⊆ δ(q0 , q0 )∗ ·δ(q0 , qe )) Let w ∈ L(G). Pick q0′ , . . . , qn′ ∈
Q and w1 , . . . , wn ∈ Σ∗ such that w = w1 · · · wn , q0′ = q0 , wi ∈ δ(qi−1 ′ , q ′ ) for 1 6 i 6 n,
i
and qn′ = qe . Since δ(qe , q0 ) = δ(qe , q0 ) = 0 and L(0) = ∅, we have q0′ , . . . , qn−1 ′ = q0 .
Thus w1 , . . . , wn−1 ∈ δ(q0 , q0 ) and wn ∈ δ(q0 , qe ). Hence w = w1 · · · wn ∈ δ(q0 , q0 )∗ ·
δ(q0 , qe ).
(δ(q0 , q0 )∗ · δ(q0 , qe ) ⊆ L(G)) Let w ∈ δ(q0 , q0 )∗ · δ(q0 , qe ). Choose w1 , . . . , wn ∈
δ(q0 , q0 ), w′ ∈ δ(q0 , qe ) such that w = w1 · · · wn w′ . Put qi′ = q0 for 0 6 i 6 n and qn+1 ′ =
′ ′ ′
qe . Since wi ∈ δ(q0 , q0 ) = δ(qi−1 , qi ) for 1 6 i 6 n, and w ∈ δ(q0 , qe ) = δ(qn , qn+1 ), it
follows that w = w1 · · · wn w′ ∈ L(G).
Exercise 2.3.15. Construct a four-state GFA G, with intermediate states p and q, such
that the regular expression belonging to (G\p)\q is different from the regular expression
belonging to (G\q)\p.
1
a
b
e 1
0 f
c
d
2
2.3. REGULAR EXPRESSIONS 45
Elimination of state q2 first, followed by elimination of state q1 yields the GFA sequence
e 1 b + f ·d e 1
0 0
∗
yielding the regular expression r2,1 = c·d + a·(b + f ·d) . However, elimination of
state q1 first, followed by elimination of state q2 yields the following two GFA:
e 1 e 1
0 c + a·f 0
∗
yielding the regular expression r1,2 = a·b + (c + a·f )·d . In line with the proof of
Theorem 2.31, both regular expressions represent the same language. It holds that
L(r1,2 ) = L(r2,1 ) = { ab, afd , cd }∗ .
Exercise 2.3.16. Give a language-equivalent GFA G for the DFA D below, successively
eliminate the intermediate states of G, and derive a regular expression rD that represents
the language of D.
a
0 1
b
b
a,b a
Answer to Exercise 2.3.16 We have the following GFA sequence of G, G\q2 and
(G\q2 )\q1 .
46 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
a a b·(a + b) +
0 1 0 1 0 a·(b + a·(a + b))
b b + a·(a + b)
b a b b + aa
a+b a
e 2 e e
1
∗
From (G\q2 )\q1 we obtain rD = b · (a + b) + a · (b + a · (a + b)) · (b + aa).
Exercise 2.3.17. (a) Give a language-equivalent GFA G for the DFA D below, suc-
cessively eliminate the intermediate states of G, and derive a regular expression rD
that represents the language of D.
b b
a
0 1
a
Answer to Exercise 2.3.17 (a) The GFA G and G\q1 look as follows
G b + a·b∗ ·a G\q1
b b
a
0 1 0
a
1 1
1 + a·1
e e
∗
From G\q1 we obtain rD = b + a · b∗ · a · (1 + a · 1).
(b) Since D accepts all strings over {a, b}, hence L(D) = {a, b}∗ , the regular expres-
sion (a + b)∗ represents D, i.e. L((a + b)∗ ) = L(D).
Exercise 2.3.18. Give a language-equivalent GFA G for the DFA D below, successively
eliminate the intermediate states of G, and derive a regular expression rD that represents
the language of D.
2.3. REGULAR EXPRESSIONS 47
c a
a
0 1
c
b a
c b
Answer to Exercise 2.3.18 We have the following GFA sequence of G, G\q2 and
(G\q2 )\q1 .
G G\q2
c a c + b·b ·c
∗
a + b·b ·b
∗
a
a + b·b∗ ·a
0 1
c 0 1
a c + b·b∗ ·b
1
c b b 1
2 e b·b∗
1 e
b
(G\q2 )\q1
c + b·b∗ ·c +
(a + b·b∗ ·a) · (a + b·b∗ ·a)∗ · (c + b·b∗ ·b)
∗
From (G\q2 )\q1 we obtain rD = c + b·b∗ ·c + (a + b·b∗ ·a) · (a + b·b∗ ·a)∗ · (c + b·b∗ ·b) ·
b·b∗ + (a + b·b∗ ·a) · (a + b·b∗ ·a)∗ · (1 + b·b∗ ) .
Exercise 2.3.19. Guess a regular expression for each of the following languages. Next
provide a DFA for each language and construct a regular expression via elimination of
states.
b
b a
b
b a
0 1 2
∗
and the regular expression b + a · (b + a · a∗ · b) (1 + a).
(b) The DFA D, given by
a
a a, b
b
a b
0 1 2 3
b
e e
e e
2.3. REGULAR EXPRESSIONS 49
∗
and the regular expression a + b · (b + aa) · (1 + b + ba).
(c) The DFA D, given by
b a
3 0 1
a b
a, b
b a b a
4 5 2
b a
G G\q5
e e
1 1
b a b a
3 0 1 3 0 1
a b a b
a, b
b a b a b a b a
4 5 2 4 2
b a
ba
1 1
b a a
3 0 1 b·(ba)∗ ·a 0 1
a b b
b a b a
2 2
e e
ab
1 1
a
b·(ba)∗ ·a 0 1 b·(ba)∗ ·a + a·(ab)∗ ·b 0
b
50 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
∗
and the regular expression (b · (ba)∗ · a) + (a · (ab)∗ · b) .
Exercise 2.3.20. Construct for each of the following regular expressions a language-
equivalent NFA.
(a) a · (b + c) · d∗
(b) (a + b) · c∗ · d
(a)
b
τ
τ τ
a τ τ τ d τ
τ τ
τ
c
(b)
a
τ
τ τ
τ τ c τ τ d
τ τ
τ
b
(c)
τ τ
a d
τ τ τ τ
τ τ τ c τ τ τ
τ τ τ τ
b e
τ τ
2.4. PROPERTIES OF THE CLASS OF REGULAR LANGUAGES 51
(d) τ
τ
τ
τ
τ τ
τ τ
τ τ τ τ
τ τ
τ b τ
τ τ
c
τ τ
d
The next theorem states that NFAs, DFAs and regular expressions define the same class
of languages, viz. the class of regular languages. The theorem, or rather the theorems
its proof refers to, also provides flexibility in the representation of a regular language.
Given a regular language L as either the language accepted by an NFA or by a DFA
or as the language of a regular expression, we can construct an NFA or a DFA that
accepts L or a regular expression who’s language is L.
Theorem 2.36. Let L be a language. The following three statements are equivalent:
Proof. [(i) ⇒ (ii)] By definition there exists an NFA N such that L = L(N ). By
Theorem 2.19 there exists a DFA D such that L(N ) = L(D). Then, clearly, L = L(D)
for the DFA D.
[(ii) ⇒ (iii)] Suppose L = L(D) for a DFA D. By Theorem 2.31 there exists a
regular expresion r such that L(D) = L(r). Thus clearly, L = L(r) for the regular
expression r.
52 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
[(iii) ⇒ (i)] Suppose L = L(r) for a regular expression r. By Theorem 2.33 there
exists an NFA N such that L(N ) = L(r). Therefore L = L(N ) and L is a regular
language.
Next we investigate closure properties of the class of regular languages. The following
theorem states that the class of regular languages is closed under union, complement
and intersection. We use the flexibility provided by Theorem 2.36 to choose or use the
representation of a regular language that suits best.
Theorem 2.37.
Proof. (a) By Theorem 2.36 we can find regular expressions r1 and r2 such that L1 =
L(r1 ) and L2 = L(r2 ). Then we have L1 ∪ L2 = L(r1 ) ∪ L(r2 ) = L(r1 + r2 ). Thus, by
Theorem 2.36 again, L1 ∪ L2 is a regular language.
(b) Let, applying Theorem 2.36, D = (Q, Σ, δ, q0 , F ) be a DFA such that L(D) = L.
Define the DFA D′ = (Q, Σ, δ, q0 , F ′ ) by putting F ′ = Q \ F . Thus q ∈ Q is a final
state in D′ iff q is not a final state in D. By definition we have w ∈ L(D′ ) if both
(q0 , w) ⊢∗D′ (q, ε) and q ∈ F ′ . This is equivalent to (q0 , w) ⊢∗D (q, ε) and q ∈
/ F , by
definition of F ′ . But, this is exactly when w ∈ / L(D), since D is deterministic. (See
Lemma 2.5.)
(c) By the laws of De Morgan, L1 ∩ L2 = ( LC C C C C
1 ∪ L2 ) . The languages L1 and L2 are
C C
regular, by regularity of L1 and L2 and part (b). Thus L1 ∪ L2 is regular, by part (a).
Therefore, ( LC C C
1 ∪ L2 ) is regular, again by part (b).
Note, for item (b), it is important that the automaton we consider is deterministic.
Changing acceptance and non-acceptance in an NFA does not lead in general to the
complement of the accepted language.
A proof for part (a) and (c) of the theorem based on the construction of an automaton,
as the proof for part (b), is possible as well. For this we make use of the so-called product
automaton of two DFA. We first consider the case of the union L1 ∪ L2 of two regular
languages L1 and L2 over some alphabet Σ.
Suppose the DFA D1 and D2 , with Di = ( Qi , Σ, δi , q0i , Fi ) for i = 1, 2, accept L1
and L2 , respectively. We define the product DFA D = ( Q, Σ, δ, q0 , F ) as follows:
(i) The set of states Q is the Cartesian product Q1 × Q2 of Q1 and Q2 . Thus a state
of Q is a pair of states hq1 , q2 i with q1 ∈ Q1 , q2 ∈ Q2 .
(ii) The initial state of Q is therefore also a pair, viz. hq01 , q02 i, consisting of the initial
state q01 of Q1 , and the initial state q02 of Q2 .
2.4. PROPERTIES OF THE CLASS OF REGULAR LANGUAGES 53
(iv) The set of final states F comprises, in the case of the union, all pairs in Q of
which at least one component is a final state, for D1 , for D2 , or for both. Thus
F = F1 × Q2 ∪ Q1 × F2 . Put differently,
F = { hq1 , q2 i ∈ Q | q1 ∈ F1 ∨ q2 ∈ F2 }
Next, we verify that D accepts L1 ∪ L2 . For this we make the following claim, which can
be proven by induction on the length of w. Claim: for q1 , q1′ ∈ Q1 , q2 , q2′ ∈ Q2 , w ∈ Σ∗
it holds that
∗ ∗ ∗
(q1 , w) ⊢1 (q1′ , ε) ∧ (q2 , w) ⊢1 (q2′ , ε) ⇐⇒ (hq1 , q2 i, w) ⊢D (hq1′ , q2′ i, w′ ) (2.2)
Using the claim we derive, for w ∈ Σ∗ ,
w ∈ L(D1 ) ∪ L(D2 )
⇔ ∃q1 ∈ F1 : (q01 , w) ⊢∗1 (q1 , ε) ∨ ∃q2 ∈ F2 : (q02 , w) ⊢∗2 (q2 , ε)
⇔ ∃q1 ∈ F1 , q2 ∈ Q2 : (q01 , w) ⊢∗1 (q1 , ε) ∧ (q02 , w) ⊢∗2 (q2 , ε) ∨
∃q1 ∈ Q1 , q2 ∈ F2 : (q01 , w) ⊢∗1 (q1 , ε) ∧ (q02 , w) ⊢∗2 (q2 , ε)
⇔ ∃q1 ∈ F1 , q2 ∈ Q2 : (hq01 , q02 i, w) ⊢∗D (hq1 , q2 i, ε) ∨
∃q1 ∈ Q1 , q2 ∈ F2 : (hq01 , q02 i, w) ⊢∗D (hq1 , q2 i, ε)
⇔ ∃q ∈ F : (q0 , w) ⊢∗D (q, ε) (by definition of F )
⇔ w ∈ L(D)
Thus L1 ∪ L2 = L(D1 ) ∪ L(D2 ) = L(D). Since L1 ∪ L2 is a language accepted by a DFA,
it is a language accepted by an NFA by Theorem 2.36. Hence, L1 ∪ L2 is regular.
In order to show that the intersection L1 ∩ L2 of two regular langauges is regular
too, we can exploit the product automaton again without only a slight adaptation of its
set of final states. Assume that the DFA D1 and D2 , with Di = ( Qi , Σ, δi , q0i , Fi ) for
i = 1, 2, accept L1 and L2 , respectively. Define the product DFA D = ( Q, Σ, δ, q0 , F )
where Q, δ, and q0 are as before, and now F = F1 × F2 . Thus, a state hq1 , q2 i ∈ Q1 × Q2
is final iff both q1 ∈ Q1 and q2 ∈ Q2 are final, i.e. q1 ∈ F1 and q2 ∈ F2 .
The proof that L(D) = L(D1 ) ∩ L(D2 ) makes use of the claim of Equation (2.2) too.
Now we argue
w ∈ L(D1 ) ∩ L(D2 )
⇔ ∃q1 ∈ F1 : (q01 , w) ⊢∗1 (q1 , ε) ∧ ∃q2 ∈ F2 : (q02 , w) ⊢∗2 (q2 , ε)
⇔ ∃q1 ∈ F1 , q2 ∈ F2 : (hq01 , q02 i, w) ⊢∗D (hq1 , q2 i, ε)
⇔ ∃q ∈ F : (q0 , w) ⊢∗D (q, ε) (by the current definition of F )
⇔ w ∈ L(D)
54 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
In order to show that a language is a regular language, we can either show that it is the
language of a regular expression, that is the language accepted by a DFA, or that it it
the language accepted by an NFA. However, not every language is a regular language.
But, so far we do not have means to acutally show that a language isn’t regular. The
next theorem, aptly called the Pumping Lemma, provides a tool to do so.
Theorem 2.38 (Pumping Lemma for regular languages). Let L be a regular language
over an alphabet Σ. There exists a constant m > 0 such that each w ∈ L with |w| > m
can be written as w = xyz where x, y, z ∈ Σ∗ are strings such that y 6= ε, |xy| 6 m, and
for all k > 0: xy k z ∈ L.
Proof. Suppose, with appeal to Theorem 2.36, L = L(D) for a DFA D. Choose m to
be the number of states of D. Suppose w ∈ L and |w| > m. Say w = a1 · · · an , thus
n > m. Pick n + 1 states q0 , . . . , qn with q0 the initial state of D, δD (qi−1 , ai ) = qi , for
1 6 i 6 n, and qn a final state. Thus (qi−1 , ai ai+1 · · · an ) ⊢D (qi , ai+1 · · · an ). Since D
has m states, the first m+1 states q0 , . . . , qm cannot all be different. Pick m1 , m2 such
that 0 6 m1 < m2 6 m and qm1 = qm2 . Put
Since qm1 = qm2 , it follows that (qm1 , y) ⊢∗D (qm1 , ε). Thus (qm1 , y k ) ⊢∗D (qm1 , ε) (by
Lemma 2.5 and an inductive argument), and (qm1 , y k ) ⊢∗D (qm2 , ε) for arbitrary k > 0.
Therefore
∗ ∗ ∗
(q0 , x) ⊢D (qm1 , ε), (qm1 , y k ) ⊢D (qm2 , ε), and (qm2 , z) ⊢D (qn , ε)
for arbitrary k > 0, and hence (q0 , xy k z) ⊢∗D (qn , ε). Thus w = xy k z ∈ L(D) = L for
all k > 0, since qn is a final state.
Essential use is made of the fact that the DFA has finitely many states only: m+1 states
are chosen, viz. q0 , . . . , qm , of which there are at most m states different. So, at least
one state is doubled. This state is the begin and end point of the loop y that can be
taken any number of times –zero, one ore more– depending on the input string.
Consider the DFA D given by Figure 2.18. We claim that a choice for m = 3 will satisfy
the claim of the Pumping Lemma. A string w of 3 symbols or more symbols that is
2.4. PROPERTIES OF THE CLASS OF REGULAR LANGUAGES 55
a a
0 1 2
b a,b
X
a,b
Figure 2.18: DFA hits the b -loop for input of sufficient length
accepted by D follows a path from the initial state q0 to the final state q2 visiting 4 or
more states. There only 3 different states to consider as qx is a sink state. So, one or
more states are visited more often. In this particular case this is state q1 . It follows that
the b -loop is done mulitple times, in fact ℓ = |w| − 2 times. If we split up w in x, y
and z, i.e. w = xyz, with x = a, y = b, and z = b ℓ−1 a, we have |xy| 6 3, y 6= ε and
xy k z = ab k b ℓ−1 a = ab k+ℓ−1 a is accepted by D.
The Pumping Lemma for regular languages is mainly used to prove negative results,
i.e. it is used to prove that a language is not regular. One can do so by exploiting the
‘reverse’ of the Pumping Lemma: for each m > 0 a string w ∈ L is given for which
no split up in x, y and z meeting the extra requirements in possible. In particular, a
split-up of w as w = xyz with |xy| 6 m and y 6= ε will give rise to a string w′ = xy k z,
for some k > 0, which is not in L. By Theorem 2.38 it then follows that the language
cannot be regular. We provide two examples of this technique.
Example 2.39. The language L = { an b n | n > 0 } is not a regular language. Let m > 0
be arbitrary. Consider the string w = am bm . We have that w ∈ L. Suppose we split
w = xyz such that |xy| 6 m, and y 6= ε. Then the string y is a non-empty string of a’s,
say y = aℓ . Thus the string w′ = xy 2 z = am+ℓ bm , hence w′ ∈/ L. We conclude that
there is no constant m as mentioned by the Pumping Lemma, and therefore L is not a
regular language.
2
Example 2.40. The language L = { an | n > 0 } is not a regular language. Choose
2
any m > 0. Consider w = am ∈ L. Suppose we can split w = xyz such that |xy| 6 m,
and y 6= ε. Then the string y is a non-empty string of a’s, say y = aℓ with 1 6 ℓ 6 m.
2
Put w′ = xy 2 z, then we have w′ = am +ℓ . But m2 < m2 + ℓ 6 m2 + m < m2 + 2m + 1 =
(m + 1)2 , thus m2 + ℓ isn’t a square. So, w′ ∈ / L. Thus, we conclude that there is
no constant m as mentioned by the Pumping Lemma, and therefore L is not a regular
language.
We close the chapter by looking into two decision algorithms for regular languages. The
first decision algorithm needs to determine whether given a regular language L as input,
56 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
L is or is not the empty language. We first solve the question if L is given by a regular
expression. With appeal to Theorem 2.36 we can conclude that the theorem holds as
well if L is given as the language accepted by an NFA.
• L = ∅ if r = 0;
• L 6= ∅ if r = 1;
• L 6= ∅ if r = a for some a ∈ Σ;
• L = ∅ if r = r1 + r2 for two regular expressions r1 and r2 and both L(r1 ) and L(r2 )
are empty, L 6= ∅ if L(r1 ) or L(r2 ) is non-empty;
Note that the decision procedure terminates since the recusive calls for r1 , r2 and r′
above involve a structurally simpler argument.
Now, suppose L = L(N ) for an NFA N . Construct, using the algorithms given in
the proofs of Theorem 2.19 and Theorem 2.31, a regular expression r such that L = L(r)
and decide whether L(r) = ∅.
Proof. Construct, using the algorithm given in the proof of Theorem 2.19, a DFA D
such that L(D) = L(N ). Simulate D starting from its initial state on input w, say
(q0 , w) ⊢∗D (q ′ , ε) for some state q ′ of D. Decide w ∈ L if q ′ is a final state of D; decide
w∈ / L otherwise.
Exercise 2.4.21. Prove using the Pumping Lemma, Theorem 2.38, that the following
languages are not regular.
(b) This is a variant. Pick any m > 0. Consider the string w = am bm−1 . We have
w ∈ L2 . Assume we can write w = xyz for strings x, y and z such that |xy| 6 m
and y 6= ε. Then we have x = a ℓ1 with 0 6 ℓ1 < m, y = a ℓ2 with 0 < ℓ2 6 m,
and z = am−(ℓ1 +ℓ2 ) bm−1 . Consider the string w′ = xy 0 z, i.e. w′ = xz since y 0 = ε.
Then we have w′ = a ℓ1 am−(ℓ1 +ℓ2 ) . But, since ℓ1 + m − (ℓ1 +ℓ2 ) 6 m−1, w′ ∈ / L2 .
We conclude that, since no m > 0 exists meeting the requirements of the Pumping
Lemma, L2 is not a regular language.
(c) Let m > 0. Consider the string w = am bm c2m . Then w ∈ L3 . As usually, assume
we can write w = xyz for strings x, y and z such that |xy| 6 m and y 6= ε. Then
it must be the case that x = a ℓ1 with 0 6 ℓ1 < m, y = a ℓ2 with 0 < ℓ2 6 m,
and z = am−(ℓ1 +ℓ2 ) bm c2m . Consider the string w′ = xy 2 z. Then it holds that
w′ = a ℓ1 a ℓ2 a ℓ2 am−(ℓ1 +ℓ2 ) bm c2m = am+ℓ2 bm c2m . But, then w′ ∈
/ L3 since ℓ2 > 0
and therefore m + ℓ2 + m 6= 2m.
Exercise 2.4.22. Prove that the language L4 = { vv R | v ∈ {a, b}∗ } is not regular.
Answer to Exercise 2.4.22 Let m > 0 be arbitrary. Consider the string w = am bbam .
We have w ∈ L4 . Suppose w = xyz is a split-up of w with |xy| 6 m and y 6= ε. Then x =
a ℓ1 with 0 6 ℓ1 < m, y = a ℓ2 with 0 < ℓ2 6 m, and z = am−ℓ1 −ℓ2 bbam . Now consider the
string w′ = xy 2 z. It holds that w′ = a ℓ1 a ℓ2 a ℓ2 am−ℓ1 −ℓ2 bbam = aℓ1 +ℓ2 +ℓ2 am−ℓ1 −ℓ2 bbam =
am+ℓ2 bam . Since ℓ2 6= 0 we have m + ℓ2 6= m. So, bb is not in the middle of the string w′ ,
hence w′ is not of the form vv R . We conclude that there is no constant m as mentioned
by the Pumping Lemma, and therefore L4 is not a regular language.
0 < ℓ2 6 m, and z = a p−(ℓ1 +ℓ2 ) . Now consider the string w′ = xy p+1 z. It holds that
w′ = aℓ1 +(p+1)·ℓ2 +p−(ℓ1 +ℓ2 ) = a p+p·ℓ2 . Since ℓ2 6= 0, the number p + p·ℓ2 is not prime, for
p + p·ℓ2 = p·(1 + ℓ2 ). So, there is no constant m as mentioned by the Pumping Lemma,
and therefore L5 is not a regular language.
Exercise 2.4.24.
(b) Prove that the language L6 = { an | n = 2k for some k > 0 } is not regular.
Exercise 2.4.25. Prove that the following languages are not regular.
(a) Choose any m > 0. Consider the string w = am bm . Clearly #a (w) = #b (w), thus
w ∈ L7 . Assume w = xyz for strings x, y and z with |xy| 6 m and y 6= ε. Then
x = a ℓ1 with 0 6 ℓ1 < m, y = a ℓ2 with 0 < ℓ2 6 m, and z = a m−(ℓ1 +ℓ2 ) bm . Put
w′ = xy 2 z. Then w′ = am+ℓ2 bm . Since #a (w′ ) = #b (w′ ), we have w′ ∈
/ L7 . So,
no m > 0 has the properties as guaranteed by the Pumping Lemma for a regular
language. Therefore L7 is not regular.
(b) Applying the Pumping Lemma directly doesn’t work. Instead we make use of a
closure property of the class of regular languages. According to Theorem 2.37b the
complement of a regular language is a regular language itself. Now, the complement
of the language L8 is precisely the language L7 . But, according to part (a), L7
isn’t a regular language. So, L8 is neither.
2.4. PROPERTIES OF THE CLASS OF REGULAR LANGUAGES 59
(a) Consider the DFA D that accepts the language L9 = { abcw | w ∈ {a, b, c}∗ }.
D
a b c
0 1 2 3 a, b,c
a,c
b,c a,b
X
a,b,c
(b) Prove that the class of regular languages is closed under reversal, i.e. if a language L
is regular, then so is LR = { wR | w ∈ L }.
(a) We add a new initial state q0′ that is connected to the final state q3 of D, which
is not final in N . The only final state of N is q0 . All arrows of D are reversed
in N . Note multiple outgoing transitions for various states on various symbols.
Also note, the trap state qx of D is not reachable in N , and can be left out.
N
a b c
0 1 2 3 a, b,c
a,c
τ
b,c x a,b
0′
a,b,c
(b) Suppose, in view of Theorem 2.36, that L is the language accepted by the DFA D =
( Q, Σ, δD , q0 , F ). Define the NFA N = ( Q′ , Σ, →N , q0′ , {q0 } ) as follows: Let q0′
be a fresh state not in Q. We put Q′ = Q ∪ {q0′ }. The transition relation →N
satisfies
τ
∀q ∈ Q : q0′ −
→N q if q ∈ F
a
∀q, q ′ ∈ Q ∀a ∈ Σ : q′ −
→N q if δD (q, a) = q ′
60 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
One can prove, exploiting the fact that N has no τ -transitions for states q ∈ Q,
that
∗ ∗
(q, w) ⊢D (q ′ , ε) ⇐⇒ (q ′ , wR ) ⊢N (q, ε) (2.3)
From this it follows that
w ∈ L(D) ⇔ ∃q ∈ F : (q0 , w) ⊢∗D (q, ε)
⇔ ∃q ∈ F : (q, wR ) ⊢∗N (q0 , ε) ⇔ wR ∈ L(N )
X △Y = { x ∈ X | x ∈
/ Y }∪{y ∈Y |y ∈
/X}
Prove that the class of regular languages is closed under symmetric difference, i.e. if the
languages L1 and L2 are regular, then so is L1 △ L2 .
Answer to Exercise 2.4.27 We construct variant of the product automaton that accepts
L1 △ L2 . Suppose the DFA D1 and D2 over the alphabet Σ, with Di = ( Qi , Σ, δi , q0i , Fi )
for i = 1, 2, accept L1 and L2 , respectively. Define the product DFA D = ( Q, Σ, δ, q0 , F )
where Q, δ, and q0 are as before:
(i) Q = Q1 × Q2 ;
F = { hq1 , q2 i ∈ Q1 × Q2 | (q1 ∈ F1 ∧ q2 ∈
/ F2 ) ∨ (q1 ∈
/ F1 ∧ q2 ∈ F2 ) }
w ∈ L(D1 ) △ L(D2 )
⇔ w ∈ L(D1 ) \ L(D2 ) ∨ w ∈ L(D2 ) \ L(D1 )
⇔ ∃q1 ∈ Q1 , q2 ∈ Q2 : (q01 , w) ⊢∗1 (q1 , ε) ∧ (q02 , w) ⊢∗2 (q2 , ε) ∧
(q1 ∈ F1 ∧ q2 ∈/ F2 ) ∨ (q1 ∈ / F1 ∧ q2 ∈ F2 )
⇔ ∃q1 ∈ Q1 , q2 ∈ Q2 : (hq01 , q02 i, w) ⊢∗D (hq1 , q2 i, ε) ∧ hq1 , q2 i ∈ F
⇔ ∃q ∈ F : (q0 , w) ⊢∗D (q, ε)
⇔ w ∈ L(D)
We start off with the central notion of this section, viz. L-equivalence for the states of a
DFA accepting the language L.
Definition 2.43. Let D be a DFA with set of states Q, set of final states F , and accepted
language L. Two states q1 , q2 of D are called L-equivalent, notation q1 ≈L q2 , if
δ(q1 , w) ∈ F ⇐⇒ δ(q2 , w) ∈ F
From the definition it follows that δ(q1 , a) ≈L δ(q2 , a) if q1 ≈L q2 , for all a ∈ Σ. This can
be seen as follows: Put q1′ = δ(q1 , a) and q2′ = δ(q2 , a). Then, for all w ∈ Σ∗ , δ(q1′ , w) ∈ F
iff δ(q1 , aw) ∈ F iff δ(q2 , aw) ∈ F iff δ(q2′ , w) ∈ F .
Example 2.44. Consider the DFA D given by Figure 2.19. Put L = L(D). The states q0
and q2 of this automaton are L-equivalent: δ(q0 , w) ∈ F iff w ∈ (bb)∗ +(bb)∗ ·aa·(aa+bb)∗
iff w ∈ (aa + bb)∗ , and δ(q2 , w) ∈ F iff w ∈ (aa + bb)∗ .
It follows that the states q4 and q6 are L-equivalent too, since (i) q4 , q6 ∈ / F,
(ii) δ(q4 , aw) ∈ F iff δ(q5 , w) ∈ F iff δ(q6 , aw) ∈ L, and (iii) δ(q4 , bw) ∈ F iff δ(q0 , w) ∈ F
iff δ(q2 , w) ∈ F iff δ(q6 , bw) ∈ F .
The state q1 is not L-equivalent to the states q0 and q2 . The latter are final states,
thus δ(q0 , ε), δ(q2 , ε) ∈ F , but the former is not a final state, thus δ(q1 , ε) ∈ / F . Also,
q1 is not L-equivalent to any of q4 , q5 and q7 . For example δ(q1 , a) ∈ F , which does not
hold for the other states mentioned.
/ F , for all w ∈ Σ∗ . For state q3
Finally, since state q5 is a trap state, we have δ(q5 , w) ∈
is holds that δ(q3 , w) ∈ F iff w ∈ b∗ ·a, and for state q7 , δ(q7 , w) ∈ F iff w ∈ a·b∗ ·a. It
follows that q3 is not equivalent to any other state, and so are q5 and q7 . Note, states
q3 and q7 are not reachable from the initial state q0 .
b
a
a a
0 1 2 3
a
b b b b b a
4 a 5 a 6 7
b
a, b
Example 2.46. Returning to the DFA D of Figure 2.19, we distinguish the L-equivalence
classes {q0 , q2 }, {q1 }, {q4 , q6 } and {q5 }, as well as {q3 } and {q7 }. Following Defini-
tion 2.45, we obtain a DFA DL as depicted in Figure 2.20: the states are the equivalence
classes of ≈L , the equivalence class {q0 , q2 } which contains q0 is the initial state, fi-
nal state is the equivalence class {q0 , q2 } too. The transitions are inherited from the
DFA D. E.g., δL ({q0 , q2 }, a) = {q1 } since both δ(q0 , a) = q1 , and δ(q2 , a) = q1 . Likewise,
δL ({q0 , q2 }) = {q4 , q6 } since δ(q0 , a) = q4 and δ(q2 , a) = q6 . Also, δL ({q3 }) = {q0 , q2 }
since δ(q3 , a) = q2 . Note, we do not have δ(q3 , a) = q0 .
If we remove from the quotient DFA DL of Figure 2.20 the non-reachable states {q3 }
and {q7 } we obtain a minimal representation of the DFA D of Figure 2.19. See Fig-
ure 2.21. Off course, we could have better started from a DFA, smaller than D, having
reachable states only, since this reduces the number of L-equivalences to check. Intu-
itively, after the superfluous states q3 and q7 are dispensed with, the states q2 and q6 are
2.5. CONSTRUCTING A MINIMAL DFA 63
a b
a
02 1 3
a
b b b a
46 a 5 a,b 7
a
02 1
a
b b b
46 a 5 a,b
folded along the line through states q1 and q5 onto the states q0 and q4 , respectively. Note
that this way the transitions involved, as well states being final or not, are preserved.
Clearly, the DFA of Figure 2.21 also accepts the language (aa + bb)∗ and has 4 states
only. The construction of (i) restricting to reachable states, and (ii) taking a quotient
modulo ≈L , indeed provides a DFA accepting the same language as the original DFA.
Theorem 2.48 below claims that this is the case generally. In addition, the theorem
states that the DFA obtained this way is of minimal size. For the proof of the theorem
we need an auxiliary result.
Lemma 2.47. Let the DFA D and DL be as given by Definition 2.45. For all states q ∈ Q
and all states C ∈ QL such that q ∈ C, it holds that
δ(q, w) ∈ F ⇐⇒ δL (C, w) ∈ FL
Next we show the correctness of the quotient construction to find a minimal DFA DL
accepting the same language as a given DFA D. Initially we assume that D has only
reachable states.
Theorem 2.48. Let D be a DFA with accepted language L. Assume that all states
of D are reachable. Let DL be the quotient DFA of D with set of states QL . Then
L(DL ) = L. Moreover, if for a DFA D ′ with set of states Q′ it holds that L(D ′ ) = L,
then |QL | 6 |Q′ |.
∀C ∈ QL ∃q ′ ∈ Q′ ∀w ∈ Σ∗ : δL (C, w) ∈ FL ⇐⇒ δ ′ (q ′ , w′ ) ∈ F ′
Since all states of DL are reachable, this notion is well-defined. With this notion in
place, we proceed proving the claim, by induction on the minimal path length mpℓ(C)
of a state C of DL .
Basis, mpℓ(C) = 0: It holds that C = C0 . Since L(DL ) = L(D), as shown above,
and, by assumption L(D ′ ) = L(D), we have L(DL ) = L(D ′ ). Thus δL (C0 , w) ∈ FL iff
δ ′ (q0′ , w) ∈ F ′ . So, we can pick q0′ to correspond to C0 . Induction step, mpℓ(C) = n + 1:
Suppose C = δL (C0 , va) for a string v ∈ Σn and a symbol a ∈ Σ. Put C = δL (C0 , v).
Then mpℓ(C) = n and δL (C, a) = C. By induction hypothesis we can choose a state q̄ ′ ∈
Q′ such that δL (C, w) ∈ FL iff δ ′ (q̄ ′ , w) ∈ F ′ , for all w ∈ Σ∗ . In particular, δL (C, aw′ ) ∈
2.5. CONSTRUCTING A MINIMAL DFA 65
FL iff δ ′ (q̄ ′ , aw′ ) ∈ F ′ , for all w′ ∈ Σ∗ . Consider q ′ = δ(q̄ ′ , a). We have, for each string
w ∈ Σ∗ ,
δL (C, w) ∈ FL
⇐⇒ δL (C, aw) ∈ FL (since δL (C, a) = C)
⇐⇒ δ ′ (q̄ ′ , aw) ∈ F′ (induction hypothesis)
⇐⇒ δ ′ (q ′ , w) ∈ F′ (since δ ′ (q̄ ′ , a) = q ′ )
This proves the claim.
Now, choose for each state C of DL , with the help of the claim, a state qC ′ of D ′
′ ′ ′ ∗ ′
such that δL (C, w) ∈ FL iff δ (qC , w) ∈ F , for all w ∈ Σ . Then it holds that qC1 6= qC ′
2
if C1 6= C2 , for all C1 , C2 ∈ QL . For, suppose qC ′ = q′ for some C , C ∈ Q . Then
1 C 2 1 2 L
we have δL (C1 , w) ∈ FL iff δL (C2 , w) ∈ FL , for all w ∈ Σ∗ . Now pick, q1 ∈ C1 , q2 ∈ C2 .
Then, by Lemma 2.47, we have δ(q1 , w) ∈ F iff δL (C1 , w) ∈ FL , and δ(q2 , w) ∈ F iff
δL (C2 , w) ∈ FL , for all w ∈ Σ∗ . Thus δ(q1 , w) ∈ F iff δ(q2 , w) ∈ F , for all w ∈ Σ∗ .
Hence, q1 ≈L q2 and C1 = C2 , since C1 and C2 as states of QL are equivalence classes
of ≈L .
We conclude that all states qC ′ ∈ Q′ , for C ∈ Q , are different, i.e. the mapping
L
C 7→ qC from QL to Q is an injection. Hence D ′ has at least as many states as DL .
′ ′
To smoothen the proof, the theorem above assumes that the DFA D started from has
reachable states only. This is by no means essential. Given any DFA D having or not
having non-reachable states, we can first restrict to its reachable states, and next take
the quotient automaton. This leads to a minimal representation of D, i.e. to a DFA Dmin
that accepts the same language and is minimal in the number of states.
Corollary 2.49. Let D be a DFA with accepted language L, let D ◦ be its restriction
to reachable states, and DL◦ be the quotient DFA of D ◦ . Then there exists no DFA that
accepts L and has fewer states than DL◦ .
Proof. Since L(D ◦ ) = L, the quotient DFA DL◦ is well-defined. According to Theo-
rem 2.48, every DFA accepting L has as least as many states as DL◦ .
With the above results in available, we can construct a minimal DFA representation for a
regular language L given by a DFA accepting it, if we can find the L-equivalence classes
of the set of states. Rather than checking each pair of states against the Definition 2.43
which involves strings of arbitrary length, we can identify the equivalence classes by
stepwise refinement. We start from two so-called blocks, one block holding all non-final
reachable states and another block holding all final states, and split blocks into smaller
subblocks by checking single transitions, thus involving one-letter words only. Before we
describe the general algorithm, we first discuss an example.
Example 2.50. Consider the DFA of Figure 2.22, accepting the set of strings over {a, b}
containing a substring baa. The DFA has set of states Q = {q0 , q1 , q2 , q3 , q4 , q5 , q6 , q7 }.
Initially, we consider two blocks of states, viz. B012456 = {q0 , q1 , q2 , q4 , q5 , q6 } and B37 =
{q3 , q7 }, the one containing all non-final reachable states, the other containing all final
66 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
b a
0 1 a 2 3 a,b
a a b b a
a
a
4 5 6 7 b
b
b
b
a,b a
a a
b 8 9 10 11
b
reachable states. Note, the non-reachable states q8 , q9 , q10 and q11 are not considered.
Next we determine for each state in the blocks to what blocks, rather than states, an
a-transition and a b-transition is possible. E.g., state q0 has an a-transition to state q4 of
block B012456 , and a b-transition to state q1 also of block B012456 . State q2 however, has
an a-transition to state q3 of block B37 , and a b-transition to state q5 of block B012456 .
This leads to the following table, with two columns for the blocks B012456 and B37 , eight
rows for each individual reachable state.
012456 37
0 a,b
1 a,b
2 b a
4 a,b
5 a,b
6 b a
3 a,b
7 a,b
0145 26 37
0 a,b
1 b a
4 a,b
5 b a
2 b a
6 b a
3 a,b
7 a,b
We see that states q0 and q4 on the one hand, and states q1 and q5 on the other hand,
show different rows. We have δ(q, a) ∈ B0145 ⊆ Q\F for q = q0 , q4 , but δ(q, a) ∈ B37 ⊆ F
for q = q1 , q5 . Therefore, we split block B0145 into B04 = {q0 , q4 } and B15 = {q1 , q5 }.
The other blocks don’t need to be split; the rows are identical for each of these blocks.
Adding another column, now distinguishing B04 , B15 , B26 , and B37 , we obtain the
following transition table.
04 15 26 37
0 a b
4 a b
1 b a
5 b a
2 b a
6 b a
3 a,b
7 a,b
Now, there is within each block no distinguishing row: we have found the L-equivalence
classes of the set of states Q. These are the blocks we have now, viz. {q0 , q4 }, {q1 , q5 },
{q2 , q6 }, and {q3 , q7 }. Note, in the analysis above we have only considered single sym-
bols to establish the L-equivalence classes, rather than strings of arbitrary length.
Completing the quotient construction, (i) we take the equivalence class {q0 , q4 }
as initial state C0 , because its contains the original initial state q0 , (ii) we have the
equivalence class {q3 , q7 } as the only final states, since it is the only equivalence class
containing final states, thus QF = { {q3 , q7 } }, (iii) we have inherited transitions, e.g.
δL ({q0 , q4 }, a) = {q0 , q4 } since δ(q0 , a) = q4 ∈ {q0 , q4 } (as well as δ(q4 , a) = q0 ∈
{q0 , q4 }) and δL ({q0 , q4 }, b) = {q1 , q5 } since δ(q0 , b) = q5 ∈ {q1 , q5 }. The resulting
quotient DFA is the smallest DFA in number of states which accepts the regular lan-
guage (a + b)∗ ·baa·(a + b)∗ , and is depicted in Figure 2.23.
Pseudo-code for the general DFA minimization algorithm is given in Figure 2.24. We
assume that the given DFA D = (Q, Σ, δ, q0 , F ) has reachable states only. Otherwise,
a reachability algorithm should be run first.
68 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
a a,b
a
b a
04 15 26 37
b
Figure 2.23: Minimal DFA accepting (a + b)∗ ·baa·(a + b)∗
Theorem 2.51. Let D = (Q, Σ, δ, q0 , F ) be a DFA with reachable states only. Define
the relations ≡n ⊆ Q × Q, for n > 0, by
q ≡0 q ′ ⇐⇒ q, q ′ ∈ Q\F ∨ q, q ′ ∈ F
q ≡n+1 q ′ ⇐⇒ q ≡n q ′ ∧ ∀a ∈ Σ : δ(q, a) ≡n δ(q ′ , a)
a b
0 1 2 a,b
b a
b
a,b 3 a 4 5 6 a,b
b a a
b
a,b 7 a 8 9 10 a,b
For the proof of part (e) we reason as follows: Pick q, q ′ ∈ B for some block B of the final
partitioning Pn . For Pn it holds that Pn−1 , since only if all blocks remain unaltered, the
progress variable continue is not set to true. Thus, by part (c), ≡n = ≡n−1 . Hence,
by part (d), ≡n−1+k = ≡n−1 , for k > 0, and hence ≡n+k = ≡n , for k > 0. By part (b)
we obtain
q ≡n q ′ iff ∀w ∈ Σ∗ : δ(q, a) ∈ F ⇐⇒ δ(q ′ , a) ∈ F
Since, by assumption q, q ′ ∈ B for some block B of Pn , we have q ≡n q ′ , by part (c). It
follows that q ≈L q ′ , by definition of ≈L .
Example 2.52. As another example of our minimization technique, consider the DFA
depicted in Figure 2.25 having Q = {q0 , q1 , q2 , q3 , q4 , q5 , q6 , q7 , q8 , q9 , q10 } as its set of
states. Note, all states are reachable. Again we start with two blocks, the non-final states
B014589 = {q0 , q1 , q4 , q5 , q8 , q9 } and the final states B236710 = {q2 , q3 , q6 , q7 , q10 }. Next
we determine for each individual state, q0 to q10 , to which blocks their transitions for a
and b lead. This is recorded in the left part of the table below.
a a
0 159
b b
b 48 a 236710 a,b
We see that block B014589 splits in three subblocks, B0 , B159 and B48 , while the block B236710
remains as is. The next iteration does not lead to further refinement. The resulting quo-
tient DFA, with a minimal number of states, is given in Figure 2.26. The insight is that
a word with first symbol a will be accepted if a symbol b follows at some point, and
likewise, a word starting with b will be accepted if an a occurs after zero or more b’s.
Concluding the section we show that the quotient construction of dividing out by L-
equivalence does not work for minimization of NFA. Consider the NFA N of Figure 2.27
accepting the language L = (a+b)∗ ·a. We adapt Definition 2.43 for N = (Q, Σ, δ, q0 , F )
as follows: q ≈L q ′ iff ∀w ∈ Σ∗ : δ(q, w) ∩ F = ∅ ⇐⇒ δ(q ′ , w) ∩ F = ∅. Using this
definition we see that the states q0 and q1 are not L-equivalent: δ(q0 , a) = {q0 , q2 } thus
δ(q0 , a) ∩ F 6= ∅, but δ(q1 , a) = {q0 } thus δ(q1 , a) ∩ F = ∅. Also, both q0 and q1 are not
L-equivalent to q2 : δ(q0 , ε), δ(q1 , ε) ∩ F = ∅, but δ(q2 , ε) ∩ F 6= ∅. Thus, all three states
q0 , q1 and q2 are pairwise not L-equivalent. However, N is not minimal in the number
of states. The NFA obtained from N by deleting the state q1 consists of two states, i.e.
one less, and also accepts L.
a a a a a a a a
0 1 2 3 0 1 2 3
b b b b b b b b
a a a a a a a a
4 5 6 7 4 5 6 7
b b b b b b b b
8 a 9 a 10 a 11 8 a 9 a 10 a 11
a a,b a a,b
b b b b
Exercise 2.5.29. Give an example of a DFA accepting a language L with four states in
total, two reachable and two non-reachable, where each reachable state is L-equivalent
to a non-reachable state.
Answer to Exercise 2.5.29
a,b
0 1 a,b
a,b
2 3 a,b
Exercise 2.5.30. Consider the two DFA of Figure 2.28 with accepted languages Lℓ
and Lr . In the left automaton state q11 is accepting, in the right automaton it is not.
(a) For the automaton on the left, how many states are Lℓ -equivalent to state q0 , to
state q5 , to state q10 , and to state q11 , respectively.
(b) For the automaton on the right, how many states are Lr -equivalent to state q0 , to
state q3 , to state q9 , and to state q11 , respectively.
Answer to Exercise 2.5.30
(a) In the DFA on the left of Figure 2.28, state q0 is Lℓ -equivalent to itself only. The
same for state q5 . All final states are Lℓ -equivalent to state q10 and state q11 .
74 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
(b) In the DFA on the right of Figure 2.28 each state is only Lr -equivalent to itself.
Exercise 2.5.31. Let D be a DFA accepting the language L. Obtain the DFA D ◦
by deleting the non-reachable states from D. Clearly, D ◦ accepts L too. Consider the
quotients DL and DL◦ , respectively.
(a) Suppose q ∈ C for a state q of D and a state C of DL . If the string w ∈ Σ∗ is such
that δ(q0 , w) = q, then δL (C0 , w) = C.
(b) Prove that a non-reachable state of DL , if present, consists of non-reachable states
of D only.
(c) Prove that each state of DL◦ is reachable.
(d) Conclude that is doesn’t make an essential difference for the construction of a
minimal DFA if the non-reachable states are removed before or after the quotient
construction.
Answer to Exercise 2.5.31
(a) Induction on the length of w. Basis, |w| = 0: Then w = ε. Thus q = q0 , and
C = C0 since q0 = q ∈ C. Induction step, |w| > 0: Pick a ∈ Σ and v ∈ Σ∗ such
that w = va. Put q̄ = δ(q0 , v). Then q = δ(q̄, a). Suppose q̄ ∈ C. By induction
hypothesis we have δ(C0 , v) = C. Since q̄ ∈ C, q ∈ C, and q = δ(q̄, a) it follows
that C = δL (C, a). Therefore, δL (C0 , w) = δL (C0 , va) = δL (C, a) = C.
(b) By part (a), if q ∈ Q is reachable in D and q ∈ C, then C ∈ QL is reachable in DL .
Put differently, if C ∈ QL is not reachable in DL , then C contains no reachable
state of D.
(c) By construction of DL◦ each state C ∈ QL is non-empty and contains reachable
states of D ◦ only. Thus, by part (a), each state C ∈ QL is reachable in DL◦ .
(d) On the one hand, DL◦ is the smallest DFA accepting L. On the other hand, by
parts (b) and (c), DL◦ can be seen as obtained from DL by removing non-reachable
states.
Exercise 2.5.32. Construct a DFA with three states that is language equivalent to the
DFA given in Figure 2.29.
Answer to Exercise 2.5.32
a
1 2
a a
0 b a b
b
3 4 a,b
b
b
0 a 1 2
a,b
a a b
b a,b
3 4 5 a,b
Exercise 2.5.33. Construct a DFA with two states that is language equivalent to the
DFA given in Figure 2.30.
Answer to Exercise 2.5.33 First restrict to reachable states only, i.e. to q0 , q3 , q4 ,
and q5 , and discarding q1 and q2 . Next, compute L-equivalence classes.
03 45
0 a b
3 a b
4 a,b
5 a,b
No further split of the initial blocks {q0 , q3 } and {q4 , q5 }. This leads to the following
minimal DFA that is language equivalent to the DFA of Figure 2.30.
a a,b
b
03 45
Exercise 2.5.34. Construct a DFA with a minimal number of states that is language
equivalent to the DFA given in Figure 2.31.
76 CHAPTER 2. FINITE AUTOMATA AND REGULAR LANGUAGES
b a
a a
0 1 2
b a b
3 4 a 5
b
a,b a
034 125 04 12 3 5 04 1 2 3 5
0 b a 0 a b 0 a b
3 a,b 4 a b 4 a b
4 b a 1 a,b
1 a,b 2 a b
2 a,b
5 b a
b a
a a
04 1 2
b b
a
3 5
a,b a
Exercise 2.5.35. Construct a DFA with a minimal number of states that is language
equivalent to the DFA given in Figure 2.32.
Answer to Exercise 2.5.35
0234 1 03 24 1 03 2 4 1
0 a b 0 a b 0 a b
2 a,b 3 a b 3 a b
3 a b 2 a,b
4 a,b 4 a b
a,b
b
1 3
a a a,b a
a
0 2 4
b b