0% found this document useful (0 votes)
60 views

Solutions To Problem Set 2

This document contains solutions to problems from Problem Set 2 of CS172: Automata, Computability and Complexity at UC Berkeley. It includes solutions to constructing DFAs and NFAs to recognize certain languages, proofs that languages require a minimum number of states in any DFA, determining whether languages are regular using the pumping lemma, writing regular expressions for specified languages, and showing that the operation of removing extending prefixes from a language results in a regular language if the original language is regular.

Uploaded by

Ajith Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Solutions To Problem Set 2

This document contains solutions to problems from Problem Set 2 of CS172: Automata, Computability and Complexity at UC Berkeley. It includes solutions to constructing DFAs and NFAs to recognize certain languages, proofs that languages require a minimum number of states in any DFA, determining whether languages are regular using the pumping lemma, writing regular expressions for specified languages, and showing that the operation of removing extending prefixes from a language results in a regular language if the original language is regular.

Uploaded by

Ajith Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

U.C.

Berkeley CS172: Automata, Computability and Complexity Solutions to Problem Set 2


Professor Luca Trevisan 2/8/2007
Solutions to Problem Set 2
1. Let k be a positive integer. Let = {0, 1}, and L be the language consisting of all strings
over {0, 1} containing a 1 in the kth position from the end (in particular, all strings of length
less than k are not in L). [8 + 8 + 14 = 30 points]
(a) Construct a DFA with exactly 2
k
states that recognizes L.
Solution: Construct a DFA with one state corresponding to every k-bit string. For-
mally, let Q = {0, 1}
k
. We keep track of the last k bits read by the machine. Thus, for a
state x
1
. . . x
k
, we dene the transition on reading the bit b as (x
1
. . . x
k
, b) = x
2
. . . x
k
b.
Take q
0
= 0
k
and F = {x
1
. . . x
k
Q | x
1
= 1}. Note that this does not accept any
strings of length less than k (as we start with the all zero state) since the kth position
from the end does not exist, and is hence not 1.
(b) Construct a NFA with exactly k + 1 states that recognizes L.
Solution: We construct an NFA with Q = {0, 1, . . . , k}, with the names of the states
corresponding to how many of the last k bits the NFA has seen. Dene (0, 0) = 0,
(0, 1) = {0, 1} and (i 1, 0/1) = i for 2 i k. We set q
0
= 0 and F = {k}. The
machine starts in state 0, on seeing a 1 it may guess that it is the kth bit from the
end and proceed to state 1. It then reaches state k and accepts if and only if there are
exactly k 1 bits following the one on which it moved from 0 to 1.
(c) Prove that any DFA that recognizes L has at least 2
k
states.
Solution: Consider any two dierent k-bit strings x = x
1
. . . x
k
and y = y
1
. . . y
k
and
let i be some position such that x
i
= y
i
(there must be at least one). Hence, one of the
strings contains a 1 in the ith position, while the other contains a 0. Let z = 0
i1
. Then
z distinguishes x and y as exactly one of xz and yz has the kth bit from the end as 1.
Since there are 2
k
binary strings of length k, which are all mutually distinguishable by
the above argument, any DFA for the language must have at least 2
k
states.
2. [10 + 10 + 10 = 30 points]
(a) Let A be the set of strings over {0, 1} that can be written in the form 1
k
y where y
contains at least k 1s, for some k 1. Show that A is a regular language.
Solution: It is easy to see that any string in A must start with a 1, and contain at
least one other 1 (in the matching y segment). Conversely, any string that starts with
a 1 and contains at least one other 1 matches the description for k = 1. Hence, A is
described by the regular expression 1 0

1 (0 1)

, and is therefore regular.


(b) Let B be the set of strings over {0, 1} that can be written in the form 1
k
0y where y
contains at least k 1s, for some k 1. Show that B is not a regular language.
Solution: Assume to the contrary that B is regular. Let p be the pumping length
given by the pumping lemma. Consider the string s = 1
p
0
p
1
p
B. The pumping lemma
guarantees that s can be split into 3 pieces s = abc, where |ab| p. Hence, y = 1
i
for
some i 1. Then, by the pumping lemma, ab
2
c = 1
p+i
0
p
1
p
B, but cannot be written
in the form specied, a contradiction.
1
(c) Let C be the set of strings over {0, 1} that can be written in the form 1
k
z where z
contains at most k 1s, for some k 1. Show that C is not a regular language.
Solution: Assume to the contrary that C is regular. Let p be the pumping length
given by the pumping lemma. Consider the string s = 1
p
0
p
1
p
B. The pumping lemma
guarantees that s can be split into 3 pieces s = abz, where |ab| p. Hence, b = 1
i
for
some i 1. Then, by the pumping lemma, ac = 1
pi
0
p
1
p
C, but cannot be written in
the form specied, a contradiction.
3. Write regular expressions for the following languages: [12 + 8 = 20 points]
(a) The set of all binary strings such that every pair of adjacent 0s appears before any pair
of adjacent 1s.
Solution: Using R(L), to denote the regular expression for the given language L, we
must have R(L) = R(L
1
)R(L
2
), where L
1
is the language of all strings that do not
contain any pair of 1s and L
2
is the language of all strings that do not contain any pair
of 0s. For a string in L
1
, every occurrence of a 1, except possibly the last one, must be
followed by a 0. Hence, R(L
1
) = (0 + 10)

(1 + ). Similarly, R(L
2
) = (1 + 01)

(0 + ).
Thus, R(L) = (0+10)

(1+)(1+01)

(0+), which simplies to (0+10)

(1+01)

(0+).
(b) The set of all binary strings such that the number of 0s in the string is divisible by 5.
Solution: Any string in the language must be composed of 0 or more blocks, each hav-
ing exactly ve 0s and an arbitrary number of 1s between them. This is given by the reg-
ular expression (1

01

01

01

01

01

). However, this does not capture the strings con-


taining all 1s, which can be included separately, giving the expression (1

01

01

01

01

01

)+
1

for the language.


4. We say a string x is a proper prex of a string y, if there exists a non-empty string z such
that xz = y. For a language A, we dene the following operation
NOEXTEND(A) = {w A | w is not a proper prex of any string in A}
Show that if A is regular, then so is NOEXTEND(A).[20 points]
Solution: Given a DFA for the language A, we want to accept only those strings which reach
a nal state, but to which no string can be added to reach a nal state again. Hence, we
want to accept strings ending in exactly those nal states, from which there is no (directed)
path to any nal state (not even itself).
For a given state q F, we can check if there is a path from q to any state in F (or a cycle
involving q) by a DFS. Let F

F be the set of all the states from which there is no such path.
Then changing the set of nal states of the DFA to F

gives a DFA for NOEXTEND(A).


2

You might also like