Solutions - MidTerm Exams
Solutions - MidTerm Exams
Solutions - MidTerm Exams
Instructions:
• Answer ANY two questions from Section A and ANY two questions from Section
B
• Show your work completely. No work, no credit.
• Receiving or giving assistance from a colleague of yours is prohibited
Section A
Answer any 2 of the following 3 questions
1. The formal description of a DFA M1 is ({q0 , q1 , q2 , q3 , q4 }, {0, 1}, δ, q0, {q4 }), where δ is given
by the following table.
0 1
q0 q1 q0
q1 q1 q2
q2 q3 q0
q3 q1 q4
q4 q4 q4
1 0 0 0, 1
0 1 0 1
q0 q1 q2 q3 q4
1
Figure 1: DFA recognizing L(M1 )
(b) (3 pts) What sequence of states does the machine go through on input 00111010?
Answer: The sequence of states that M1 goes through on input w = 00111010 is
q0 → q1 → q1 → q2 → q0 → q0 → q1 → q2 → q3 .
q0 → q0 → q0 → q0 → q1 → q2 → q3 → q1 .
a, b
ǫ a
q0 q1 q2
b b b ǫ
q3 q4
a
Figure 2: NFA
Similarly,
δ ′ (q0′ , b) = ∪r∈E(q0 ) δ(r, b) = E({q0 }) ∪ {q3 } = {q0 , q1 , q3 }.
Repeating this calculation for the newly introduced states, we have the following.
δ ′ ({q0 , q1 , q2 }, a) = {q0 , q1 , q2 }
δ ′ ({q0 , q1 , q2 }, b) = {q0 , q1 , q2 , q3 , q4 }
δ ′ ({q0 , q1 , q3 }, a) = {q0 , q1 , q2 , q4 }
δ ′ ({q0 , q1 , q3 }, b) = {q0 , q1 , q3 }
δ ′ ({q0 , q1 , q2 , q3 , q4 }, a) = {q0 , q1 , q2 , q4 }
δ ′ ({q0 , q1 , q2 , q3 , q4 }, b) = {q0 , q1 , q2 , q3 , q4 }
Finally,
δ ′ ({q0 , q1 , q2 , q4 }, a) = {q0 , q1 , q2 }
δ ′ ({q0 , q1 , q2 , q4 }, b) = {q0 , q1 , q2 , q3 , q4 }
At this point, there are no new states and the process terminates.
The relevant part of the equivalent DFA M is illustrated in Figure 3.
(b) (10 pts) Convert the regular expression a(abb)∗ ∪ b to an equivalent NFA using the pro-
cedure discussed in class.
a
a
{q0 , q1 } {q0 , q1 , q2 }
b
b a
b
a
{q0 , q1 , q3 } {q0 , q1 , q2 , q4 } {q0 , q1 , q2 , q3 , q4 }
a
b b
3. (a) (10 pts) Find the language L(G) over {a, b, c} generated by the grammar G with produc-
tions
S → aSb, aS → Aa, Aab → c.
Answer First we must apply the first production one or more times to obtain the word
w = an Sbn where n > 0. To eliminate S, we must apply the second production to obtain
the word w′ = am abAbm where m = n − 1 ≥ 0. Now we can apply the third production
to finally obtain the word wn = am cbm where m ≥ 0. Accordingly,
That is, L(G) consists of all words with the same nonnegative number of a’s and b’s
separated by a c.
(b) (15 pts) Convert the following CFG into an equivalent CFG in Chomsky normal form,
using the procedure discussed in class.
A → BAB|B|ǫ
B → 00|ǫ
Answer:
1. Add a new start symbol S → A
S → A
A → BAB|B|ǫ
B → 00|ǫ
S → A
A → BAB|B|BB|AB|BA
B → 00
2. Eliminate unit rules: A → B and S → A
S → BAB|00|BB|AB|BA
A → BAB|00|BB|AB|BA
B → 00
S → Y B|X0 X0 |BB|AB|BA
A → Y B|X0 X0 |BB|AB|BA
B → X0 X0
Y → BA
X0 → 0
Section B
Answer any 2 of the following 3 questions
(a) (10 pts) Give an implementation-level description of a Turing machine that recognizes
L1 .
Answer
M =”On input string w
1. Zig-zag across the tape to corresponding positions on either side of the # symbol to
check whether these positions contain the same symbol. If they do not, or if no #
is found, reject. Mark symbols with an x as they are checked to keep track
of which symbols correspond.
2. When all symbols to the left of the # symbol have been crossed off, check
for any remaining symbols to the right of the #. If any symbols remain,
reject; otherwise, accept.
(b) (15 pts) Give a formal description of the Turing machine described in part (a).
Formal description of the TM
M = (Q, Σ, Γ, δ, q0 , qa , qr )
#→R
0 → x, R q10 1 → x, R
x→R
0, 1 → R q1 ⊔→L q4 0, 1 → R
#→R qa #→R
0, 1 → R q3 x→R q6 0, 1 → R
0, 1, # → L
x, ⊔ → L q9 x, ⊔ → L
0 → x, L 1 → x, L
q5 q8
1→L 0→L
qr
q0 q1
⊔→R a, b → R ⊔→L
qrej q2 a, b → L
x→L ⊔→R
q3
a → x, R b → x, R
a, b → R q4 q5 a, b → R
x, ⊔ → L x→R x, ⊔ → L
q6 q7 x→R q
acc
a, b → x, L a, b → x, L
q8
a, b → L
3. Each of the languages below in parts (a), and (b) is of one of the following types:
For each of the following languages, specify which type it is. Also, follow these instructions:
• If a language L is of Type REG., give a regular expression and a DFA for L
• If a language L is of Type CFL., give a context-free grammar and a PDA for L. Also,
prove that L is not regular.
• If a language L is of Type DEC., give a description of a Turing machine that decides L.
Also, prove that L is not context-free.
(a) (13 pts) A = {w ∈ Σ∗ |w = wR and the length of w is divisible by 4}, where Σ = {0, 1}.
Answer A is of type CFL. A CFG for A has rules S → 00S00|01S10|10S01|11S11|ǫ. A
PDA for A is as follows:
We now prove that A is not regular by contradiction. Suppose that A is regular. Let
p ≥ 1 be the pumping length of the pumping lemma (Theorem 1.I). Consider string
ǫ, ǫ → $ ǫ, ǫ → ǫ ǫ, $ → ǫ
q0 q1 q2 q5
0, ǫ → 0 0, ǫ → 0 0, 0 → ǫ 0, 0 → ǫ
1, ǫ → 1 1, ǫ → 1 1, 1 → ǫ 1, 1 → ǫ
q3 q5
s = 0p 12p 0p ∈ A, and note that |s| = 4p > p, so the conclusions of the pumping lemma
must hold. Thus, we can split s = xyz satisfying conditions (1) xyiz ∈ A for all i ≥ 0, (2)
|y| > 0, and (3) |xy| ≤ p. Because all of the first p symbols of s are 0s, (3) implies that x
and y must only consist of 0s. Also, z must consist of the rest of the 0s at the beginning,
followed by 12p 0p . Hence, we can write x = 0j , y = 0k , z = 0m 12p 0p , where j + k + m = p
because s = 0p 12p 0p = xyz = 0j 0k 0m 12p 0p . Moreover, (2) implies that k > 0. Finally, (1)
states that xyyz must belong to A. However,
a a
q3 q2
a
b b