Homework 1

DCP3122 Introduction to Formal Languages, Spring 2015
24-Mar-2015
Homework 1 - Solution
Instructor: Prof. Wen-Guey Tzeng
1. For Σ = {a, b}, construct DFA that accepts all strings with at least one a and exactly two
b’s.
Ans. The following graph represents the DFA M = ({q0 , q1 , . . . , q6 }, {a, b}, δ, q0 , {q5 }) that
accepts all strings with at least one a and exactly two b’s, where δ is described as in the
graph.
2. Given DFA for the language L = {abn am : n ≥ 2, m ≥ 3}.
Ans. The following graph represents the DFA M = ({q0 , q1 , . . . , q7 }, {a, b}, δ, q0 , {q6 }) that
accepts L = {abn am : n ≥ 2, m ≥ 3}, where δ is described as in the graph.
3. For Σ = {a, b}, find DFA for the language L = {w : (na (w) + 2nb (w)) mod 3 < 2}.
Ans. The following graph represents the DFA M = ({q0 , q1 , q2 }, {a, b}, δ, q0 , {q0 , q1 }) that
accepts L = {w : (na (w) + 2nb (w)) mod 3 < 2}, where δ is described as in the graph.
4. Construct an accepting DFA for the set of strings on {0, 1} defined by the requirement: The
leftmost symbol differs from the right most one.
Ans. The following graph represents the DFA M = ({q0 , q1 , q2 , q3 }, {0, 1}, δ, q0 , {q2 , q4 })
1-1
that accepts all strings that the leftmost symbol differs from the right most one, where δ is
described as in the graph.
5. Show that the language L = {vwv : v, w ∈ {a, b}∗ , |v| = 2} is regular.
Ans. The following shows an NFA M = ({q0 , q1 , q2 , . . . , q14 }, {a, b}, δ, q0 , {q8 , q10 , q12 , q14 })
that accepts L = {vwv : v, w ∈ {a, b}∗ , |v| = 2}, where δ is described as in the graph.
By theorem, we know that if L is accepted by an NFA, there exists a DFA that accepts L.
Therefore, L is regular.
6. Show that the set of all real numbers in C is a regular language.
Ans. The following shows an NFA M = ({q0 , q1 , . . . , q4 }, {+, −, ., 0, 1, . . . , 9}, δ, q0 , {q2 , q4 })
that accepts the set of all real numbers in C, where δ is described as in the graph.
By theorem, we know that if L is accepted by an NFA, there exists a DFA that accepts L.
Therefore, L is regular.
1-2
7. For the NFA in the following figure, find δ ∗ (q0 , 100), δ ∗ (q1 , 01), δ ∗ (q0 , 1010), and δ ∗ (q1 , 00).
Ans. δ ∗ (q0 , 100) = ∅, δ ∗ (q1 , 01) = ∅, δ ∗ (q0 , 1010) = {q0 , q2 }, and δ ∗ (q1 , 00) = ∅. λ
8. Design an NFA with no more than five states for the set {ababn : n ≥ 0} ∪ {aban : n ≥ 0}.
Ans. The following is an NFA M = ({q0 , q1 , . . . , q4 }, {a, b}, δ, q0 , {q3 , q4 }) that accepts the
set {ababn : n ≥ 0} ∪ {aban : n ≥ 0}, where δ is described as in the graph.
9. Which of the strings 00, 01001, 10010, 000, 0000 are accepted by the following NFA?
Ans. 01001 and 000 are accepted.
10. What is the complement of the language accepted by the following NFA?
Ans. This NFA accepts L = {an : n ≥ 1}. The complement of L is
L = {an : n < 1} = {a0 } = {λ}.
1-3
11. Convert the following NFA into an equivalent DFA.
Ans. The following is the result DFA M = ({q01 , q012 , q12 }, {0, 1}, δ, q01 , {q01 , q012 , q12 }),
where δ is described as in the graph.
12. Show that if L is regular, so is LR .
Ans. Informally, the following procedure shows that we can always construct a finite accepter
MLR that accepts LR for a given finite accepter ML that accepts L.
• Reverse all transitions in ML .
• Add a new initial stare qs and generate λ-transitions from qs to each of the final states
in ML .
• Turn all final states of ML into normal states of MLR and turn the initial states of ML
into a final state of MLR .
Formally, let ML = (QL , Σ, δL , q0 , FL ) be an NFA that accepts L. The NFA MLR defined
below accepts LR .
(a) MLR = (QL ∪ {qs }, Σ, δLR , qs , {q0 }) and qs ∈
/ QL .
(b) δL (qi , a) = qj ⇐⇒ δLR (qj , a) = qi for all a ∈ Σ and qi , qj ∈ QL .
(c) δLR (qs , λ) = q for all q ∈ FL .
From (b), if w ∈ L(ML ), δL∗ (q0 , w) = qj ∈ FL ⇐⇒ δL∗ (qj , w) = q0 . From (c), δLR (qs , λ) = qj
for all qj ∈ FL . Thus, we have that for all w ∈ L(ML ), wR ∈ L(MLR ).
13. Find minimal DFA for the language L = {an : n 6= 2 and n 6= 4}. You have to prove that the
result is minimal.
Ans. The following is the result DFA M = ({q0 , q1 , . . . , q5 }, {a}, δ, q0 , {q0 , q1 , q3 , q5 }), where
δ is described as in the graph.
By using the mark procedure, we can finally partition the state set as {q0 }, {q1 }, {q2 }, {q3 },
{q4 }, and {q5 }. Thus, such a DFA is minimal.
1-4