You are on page 1of 4

Homework 2 CS 321 1. Give a DFA for the following problems. Assume the alphabet is {a, b}.

(a) All strings over with at least one a and exactly two bs.
a a a q1 b q5 b q7 q2 b a q3 b q4 a b b qd a,b a

(b) L = {w : na (w) + 2nb (w) mod 3 < 2}. (qx represents the state where (na (w) + 2nb (w))mod3 = x)
a b q0 q1 b a b q2

(c) All strings of length 4 or greater in which the leftmost 3 symbols are the same, but dierent from the rightmost symbol.

2. A run in a string is a substring of length at least two, as long as possible and consisting of entirely of the same symbol. For instance, the string abbbaab has a run of bs of length 3 and a run of as of length 2. Find the DFAs for the following languages on a, b (a) L = {w : every run of a s has length either 2 or 3 }

The above DFA accepts all strings where every run of as has length either 2 or 3. For example, a, bb, bab, aaabaaabbaa, aabbabaa are all accepted strings. But aaabaaaabb, aababaaaa are not. In the above DFA q 1(no a), q 2(a singleton a), q 3(a run of as of length 2) and q 4(a run of as of length 3) are accepting states and we go back to q 1 again only after getting one b following no a, a singleton a, a run of as of length 2 or 3. q 5 is the trap state where we reach only after getting a run of a s of length more than 3 in the input string. 1

(b) L = {w : there are exactly two runs of as of length 3 }.


 

<

>

<

"

&

'

The above DFA accepts aaabbaaa, aababaaabaaabaaaa but not aaababaaabaaaba. Well, in the DFA we have to keep track of how many runs of as of length 3 we have already seen. The states in the above DFA indicate what have been scanned so far in the input sting. The rst ve states q 1, q 2, q 3, q 4 and q 5 keep track of the rst run of as of length 3. Any time of scanning for a run of a s of length 3, a mismatch yields a transition back to state q 1 and starts matching again. At state q 4, if the next input symbol is b, the transition function takes to state q 6 which starts nding the match for the second run of as of length in the same way. This matching comprises of transitions from and to q 6, q 7, q 8, q9 and q 10. q 9 is the rst accepting state which can be reached after scanning two runs of as of length 3. Once two runs of as of length 3 are found, after preceded by any numbers of bs any number or run of as other than of length 3 are accepted. States q11 , q12 , q13 , q 14, q15 and qt keep track of the number of as. Reaching to qt indicates that one b followed by the third run of as of length 3 is found. We trap that string by allowing no transition out of that state. Other than that, from states q11 , q12 , q13 , q 14, q15 have transitions to q11 if the next symbol is b while scanning the third run of as of length 3 in the input string. 3. Design an NFA with no more than 5 states for the set {ababn : n 0} {aban : n 0}.
a

q3 a q0 a q1 b q2 a b

q4

4. Show that if L is regular, so is LR . Let LR = {wR : w L} and we have to prove that LR is regular. Let, L is regular so we can construct a DFA M = {Q, , , q0 , F } for. To show that LR is also regular we construct an NFA M = {Q , , , q0 , F } where, 2

Q = Q q0 , where q0 / Q , q0 is the newly added state and the initial state of M. (q, a) = p, where p, q Q, a , and (p, a) = q = f , f F , q = q0 and a = = , otherwise F = q0 In other words, the NFA would reverse the arcs in the DFA with the same labels, add a new initial state with lambda transitions to all nal states of the old DFA, and make the initial state of the old DFA the only nal state for the NFA. This completes the description of the construction. Now we have to prove that M accepts LR . This means that we have to show that x is accepted by M i xR is accepted by M . (Only if): Consider M accepts any string x L. First assume, xR = xn , . . . , x1 LR . We want to show that M accepts xR . We know that x = x1 , . . . , xn L, so M accepts x. Let us consider the path followed in M on this input. It has the form q0 x1 q1 x2 q2 . . . xn qn . where, q0 , . . . , qn are some states in Q followed on this walk. Now consider the walk q0 qn xn qn1 , . . . , q1 x1 q0 This is a valid path on input xR in the NFA M, and it ends in the accepting state of M. So M accepts xR . (If) Now assume xR = xn , . . . , x1 is accepted by M . We have to show that x = x1 , . . . , xn is accepted by M . Since xR is accepted by M , there must be a walk of the form q0 qn xn qn1 , . . . , q 1 x1 q 0 in M . Since all arcs in M except those from q0 are constructed by reversing the arc in M , there must be a walk in M of the form: q0 x1 q1 x2 q2 . . . xn qn . This implies that x is accepted by M , completing the proof. 5. We construct the DFA according to the assumption of q1 being the only nal state:
1 1 0 q0,q1 0 1 q0 q0,q1,q2 0 1 q1,q2

6. We can do this by showing how to construct an NFA for any nite language. First we show how to construct an NFA for a single string. Let the string be a1a2..an. Then we can build an NFA for it as follows.
q1 a1 q2 a2 q3 qn-1 an qn

qn is the nal state and q0 is the initial state. Suppose the nite set contains m strings, w1, ... wm. For each string wi, we construct an NFA Ni. To accept all the strings w1...wm, we build a nite union of all the NFAs. We introduce a new initial state q and a new nal state qf. From the initial state q, we have lambda transitions to the initial states of all m NFAs. From the nal states of all the NFAs we have lambda transitions to the nal state qf. This is represented schematically by the following diagram.

lambda q0 lambda lambda

q1

N1

q1f

lambda lambda lambda qf

q2

N2

q2f

qm

Nm

qmf

It is clear that this union machine accepts a string i one of the NFAs accepts it, which happens if and only if it is in the nite set of strings from which it is built. 7. Suppose that N is an NFA that has some transitions and has possibly many nal states. Build another NFA N1 by adding a new nal state f to N with transitions from each of the nal states in N to f . Now make all the nal states in N non-nal. It is clear that N1 accepts if and only if N accepts. Besides N1 has a single nal state. Now construct another NFA N2 over the same states in N1 as follows. They have the same initial and nal states. For every state p, if there is a walk labeled a = and some transitions to state q in N1 , then put an arc from p to q labeled a in N2 . We will show by induction on w that there is a walk labeled w = from the initial state to any state in N1 if and only if it is also true in N2 . Basis: w = a. If there is a walk labeled w from the initail state to any state in N1 , there is an edge labeled a from the initial state to that state in N2 by construction. Inductive step: Suppose the claim is true for w. So there is a walk from the initial state to any state q in N1 i it is also true in N2 . Let there be a walk labeled wa from the initial state q0 to some state r in N1 . Hence there must be a walk from q0 to some state q in N1 , and hence in N2 by inductive hypothesis. There must be an edge labeled a from q to r by construction. Hence there must be a walk labeled wa in N2 . Similarly if there is a walk labeled wa in N2 from q0 to r where q occurs just before r in that walk, there must be a walk labeled w from q0 to q (by inductive hypothesis) and a walk labeled a from q to r (by construction). Hence there is a walk labeled wa from q0 to r, proving the induction. Since there is no in the original language, L(N ) = L(N1 ) = L(N2 ).

You might also like