Solution2
Solution2
Solutions to Assignment #2
1. Draw the state diagram of a DFA that recognizes the following language:
L = {w | w contains an even number of 0’s or contains exactly two 1’s}
Then draw the state diagram of an NFA for the same language. The alphabet is {0, 1}. Try to use as few
states as you can. Discuss the relative ease in designing the NFA versus the DFA.
Solution. The following is a DFA that accepts L. In the diagram, state Ei (resp. Oi ), for i = 0, 1, 2, 3+,
means that the number of 0’s seen so far is even (resp. odd) and the number of 1’s seen so far is i.
1 1 1
start E0 E1 E2 E3+ 1
0 0 0 0 0 0 0 0
1 1 1
O0 O1 O2 O3+ 1
The following is an NFA that accepts L. The top row checks for an even number of 0’s while the
bottom row checks for exactly two 1’s.
1
0
1
0
start
1 1 1
0,1
0 0 0
1
It was much easier to design the NFA than the DFA: we can use the two -transitions to “guess”
whether the string might have an even number of 0’s or might have exactly two 1’s. Then we can think
about how to check each condition separately.
Proof.
a) Let M 0 be the DFA that is obtained by swapping the final (i.e., accept) and nonfinal (i.e., noaccept)
states in the DFA M . Suppose w is a string that is in B, and hence is accepted by M . When M 0 has w as
its input, it follows the same sequence of states, but the last state of the computation, which was a final
state in M , thus is a nonfinal state in M 0 . Therefore, M 0 rejects w. On the other hand, suppose that w0
is a string that is not in B, and hence is rejected by M . Then when M 0 has w0 as its input, it follows the
same sequence of states, but the last state of the computation, which was a nonfinal state in M , thus is a
final state in M 0 . Therefore, M 0 accepts w0 . This proves that the DFA M 0 accepts exactly those strings
that are not in B, i.e., M 0 accepts the complement of B.
This shows that if B is a regular language, then the complement B of B is also a regular language.
In conclusion, the class of regular languages is closed under complement.
b) Let Σ = {0, 1} and C = {w|w ends in 0}. Here is an NFA M for C:
0,1
0 0,1
start q0 q1 q2 0,1
Note that for any string w ending in 0, we have a path, which loops on state q0 until it reads the last 0.
Then the path moves from state q0 to the final state q1 . That is, the NFA M accepts w.
Swapping the accept and non-accept states in M gives M 0 :
0,1
0 0,1
start q0 q1 q2 0,1
The NFA M 0 actually accepts all 0-1 strings: for any string w, the path simply looping at state q0 is a
path accepting w. In particular, the NFA M 0 accepts all strings ending in 0, which are in C so are not in
C. This proves that M 0 does not accept the complement of C.
Is the class of languages recognized by NFAs closed under complement?
Yes: by the theorem proved in class, languages accepted by NFAs are regular languages (i.e., they are
accepted by DFAs), while part a) showed that regular languages are closed under complement.
2
3. [Textbook, page 86, Exercise 1.16.] Covert the following two nondeterministic finite automata to
equivalent deterministic finite automata.
- 1 a q
] - 1 2
i a
J
b a,b J
aJ a,b
J
^
J
3 b Figure (b)
2
Figure (a) ]
Solution. Instead of using the algorithm given in the textbook, we follow the algorithm given in class.
That is, we start with the start state s0 = -closure({1}) of the DFA MD , which is the -closure of the
start state 1 in the NFA MN , and construct only those states in the DFA MD that are reachable from s0 .
The constructions are given in the following tables, where each row corresponds to a state in the
DFA MD . The first column gives the state name in the DFA MD , which is a subset of states in the
NFA MN . The second and third columns give the subsets reacheable from the subset in the first column
following the arc labeled with the symbols a and b, respectively. Note that the states in the DFA MD are
the -closures of the subsets obtained in columns 2 and 3.
(a) The DFA MD for figure (a) is (states s0 and s1 are final states because they contain the final state
1 of the given NFA MN ):
State in MD a b
s0 = -closure({1}) = {1} {1, 2} {2}
s1 = -closure({1, 2}) = {1, 2} {1, 2} {1, 2}
s2 = -closure({2}) = {2} {1}
(b) The DFA MD for figure (b) is (states s0 , s1 and s2 are final states because they all contain the
final state 2 of the given NFA MN ):
State in MD a b
s0 = -closure({1}) = {1, 2} {1, 3}
s1 = -closure({1, 3}) = {1, 2, 3} {1, 2, 3} {2, 3}
s2 = -closure({2, 3}) = {2, 3} {1, 2} {2, 3}
The following figures are the DFAs given in the above tables:
a,b
a -
a
- s0 a - s1 - s0 s1
]
]
I I
@
b @ b
b a@
@
R
s2 s2 b
DFA for (a) DFA for (b) ]
3
4. Give regular expressions that generate the following languages, assuming the alphabet {0, 1}:
a) L1 = {w | w starts with 0 and has odd length, or starts with 1 and has even length};
b) L2 = {w | w does not contain the substring 110};
c) L3 = {w | w contains an even number of 0’s, or contains exactly two 1’s}.
Solution. a) Strings of even length can be given by the regular expression ((0 | 1)(0 | 1))∗ . Thus, the
regular expression for language L1 is:
b) Let x be such a string in L2 . If for a symbol 1 in x there are still symbol 0’s on the right of this
symbol 1, then this symbol 1 must be followed immediately by one or more symbol 0’s (otherwise we
will always be able to fine a string 110 in x). That is, this symbol 1 must have the pattern 100∗ . On the
other hand, if there is no symbol 0 on the right of the symbol 1, then all symbols from this symbol 1
to the end of the string x are 1’s, whose pattern should be 1∗ . Moverover, the string x can start with a
subtring with arbitrarily many 0’s, i.e., a pattern 0∗ . Thus, the regular expression for the language L2 is
0∗ (100∗ )∗ 1∗
c) The strings that contain an even number of 0’s can be given by the regular expression 1∗ (01∗ 01∗ )∗ 1∗ ,
while the strings that contain exactly two 1’s can be given by the regular expression 0∗ 10∗ 10∗ . Thus, the
regular expression for the language L3 is
5. [Textbook, page 86, Exercise 1.21(b)] Use the procedure described in Lemma 1.60 to convert the
following automaton to an regular expression.
a
a,b -
- 1 2
]
]
J
J b
aJ
b
J
J
3
Solution. We follow the procedure given in Lemma 1.60, which was also discussed in detail in class.
4
Step 1. Introduce a new start state s and a new final state a:
a
- a|b -
- s 1 2
]
]J
J b 6
aJ
b
J
J ?
a 3
Therefore, the regular expression for the given automaton is: | ((a|b)a∗ b) ((b|a(a|b))a∗ b)∗ (a|).
6. [CSCE-433 Students only] Let Ln be the set of all binary strings of length at least n that are the
base-2 representations of integers that are a multiple of 2n . Show that for any fixed n ≥ 2, Ln is a regular
language.
Proof. It is imperative to note here that n is a fixed integer. Thus, we are considering a template such
as L2 , L3 , or L100 , etc. Therefore, when we consider the language Ln , we assume that n is fixed for all
strings in the language.
A binary number is a multiple of 2n if its binary representation ends with n 0’s. It is quite easy to
construct a NFA M that accepts such strings, i.e., M accepts the language Ln . To be more specific, we
assume that a positive binary number must start with a 1. Note that 0 is not in the language Ln because by
definition, all strings in Ln have length at least n. The NFA M is given by the following state diagram:
5
0,1
+
- s0 1- 0- 0- 0 - .... 0- 0-
s1 s2 s3 sn sn+1
Alternatively, it is easy to see that the regular expression 1(0|1)∗ 0n describes the language Ln , where
0n is a shorthand for the contatenation of n 0’s.
Thus, both the construction of the NFA M above and the regular expression given above prove that
the language Ln is a regular language.
7. [CSCE-627 Students only. Textbook, page 87, Exercise 1.22.] In certain programming languages,
comments appear between delimiters such as /# and #/. Let C be the language of all valid delimited
comment strings. A member of C must begin with /# and end with #/ but have no intervening #/. For
simplicity, assume that the alphabet for C is Σ = {a, b, /, #}.
a) Give a DFA that recognizes C.
b) Give a regular expression that generates C.
a, b, / #
+ +
- s0 /- #- #- /-
s1 s2 s3 s4
Y
a, b
The two states s2 and s3 need some explanations. The state s2 marks the situation that after seeing
the head “/#” of the comment, the last symbol seen is not “#”. Thus, we are safely in the middle of the
comment. The state s3 marks the situation that the last seen symbol is “#”. Thus, (1) if the next symbol
is “/”, then we should have reached the end of the comment; (2) if the next symbol is “#”, then we stay
in state s3 ; and (3) if the next symbol is “a” or “b”, then we move back to the “safe” state s2 . Therefore,
once we see the first appearance of the pattern “#/”, we should move to the final state s4 . Note that by
our convention, we assume that there is an implicit “trap state” such that if an arc labeled with a symbol
is not shown from a state, then we assume that arc goes to the trap state so the input will not be accepted.
In particular, if “#/” is not at the end of the string, then at the state s4 , the symbol after “#/” will lead
to the trap state so the input will not be accepted by the automaton.
b) We can use the procedure given in the textbook (and in class) to construct a regular expression
that describes the language accepted by the automaton constructed in a), i.e., the language C. The regular
expression is:
(/#)(a|b|/)∗ #(#|((a|b)(a|b|/)∗ #))∗ /
The construction detail is quite simple (much simpler than that for Question 5), thus is omitted.