CS-172 Homework 2: Constructing Equivalent NFAs and Regular Expressions - Prof. S. A. Sesh, Assignments of Computer Science

Solutions to homework 2 of the cs-172 computability & complexity course, spring 2009. The solutions cover the conversion of dfas to all-paths nfas and vice versa, as well as constructing regular expressions for specific languages. The document also includes counterexamples to disprove certain statements.

Typology: Assignments

Pre 2010

Uploaded on 10/01/2009

koofers-user-qlu
koofers-user-qlu 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS–172 Computability & Complexity, Spring 2009
Feb. 19, 2009
Homework 2 Solutions
Note: These solutions are not necessarily model answers. Rather, they are designed to be tutorial in nature,
and sometimes contain a little more explanation than an ideal solution. Also, bear in mind that there may
be more than one correct solution.
1 First, we observe that any DFA is an all-paths NFA, so all-paths NFAs accept all regular languages.
For the other direction, we need to show that any language accepted by an all-paths NFA is regular.
Let N= (Q, Σ, δ, q0, F )be an arbitrary all-paths NFA. We will construct a standard NFA M=
(Q,Σ, δ, q
0, F )that recognizes the same language as N. The construction will be similar to the
conversion from NFAs to DFAs discussed in class (and in Theorem 1.19 of Sipser). The resulting NFA
Mwill be almost a DFA, in that there will be at most one possible path in each computation. However,
unlike in a DFA,every time we see a “dying” computation in N, the corresponding computation in M
will die as well. (Note that this is where our construction differs from the NFA-to-DFA construction!)
Q=P(Q)
q
0={q0}
δ(R, a) = (if for some rR,δ(r, a) = ;
{qQ|qE(δ(r, a)) for some rR}otherwise.
(Recall that E(R)is the set of states reachable from Rusing zero or more ǫtransitions.) Finally, we
want Mto be accepting if its final state contains only accepting states of N, so we define F=P(F).
An alternative argument is the following: First, augment the all-paths NFA Ninto an all-paths NFA
N1such that N1accepts the same language asN, but no computation of N1dies. (One way to do this
is to add a rejecting state qdie to N; then for all states qQ {qdie}, add a transition from qto qdie
labeled by all symbols in Σunused by other outgoing arrows at q.) Now, reverse the accepting and
rejecting states of N1; call the resulting machine N2. Think of N2as a standard NFA: The language
accepted by N2is then the complement of the language Laccepted by the all-paths NFA N1. It
follows that the complement of Lis regular. Since regular languages are closed under complement
(see Problem 3 of Homework 1), Lmust be regular as well.
3 (Coming)
4 For each of these, there are many possible valid regular expressions.
(b) aeiouΣǫing
(d) 0101010
pf2

Partial preview of the text

Download CS-172 Homework 2: Constructing Equivalent NFAs and Regular Expressions - Prof. S. A. Sesh and more Assignments Computer Science in PDF only on Docsity!

CS–172 Computability & Complexity, Spring 2009 Feb. 19, 2009

Homework 2 Solutions

Note: These solutions are not necessarily model answers. Rather, they are designed to be tutorial in nature, and sometimes contain a little more explanation than an ideal solution. Also, bear in mind that there may be more than one correct solution.

1 First, we observe that any DFA is an all-paths NFA, so all-paths NFAs accept all regular languages. For the other direction, we need to show that any language accepted by an all-paths NFA is regular. Let N = (Q, Σ, δ, q 0 , F ) be an arbitrary all-paths NFA. We will construct a standard NFA M = (Q′, Σ, δ′, q 0 ′, F ′) that recognizes the same language as N. The construction will be similar to the conversion from NFAs to DFAs discussed in class (and in Theorem 1.19 of Sipser). The resulting NFA M will be almost a DFA, in that there will be at most one possible path in each computation. However, unlike in a DFA, every time we see a “dying” computation in N , the corresponding computation in M will die as well. (Note that this is where our construction differs from the NFA-to-DFA construction!)

Q′^ = P(Q) q′ 0 = {q 0 }

δ′(R, a) =

∅ if for some r ∈ R, δ(r, a) = ∅; {q ∈ Q | q ∈ E(δ(r, a)) for some r ∈ R} otherwise.

(Recall that E(R) is the set of states reachable from R using zero or more ǫ transitions.) Finally, we want M to be accepting if its final state contains only accepting states of N , so we define F ′^ = P(F ). An alternative argument is the following: First, augment the all-paths NFA N into an all-paths NFA N 1 such that N 1 accepts the same language as N , but no computation of N 1 dies. (One way to do this is to add a rejecting state qdie to N ; then for all states q ∈ Q ∪ {qdie}, add a transition from q to qdie labeled by all symbols in Σ unused by other outgoing arrows at q.) Now, reverse the accepting and rejecting states of N 1 ; call the resulting machine N 2. Think of N 2 as a standard NFA: The language accepted by N 2 is then the complement of the language L accepted by the all-paths NFA N 1. It follows that the complement of L is regular. Since regular languages are closed under complement (see Problem 3 of Homework 1), L must be regular as well.

3 (Coming)

4 For each of these, there are many possible valid regular expressions.

(b)

a ∪ e ∪ i ∪ o ∪ u

Σ∗^ ∪ ǫ

ing

(d) 0 ∗

(f) ( 10 ∪ 01 )∗( 1 ∪ 0 ∪ ǫ) To see the above, first notice that every even length string that belongs in the language is such that every prefix of it has equally many zeros and ones, because if not, then there are either two more zeros than ones or two more ones than zeros. It is easy to check, by induction on the length of the string, that the set of all even length strings where every prefix has the same number of zeros and ones is given by ( 10 ∪ 01 )∗. To then get our language, we simply concatenate either a zero or a one or nothing to the end of every even length string with the above property.

5 (a) False. E.g., take R = 0 and S = 1. Then the string 010 belongs to (R ∪ S)∗^ but not to R∗^ ∪ S∗.

[ Note that, when the answer is ‘False’, the only convincing way to justify it is by giving a concrete counterexample (like the one above). More general, woolly arguments don’t work. The same applies to part (c) below. ] (b) True. The fact that L(R∗) ⊆ L

(R∗)∗

is immediate because the language on the right contains all words that consist of finite sequences of words from L(R∗), so in particular it contains all words in L(R∗). We also have to show that L

(R∗)∗

⊆ L (R∗). To see this, note that any word in L

(R∗)∗

can be written in the form w 1 w 2... wn for some n ≥ 0 , where each wi is a word in L(R∗). But each wi can in turn be written in the form xi 1 xi 2... ximi for some mi ≥ 0 , where each xij is a word in L(R). So any word in L

(R∗)∗

can be written as a sequence of words from L(R), and hence belongs to L(R∗). Thus L

(R∗)∗

⊆ L (R∗), as claimed. [ You could try for some kind of informal argument for this part, e.g., based on conversion to NFAs. The above scheme is really the only convincing way to go here: note that we’re just trying to show that two sets are equal, so we have to show each is contained in the other. ] (c) False. E.g., take R = 0 and S = 0 ∪ ǫ. Then L(R∗) = L(S∗) = { 0 }∗, but L(R) = { 0 } and L(S) = { 0 , ε}.