237 set 2 solution

3
CSE237 - Introduction to the Theory of Computation Feb 8, 2007 Solution to Problem Set 2 1. Let Σ = {0, 1} and define D n = {w | w codes a binary number divisible by n} It is easy to show that D 2 is regular (since this only depends on the last bit of w). Show that D 3 is regular. (Hint: Design a machine for D 3 that has states q 0 ,q 1 and q 3 so that if the machine is fed a string s that codes a number congruent to i mod 3 then it ends in the state q i .) Solution to Problem 1. See Figure 1. q 0 q 1 q 2 1 1 0 0 0 1 Figure 1: The DFA for D 3 2. Sipser, Exercise 1.11. Solution to Problem 2. Let M =(Q, Σ, δ, q 0 ,F ) be any NFA. To convert M into an NFA M with a single accept state, we create a new state and designate it as the accept state of M , then add an ε arrow from each final state of M to the accept state of M . Formally, we define M =(Q ∪{y} , Σ ,q 0 , {y}), where y is a state different from those in Q. The transition function δ is defined as follows: δ (q,a)= δ(q,a) if q Q and a = ε δ(q,a) if q Q - F and a = ε δ(q,a) ∪{y} if q F and a = ε if q = y 1

Upload: qw11111

Post on 22-Nov-2014

1.492 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: 237 Set 2 Solution

CSE237 - Introduction to the Theory of Computation Feb 8, 2007

Solution to Problem Set 2

1. Let Σ = {0, 1} and define

Dn = {w | w codes a binary number divisible by n}

It is easy to show that D2 is regular (since this only depends on the last bit of w). Show that D3

is regular. (Hint: Design a machine for D3 that has states q0, q1 and q3 so that if the machine isfed a string s that codes a number congruent to i mod 3 then it ends in the state qi.)

Solution to Problem 1. See Figure 1.

q0 q1 q2 1

1

0

0

0

1

Figure 1: The DFA for D3

2. Sipser, Exercise 1.11.

Solution to Problem 2. Let M = (Q,Σ, δ, q0, F ) be any NFA. To convert M into an NFAM ′ with a single accept state, we create a new state and designate it as the accept state of M ′,then add an ε arrow from each final state of M to the accept state of M ′. Formally, we defineM ′ = (Q∪ {y} , Σ, δ′, q0, {y}), where y is a state different from those in Q. The transition functionδ′ is defined as follows:

δ′(q, a) =

δ(q, a) if q ∈ Q and a 6= εδ(q, a) if q ∈ Q− F and a = εδ(q, a) ∪ {y} if q ∈ F and a = ε∅ if q = y

1

Page 2: 237 Set 2 Solution

3. Sipser, exercise 1.14.

Solution to Problem 3

(a) Consider a DFA M = (Q,Σ, δ, q0, F ) that recognizes language B. Swapping the accept andnonaccept states in M yields a new DFA M = (Q,Σ, δ, q0, Q − F ). It is clear that if we feed aninput string w to both M and M , then both machines end at some state q ∈ Q. By definition ofM , w ∈ B ⇐⇒ q ∈ F . But then w 6∈ B ⇐⇒ q ∈ Q− F . This shows the machine M recognizesthe complement of B.

Now let A be any regular language, then it is recognized by some DFA M . From above, there existsa DFA M that recognizes the complement of A, which implies the complement of A is also regular.So the class of regular languages is closed under complement.

(b) An example is shown in Figure 2.

a,b

a

a,b

a

Figure 2: The NFA on the left recognizes the language C = {w | w ends with a}, butthe NFA on the right does not recognize the complement of C.

4. Sipser 1.38.

Solution to Problem 4To prove that all-NFAs recognize the class of regular languages, we need to prove two things: (i.)every regular language is recognized by some all-NFA, and (ii.) every all-NFA recognizes a regularlanguage.

To prove (i.), observe that every DFA can be viewed as an all-NFA. Let L be any regular language,then there is a DFA M = (Q,Σ, δ, q0, F ) that recognizes L. Note that for each input string x, thereis exactly one possible state q ∈ Q that M could be in after reading x. Thus, if M is viewed asan all-NFA, then it accepts x if and only if q ∈ F , which happens if and only if x ∈ L. Therefore,when M is viewed as an all-NFA, it also recognizes the language L.

To prove (ii.), let N = (Q,Σ, δ, q0, F ) be an all-NFA and let A be the language recoginized byN . We’ll show A is regular by constructing a DFA M = (Q′, Σ, δ′, q′0, F

′) that recognizes A. Theconstruction of M here is similar to that used in the proof of the equivalence of ordinary NFAs andDFAs. The only difference is at the definition the final states F ′. Precisely,

2

Page 3: 237 Set 2 Solution

1. Q′ = P(Q), where P(Q) is the set of subset of Q.

2. For R ∈ Q′ and a ∈ Σ,δ′(R, a) =

r∈R

E(δ(r, a))

Recall that for any subset S ⊂ Q, E(S) is the set of all states q ∈ Q that can be reached fromS by traveling along ε arrows, including the members of S themselves.

3. q′0 = E({q0}).4. F ′ = {R ∈ Q′ | R ⊂ F}.

Now consider an input string x ∈ Σ∗. Let R be the set of states that the all-NFA N could be inafter reading x. Then when x is fed to the DFA M defined above, M will end at state R. By thedefinition of all-NFAs, we have x ∈ A ⇐⇒ R ⊂ F . By the definition of M , M accepts x if andonly if R ∈ F ′, which is equivalent to R ⊂ F . Hence, M recognizes A.

5. Sipser 1.60.

Solution to Problem 5The diagram for an NFA with k + 1 states that recognizes Ck = Σ∗aΣk−1 is given in Figure 3,where Σ = {a, b}.

q0 q1 a

a, b

q2 a, b a, b a, b qk

Figure 3: An NFA that recognizes Ck = {a, b}∗ a {a, b}k−1.

The formal description of the NFA M in Figure 3 is as follows: M = (Q,Σ, δ, q0, {qk}), whereQ = {q0, . . . , qk} and

δ(qi, a) =

{q0, q1} if i = 0{qi+1} if 0 < i < k∅ if i = k

δ(qi, b) =

{q0} if i = 0{qi+1} if 0 < i < k∅ if i = k

δ(qi, ε) = ∅ for all i

3