Solution to Midterm 1

6
: Theory of Computation solution vinay pathak 1. Finite State Machines. (15 points) Consider the following NFA over the alphabet {0,1}: a. Convert this NFA to a minimal DFA. b. Write a regular expression for the set the machine accepts. [0+(0+1)(1+00)*01]*(0+1)(1+00)* c. Write a linear grammar where each right side is of the form aB or a. A -> 0A | 0B | 1B B -> 1B | 0C | e C -> 0B | 1A 2. More Machines. (5 points) Draw a finite state machine that accepts the complement of the language accepted by the non-deterministic machine below:

Transcript of Solution to Midterm 1

Page 1: Solution to Midterm 1

: Theory of Computationsolution vinay pathak

1. Finite State Machines. (15 points)

Consider the following NFA over the alphabet {0,1}:

a. Convert this NFA to a minimal DFA.

b. Write a regular expression for the set the machine accepts.

[0+(0+1)(1+00)*01]*(0+1)(1+00)*

c. Write a linear grammar where each right side is of the form aB or a.

A -> 0A | 0B | 1BB -> 1B | 0C | e

C -> 0B | 1A

2. More Machines. (5 points)

Draw a finite state machine that accepts the complement of the language accepted by the non-deterministic machine below:

answer:

Page 2: Solution to Midterm 1

3. Regular or Not, Here I Come. (15 points)

Determine and prove for each set below whether it is Regular or not. Be careful.

a. The set of all strings in which every third symbol is the same as the first symbol in the string.

REGULAR. This language can be accepted by the following NFA:

b. The set 1m0n1m+n, for m and n greater than or equal to one.

NOT REGULAR by the pumping lemma. Let p be the pumping length and consider the string s=1p0p12p. Now we try to break it up into s=xyz. Since |xy|<=p and |y|>0, y can only contain 1s. When we pump the string once we get xy2z = 1p+|y|0p12p which is not in the language. This contradicts the pumping lemma, so the language is not regular.

c. The set of strings where each string has an equal number of 0?s and 1?s, and every prefix of the string has at most one more 0 than 1, and at most one more 1 than 0.

REGULAR. This language can be accepted by the following NFA:

4. Closure. (10 points)

Determine whether Regular sets are closed under each of the operations below. Prove your answers by an explanation and/or example or counterexample.

a. Even(L) is the set of all strings x in L such that |x| is even.

CLOSED Even(L) is just the intersection of L with a DFA which accepts strings of even length. Since L and this DFA are both regular, and regular sets are closed under intersection, regular sets must also be closed under Even.

Page 3: Solution to Midterm 1

b. Triple(L) = {x | x=uvw, such that u, v, w are in L, and |u| = |v| = |w|}.

NOT CLOSED by counterexample. Consider the language A = 0*1. Triple(A) has the form 0n10n10n1 where n>=0. If we assume pumping length p and try to pump the string s=0p10p10p1 which is in this language, we get s=0p+|y|10p10p1 which is not. Since A is regular and Triple(A) is not, the set of regular languages is not closed under Triple.

5 NPDAs

Construct non-deterministic pushdown automata to accept the following languages.

a. {1n0n | n>0}

b. {0n12n | n>=0}

c. {1n0n | n>0} U {0n12n | n>=0}

6 CFGs

Construct context free grammars to accept the following languages.

a. {w | w starts and ends with the same symbol}

S -> 0A0 | 1A1

A -> 0A | 1A | e

b. {w | |w| is odd and its middle symbol is 0}

S -> 0 | 0S0 | 0S1 | 1S0 | 1S1

Page 4: Solution to Midterm 1

c. {0n1n | n>0} U {0n12n | n>0}

S -> 0A1 | 0B11A -> 0A1 | e

B -> 0B11 | e

d. Binary strings with twice as many 1s as 0s.

S -> e | 0S1S1S | 1S0S1S | 1S1S0S

7 AMBIGUITY Explain why the grammar below is ambiguous.

S -> 0A | 1BA -> 0AA | 1S | 1

B -> 1BB | 0S | 0

The grammar is ambiguous because we can find strings which have multiple derivations:

S0A0 0AA00 1S 1001 1B 1

0011 0 1

S0A0 0AA00 1 1S0011 0A00110 1

8 Converting to Normal Form a. Put the following grammar into Chomsky Normal Form.

S -> A | AB0 | A1AA -> A0 | eB -> B1 | BC

C -> CB | CA | 1B

Remove all e rules

S -> e | A | AB0 | A1A | B0 | A1 | 1AA -> A0 | 0B -> B1 | BC

C -> CB | CA | 1B

Remove unit rules

Page 5: Solution to Midterm 1

S -> e | A0 | 0 | AB0 | A1A | B0 | A1 | 1AA -> A0 | 0B -> B1 | BC

C -> CB | CA | 1B

Convert remaining rules into proper form

S -> e | A0 | 0 | AS1 | B0 | A1 | 1AA -> A0 | 0B -> B1 | BCC -> CB | CA | 1B

S1 -> B0 | 1A

S -> e | AN0 | AS1 | BN0 | AN1 | N1AA -> AN0 | 0B -> BN1 | BCC -> CB | CA | N1BS1 -> BN0 | N1AN0 -> 0N1 -> 1

NOTE: We did not need to create a new start state because the given one did not appear in the right side of any rule.

b. Convert the following grammar into an equivalent one with no unit productions and no useless symbols.

S -> A | CBA -> C | DB -> 1B | 1C -> 0C | 0

D -> 2D | 2

Converts to

S -> 0C | 0 | 2D | 2 | CBA -> C | DB -> 1B | 1C -> 0C | 0

D -> 2D | 2

A is now useless and can be removed.