Вы находитесь на странице: 1из 4

CS 2030 : Theory of Computation

Solutions to Midterm 1
Subrahmanyam Kalyanasundaram
April 5, 2014

Some of these are solution sketches. Please work out the full details as an exercise.
1. (a) [9 points] Consider the function h : . For each symbol a , h maps a to a
string h(a) . Now, extend the definition of h to strings as follows: for any string
x , replace each symbol a in x by h(a).
For example. If h(0) = 00, h(1) = 01, then h(011) = 000101.
Given a language L , define h(L) = {h(x) | x L}. If L is regular, show that h(L)
is regular as well.
This can be done by modifying the DFA which recognizes the language L. We modify
the DFA as follows: for each transition representing a , replace the transition (by
adding intermediate states, if necessary) by the string h(a). We will get an NFA because
not all symbols might have exiting arrows from all the states. Now one can show that
the modified machine accepts the language h(L). As an exercise, work out a similar
solution using regular expressions.
(b) [6 points] For = {a, b, c}, use part (a) to show that the language A = {a3 bn3 cn | n >
3} is not regular.
Use the following h: h(a) = a, h(b) = a and h(c) = c. Now we get that h(A) =
{an cn | n > 3}. This language is not regular and hence A is not regular.
In problem 1, we proved that regular languages are closed under homomorphisms. h is an
example of a homomorphism. In the exercise below, we generalize the above problem to
use substitutions. Homomorphisms and substitutions are very useful in determining whether
languages are regular.
Exercise: Let h be defined as follows. h : P( ), where P( ) denotes the power set
(the set of all subsets) of . This means that h can take any symbol from to a set of
strings in . Instead of one string, h(a) can be one among many strings. Prove that regular
languages are closed under h with this definition as well.
2. Consider the NFA in the figure below.
(a) [2 marks] What is the language described by the above NFA?
The language described the NFA is the set of all strings in {a, b} which have ab as a
substring.

(b) [6 marks] Convert the NFA to a DFA using the conversion algorithm that we saw in
class. You may omit the states that are not reachable in the DFA.
We can start with the starting state of the DFA, which is the state corresponding to {0},
let us call it q{0} . When we see an a from q{0} , we can go to the state 0 or 1 in the NFA,
so the transition from q{0} upon seeing an a is to the state q{0,1} . Similarly upon seeing
a b at q{0} , we have a self loop to q{0} .
Since the non-reachable states are not required, we can proceed in a similar manner,
identifying the transitions from the new states that we reach in the DFA. The full table
of transitions for the DFA is given below.

q{0}
q{0,1}
q{0,2}
q{0,1,2}

q{0,1}
q{0,1}
q{0,1,2}
q{0,1,2}

q{0}
q{0,2}
q{0,2}
q{0,2}

The starting state is q{0} and the set of accepting states are the set of states which have
a non-empty intersection with the accepting states of the NFA. So the set of accepting
states for the DFA is {q{0,2} , q{0,1,2} }.
(c) [2 marks] Is the DFA obtained in part (b) a minimal DFA? Answer with a proper
justification.
No, the above DFA is not a minimal DFA. The reason is that the accepting states q{0,2}
and q{0,1,2} can be combined to form a single accepting state which loops to itself on
seeing an a or b. It can be easily checked that this DFA is equivalent to the DFA that
we obtained as a result of the conversion in part (b).
3. (a) [7 marks] Let C be a context-free language and R be a regular language. Prove that
the language C R is context free.
Let P be the PDA that recognises C and D be the DFA that recognises R. We can build
a PDA P that recognises C R. The PDA P is built based on the Cartesian products
of the states of P and D.
Let P = (Q1 , , , 1 , q1 , F1 ) and D = (Q2 , , 2 , q2 , F2 ). We will have P = (Q, , , , q0 , F )
where Q = Q1 Q2 , F = F1 F2 and q0 = (q1 , q2 ). The transition function is defined
as follows:
(
a 6= , qi0 = 1 (qi , a) and (qj0 , b0 ) 2 (qj , a, b)
0 0
0
((qi , qj ), b ) ((qi , qj ), a, b) if
a = , qi0 = qi
and (qj0 , b0 ) 2 (qj , , b)
The first co-ordinate follows the movements of D and the second co-ordinate follows the
movements of P .
2

(b) [3 marks] Use part (a) to show that the language B described below is not a CFL.
B = {w|w {a, b, c} and contains equal numbers of as bs and cs}
Suppose the given language B is a CFL. Notice that the language generated by the
regular expression a b c is a regular language. Then by part (a), the intersection of the
two languages is a CFL. But the intersection of the two languages is {an bn cn |n 0}
which is known to be not a CFL. Hence the assumption that B is a CFL is wrong.

4. For the strings w and x, write w = x if the symbols of w are a permutation of the symbols

of x. In other words, w = x, if w and x have the same symbols in the same quantities, but
possibly in a different order.

For any string w, define SCRAMBLE(w) = {x | w = x}. For any language L, let
SCRAMBLE(L) = {x | x SCRAMBLE(w) for some w L}.
(a) [7 marks] Let = {0, 1} and L be a regular language over . Show that SCRAMBLE(L)
is context-free.
Since L is a regular language, consider the DFA D that recognises L. We can modify D
to get a PDA P that recognises SCRAMBLE(L). Let D = (Q1 , , 1 , q1 , F1 ). The PDA
P would be P = (Q2 , , , 2 , q2 , F2 ) where = {$} = {0, 1, $}.
Let us describe P in detail. Q2 = Q1 {q2 , qa }, where q2 is the start state of P and qa
is the single accept state of P . This means that F2 = {qa }. Now we will describe 2 .
From q2 , P can only make an empty transition, and insert a delimiter symbol $ into the
stack, moving to q1 (the start state of D). That is 2 (q2 , , ) = {(q1 , $)}. This is the
only transition allowed from q2 .
For all other states q Q1 , suppose 1 (q, 0) = q 0 . Then we have 2 (q, 0, ) = {(q 0 , )}. In
addition, we have 2 (q, 1, 0) = {(q 0 , )} and 2 (q, 1, ) = {(q 0 , 1)}. The first rule allows
P to make the same moves as D. The next two rules allow P to make a transition on 1
instead of 0, by either popping a 0 from the stack or pushing a 1 in the stack. These rules
make use of the stack to keep track if a 0 was used instead of a 1 or vice versa. Similarly
if 1 (q, 1) = q 0 . Then we would add rules 2 (q, 1, ) = {(q 0 , )}, 2 (q, 0, 1) = {(q 0 , )} and
2 (q, 0, ) = {(q 0 , 0)} to P .
Finally, we also have 2 (q, , $) = {(qa , )} for every q F1 . This rule lets P move from
any accepting state of D to the accepting state of P provided the stack is empty.
(b) [3 marks] What happens in part (a) if = {0, 1, 2}? Prove your answer.
In this case, SCRAMBLE(L) is not context-free. We cannot use the proof of part (a). As
a counter example, consider the SCRAMBLE of the language L = {(012)n | n 0}. Here
L is regular, but SCRAMBLE(L) is not context-free. SCRAMBLE(L) is the language
that consists of an equal number of 0s, 1s and 2s. We know that it is not context free
by problem 3(b).
5. [10 marks] Let D = {an bn | n 0}. Provide a CFG that generates D. You should give a
proof that your CFG generates D.
The main idea of this solution is to note that D consists of mainly 3 parts; strings not of the
form a b or those which have ba as a substring, strings of the form ai bj where i < j and
3

those of the form ai bj where i > j. It is easy and straightforward to write CFGs for each of
these three and combine them to get a CFG for D.
6. [10 marks] Use pumping lemma to show that the language E = {w#x | w is a substring of x}
is not a CFL.
Let p be the pumping length. Consider the string 0p 1p #0p 1p . This string is in E. If it can
be pumped, it can be split into uvxyz where |vxy| p and |vy| > 0 such that for all i 0,
uv i xy i z E. If v of y contains the # symbol, then pumping up creates a string with multiple
#s and hence not in E. If both v and y lie only on one side of the #, then pumping up or
down creates a string not in E. So v and y have to be on either side of the #. Consequently,
v has to be of the form 1 and y has to be of the form 0 . If v is non empty, pumping up
creates a string not in E. If v is an empty string, then y has to be non-empty and then
pumping down creates a string not in E. Hence the string 0p 1p #0p 1p cannot be pumped,
and so E is not a CFL.
7. Let a k-PDA be a pushdown automaton that has k stacks, in addition to the input tape.
Thus a 0-PDA is an NFA and a 1-PDA is a conventional PDA. We know that 1-PDAs are more
powerful than 0-PDAs.
(a) [9 points] Show that 2-PDAs are more powerful than 1-PDAs.
The idea is to use both the stacks as a tape. The left stack would correspond to the
portion to the left of the tape head and the right stack would correspond to the portion
to the right of the tape head. On starting the 2-PDA, push a $ symbol into the left
stack, read the entire input and push it into the left stack. Then push a $ symbol on to
the right stack, and pop the symbols off the left stack, one by one, pushing them into
the right stack. Stop when we see the $ symbol. Now we are in a position corresponding
to the start state of the TM.
Now the 2-PDA proceeds to simulate the TM as follows. When it needs to move the
tape head to the left, it pops left stack and pushes into the right stack. When it needs
to move to the right, it does the opposite. When it needs to rewrite a symbol, it does
so accordingly. It accepts/rejects when the TM enters an accept/reject state.
Exercise: Without simulating a TM, construct a 2-PDA which can recognize the language {0n 1n 2n | n 0}. Conclude that 2-PDAs are more powerful than 1-PDAs.
(b) [6 points] Show that 3-PDAs are not more powerful than 2-PDAs, by giving a short
proof.
We saw that TMs can be simulated by 2-PDAs. We shall show that 3-PDAs can be
simulated by a TM, and conclude that they are no more powerful than a 2-PDA. Since
multitape TMs are equivalent to single tape TMs, we could use a 3-tape TM to simulate
a 3-PDA. Each tape simulates a stack of the 3-PDA. The simulation is straightforward,
since TMs have more freedom to access the tapes than a PDA has to access it stacks.

Вам также может понравиться