CSE 355 - Homework One Solution

advertisement
CSE 355 - Homework One Solution
Due: 18 September 2013
Please note that there is more than one way to answer most of
these questions. The following only represents a sample solution.
(1) Give a state diagram and the table specifying the transition function, for a DFA to recognize:
(a) {w ∈ {0, 1}∗ : w ∈ 0∗ 1∗ 0∗ 1∗ }
DFA:
0
1
1
q0
0
0
q1
1
1
q2
0,1
0
q3
q4
Table:
State
q0 - start, final
q1 - final
q2 - final
q3 - final
q4
Transition on 0
q0
q2
q2
q4
q4
Transition on 1
q1
q1
q3
q3
q4
(b) {w ∈ {0, 1}∗ : w contains at most two 0s or contains at least three 1s}
DFA:
0
q0
q1
0
q2
1
1
0
q4
1
q5
0
1
q9
q3
1
0
q6
1
q8
0
1
0
q7
1
0
q 10
1
1
0,1
1
0
1
0
q 11
1
q 12
0
0
Table:
State
q0 - start, final
q1 - final
q2 - final
q3
q4 - final
q5 - final
q6 - final
q7
q8 - final
q9 - final
q10 - final
q11
q12 - final
Transition on 0
q1
q2
q3
q3
q5
q6
q7
q7
q9
q10
q11
q11
q12
Transition on 1
q4
q5
q6
q7
q8
q9
q10
q11
q12
q12
q12
q12
q12
(c) {w ∈ {0, 1}∗ : w contains at least two 0s but does not contain the substring 000}
DFA:
q0
0
1
q1
0
1
1
q4
q2
0
0
q5
0
q3
0,1
q6
1
1
0
1
Table:
State
q0 - start
q1
q2 - final
q3
q4
q5 - final
q6 - final
Transition on 0
q1
q2
q3
q3
q5
q2
q5
2
Transition on 1
q0
q4
q6
q3
q4
q6
q6
(d) (not to be graded) {w ∈ {0, 1}∗ : w either contains an even number of 0s and an even
number of 1s or contains an odd number of 0s and an odd number of 1s}.
Note: This language is the same as {w ∈ {0, 1}∗ : w has an even length} since
if w contains an even number of 0s and an even number of 1s, then the length of
w is even. Similarly, if w contains an odd number of 0s and an odd number of 1s
then the length of w is also even (the sum of two odd numbers is even). Either
way, the length of w is even. Conversely, if the number of 0s is even while the
number of 1s is odd or the number of 1s is even while the number of 0s is odd,
then the length of w will be odd (the sum of an odd number and an even number
is odd). Thus, a string is in the language of this problem iff the length of the
string is even. Therefore, what follows is the DFA for the simpler language of
{w ∈ {0, 1}∗ : w has an even length}.
DFA:
0,1
q0
q1
0,1
Table:
State
q0 - start, final
q1
Transition on 0
q1
q0
Transition on 1
q1
q0
(e) (not to be graded) {w ∈ {0, 1}∗ : w has odd length and an odd number of 0s}.
DFA:
0
q0
1
q1
0
1
1
0
q2
1
q3
0
Table:
State
q0 - start
q1 - final
q2
q3
Transition on 0
q1
q0
q3
q2
3
Transition on 1
q2
q3
q0
q1
(2) Give a state diagram for an NFA with as few states as you can to recognize:
(a) {w ∈ {0, 1}∗ : w ∈ 0∗ 1∗ }
NFA:
0
1
1
q0
q1
(b) {w ∈ {0, 1}∗ : w ∈ (0∗ 1∗ )∗ }
Note: This language, let’s call it L, is the same as Σ∗ = {w ∈ {0, 1}∗ }, that
is the language containing every string over 0 and 1. Clearly, if w ∈ L, then
w ∈ Σ∗ . Conversely, if w ∈ Σ∗ , then w ∈ L since one can generate a single 0 or 1
repeatedly until w is described by the regular expression (0∗ 1∗ )∗ . Thus, the NFA
is the same as that for Σ∗ which is given.
NFA:
0,1
q0
(c) {w ∈ {0, 1}∗ : w 6= }
NFA:
0,1
0,1
q0
q1
(d) (not to be graded) {w ∈ {0, 1}∗ : the length of w is odd but not a multiple of three}
NFA:
q0
0,1
q1
0,1
0,1
q2
0,1
4
q3
0,1
q4
0,1
q5
(e) (not to be graded) {w ∈ {0, 1}∗ : the first and last characters of w are the same}
NFA:
0,1
0
q1
0
0,1
q0
1
q2
q3
1
0,1
(3) Show that the number of different regular languages over alphabet {0, 1} is infinite.
Answer: Consider the infinite family of languages Ln = {0n } for all n ≥ 0. Some of
the languages in this family are
L0 = {}
L1 = {0}
L2 = {00}
..
.
Since, for all n, Ln is finite (it only contains one string), they are all regular (in
fact, for each n, 0n is a regular expression that describes Ln ). Therefore, there are
infinitely many regular languages over {0, 1}.
5
(4) Let Qmax and σmax be positive integers. Show that the number of different languages that are
recognized by DFAs (Q, Σ, δ, q0 , F ) with |Q| ≤ Qmax and Σ ⊆ {0, 1, . . . , σmax − 1} is finite.
Answer: First we will show how many DFAs can be made with |Q| states and |Σ|
symbols in the alphabet. Since every DFA is described by its 5-tuple (Q, Σ, δ, q0 , F ),
we will see how many different combinations of δ, q0 , and F we can have with |Q|
states and |Σ| symbols in the alphabet.
• δ: Since δ is a total function from Q × Σ to Q, there are |Q|(|Q|∗|Σ|) different
possible functions δ can take. This follows from the facts that for any finite sets
A and B, there are |B||A| functions from A to B and |A × B| = |A| ∗ |B|.
• q0 : Since q0 ∈ Q, we have |Q| different choices for which state to start in.
• F : Since F ⊆ Q, and there are 2|Q| subsets of Q, we only have that many choices
for F .
Since, the choice of δ, q0 and F are all independent, to get the total number of DFAs
that can be formed with |Q| states and |Σ| symbols in the alphabet, we multiply the
results together. Thus
number of DFAs = |Q|(|Q|∗|Σ|) ∗ |Q| ∗ 2|Q| .
(1)
Now, since we are given that |Q| ≤ Qmax and Σ ⊆ {0, 1, . . . , σmax − 1}, meaning
|Σ| ≤ σmax , we get
max ∗σmax )
number of DFAs ≤ Q(Q
∗ Qmax ∗ 2Qmax .
max
(2)
Finally, there are only Qmax positive numbers less than or equal to Qmax and 2σmax
subsets of {0, 1, . . . , σmax − 1}. For each different |Q| and Σ, the number of DFAs
that can be constructed from them is less than the term in (2) above. Therefore,
adding all the possible DFAs that can be constructed is less than adding (2) above
for each of the Qmax ∗ 2σmax combination of |Q| and Σ. Whence, we get the
max ∗σmax )
∗ Qmax ∗ 2Qmax .
total number of DFAs ≤ Qmax ∗ 2σmax ∗ Q(Q
max
(3)
While the amount of DFAs grows rapidly, since all the terms are finite, the result is
still finite. Thus, there are finitely many DFAs with the desired properties. Now each
DFA recognizes exactly one language, so the number of regular languages recognized
by the DFAs is also finite (in fact, some DFAs will recognize the same language).
6
(5) For a string w, even(w) is the string that consists of every second letter of w. For example,
even(even) = vn and even(example) = xml. Show that if there is a DFA that recognizes the
language L, there is an NFA that recognizes the language {even(w) : w ∈ L}.
Answer: Let D = (Q, Σ, δ, q0 , F ) be the DFA that recognizes L. We have to construct an NFA, N = (QN , Σ, δN , q0 N , FN ) that recognizes even(L) = {even(w) : w ∈
L}. We will do so as follows:
• Let QN = Q × {even, odd}. In essence we are doubling the states, where each
pair (q, even) represents reaching state q after reading an even number of symbols
with D, while the pair (q, odd) represents reaching state q after reading an odd
number of symbols with D.
• Let q0 N = (q0 , even). That is, we are starting in the old start state and have yet
to read any symbols (0 is even).
• Construct δN as follows:
– δN ((q, even), ) = {(δ(q, a), odd) : a ∈ Σ}
This transition nondeterministically goes to all the states that could be
reached from q on reading the next symbol. This allows us to get to the
same states as if we read in the next odd positioned symbol in a string
with D, but without actually reading in a symbol with N , due to the
-transition.
– δN ((q, odd), a) = {(δ(q, a), even)}.
This transition reads in the next symbol in a string (corresponding to
an even positioned symbol in the original string) and transitions as the
original transition does, but now we are at a state that could be reached
with reading an even number of symbols with D.
– These two transitions will alternate trying all the position that could be
reached if an odd positioned symbol was read in the original DFA, followed
by the the states that would be reached while actually reading in the next
even positioned symbol in the original string, followed by all the places that
could be reached in the original on an odd positioned symbol, and so on until
all the symbols have been read.
• Let FN = F ×{even, odd}. That is if q was a final state in D, then both copies of q
will be a final state in N . When (q, even) is the final state in a computation, that
corresponds to the original string having even length, since the final transition
is on an even positioned symbol (as in the even(even) example in the problem
description). When (q, odd) is the final state in a computation, that corresponds
to the original string having odd length, since after reading in an even number of
symbols, one more symbol must be read by the original DFA before accepting,
which the -transition will provide in the NFA (as in the even(example) example
in the problem description).
Thus we have constructed an NFA N that uses -transitions to keep track of where
the original DFA D could transition to on an odd positioned symbol in a string, and
read in the even symbol only resulting in one branch of N ’s computation being in
the same state as D’s computation after reading both the odd and even positioned
symbols. Thus N will accept strings of the even positioned symbols in a string in L,
for both even and odd length strings. That is, N will recognize even(L). This shows
that if L is regular then even(L) is also regular.
7
Download