Sei sulla pagina 1di 3

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 is
fed 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.

0 1

1 0
q0 q1 q2
1 0

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 NFA


M 0 with a single accept state, we create a new state and designate it as the accept state of M 0 ,
then add an ε arrow from each final state of M to the accept state of M 0 . Formally, we define
M 0 = (Q ∪ {y} , Σ, δ 0 , q0 , {y}), where y is a state different from those in Q. The transition function
δ 0 is defined as follows:


 δ(q, a) if q ∈ Q and a 6= ε

δ(q, a) if q ∈ Q − F and a = ε
δ 0 (q, a) =

 δ(q, a) ∪ {y} if q ∈ F and a = ε

∅ if q = y

1
3. Sipser, exercise 1.14.

Solution to Problem 3

(a) Consider a DFA M = (Q, Σ, δ, q0 , F ) that recognizes language B. Swapping the accept and
nonaccept states in M yields a new DFA M = (Q, Σ, δ, q0 , Q − F ). It is clear that if we feed an
input string w to both M and M , then both machines end at some state q ∈ Q. By definition of
M , w ∈ B ⇐⇒ q ∈ F . But then w 6∈ B ⇐⇒ q ∈ Q − F . This shows the machine M recognizes
the complement of B.
Now let A be any regular language, then it is recognized by some DFA M . From above, there exists
a 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,b

a a

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

4. Sipser 1.38.

Solution to Problem 4
To 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 regular
language.
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, there
is exactly one possible state q ∈ Q that M could be in after reading x. Thus, if M is viewed as
an 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 by
N . We’ll show A is regular by constructing a DFA M = (Q0 , Σ, δ 0 , q00 , F 0 ) that recognizes A. The
construction of M here is similar to that used in the proof of the equivalence of ordinary NFAs and
DFAs. The only difference is at the definition the final states F 0 . Precisely,

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

2. For R ∈ Q0 and a ∈ Σ, [
δ 0 (R, a) = E(δ(r, a))
r∈R

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

3. q00 = E({q0 }).

4. F 0 = {R ∈ Q0 | R ⊂ F }.

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

5. Sipser 1.60.

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

a, b

q0 a q1 a, b q2 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 }), where
Q = {q0 , . . . , qk } and 
 {q0 , q1 } if i = 0
δ(qi , a) = {qi+1 } if 0 < i < k

∅ if i = k

 {q0 } if i = 0
δ(qi , b) = {qi+1 } if 0 < i < k

∅ if i = k
δ(qi , ε) = ∅ for all i

Potrebbero piacerti anche