Solutions - MidTerm Exams

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

NILE UNIVERSITY OF NIGERIA, ABUJA

FACULTY OF NATURAL & APPLIED SCIENCES


CSC 817: THEORY OF COMPUTATION; Level GRADUATE
Midterm Examnation - Answer Key
18 January 2021

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

(a) (10 pts) Give the state diagram of this machine.


Answer: Figure 1.

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 .

(c) (2 pts) Does the machine accept the string 1110100?


Answer: The string w = 1110100 drives M1 through the following states:

q0 → q0 → q0 → q0 → q1 → q2 → q3 → q1 .

Since q1 is not an accepting state, M does not accept 1110100.


(d) (10 pts) What is the language of M1
Answer:
L(M1 ) = {w ∈ Σ∗ |w contains the substring 0101}.
2. (a) (15 pts) Construct a deterministic finite automaton equivalent to the nondeterministic
finite automaton given in Figure 2.

a, b
ǫ a
q0 q1 q2

b b b ǫ

q3 q4
a

Figure 2: NFA

Answer: Let N = (Q, Σ, δ, q0 , F ) be the automaton of Figure 2, where Q = {q0 , q1 , q2 , q3 , q4 }, Σ =


{a, b}, and F = {q2 }. Let M = (Q′ , Σ, δ ′ , q0′ , F ′ ) be the equivalent DFA. Since N has 5
states, M will have 25 = 32 states. However, only a few of these states will be relevant to
the operation of M - namely, those states that can be reached from q0′ by reading some
input string. We shall construct this part of M by starting from q0′ and introducing a new
state only when it is needed as the value of δ ′ (q, a) for some state q already introduced
and some a ∈ Σ.
For each of the states of N we have: E(q0 ) = {q0 , q1 }, E(q1 ) = {q1 }, E(q2 ) = {q2 }, E(q3 ) =
{q3 }, and E(q4 ) = {q2 , q4 }. Since q0′ = E(q0 ) = {q0 , q1 }, it follows that

δ ′ (q0′ , a) = ∪r∈E(q0 ) δ(r, a) = E({q0 }) ∪ {q2 } = {q0 , q1 , q2 }.

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

Figure 3: Transition diagram of DFA M

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,

L(G) = {am cbm |m is nonnegative}

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|ǫ

2. Eliminate e-rules: A → ǫ and B → ǫ

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

2. Put the expressions in CNF

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

1. Consider the language

L1 = {w#wR |w ∈ {0, 1}∗ and wR is w reversed}

(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 )

The state diagram is given in Figure 4.


q0

#→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

Figure 4: Decider for w#wR


2. Let Σ = {a, b} and consider the following language consisting of words of odd length with one
b in the middle position.
L = {w ∈ Σ∗ ||w| is odd and there is a b in the middle position of w}
Show that L is decidable by implementing a Turing machine that decides this language.
(a) (10 pts) Briefly describe in words the behavior of your machine. State what symbols are
in Γ, the tape alphabet.
Answer: Let D be the Turing decider for L. Then D operates as follows.
1. Make sure the input string is odd-length and contains only a and b:
This approach uses a two-state procedure to search for the end of
the word, oscillating between a reject and a ”continue” state.
So always moving right....
A. Read an a, b, or blank. If blank, reject.
B. Read an a, b, or blank. If blank, continue on to 2.
Otherwise, go back to A.
2. Now verify the middle character is a ”b”:
Move the head back to the beginning.
Move right until a non-marked character is found:
If it’s an ”a”:
- Mark it with an X.
- Scan right, until an X or blank is encountered, then move left.
- If this is an X, then we didn’t find a match, which means that
”a” was the middle character. Reject.
- Otherwise, mark with X, rewind, repeat.
If it’s a b:
- Mark it with an X.
- Scan right, until an X or blank is encountered, then move left.
- If this is an X, then we just discovered the middle character is a b. Accept.
- Otherwise, mark with X, rewind, repeat.
Alternative Solution: Construct a non-deterministic Turing machine that non-deterministically
chooses a b in the input (if no b is found the machine rejects the input word) and then
checks that the strings in both sides of that b are of equal length.
(b) (15 pts) Provide a diagram of states and transitions for your machine. Remember to
make all the transitions explicit in your diagram.
Solution The Turing Machine M that decides L is M = (Q, Σ, Γ, δ, q0 , qa , qr ) where
Q = {q0 , q1 , q2 , q3 , q4 , q5 , q6 , q7 , qa , qr }
Σ = {a, b}
Γ = Γ = {a, b, X, ⊔, ⊲}
δ = transition function
q0 start state
qa accept state
qr reject state
where ⊔ is the blank symbol, ⊲ is the start of tape symbol, and X is the marker. The
state transition diagram is given in Figure ??
a, b → R

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

Figure 5: Turing decider for L

3. Each of the languages below in parts (a), and (b) is of one of the following types:

Type REG. It is regular


Type CFL. It is context-free, but not regular

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,

xyyz = 0j 0k 0k 0m 12p 0p = 0p+k 12p 0p

because j + k + m = p. But, k > 0 implies reverse(xyyz) 6= xyyz, which means xyyz ∈


/ A,
which contradicts (1). Therefore, A is a nonregular language.
(b) (12 pts) C = {w ∈ Σ∗ |na (w) mod 4 = 1}, where Σ = {a, b} and na (w) is the number
of a’s in string w. For example, na (babaabb) = 3. Also, recall that j mod k returns the
remainder after dividing j by k, e.g., 3 mod 4 = 3, and 9 mod 4 = 1.
Answer C is of type REG. A regular expression for C is (b∗ ab∗ ab∗ ab∗ ab∗ )∗ b∗ ab∗ , and a
DFA for C is below:
b b
a
q0 q1

a a

q3 q2
a
b b

You might also like