Month_8

Month 8: Theory of Computation
Problem Set 1 - Prof. Shai Simonson
1. DFAs
Draw Deterministic Finite Automata to accept the following sets of strings over the
alphabet {0,1}:
a.
b.
c.
d.
e.
f.
All strings that contain exactly 4 "0"s.
All strings ending in "1101".
All strings containing exactly 4 "0"s and at least 2 "1"s.
All strings whose binary interpretation is divisible by 5.
(1.4c) All strings that contain the substring 0101.
(1.4e) All strings that start with 0 and have odd length or start with 1 and have even
length.
g. (1.4f) All strings that don’t contain the substring 110.
h. (1.4g) All strings of length at most five.
i. (1.4i) All strings where every odd position is a 1.
2. NFAs
Draw Non-deterministic Finite Automata with the specified number of states to
accept the following sets:
a. All strings containing exactly 4 "0"s or an even number of "1"s. (8 states)
b. All strings such that the third symbol from the right end is a "0". (4 states)
c. All strings such that some two zeros are separated by a string whose length is 4i for
some i >= 0. (6 states)
d. (1.5b) All strings that contain the substring 0101. (5 states)
e. (1.5c) All strings that contain an even number of zeros or exactly two ones. (6 states)
f. (1.5e) The language 0*1*0*0. (3 states)
3. Converting NFAs to DFAs
a. Convert the NFA in 2f into a Deterministic Automaton.
b. 1.12a in the text.
c. 1.12b in the text.
4. Discrete Math Review – Proofs
Analyze the two languages below. They are two descriptions of the same language –
strings of balanced parentheses.
Language 1: The set of strings where each string w has an equal number of zeros
and ones; and any prefix of w has at least as many zeros as ones.
Language 2: The set of strings defined inductively as follows: if w is in the set then
0w1 is also in the set; if u and v are in the set then so is uv; and the empty string is
in the set.
a. Prove that every string in Language 2 is contained in Language 1.
b. Extra Credit: Prove they are equal (i.e. Language 1 is also contained in Language 2).
5. Closure Problems
You may use examples to illustrate your proofs.
a. Prove that if L1 is regular and L2 is regular then so is L1-L2 (the set of all strings in
L1 but not in L2).
b. Prove that if L is regular then Prefix(L) is regular. Prefix(L) is the set of all strings
which are a proper prefix of a string in L.
c. Prove that Regular Sets are closed under MIN. MIN(R), where R is a regular set, is
the set of all strings w in R where every proper prefix of w is in not in R. (Note that
this is not simply the complement of PREFIX).
d. Prove that Regular Sets are NOT closed under infinite union. (A counterexample
suffices).
e. What about infinite intersection?
f. Extra Credit: (1.42) Prove that if L is regular so is Half(L). Half(L) is the set of all
first halves of strings in L.
6. Regular Expressions
Write regular expressions for each of the following languages over the alphabet
{0,1}. Provide justification that your regular expression is correct.
a. The set of all strings in which every pair of adjacent zeros appears before any pair of
adjacent ones.
b. The set of all strings not containing 101 as a substring.
c. The set of all strings with at most one pair of consecutive zeros and one pair of
consecutive ones.
7. Converting Finite Automata to Regular Expressions
a. 1.16a in the text.
b. 1.16b in the text.
8. Regular Expression Identities
Prove (give at least a few words of justification), or disprove (by counterexample)
that each pair of regular expressions represent the same language. Assume that r, s
and t represent regular expressions over the alphabet {0,1}.
a. r(s + t) and rs + rt
b. (r*)*and r*
c. (r + s)* and r*s*
9. Final States
a. Explain why every NFA can be converted to an equivalent one that has a single final
state.
b. Give a counterexample to show that this is not true for DFA’s.
c. Extra Credit: Describe the languages that are generated from a DFA with just one
final state.
10. Optional Extra Problems
a. Draw a Finite Automaton to accept the following regular expression and succinctly
describe the set in English.
[00 + 11 + (01 + 10)(00 + 11)*(01 + 10)]*
b. The language of addition: 1.25 in the text.
c. Show that the following is a regular language:
(1.41) The strings that contain an equal number of occurrences of the substrings 01
and 10
Month 8: Theory of Computation
Problem Set 1 Solutions - Mike Allen and Dimitri Kountourogiannis
1. DFAs
a. All strings that contain exactly 4 0s.
b. All strings ending in 1101.
c. All strings containing exactly 4 0s and at least 2 1s.
d. All strings whose binary interpretation is divisible by 5.
e. (1.4c) All strings that contain the substring 0101.
f. (1.4e) All strings that start with 0 and has odd length or start with 1 and has even
length.
g. (1.4f) All strings that don't contain the substring 110.
h. (1.4g) All strings of length at most 5.
i. (1.4i) All strings where every odd position is a 1.
2. NFAs
a. All strings containing exactly 4 0s or an even number of 1s. (8 states)
b. All strings such that the third symbol from the right end is a 0. (4 states)
c. All strings such that some two zeros are separated by a string whose length is 4i for
some i>=0. (6 states)
d. (1.5b) All strings that contain the substring 0101. (5 states)
e. (1.5c) All strings that contains an even number of 0s or exactly two 1s. (6 states)
f. (1.5e) The language 0*1*0*0 (3 states)
3. Converting NFAs to DFAs
a. Convert the NFA in 2f into a DFA.
b. 1.2a in the text
c. 1.2b in the text
4. Discrete Math Review - Proofs
L1: The set of strings where each string w has an equal number of zeros and ones;
and any prefix of w has at least as many zeros as ones.
L2: The set of strings defined inductively as follows: if w is in the set then 0w1 is also
in the set; if u and v are in the set then so is uv; and the empty string is in the set.
a. Prove that every string in L2 is contained in L1
We can analyze L2 inductively to see that it maintains the property of L1 for
each case:
1. The empty set. This is a member of L1, since it satisfies the properties
vacuously.
2. 0w1. Assuming that w is in L1, we maintain the equal number of 0s and 1s
because we add one of each. We also maintain the prefix condition, since the
0 is added before the 1.
3. uv. Assuming that u and v are both in L1, simply concatenating them together
will maintain the equal number of 0s and 1s. The prefix condition is slightly
more difficult. We consider the following prefixes:
a. PREFIX(u). Since u is in L1, this must be in L1.
b. u. Again, since u is in L1, this must be in L1.
c. uPREFIX(v). Since u has an equal number of 0s and 1s, and v is in L1,
this must maintain the prefix property.
b. For those of you who are paying attention, this problem is extemely similar to the
stream-crossing ghostbusters problem from algorithms. The proof is by induction on
the length of strings in L1:
1. The base case is the empty string. This is in L2 by definition.
2. For the inductive step, suppose that all strings in L1 of length <= n are in L2.
Let w be a string in L1 of lenght n+1 and suppose it is of the form
A1A2...An+1, where Ai is either 0 or 1. Let j be the first index with the property
that A1A2...Aj has the same number of zeros and ones. There are two cases to
analyze.
a. j < n+1. Then not only does u = A1A2...Aj have the same number of
zeros and ones, any prefix of u will have at least as many zeros as ones
since it is also a prefix of w. So u is in L1. Let v = Aj+1Aj+2...An+1.
Then v must have the same number of zeros and ones since both u and
w=uv do. Also, any prefix x of v cannot have more ones than zeros in
it since then ux would be a prefix of w that had more ones than zeros.
Therefore v must be in L1. Since both u and v are of length <= n, by
the induction hypothesis they are in L2. Therefore w = uv must be in
L2, by the definition of L2.
b. j = n+1. Then w = 0u1 for some string u, and u has the same number of
zeros and ones, since w does. Also, no prefix x of u can have more
ones than zeros, since then 0x would either have more ones than zeros
which is impossible by hypothesis, or 0x would have the same number
of ones as zeros, which is also impossible by since j = n+1. Therefore
we can conclude that u is in L1, and since it is of length <=n it is in L2
by the induction hypothesis.
This completes the inductive step, and therefore L1 is contained in L2.
5. Closure Problems
a. Prove that if L1 is regular and L2 is regular then so is L1-L2 (the set of all strings in
L1 but not in L2).
L1-L2 is the same as the intersection of L1 and the complement of L2. Since
the set of regular languages is closed under each of these operations, L1-L2
must be regular.
b. Prove that if L is regular then Prefix(L) is regular. Prefix(L) is the set of all strings
which are a proper prefix of a string in L.
We can construct a DFA to decide Prefix(L) by taking the DFA for L and
marking all states from which an accept state is reachable as accept states.
So, Prefix(L) must be regular.
c. Prove that Regular Sets are closed under MIN. MIN(R), where R is a regular set, is
the set of all strings w in R where every proper prefix of w is in not in R. (Note that
this is not simply the complement of PREFIX).
We can construct a DFA to decide MIN(R) by taking the DFA for R and
redirecting all outgoing arrows from all the accept states to a dead state. So,
MIN(R) must be regular.
d. Prove that Regular Sets are NOT closed under infinite union. (A counterexample
suffices).
Consider the sets {0}, {01}, {0011}, etc. Each one is regular because it only
contains one string. But the infinite union is the set {0i1i | i>=0} which we know
is not regular. So the infinite union cannot be closed for regular languages.
e. What about infinite intersection?
We know that
{0i1i | i>=0} = {0} U {01} U {0011} U ...,
Taking complements and applying DeMorgan's law gives us
{0i1i | i>=0}c = {0}c ^ {01}c ^ {0011}c ^ ...,
Where we are using U to deonte union and ^ to denote intersection. Recall
the complement of a regular language is regular, and hence the complement
of a not-regular language is not regular. So we can conclude that the left hand
side of the equation is not-regular, and each term in the intersection is
regular. Therefore infinite intersection does not preserve regularity.
f. extra credit Let (Q, Sigma, delta, q, F) be a deterministic machine that accepts L.
Then a machine that accepts Half(L) is
g. (QxQxP(Q), Sigma, delta', {(q, r, {r}) }, { (r1,r2,S) in QxQxP(Q) | r1=r2 and S contains an
element of F} )
where P(Q) is the power set of Q, i.e. all subsets of Q., and
delta'(r1, r2, S, a) = (delta(r1,a), r2, Sa)
and Sa denotes the set of states that can be reached from some state in S in one step
with input a. To simplify the notation, we are allowing multiple start states {(q, r, {r})
}. This could be also achieved by introducing a new state and introducing epsilon
moves. The reason that this accepts Half(L) is that the term r2 in (r1,r2,S) remembers
the midpoint of a potential string in L, and S represents the states one can reach from
r2 in the number of steps it takes to get from the start q to r1. When r1=r2, then S
contains all the paths that one can get to in twice as many steps as it took to get from
q to r1.
An intuitive explanation The Half(L) problem is given a string w is there a
string x of the same length as w such that wx is in the language L. This is
hard to solve directly, so we break it into a number of subproblems of the
following form: Fix a machine M that generates L and pick a state r in that
machine.
The problem Half(L,r)is then: Given a string w, is there a string x of the same
length as w such that wx is in the language L and after reading in w, the
machine M is in the state r.
We can reduce solving Half(L) to solving Half(L,r) for each state r in the
machine M and oring the result. The reason this is good is that the problem
Half(L,r) decomposes naturally into two other simple problems:
If we make the machine M' by making all accept states in M be reject states,
and by making state r an accept state, does M' accept the string w?
and
If we make the machine M'' by making state r the start state, and changing all
0 transitions to 0,1 transitions and similarly all 1 transitions to 0,1 transitions,
does the machine M'' accept the string w?
What we have done in the second case is to ingnore what the value of any
character in the string is. This is how to make a machine to accept all strings
that have the same length as strings accepted by a given machine. Putting all
this together should result in a similar machine to what is given for a solution
here, with possibly some missing extraneous states.
6. Regular Expressions
a. (10+0)*(1+10)*
(10+0)* will generate all strings that do not contain a pair of 1s, and (1+10)*, the
strings that do not contain a pair of 0s. So, the concatenation will generate all strings
in which every occurrence of 00 precedes every occurrence of 11.
b. 0*(1+000*)*0*
We can generate a string which does not contain the occurrence 101 by making sure
that any 0 in the middle (between two 1s) of the string must be paired with another 0.
c. (e+0)(10)*(e+0)(10)*(e+1)(10)*(e+1) + (e+0)(10)*(e+1)(10)*(e+0)(10)*(e+0)
The both terms are just alternating 1's and 0s , eg (e+0)(10)*(e+1) where you are
allowed to inert at most one extra 1 or 0 in between. We need two terms, depending
on whether the double 1 or the double 0 comes first.
7. Converting Finite Automata to Regular Expressions
a. (1.16a) (a* + ba*b)*ba*
b. (1.16b) [(a+b)(a+bb)*ba]*[e+(a+b)(a+bb)*b]
8. Regular Expression Identities
a. r(s + t) and rs + rt are equivalent because the first describes a string from r followed
by either a string from s or a string from t, and the second describes a string from r
followed by a string from s or a string from r followed by a string from t and these
two are clearly the same thing.
b. (r*)*and r* are equivalent because the first describes the concatenation of an arbitrary
number of terms that themselves are concatenations of arbitrary numbers of terms in
r. This is the same as r* which is the concatenation of an arbitrary number of terms in
r.
c. (r + s)* and r*s* are not equivalent because if s1 is a string in s and r1 is a word in r
then s1r1 is in (r+s)* but not r*s* but not the latter.
9. Final States
a. Every NFA can be converted into an equivalent NFA with only a single accept state
by creating a new accept state with epsilon moves from each of the old accept states.
b. This does not work for DFAs. The DFAs of problems 1g, 1h, and 1i are all good
counterexamples.
In general if the minimum DFA for a regular language has more than one final
state, then the language cannot be generated by a DFA with one final state.
This is because minimization cannot increase the number of final states.
c. Claim: The regular languages that can be represented by a DFA with one final state
are of the form RS*, where R and s are regular prefix-free languages.
Proof: We need the following lemma first: A prefix free regular language M
can generated by a machine with one final state. Suppose we have DFA
representation of M that has multiple final states. Then all outgoing transitions
from those final states must go to dead states since M is prefix free. But when
we mimize the DFA, all the dead states will become equivalent, and therefore
all the final states will become equivalent too.
We also need the following lemma: The Kleene star, M*, of prefix free regular
language M can be generated by a machine with one final state. From the
previous lemma we know there is a DFA that generates M that has one final
state. We can make M* by taking the minimal DFA that accepts M and
removing the transitions from the final state and collapsing it together with the
initial state (while keeping it a final state).
From these to lemmas it is clear that RS* can be generated by a machine
with one final state if R and S are prefix free, because we can just
concatenate the machines for R and S*.
Conversely, if L is generated by a DFA M with one final state, then L = Min(L)
( Min(L') )*, where L' is the language of the machine M' has the same states,
transitions, and final state as M, and where we choose the final state of M to
be the start state of M'. Since the Min of a language is always prefix free, L is
of the form we claim.
10. Optional Extra Problems
a. Convert [00 + 11 + (01 + 10)(00 + 11)*(01 + 10)]* to a Finite Automaton.
We just reverse the procedure for converting an NFA to a regular expression
by ripping-in states.
(note: the rightmost state in the second diagram corresponds to the bottom
right state in the third diagram.)
b. (1.25) Let B = {w | the bottow row of w is the sum of the top two rows}. The reverse
of B can be decided by the NFA below, and since the set of regular languages is
closed under reversal, B must be regular as well.
The NFA below determines if a string of columns composes a legal addition
equation where the top two rows sum to the third. The two states correspond
to whether the previous column led to a carryout or not, and the legal
transistions for each state correspond to columns which maintain the
correctness of the equation. If an invalid column is added, no valid outgoing
arrow is found and the computation dies (thus rejecting the input).
c. (1.41) Let D = {w | w contains an equal number of occurrences of 01 and 10}. This
language can be decided by the DFA below, and so must be regular.
The DFA works because the number of 01 transistions must always we within
one of the number of 10 transistions, so we need only remember which
transistion came first (top path vs. bottom path), and whether we have seen
an even number or odd number of transistions (left state vs. right state).
Month 8: Theory of Computation
Problem Set 2 - Prof. Shai Simonson
1. Minimizing FSM's
Consider the Finite Automaton below. Construct the smallest Deterministic Finite
Automaton which accepts the same language. Finally, draw a regular expression
that represents the language accepted by your machine and draw a Regular
Grammar that generates it.
2. Regular or Not?
You must prove that your choice is correct
a. The set of strings that have an even number of double zeros in them. (Note that three
zeros in a row count as 2 double zeros).
b. The set of strings over the alphabet {0} of the form 0n where n is not a prime.
c. The set of all strings of the form xwxR where x and w are non-empty strings over the
alphabet {0,1}, and the big R over the x means the reverse of x.
d. The set of all strings over the alphabet {0} whose length is n! for some n > 0.
e. The set of all binary strings that read backwards the same as forwards (pallindromes).
f. 1.17 a,b,c in the text
g. 1.18 in the text
h. 1.23 a,c in the text
i. 1.37 in the text
3. Decision Algorithms
Give decision algorithms to determine if a Regular set
a. Contains all strings of the form 0*1*.
b. Is co-finite. (its complement is finite).
c. 4.14 in the text
4. Regular Grammars
a. Write down a regular (also called left-linear) grammar to generate the set of strings
that are evenly divisible by 5 when interpreted as a binary string.
b. A right-linear grammar is a context-free grammar where each production must be
either in the form A->Ba, or A->b, where a and b are terminal symbols and A and B
are non-terminals. (The regular grammars we did in class are called left-linear).
Right-linear grammars are also equivalent to finite state machines. Explain how to
convert a given finite state machine, to an equivalent right-linear grammar. You may
use an example to illustrate.
5. Single Symbol Regular Languages
a. Prove that every language of the form 0mx+b, where m and b are positive integer
constants and x ranges from 0 to infinite, is regular.
b. Describe a regular set over the alphabet {0} that is NOT of the form from part (a).
c. Extra Credit: Characterize all regular sets over the alphabet {0}, and prove your
answer. That is, prove that every regular set over the alphabet {0} is of some
particular form.
6. Triple Extra Credit: Minimizing FSM’s
Describe a method to implement the FSM minimization algorithm that runs in O(n log
n) time, rather than O(n2). Write a program implementing your method.
Month 8: Theory of Computation
Problem Set 2 Solutions - Mike Allen and Dimitri Kountourogiannis
1. Minimizing DFAs
a. Convert to a DFA
c. Conver to a regular expression:
0 + (00 + 1 + 11)00*
b. Convert to a minimal DFA
d. Convert to a regular grammar using the NFA:
Using the DFA:
A -> 0 | 0B | 1C | 1D
S -> 0A | 1B
B -> 0D
A -> 0C | e
C -> 1D
B -> 0D | 1C
D -> 0 | 0D
C -> 0D
Using the regular expression:
S -> 0 | 110A | 000A | 10A
A -> 0 | 0A | e
2. Regular or Not?
a. NOT. (1.17b) {www | w is {a,b}*}
Assume that the language is regular. Let p be the pumping length, and
choose s to be the string 0p10p10p1. Now we try to break it up into s=xyz.
Since |xy| <= p and |y|>0, y can only contain 0's. When we pump the string
even just once we get xy2z = 0p+|y|10p10p1, and this is not of the form www,
since |y| > 0. This contradicts the pumping lemma, so the language is not
regular.
b. NOT. (1.23c) {0m1n | m is not equal to n}
We know that
{0n1n | n >= 0} = {0m1n | m,n >= 0}^{0*1*}c,
where we are using ^ to denote intersection and c to denote complement. The proof is
by contradiction. If {0m1n | m is not equal to n} really were regular then {0n1n | n >=
0} would also be regular because 0*1* is regular and because of the closure
properties of regular sets. Therefore it can't be regular
There is a direct way to prove it as well: If p is the pumping length and we
take the string s = 0p1p+p!, then no matter what the decomposition s = xyz is
the string xy1+p!/|y|z will equal 0p+p!1p+p! which is not in the language.
c. NOT. (1.23a) {0m1n0m | m,n >= 0}
Assume that the language is regular. Let p be the pumping length, and
choose s to be the string 0p10p. Now we try to break it up into s=xyz. Since
|xy| <= p, y can only have zeros in it. Now xyjz = 0p+ (j-1) |y|10p, and since |y|>0
the number of 0's on the left and right sides of xyjz will not be the same for
any j>1 so xyjz will not be in the language, contradicting the pumping lemma.
Therefore {0m1n0m | m,n >= 0} is not regular
d. REGULAR. The set of strings that have an even number of double zeros in them.
This can be decided by the DFA below.
e. REGULAR. The set of all strings of the form xwxR where x and w are strings over
the alphabet {0,1}.
This description is somewhat misleading. Since w can represent any string
and x can be 0 or 1, this is the same as all strings which begin and end with
the same character. This is easily seen to be a regular language.
f. NOT. The set of all strings over the alphabet {0} whose length is n! for some n>0.
Assume that the set is regular. Let p be the pumping length. Without loss of
generality we can assume that p is at least 2. (We can always increase the
pumping length. We only do this because some of our calculations don't work
for p = 1) Then, according to the pumping lemma, we can break the string
s=0p! into s=xyz where y has positive length and |xy| <= p. Then s=xy2z =
0p! + |y| must also be in the language, so p!+|y| must also be a factorial. But
(p+1)!-p! = (p)p! > p >= |y| so it follows that p! + |y| < (p+1)!, and therefore p! +
|y| is not a factorial. This is a contradiction so the language cannot be regular.
g. NOT. The set of strings over the alphabet {0} of the form 0n where n is not prime.
To prove this language is not regular, we instead examine the complement
because the set of regular languages is closed under complement. Assume
that the set is regular. Let p be the pumping lenght of the language. Then,
according to the pumping lemma, we break the string s=0p into s=xyz where y
has positive length. Then, s=xyiz = 0p + (i-1) |y| must also be in the set for any i.
In particular let i = p+1. Then xyp+1z=0p+p|y| must be in the set so p + p|y| = p(1
+|y|) must be prime. Thus we have a contradiction and the set cannot be
regular.
3. Find the Flaw in the Proof
The flaw in the proof is the following: if p is the pumping length of 0*1* and s = 0 p1p
is decomposed into xyz in the usual manner then s absolutely positively can be
pumped since xyjz is equal to 0p+(j-1)|y|1p which will still be in 0*1*.
4. A counterexample to the converse of the pumping lemma
The pumping lemma only states that if a language is regular, then it can be pumped.
The converse, if a language can be pumped, then it is regular, is not necessarily
true, so irregular languages may satisfy the pumping lemma's conditions without
contradicting it. In this particular case of
{aibjck | i,j,k >= 0,if i=1 then j=k},
if s is any string in the given language then the decomposition s = xyz with x = the
empty string, y = to the first character of s, z = the rest of s, will always work with the
pumping lemma. The resulting xyjz will always be in the language.
5. Decision Algorithms
a. Contains all strings of the form 0*1*.
Given a DFA, M, we can determine if it accepts all strings of the form 0*1* by
taking its complement and intersecting it with the DFA which accepts 0*1*,
and then minimizing the resulting DFA. If the result is the machine that
accepts nothing, then accept M, otherwise reject M.
b. Is co-finite.
Given a DFA, M, we can determine if it is cofinite by taking its complement
and then checking to see whether the result has any cycles from which an
accept state is reachable. If it does not, then accept M, otherwise reject M.
c. Has at least one string w that has 111 as a substring.
Given a DFA, M, we can determine if it accepts at least one string of the form
111 by intersecting it with another DFA which accepts (0+1)*111(0+1)* and
minimizing the resulting DFA. If the result accepts anything, then accept M,
otherwise reject M.
6. Regular Linear Grammars
a. A regular grammar to generate the set of strings that are evenly divisible by 5
A0 -> 0A0 | 1A1 | e
A1 -> 0A2 | 1A3
A2 -> 0A4 | 1A0
A3 -> 0A1 | 1A2
A4 -> 0A3 | 1A4
b. A finite automaton can be converted into a right-linear grammar as follows:
1. Reverse all the arrows in the finite automaton.
2. Create a start state with epsilon transistions pointing towards each of the old
accept states.
3. Change the old start state to be the accept state.
4. Convert the automaton as you would to get a left-linear grammar, except that
terminals are placed on the right side. (ie A->Ba instead of A->aB)
7. Linear Grammars and Palindromes
a. Since we know that left and right linear grammars describe only regular languages
and the set of palindromes over the language {0,1} is not regular, neither grammar
can describe a palindrome.
b. If we allow a mixed left-right linear grammar, we can describe the palindromes as
follows:
S -> 0A | 1B | 0 | 1| e
A -> S0
B -> S1
8. Single Symbol Regular Languages
a. Languages of the form 0mx+b are accepted by DFAs of the form below, so they must
be regular.
b. Unions of machines from the set described by (a) are regular, though not of the form
0mx+b.
c. Extra Credit: This is actually fairly simple using the DFA characterization of regular
sets. Since there can only be one transiton from each state the DFA is completely
specified by giving
i.
The number of states, n, in the machine.
ii.
An n-bit number, f, which tells us whether each state is final or not.
iii. A periodicity number, m, which has some value 0 <= m <= n
The DFA is constructed by making state 1 the initial state, setting the j-th
state to accept if and only if the j-th bit of the number f is a 1, adding a
transition on 0 from state j to state j+1 for 1<= j <= j-1 and finally if m is not
zero, adding a transition from state n to state n-m+1. The language
constructed from this DFA will finite if m=0, and otherwise will be eventually
periodic, of period m.
It pretty easy to see from the above description that, up to a finite set, every
regular language over 0 is of the form
(0b_1 + 0b_2 + ... + 0b_k)(0m)*,
for some choice of m, where {b_1, b_2, ..., b_k} is some subset of {0,1,2,...,m1}
9. (extra credit) Simulating NFAs
10. (triple extra credit) Minimizing DFAs
Month 8: Theory of Computation
Problem Set 3 Solutions - Mike Allen
1. 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}
d. (0+1)* - {ww | w in {0,1}*} (complement of ww)
2. DPDAs
Construct deterministic pushdown automata to accept the following languages.
a. {10n1n | n>0} U {110n12n | n>0}
b. Binary strings tha contain an equal number of 1s and 0s.
c. Binary strings with twice as many 1s as 0s.
d. Binary strings that start and end with the same symbol and have the same number of
0s as 1s.
3. CFGs
Construct context free grammars to accept the following languages.
a. (2.4b) {w | w starts and ends with the same symbol}
S -> 0A0 | 1A1
A -> 0A | 1A | e
b. (2.4c) {w | |w| is odd}
S -> 0A | 1
A -> 0S | 1S | e
c. (2.4d) {w | |w| is odd and its middle symbol is 0}
S -> 0 | 0S0 | 0S1 | 1S0 | 1S1
d. {0n1n | n>0} U {0n12n | n>0}
S -> 0A1 | 0B11
A -> 0A1 | e
B -> 0B11 | e
e. {0i1j2k | i!=j or j!=k}
S -> AC | BC | DE | DF
A -> 0 | 0A | 0A1
B -> 1 | B1 | 0B1
C -> 2 | 2C
D -> 0 | 0D
E -> 1 | 1E | 1E2
F -> 2 | F2 | 1F2
f. Binary strings with twice as many 1s as 0s.
S -> e | 0S1S1S | 1S0S1S | 1S1S0S
4. Ambiguity
a. Explain why the grammar below is ambiguous.
S -> 0A | 1B
A -> 0AA | 1S | 1
B -> 1BB | 0S | 0
The grammar is ambiguous because we can find strings which have multiple
derivations:
S
S
0A
0A
0 0AA
0 0AA
00 1S 1
00 1 1S
001 1B 1
0011 0A
0011 0 1
00110 1
b. (extra credit)
c. Demonstrate that G (see problem set) is ambiguous.
The ambiguity is primarly due to the following rules:

IF-THEN -> if condition then STMT

IF-THEN-ELSE -> if condition then STMT else STMT
If we start with an IF-THEN statement and substitute an IF-THEN-ELSE for
the consequent, we get:
if condition then if condition then STMT else STMT
However, if we start with an IF-THEN-ELSE clause and substitute an IFTHEN for the consequent, we get the same thing. So, it is ambiguous. Note
that many real programming languages (such as C and Java) exhibit this
exact ambiguity in their syntax.
d. (extra credit)
5. Converting to Normal Form
a. Put the following grammar into Chomsky Normal Form.
S -> A | AB0 | A1A
A -> A0 | e
B -> B1 | BC
C -> CB | CA | 1B
Remove all e rules
S -> e | A | AB0 | A1A | B0 | A1 | 1A
A -> A0 | 0
B -> B1 | BC
C -> CB | CA | 1B
Remove unit rules
S -> e | A0 | 0 | AB0 | A1A | B0 | A1 | 1A
A -> A0 | 0
B -> B1 | BC
C -> CB | CA | 1B
Convert remaining rules into proper form
S -> e | A0 | 0 | AS1 | B0 | A1 | 1A
A -> A0 | 0
B -> B1 | BC
C -> CB | CA | 1B
S1 -> B0 | 1A
S -> e | AN0 | AS1 | BN0 | AN1 | N1A
A -> AN0 | 0
B -> BN1 | BC
C -> CB | CA | N1B
S1 -> BN0 | N1A
N0 -> 0
N1 -> 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 | CB
A -> C | D
B -> 1B | 1
C -> 0C | 0
D -> 2D | 2
Converts to
S -> 0C | 0 | 2D | 2 | CB
A -> C | D
B -> 1B | 1
C -> 0C | 0
D -> 2D | 2
A is now useless and can be removed.
6. Derivations in Chomsky Normal Form
a. (2.19) Show that if G is a CFG in Chomsky normal form, then for any string w in
L(G) of length n>=1, exactly 2n-1 steps are required for any derivation of w.
We begin with a single nonterminal ("S") and form the string by making
substitutions according to the rules. Each rule of the form A->BC adds a new
nonterminal, and each rule of the form A->a converts one nonterminal into a
terminal. Since it takes n-1 steps to grow our string from the original
nonterminal to n nonterminals, and then n steps to convert each of those
nonterminals into terminals, it takes 2n-1 steps to derive a string of length n.
b. (2.20) Let G be a CFG in Chomsky normal from that contains b variables. Show that
if G generates some string using a derivation with at least 2b steps, then L(G) is
infinite.
L(G) is infinite if any nonterminal shows up in a reduction of itself because this
indicates a loop in the grammar. In the largest possible derivation, starting
with the start variable ("S") we replace it with two other variables and each of
those are replaced with two other variables and so on. Since no variable can
be repeated along any branch of the tree without creating a cycle, the tree will
be depth b and will contain 2b-1 nodes. So, if a derivation requires 2b or more
steps, the grammar must contain a cycle and L(G) must be infinite.
7. Two Way PDAs
Show that the set {0n1n0n | n>0} is accepted by a 2-way PDA.
This 2-way PDA works by moving right across the string to make sure it begins with
0n1n. Then it moves left to the beginning of the 1s and continues to the right to check
for 1n0n.
Month 8: Theory of Computation
Problem Set 4 Solutions - Rusty Chris, Dimitri Kountourogiannis, and Mike Allen
1. Context Free or Not
a. CF. Here is a grammar that will generate the language.
b.
S -> 1A0
c.
A -> 1A0 | B
d.
B -> CC
e.
C -> 0D1
f.
D -> 0D1 | e
g. Not CF. Assume for the purpose of contradiction that it is. Then let the pumping
length be p. Consider the string s=0p1p#0p1p, which is in the language. If we
decompose s into s=uvwxy as in the statement of the pumping lemma, there are three
cases to consider.
1. If vwx is contained in the first half of the string s, then pumping up even once
(that is, taking the string uv2wx2y) will give us a string where the first half is
longer than then second half, which means it can't be a substring of the second
half.
2. If vwx is contained completely in the second half, then when we pump down
(that is, take the string uwy). Then the second half will be shorter than the first
half (since |vwx|>=1) and so the pumped down string will not be in the
language.
3. If vwx overlaps with the symbol #, then it must be the case that the # is
contained in w, or else pumping up would give too many # symbols. So either
the v will be a string of 1's or the x will be a string of 0's or both (We need the
fact that |vwx| <= p here). If v is a nonempty string of 1's then pumping up
will give the left half more 1's then the right side, so the left side will not be a
subset of the right side. If x is a nonempty string of zeros then pumping down
will give the right side fewer zeros than the left so the left side will not be a
subset of the right side.
In any case we come to the conclusion that no matter how we choose the
decomposition, the conditions of the pumping lemma will be violated. So the
language cannot be regular.
h. Not CF. Suppose it is. Then let the pumping length be p. Consider the string
s=0p1p01p, which is in the language. If we decompose s into s=uvwxy as in the
statement of the pumping lemma, then an argument like the previous one shows that
if vwx is anything but the lone zero between the ones, then pumping up will give you
something that is not in the language. On the other hand if vwx is the lone zero, then
pumping down will give you. 0p1p1p, which is not in the language. This contradicts
the pumping lemma, so therefore the language is not context free.
i. CF. Here is a grammar that generates the language, with some comments on the side
to explain what each non-terminal represents.
j.
S -> ABA |
k.
A -> e | AC
l.
C -> 01 | 0C1
m.
B -> 0B1 | D | F ; 0^m1^nm!=n
n.
D -> 0 | 0D
; one or more 0s
o.
F -> 1 | 1F
; one or more 1s
; 0^n1^n
Why this works: Since R=(00*11*)(00*11*)* is regular, the complement ~R includes
all strings which are not even of the correct form. We can Union 0*1* complement...
nondeterministically guess which pair mismatches.
2. Decision Algorithms
a. If it were a non-deterministic machine, then the problem would be undecidable, but
since Deterministic machines are closed under complement, we can do this.
Complement the machine. Then form a grammar from the machine. Then check
whether the start symbol is useless. If the start symbol is useless then the
complemented machine has an empty language and so the original machine generates
every string. If the start symbl is not useless, then the original machine does not
generate everything.
b. Convert the grammar to Chomsky Normal Form. Check all possible derivations of
2n-1 steps, where n is the length of the string z in the language.
c. We need to see if the language has any intersection with 1*. Convert the CFG into a
PDA called M.Since 1* is regular and Context Free Languages are closed under
intersection with regular languages, we can intersect the DFA that generates 1* with
M. Convert the resulting machine back to a Grammar. Check if the resulting language
is empty by testing whether the start symbol is useless. Another way to do this is to
put the grammar in Chomsky Normal form, and then compute all derivations of
length less than or equal to 2|V|+1, where |V| is the number of nonterminal variables in
the CNF grammar. If no 1* is derivable in this many steps, then none will ever be.
3. Closure Problems of CFLs
a. The intersection of a PDA M1 and an FSM M2 is constructed in a manner analogous
to how two FSMs are intersected. The stack of the constructed PDA is used to
simulate the stack of M1.
Note the accept state is not reachable in this case, which is good since the
intersection of the two languages is empty.
b. This is trivial because we can take any CFL that is not regular and intersect in with
any Regular language that contains it, for example we can let L= {0n1n | n >=0} and R
= (0+1)*. Then L intersected with R is just L again, which we know to be nonregular.
c. We modify the DFA for L so that the stack to supplies the input to the machine and to
each state we add a gizmo that reads in string input and pushes it onto the stack 3
characters at a time.
4. Parsing and the CYK Decision Algorithm
a. The CYK parse table for 00000
b.
| 1
2
3
4
5
c.
---|---------------------------------------
d.
1 | A,C A,C A,C A,C A,C
e.
2| B
f.
3 | S,A,C S,A,C S,A,C
g.
4| B
h.
5 | S,A,C
B
B
B
B
Here we conclude that 00000 is derivable from the start symbol. Since S is in the fifth
row, first column of the table.
The CYK parse table for 000000
| 1
2
3
4
5
6
---|--------------------------------------------1 | A,C A,C
2| B
B
A,C A,C A,C A,C
B
B
B
3 | S,A,C S,A,C S,A,C S,A,C
4| B
B
B
5 | S,A,C S,A,C
6| B
Here we conclude that 000000 is not derivable from the start symbol. Since S is not in
the sixth row, first column of the table. It's not so hard to see that all strings of zeros
of odd length >= 3 are derivable from this grammar.
i. An NPDA for the grammar above.
6. Turing Machine Basics
a. (text 3.1a) Run M2 on "0"
b.
q10-
c.
-q2-
d.
--qaccept
e. (text 3.1c) Run M2 on "000"
f.
q1000-
g.
-q200-
h.
-xq30-
i.
-x0q4-
j.
-x0-qreject
k. (text 3.2a) Run M1 on "11"
l.
q111-
m.
-q31-
n.
-1q3-
o.
reject
p. (text 3.2d) Run M1 on "10#11"
q.
q0110#11-
-q090#11-
r.
-q030#11-
-0q09#11-
s.
-0q03#11-
-0#q1111-
t.
-0#q0511-
-0q12#x1-
u.
-0#1q051-
-q120#x1-
v.
-0#11q05-
q12-0#x1-
w.
-0#1q071-
-q130#x1-
x.
-0#q0711-
-xq08#x1-
y.
-0q07#11-
-x#q10x1-
z.
-q070#11-
-x#xq101-
aa.
q07-0#11-
reject
7. Turing Machine Design
a. To accept odd-valued binary strings, we only have to look at the last bit.
The TM moves right until it reads a blank, moves left one space and accepts
if and only if there is a 1 on the tape.
b. Simulate adding a and b, marking the digits right to left as we go and
verifying the digits of c.
1. Add a $ to the beginning of the tape and shift the rest of the input.
2. Move right past the input string, write another # and a 0. This is the
carry bit. Move left until you hit the $.
3. Move right to the first #, then move left until you find an unmarked
digit.
4. Remembering this digit, move right past a #.
5. Move right to the second #, move left until you find an unmarked
digit.
6. Remembering this second digit, move right past the second #.
7. Move right past the last #.
8. If the two bits read, plus the carry bit, are equal 2 or 3, write a one to
the carry bit.
9. Move left past the # and stop at the first unmarked bit. If the the
remembered bits plus the old carry bit are equal to 1 or 3, check for a
1 under the head, and mark it. Otherwise, check for a zero, and mark
it. Reject if the check fails.
10. Move left to the $, and repeat from 3.
11. When there are no unmarked digits in a and b, move to the carry bit.
12. If the carry bit is 1, check for an unmarked 1 in c. Accept as long as
there are no unmarked 1s in c.
c. This solution is a bit non-intuitive, because it does not construct the odds by
adding one each time, but by constructing all possible odd-valued n-bit
strings by prepending 0 and 1 to all odd-valued n-1-bit strings. The resulting
machine, though, is considerably simpler.
1. Write $*1* to the tape
2. Move to the left pass a * and until you hit another *.
3. Moving right, copy symbols to the end of the tape, replacing the first
* with *0, and #s with #0. Stop when you encounter a second *. Don't
copy it.
4. Move left back past two stars and stop on the third.
5. Copy the string between the stars again to the end of the tape, this
time prefixing each bit with a 1. Also, this time write a # instead of a
leading *. Stop when you get to the second *, but copy it to the end of
the tape.
6. From the end of the tape, repeat back to step 2.
8.
Turing Decidability and Recognition
Show that the set of decidable languages is closed under...
a. (text 3.14a) UNION.
Create a TM M from the two input machines M1 and M2 where:
M = "On input w:
1. Run M1. If it accepts, accept.
2. Run M2. If it accepts, accept.
3. Otherwise, reject."
Since M1 and M2 are decidable, we are guaranteed that they will
accept or reject in finite time (ie they will not loop forever). So, TM M
will be decidable.
b. (text 3.14d) COMPLEMENTATION.
We again create a TM M from the input machine M1 where:
M = "On input w:
1. Run M1. If it accepts, reject.
2. Otherwise, accept."
Show that the set of recognizable languages is closed under...
c. (text 3.15a) UNION.
Create a TM M from the two input machines M1 and M2 where:
M = "On input w:
1. Run M1 and M2 in parallel. If either accepts, accept.
2. Otherwise, reject."
Since M1 and M2 might not ever reject, we must run them in parallel
to guarantee that if either of them accepts, we can accept. Note that
step 2 might never be reached, but that is okay, since if we only
recognize the language, we do not have to reject.
d. (text 3.15c) STAR.
Create a TM M from the input machine M1 where:
M = "On input w: In parallel:
1. Enumerate all the strings that M1 accepts onto a list L1
2. Create a list L2 which contains the * of all the items in L1. If w
appears on L2, accept."
We know that we can enumerate all the strings that M1 accepts to
create L1, but creating the * of that list is much trickier. We must
choose an ordering that reaches any string in finite time. One
possible ordering would be to include the strings made by repeating
the first n strings of L1 up to n times letting n go from 0 to infinity.
Since all strings in the *'d language will eventually appear on L2, we
can accept w if it is in the language.
Month 8: Theory of Computation
Problem Set 5 Solutions - Mike Allen
1. Name that Language
a. DCFL. {0n1m0p1q | n+m = p+q and n,m,p,q > 0}
b. DCFL. {0n1m0m1n | n,m > 0}
c. regular . {0n1m0p1q | n,m,p,q > 0}
d. CFL. The set of strings over alphabet {0,1,2} with an equal number of 0s and 2s or
an equal number of 0s and 1s.
e. regular. {0m | m = 2k+1 where k>0}
f. regular. The set of strings with 3n 0s and 4m 1s for m,n > 0.
g. DCFL. The set of strings with at least ten times as many 0s as 1s.
h. regular. The set of strings that are either odd length or contain 5 consecutive 1s.
i. TM. {0m10m! | m>0}
j. DCFL. The set of stings over alphabet {0,1,2} where the number of 1s equals the
number of 2s and every 0 is followed immediately by at least one 1.
3. An Undecidability Problem
Prove that the problem of determining if the languages generated by two CFGs are
equal is undecidable.
We show this by reducing the problem of determining whether a CFG accepts
everything to the problem of determing if the languages generated by two CFGs are
equal by taking our input CFG and comparing it to the CFG that generates
everything. Since we know from class that the "everything" problem is undecidable,
the "equal" probably must also be.
4. R.E. or not?
Determine for each of the following languages whether whether or not it is
recursively enumerable and whether the complement is or is not.
a. no yes The language of all TMs that accept nothing.
We need to try an infinite number of strings in a TM to determine that it
accepts nothing, but we only need to find a single string that it accepts to
show that it accepts something.
b. no no The language of all TMs that accept everything.
We need to try an infinite number of string in a TM to determine whether it
accepts everything, and we may or may not need to if it does not.
c. no no The language of all TMs that accept Regular languages.
We can enumerate all possible regular languages, but testing every regular
language against every TM would take forever. If fact, even testing a TM
against a single infinite regular language would take forever.
d. no yes The language of all PDAs that accept everything.
We would have to try every string before declaring that a PDA accepts
everything, but since membership in a CFG (equivalent to a PDA) is
decidable, we determine that a PDA does not accept anything once it rejects
anything.
e. yes no The language of all CFGs that are ambiguous.
We can determine that a CFG is ambiguous by finding a single string which
has an ambiguous derivation, but we cannot determine if a CFG is
unambiguous unless we try everything string in it.
5. A Refutation of the Halting Problem?
Consider the language of all TMs that given no input eventually write a non-blank
symbol on their tapes. Explain why this set is decidable. Why does this not conflict
with the halting problem?
The state of a Turing machine is determined by the state of its controller (a DFA)
and the state of the tape. Since the controller is finite, there are only a finite number
of states possible before the TM is forced to either loop or write a symbol, so we
simply run the Turing machine for that number of steps and then accept if it has
written a symbol and reject otherwise.
This does not conflict with the halting problem because we are considering only a
subset of Turing machines (those roughly equivalent to DFAs) and not really
addressing the halting problem for Turing machines.
6. PCP for One-Character Strings
Prove that the Post Correspondence Problem is decidable for strings over the
alphabet {0}.
We can simplify this problem by assigning each domino a value which is the number
of 0s on the top row minus the number of zeros on the bottom row. Our goal, then, is
to choose dominos whose values sum to zero. We consider a few cases:
1. If the set contains a "0" domino, that domino alone is a legal solution, so we accept.
2. If the set contains a "+m" and "-n" domino, we construct a solution from n "+m"s and
m "+n"s and accept.
3. If the set contains only "+" dominos or "-" dominos we cannot add them to zero, so
we reject.
Since we can accept or reject based on a quick examination of the set of dominos,
the problem is decidable.
7. Satisfiablity for DNF Formulas in P
Prove that the problem of determining whether there is a T/F assignment that makes
a given DNF true can be solved in polynomial time. How is it possible that CNF is
still NP-complete?
A DNF takes the following form
( .. ^ .. ^ ..) V (.. ^ .. ^ ..) V ...
Since the terms are OR'd together, if any of them are true, the entire expression will be true,
so determining whether a DNF is satisfiable is the same as determining if any of the terms is
independently satisfiable. A term is satisfiable if it does not contain any incompatible values
(ie xn and not xn). So, we can check for satisfiability by a simple scan of the input which can
easily be accomplished in polynomial time.
Even in light of this property of the DNF, the CNF can still be NP-complete as long
as the reduction from CNF to DNF cannot be performed in polynomial time.
8. A Punchcard puzzle that is NP-complete.
(text 7.26) Prove that the Punchcard Puzzle is NP-complete.
We show that the problem is in NP by showing that it is verifiable in polynomial time.
To do this, we simply stack the cards according to the answer presented in the
certificate to determine if they cover all the holes. This can be accomplished easily in
polynomial time.
To show that it is NP-complete, we reduce 3-SAT to it. We create cards {x1,x2,...} for
each variable in the 3-SAT formula and create a hole position in each column for
each term of the formula. Then, we punch holes in the left column of the card in
every position which corresponds to a term that does not contain that card's variable
and in the right column for every term which does not contain that card's variable's
complement. The Punchcard problem is only satisfiable if every hole can be covered
by one of the card which implies that every term in 3-SAT problem is satisfiable.
9. PSPACE-hard implies NP-hard
(Text 8.6) Show that any PSPACE-hard language is also NP-hard.
First we must show that the language is not in NP. This is trivial since NP is a subset
of PSPACE, and therefore, anything outside of PSPACE is also outside of NP.
Then we must show that any problem in NP can be reduced to any PSPACE-hard
language. This is also fairly simple, since any SAT problem can be reduced to a
TQBF problem by simply appending "there exists xn" to the front of the SAT
expression for each variable xn and then solving it using the TQBF algorithm. Then
the TQBF problem can be reduced to any PSPACE-hard problem by the definition of
PSPACE-hard because TQBF is PSPACE-complete. Thus, any PSPACE-hard
problem is also NP-hard.
10. A TIC-TAC-TOE game in PSPACE
Show that go-moku is in PSPACE.
A quick examination of the rules of the game makes it obvious that you can solve
GM by using the MiniMax search algorithm on the game tree. Since the game tree
has depth n2 and only one branch of computation must be stored at a time, the
space necessary is O(n2) which is in PSPACE.
NOTE: This is a lot easier than determining whether it is PSPACE-complete.
11. A Punchcard Puzzle which is PSPACE-complete
Show that the two-person game variation Punchcard Puzzle is PSPACE-complete.
First we must show that the game is in PSPACE, which is easy, since it can be
solved with a MiniMax search of the game tree. Since only one branch of
computation is stored at a time, the storage is polynomial and the game is in
PSPACE.
Then, we can show that it is PSPACE-complete by reducing the FORMULA-GAME
to it. We begin with our formula and insert quantifiers with dummy variables so the
quantifiers take on the repeating form (there-exists, for-all). Now, we create a card
for each quantifier/variable and create a hole position in each column for each term
of the formula. We punch holes in the left column of the card in every position which
corresponds to a term that does not contain that card's variable and in the right
column for every term which does not contain that card's variable's complement.
Now, we play the Punchcard game with the cards by choosing which way each card
is flipped, which corresponds to choosing the T/F value of each variable in the
formula game.
Month 8: Theory of Computation
Quiz 01 - Prof. Shai Simonson
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.
c. Write a linear grammar where each right side is of the form aB or a.
(“a” a terminal and “B” a non-terminal) to generate the set.
2. More Machines. (5 points)
Draw a finite state machine that accepts the complement of the language accepted
by the non-deterministic machine below:
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.
b. The set 1m0n1m+n, for m and n greater than or equal to one.
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.
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.
b. Triple(L) = {x | x=uvw, such that u, v, w are in L, and |u| = |v| = |w|}.
5. Decision Algorithms. (5 points)
Give a decision algorithm to determine whether a regular language L1 has one or
more strings in common with the language described by the regular expression [00 +
11 + (01 + 10)(00 + 11)*(01 + 10)]*.
Month 8: Theory of Computation
Quiz 01 Solutions - Mike Allen
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:
answer:
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.
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. Decision Algorithms. (5 points)
Give a decision algorithm to determine whether a regular language L1 has one or
more strings in common with the language described by the regular expression [00 +
11 + (01 + 10)(00 + 11)*(01 + 10)]*.
1.
2.
3.
4.
Obtain a DFA for L1
Obtain a DFA for the regular expression [00 + 11 + (01 + 10)(00 + 11)*(01 + 10)]*
Intersect the two DFAs.
If the result accepts anything, accept; Otherwise reject.
Ars Digita University
Theory of Computation
Recitation 1, 05/03/01
Topics



Languages
All computational problems can be reframed as questions of membership in a language (set).
Drawing Deterministic Finite Automata (DFAs).



Drawing Nondeterministic Finite Automata (NFAs).
Converting NFAs to DFAs
Regular Languages, Closure properties of Regular languages.
Problems to work on
1.
2.
3.
4.
5.
6.
7.
8.
What is a language?
Draw a DFA that accepts the strings ending in 01.
Draw a DFA that accepts the strings that have an even number of zeros.
Draw a DFA that accepts the strings that have an even number of zeros or end in 01.
Draw a DFA that accepts the strings that have an even number of zeros and end in 01.
Draw a DFA that accepts the strings that have an even number of zeros but don't end in 01.
Draw an NFA that accepts the strings that end in 01. Try to make it as simple as possible.
Draw an NFA that accepts the strings that have an even number of zeros or end in 01 using
six states.
9. Draw an NFA that accepts any string that is a concatenation of a string that has an even
number of zeros with a string that ends in 01.
10. Draw an NFA that accepts the strings that have an even number of zeros and end in 01.
11. Draw an NFA that accepts the language 0, using only two states.
12. Convert the previous NFA into a DFA using the method described in class.
13. Draw an NFA with two states that corresponds to the language 0?2*.
14. Convert the previous NFA to a DFA.
15. What is a regular language?
16. Draw the DFA that corresponds to the language 10*10*.
17. What is the Prefix of the language corresponding to the language 10*10*.
18. Draw the DFA for the prefix of the language corresponding to the regular expression 10*10*.
19. What is the MIN of the language 10*10*?
20. Draw the DFA corresponding to the MIN of the language 10*10*.
Ars Digita University
Theory of Computation
Recitation 2, 05/04/01
Main Topics




More practice with NFAs, Converting to DFAs, getting rid of epsilons.
Formal defintion of Automata
The math mumbo-jumbo formulation is: Some five-tuple (Q, Sigma, delta, q, F)....
But that's not as bad as it seems. This is just some object that contains four other objects and
a method. Which one is the method?
Operations on Languages: Our friends PREFIX and MIN
An example Proof. Just so you have a reference, we'll show you how to "prove" that Regular
languages are closed under difference, ie. L1-L2 is regular if L1 and L2 are.

Let M1 = (Q1, Sigma, delta1, q1, F1) accept L1, and let

Let M2 = (Q2, Sigma, delta2, q2, F2) accept L2

Then

M = (Q1xQ2, Sigma, delta, (q1,q2), F1 x(Q2-F2)) accepts L1-L2

where delta(r1,r2,a) = (delta1(r1,a), delta2(r2,a))
At this point you should give a few words of justification about why you think this new
machine would accept L1-L2. If the mathematical notation is just way to much for you, just
try to explain things in intuitively.

Regular expressions
If we run out of things to talk about :-)



Generalized NFAs
NFA + reg. ex. transitions = GNFA
Converting NFAs to Regular expressions
Converting Regular expressions to NFAs
Problems to work on
1. Write out the NFA corresponding to the language and convert to a DFA: All strings that end
in 0.
2. Same for all strings whose penultimate character is 0.
3. If you like this sort of thing: same things for all strings that that have 0 as the third to the last
character.
4. One more if you'd like the practice: All strings ending in {0,1}
5. Write out the NFA corresponding to this state/transition table:
6.
0
1
7. ->p
{p,q}
8.
q
{r}
{r}
9.
r
{s}
{}
10. *s
{s}
{s}
{p}
Here the star means we have an accept state
11. Write regular expressions for all strings that end in aaba. Where Sigma is just the usual
alphabet.
12. Do the same for all strings that do not contain a.
13. Write a regular expression for the strings that contain the substring 0101, where Sigma =
{0,1}.
14. Do the same for all strings that alternate between 0 and 1 and end in 0.
15. Do the same for strings that alternate between 0 and 1.
16. Hard problem: Write a regular expression for the strings over {0,1} that are divisible by 3.
17. Hard Problem: Build an automaton that recognizes the strings that have the same number of
zeros and ones.
Ars Digita University
Theory of Computation
Recitation 3, 05/06/01
Topics






The Java Computability Toolkit.
More on Regular Expressions.
GNFAs and converting an NFA to a regular expression.
Converting a Regular Expression to an NFA.
Diagonalization.
The Pumping Lemma.
Problems to work on
1. (Warm up) Give one string that is in the language and one that is not:
1. a*b*
2. a(ba)*b
3. a* + b*
4. (b + aaa)*
5. aba+bab
2. Write a regular expression
a. That accepts the set of all strings.
b. That accepts the set of all strings not containing 00 as a substring.
3. Convert to regular expressions:
b)
a)
d)
c)
e)
f)
g)
4. Convert to an NFA:
0. (001)*(1+e)
1. (01*0 + e + 00)
2. (001)*(1+e)(01*0 + e + 00)
5. How long does a string in the following language have to be before we are guaranteed to
have a loop?
6. Is the set of all strings that are palindromes regular? Why or why not?
7. Show that the set of all strings of zeros that have length that is a perfect cube can not be
described by a regular expression.
Ars Digita University
Theory of Computation
Recitation 4, 05/08/01
Topics





How to minimize DFA's and why one would.
Turning a DFA into a Grammar
Decision Problems.
Some applications of Regular expressions
Everything is a string / number.
Problems to work on
1. Why do we care about minimizing DFA's ?
2. Minimize the following DFA. What Language does it generate?
3. Minimize the following DFA. What Language does it generate?
4. Minimize the following DFA. What Language does it generate?
5. Give a regular grammar that generates the same language as the DFA in problem 2.
6. Give a regular grammars that generates the same language as the following regular
expressions:
a. 010*
b. a(ab + bc)*
7. Is the following a valid argument? Why or why not? The language L = {0m1m 2n | m, n >= 0 }
is not regular, since we already know that M = { 0m 1m | m >= 0} is not regular by the
pumping lemma, and since L contains M it cannot therefore be regular.
8. Prove that the language { x2n | n >= 0 } is not regular.
9. Give a decision algorithm which takes as input a regular language L and decides whether L
contains the string 1000101.
10. Give a decision algorithm which takes as input a regular language L and decides whether L
equals the language {1000101}
11. Give a decision algorithm which takes as input a regular language L and decides whether L
contains the language given by the regular expression 10*10*1
12. Miscellaneous problem: Eliminate the espilon transitons of this NFA, without converting it to
a DFA (ie, figure out an easier way)
13. Mildy Challenging and Surprising Problem: Let L be any language over the alphabet {0}, not
necessarily regular. Show that L* is regular.
14. Wicked hard challenging problem: Describe a grammar that generates the language { 0p | p is
a prime }
Ars Digita University
Theory of Computation
Recitation 5 , 05/09/01
Topics


Decision Problems
Context free grammars.
Problems to work on
Decision Problems
1. Finish the decision problems form the last recitation handout.
CFG warmup
2. Give a context free grammar that generates the language of palindromes {w(0+1+epsilon)wR
| w is in (0 + 1)*}
3. Give a context free grammar that generates every possible string over {0,1}
4. Give a context free grammar that generates the language 0n1 0n 111 0m 1r where n, m, r >= 0
5. Give a context free grammar that generates the language of all strings that contain 101.
6. Give a context free grammar that generates the language of all strings with an equal number
of zeros and ones.
And/Or
7. Give a context free grammar that generates the language that has equal numbers of 0's and 1's
or contains 101.
8. Give a context free grammar that generates the language that has equal numbers of 0's and 1's
and contains 101.
Complements
9. Give a context free grammar that generates the language of all strings of the form 0m1n where
n >= m >= 0.
10. Give a context free grammar that generates the language of all strings of the form 0m1n where
n > m >= 0.
11. Give a context free grammar that generates the complement of the language 0*1*.
12. Give a context free grammar that generates the language { w | w is not equal to 0n1n for any
choice of n}
13. Challenging problem: Give a context free grammar that generates the language { w | w is not
a palindrome}
Ars Digita University
Theory of Computation
Recitation 6, 05/10/01
Topics


Rigorous proofs using the pumping lemma.
More constructions with context free grammars.

Chomsky Normal Form.
Problems to work on
1. Finish working on the CFG problems from the lst handout.
2. (Warmup). Generate the grammar for 0*1(0+1)*.
3. Using the grammar above, What are the leftmost and rightmost derivation for the strings
1001, 0011? What are the parse trees?
4. (Text 2.13) Consider the following grammar.
5.
6. S --> TT | U
7. T --> 0T | T0 | 1
8. U --> 0U00 | 1
9.
Describe the language it generates.
Chomsky Normal Form
5. Put the following grammar in Chomsky Normal form. (Text 2.14)
6.
7. A --> BAB | B | epsilon
8. B --> 00 | espilon
9.
10. Put the following grammar in Chomsky Normal form.
11.
12. A --> BAB | B | BC
13. B --> 00 | epsilon
14. C --> AA
15.
Rigorous pumping lemma proofs
Problems from previous handouts kindly reproduced here. Rigorously prove using the pumping
lemma.
7. Prove that the language { x2n | n >= 0 } is not regular.
8. Show that the set of all strings of zeros that have length that is a perfect cube can not be
described by a regular expression.
Ars Digita University
Theory of Computation
Recitation 7, 05/11/01
Topics



Using regular expressions and context free grammars with Lex and Yacc to build complex
programs quickly. A calculator example.
Chomsky Normal Form (Finish problems on Chomsky Normal Form that we didn't get to last
time,
i.e. all of them ;-)
Push Down Automata.
Problems to work on
1. Give a grammar for the language {ai bj | i <= j <= 2i}.
2. Consider the grammar
3. S --> z A s
4. A --> xb B yz
5. B --> cd A q | epsilon
Play around with this grammar and try to guess what a pumping lemma for CFG's might look
like.
6. Construct a PDA for {0n1n | n >= 0} without looking at your notes.
7. Construct a PDA for {0n1m0m1n | n,m >= 0}
Ars Digita University
Theory of Computation
Recitation 8, 05/14/01
Topics
Today we'll mainly catch up on all the problems from previous handouts we haven't done yet.


Aisde: Languages as infinitely long streams of bits, complexity and compression
Leftmost and Rightmost derivations.




Parse Trees.
Chomsky Normal Form. What it is useful for.
Converting a grammar to Chomsky normal form.
Intro to the pumping lemma for context free languages.
Languages and streams o' bits
Here is a list (well, the beginning of a list) of all possible strings that can be made from the
characters a and b. They are ordered so that all strings of length 1 come first, then strings of length 2
then strings of length 3, ... etc.
e a b aa ab ba bb aaa aab aba abb baa bab bba bbb aaaa aaab aaba aabb abaa abab abba abbb baaa
baab baba babb bbaa bbab bbba bbbb aaaaa aaaab aaaba aaabb aabaa aabab aabba aabbb abaaa abaab
ababa ababb abbaa abbab abbba abbbb baaaa baaab baaba baabb babaa babab babba babbb bbaaa
bbaab bbaba bbabb bbbaa bbbab bbbba bbbbb aaaaaa aaaaab aaaaba aaaabb aaabaa aaabab aaabba
aaabbb aabaaa aabaab aababa aababb aabbaa aabbab aabbba aabbbb abaaaa abaaab abaaba abaabb
ababaa ababab ababba ababbb abbaaa abbaab abbaba abbabb abbbaa abbbab abbbba abbbbb baaaaa
baaaab baaaba baaabb baabaa baabab baabba baabbb babaaa babaab bababa bababb babbaa babbab
babbba babbbb bbaaaa bbaaab bbaaba bbaabb bbabaa bbabab bbabba bbabbb bbbaaa bbbaab bbbaba
bbbabb bbbbaa bbbbab bbbbba bbbbbb
A language is an infinite stream of bits. Example a(a+b)*
01 0 1 1 0 0 1 1 1 1 0 0 0 0
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
1
0
1
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
1
1
1
1
1
Example (ab)*
10 0 0 1 0 0 0 0 0 0 0 0 0 0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Problems to work on
Work on the problems from previous handouts
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Ars Digita University
Theory of Computation
Recitation 9, 05/15/01
Topics




How many languages are there?
Nondeterministic Pushdown Automata.
Deterministic Pushdown Automata.
Converting Context Free Grammars to Pushdown Automata.
Problems to work on
Languages
1. Could I ever write a computer program that enumerated (listed) all the possible languages?
What about one that enumerated all the regular languages? What about one that enumerated
all the context-free languages?
Pushdown Automata Warmup
2. What is the difference between a Nondeterministic Pushdown Automaton and a
Deterministic Pushdown Automaton. Do you think they generate the same languages?
3. (Warm up) Construct a PDA that accepts the language {0,1}*.
4. (Warm up from last time:) Construct a PDA that accepts the language {0n1n | n >= 0}. Is your
answer deterministic or not?
5. Construct a PDA that accepts the language {0n1n | n >= 2}.
More Pushdown Automata
6. Construct a PDA that accepts the language { 0m1n | n > m >= 0}
7. (From last time:) Construct a PDA that accepts the language {0n1m0m1n | n,m >= 0}.
8. Construct a PDA that accepts the language { w | w is not a palindrome}
And/Or
9. Construct a PDA that accepts the language { w | w is not a palindrome and w ends with a
zero}
10. Give a PDA that accepts the language {ai bj | i <= j <= 2i}
Deterministic vs Nondeterministic PDA's
11. Construct a NPDA that accepts the language of strings with the same number of zeros and
ones.
12. Construct a DPDA that accepts the language of strings with the same number of zeros and
ones.
Converting Grammars to PDA's
13. Convert the following Grammar to a PDA.
14. S --> AB
15. A --> 0
16. B --> 1
17. Convert the following Grammar to a PDA.
18.
19. A --> BAB | B | epsilon
20. B --> 00 | espilon
21.