Midterm 2 Review Solutions

CS235 Languages and Automata
Fall 2012
MIDTERM 2 REVIEW ANSWERS
Revised 12/03/12 to fix solns to Problem 1b(iv); revised 12/13/12 to change ap/2-1 to ap-1 in Prob. 1b(v) (thanks to Irene Kwok);
revised 12/14/12 to fix broken classification of NONEMTPYDIFFTM.
Problem 1: Regular, Context-Free, and Non-Context-Free Languages
Consider the following three languages:
Lx = {aibjck | i,j,k ∈ Nat and i ≤ j + k }
Ly = {aibjck | i,j,k ∈ Nat and i < j and i < k }
Lz = {w | w ∈ (a+b+c)* and every b in w appears after at least one a
and before at least one c }
Note: the parts of this problem are independent and can be done in any order.
Part a. One of the three languages (call it Lreg) is regular.
Subpart i: Which language is Lreg? Lz
Subpart ii: Show that Lreg is regular by defining a regular expression for it.
(a+c)* + (a+c)*a(a+b+c)*c(a+c)*
Subpart iii: Show that Lreg is regular by defining an FA for it.
Here are two solutions:
a,c
a a,b,c
a,b,c
a,c
a,c
P
a
Q
c
P
R
Q
c
Subpart iv: Show that Lreg is regular by defining a right-linear CFG for it.
We can just convert the above FAs to a right-linear CFGs:
P → % | aP | cP | aQ
Q → aQ | bQ | cQ | cR
R → % | aR | cR
P → % | aP | cP | aQ
Q → aQ | bQ | cQ | cP
Subpart v: According to the pumping lemma for regular languages, Lreg has a pumping
length p such that any string s in Lreg whose length is ≥ p can be decomposed into xyz
where |xy| ≤ p, y is nonempty, and xyiz ∈ Lreg for all i ∈ Nat. Suppose s = ajbkcn where
j,k,n > 0 and j+k+n ≥ p. Show three distinct ways to decompose the s ∈ Lreg into xyz
such that it satisfies this lemma. There are *many* answers. Here are some:
x=%
y= a
z = aj-1bkcn
x = aj
y= b
z = bk-1cn
x = aj-1bk
y= c
(Problem 1 is continued on the next page)
z = cn-1
1
Problem 1 (continued)
Part b. One of the three languages (call it LCFL) is context-free but not regular.
Subpart i: Which language is LCFL? Lx
Subpart ii: Show that LCFL is context free by defining a CFG for it.
There are many solutions. Here’s a simple one:
S → aSc | Sc | T
T → aTb | Tb | %
Subpart iii: Show that LCFL is context free by defining an NPDA for it.
a,%→a
%,%→$
%,%→%
b,a→%
b,%→%
,%→b
%,%→%
c,a→%
c,%→%
%,$→%
,
(Problem 1 is continued on the next page)
2
3
Problem 1 (continued)
Subpart iv: Prove that LCFL is not regular using the pumping lemma for regular
languages.
Towards a contradiction, suppose LCFL were regular. By the pumping lemma for regular
languages, there is a pumping length p such that the string s = apbpcp would be
pumpable --- i.e., parsable into xyz s.t. |xy| ≤ p, y is nonempty, and xyiz ∈ La for all
i ∈ Nat. s must be parsed as x = ak , y = am, z = anbpcp, where k,m,n ∈ Nat, m ≥ 1 and
k + m + n = p. But xyiz = ap+m(i-1)bpcp, which is not ∈ Lb for any i >1. So LCFL cannot
be regular.
Subpart v: According to the pumping lemma for context-free languages, LCFL has a
pumping length p such that any string s in LCFL whose length is ≥ p can be decomposed
into uvwxy where at least one of v and x is nonempty, |vwx| ≤ p, and uviwxiy ∈ LCFL for
all i ∈ Nat. Show two distinct way to decompose the string s = apbp/2cp/2 ∈ Lb into uvwxy
such that it satisfies this lemma.
u = ap-1
v=a
w=%
u = ap-1
v=a
w = bp/2
x=b
x=c
y = bp/2-1cp/2
y = cp/2-1
(Problem 1 is continued on the next page)
4
Problem 1 (continued)
Part c. One of the three languages (call it LTM) is not context-free.
Subpart i: Which language is LTM? Ly
Subpart ii: Using the graphical transition notation presented in lecture and in Sipser,
draw a Turing Machine that decides the language LTM. You need not show an explicit
reject state and may assume that any transition now shown goes to the reject state. You
may introduce new tape symbols if that is helpful.
The following TM accepts the language LTM. The horizontal transitions match as with
corresponding bs and cs, replacing the matched as, bs, and cs with xs, ys, and zs
respectively. The vertical transitions ensure there are extra bs and cs (in that order)
after the matching process is complete.
a,y→R
a→x,R
y→R
b→y,R
c→z,L
x→R
y→R
b→R
b,z→R
y,z,a,b→L
,
b→R
b→R
z→R
z→R
c→R
c→R
c→R
_→R
5
Subpart iii: Prove that LTM is not context free using the pumping lemma for context-free
languages.
Towards a contradiction, suppose LTM were regular. By the pumping lemma for contextfree languages, there is a pumping length p such that the string s = apbp+1cp+1 would be
pumpable --- i.e., parsable into uvwxy where at least one of v and x is nonempty, |vwx| ≤
p, and uviwxiy ∈ Lb for all i ∈ Nat. Because vwx has length at most p, it can “cover” at
most two different letters in apbp+1cp+1.
• In the case where v and x both cover a, pumping up (i >1) yields a string outside
Lb because the number of as will be ≥ the number of bs and ≥ the number of cs.
• In the case where v and x both cover b or c, pumping down (i =0) yields a string
outside Lb because the number of as will be ≥ the number of bs or ≥ the number of
cs.
• In the case where v covers a and x covers b, pumping up (i >1) yields a string
outside Lb because the number of as will be ≥ the number of cs.
• In the case where v covers b and x covers c, pumping down (i =0) yields a string
outside Lb because the number of as will be ≥ the number of bs and ≥ the number
of cs.
• In the case where v or x covers two distinct letters, pumping up (i =1) yields a
string outside Lb because it is not of the form a*b*c*.
6
Problem 2: A Context Free Language
Consider the following context-free grammar CFG2 with start symbol S.
S → aS | aSc | T
T → bT | bTc | U
U → Ub | %
Part a Show that CFG2 is ambiguous by drawing four parse trees for the string aabbcc.
Below is one parse tree.
S
a
We can get three more variations on this parse tree by combinations of
the following transformations:
S
a
S
c
S
a
S
a
T
b
S
S
T
c
S
c
a
T
T
b
a
S
T
c
T
T
U
b
T
b
c
T
%
b
T
U
c
b
T
U
There are even more possible combinations that involve (1) using S to generate both cs; (2)
using T to generate both cs; and (3) using U rather than T to generate “extra” bs.
Part b Using set builder notation, describe the set of strings in the language generated by CFG2.
{ w | w in {a,b,c}* and the number of cs in w is ≤ the number of as + the number of bs.}
7
Part c Write a grammar CFGunambig that generates the same language as CFG2 but is
unambiguous.
The reasons why CFG2 are ambiguous are (1) both S and T can generate cs that match as and bs
and (2) S, T, and U can all generate “extra” as and bs.
The following grammar CFGunambig removes the sources of ambiguity by (1) removing the
variable U, (2) introducing variants S’ and T’ of S and T, respectively, and (3) modifying the
productions for S and T. S’ and T’ can generate extra as and bs, respectively, but cannot
generated any cs. S must match as many as to cs as possible, since the production S → S’
enters a mode in which no cs can be matched (but extra as and bs can be generated). The
production S → T enters a mode in which T must match as any remaining cs unmatched by as
but matched by bs. The production T → T’ enters a mode in which any extra bs can be
generated.
S → aSc |
S’ → aS’ |
T → bTc |
T’→ bT’ |
S’ | T
T’
T’
%
Part d Draw the unique parse tree for aabbcc using CFGunambig.
S
a
c
S
a
c
S
T
T’
b
T’
b
T’
%
Part e. Convert CFG2 to Chomsky normal form. Show all steps of the conversion process.
Step 1: Add a new start variable
S0 → % | S
S → aS | aSc | T
T → bT | bTc | U
U → Ub | %
8
Step 2: Find nullable variables: {U, T, S, S0 }
Step 3: Remove nullable variables:
S0 → % | S
S → a | ac | aS | aSc | T
T → b | bc | bT | bTc | U
U → b | Ub
Step 4: Remove unit productions:
S0 → % | a | ac | aS | aSc | b | bc | bT | bTc | b | Ub
S → a | ac | aS | aSc | b | bc | bT | bTc | b | Ub
T → b | bc | bT | bTc | b | Ub
U → b | Ub
Step 4: Complete CNF:
S0 → % | a | AC | AS | AD | b | BC | BT | BE | b | UB
S → a | AC | AS | AD | b | BC | BT | BE | b | UB
T → b | BC | BT | BE | b | UB
U → b | UB
A→ a
B→ b
C→ c
D → SC
E→ TC
Part f. Show one parse tree for the string aabbcc using the Chomsky Normal Form grammar.
CNFs aren’t necessarily unambiguous and certainly aren’t here. Below is one of many solutions.
It corresponds to the first tree shown in part a.
S0
A
a
S
A
D
a
C
S
B
b
T
c
B
C
b
c
9
Problem 3: Undecidable Languages
Part a Consider the following languages:
NONEDFA = { (<DFA>) | L(DFA) = ∅}
SOMEDFA = { (<DFA>) | L(DFA) ≠ ∅}
ALLDFA = { (<DFA>) | L(DFA) = Σ*}
NONEMPTYDIFFDFA = { (<DFA>,<DFA2>) | L(DFA1) - L(DFA2) ≠ ∅ }
Show all of these languages are decidable by describing (at a high level) algorithms for deciding them.
♥ NONEDFA: First, check that <DFA> is a valid DFA encoding; if not, reject it. Otherwise, check if
there is a path from the start state to a final state. If so, reject <DFA>; otherwise accept it. Alternatively,
use the DFA equality algorithm from class (based on cross products and final states) to determine if
<DFA> is equal to <DFAEMPTY>, where DFAEMPTY is the one-state DFA whose alphabet is the same as
<DFA> and whose single state is not final (and so whose language is empty). If they’re equal, accept;
otherwise, reject.
♥ SOMEDFA: First, check that <DFA> is a valid DFA encoding; if not, reject it. Otherwise, perform
the test for NONEDFA and invert the result.
♥ ALLDFA : First, check that <DFA> is a valid DFA encoding; if not, reject it. Otherwise, use the DFA
equality algorithm from class (based on cross products and final states) to determine if <DFA> is equal
to <DFAALL>, where DFAALL is the one-state DFA whose alphabet is the same as <DFA> and whose
state is final (and so whose language is all strings over the DFA alphabet). If they’re equal, accept;
otherwise, reject. . Alternatively, check that all paths from the start state lead to final states.
♥ NONEMPTYDIFFDFA : First, check that the input is a pair of <DFA1 > and <DFA2> that are
valid DFA encodings; if not, reject the input. . Otherwise, use the DFA difference algorithm presented in
class (based on cross products and final states) to construct DFADIFF, the difference of these two DFAs.
Finally, use the above algorithm for SOMEDFA to determine if the language of DFADIFF is nonempty.
(Problem 3 is continued on the next page)
10
Problem 3 (continued)
Part b Consider the following languages:
NONETM = { <M> | M is a Turing Machine and L(M) = ∅}
SOMETM = { <M> | M is a Turing Machine and L(M) ≠ ∅}
ALLTM = { <M> | M is a Turing Machine and L(M) = Σ*}
NONEMPTYDIFFTM = { (<M1>,<M2>) | L(M1) - L(M2) ≠ ∅ }
Subpart i Use reduction to prove that all of the above languages are undecidable. You may use
the fact (proved in class) that the following language is undecidable:
ACCEPTTM = { (<M>, w) | M is a Turing Machines and w ∈ L(M)}
If helpful, you may assume that one of the languages is undecidable to prove another undecidable.
♥ NONETM: (Note: NONETM is the same as the EMPTYTM language studied in class.) We show
how to reduce ACCEPTTM to NONETM. Since ACCEPTTM is undecidable, NONETM must also be
undecidable. Suppose there is a TM MNONE that decides NONETM. Then we could define a TM
MACCEPT that decides ACCEPTTM as follows:
MACCEPT ((<M>,w)) =
let M’(x) = M(w)
in not (MNONE (<M’>))
end
Note that L(M’) = Σ* iff M accepts w and L(M’) = ∅ iff M rejects w. So
MNONE rejects <M’> iff M’accepts w
which means that
MACCEPT accepts (<M>,w) iff M accepts w
Thus, MACCEPT accepts exactly the language ACCEPTTM. Here is the reduction expressed in
picture form:
(<M>,w)
M’(x) = ignore x
and run M on w.
<M’>
MNONE
accept
reject
MACCEPT
accept
reject
11
In the mapping reducibility terminology of Sipser (Sec. 5.3) , we write
ACCEPTTM ≤m NONETM
where the overbar indicates the negation that takes place in the reduction. Interestingly, it turns
out that
ACCEPTTM ≤m NONETM (i.e., ACCEPTTM is not mapping reducible to NONETM ; see
Sipser Ex. 5.5)
This subtle detail is important for understanding that NONETM is semi-decidable− (see part c).
♥ SOMETM: This is almost the same as the NONETM case except that no negation is required.
We show how to reduce ACCEPTTM to SOMETM. Since ACCEPTTM is undecidable, SOMETM
must also be undecidable. Suppose there is a TM MSOME that decides SOMETM. Then we could
define a TM MACCEPT that decides ACCEPTTM as follows:
MACCEPT ((<M>,w)) =
let M’(x) = M(w)
in MSOME (<M’>)
end
Note that MSOME accepts M’ iff M accepts w, so that MACCEPT accepts (<M>,w) iff M accepts
w. Thus, MACCEPT accepts exactly the language ACCEPTTM. Here is the reduction expressed in
picture form:
(<M>,w)
M’(x) = ignore x
and run M on w.
<M’>
MSOME
accept
reject
MACCEPT
accept
reject
In the mapping reducibility terminology of Sipser, we write ACCEPTTM ≤m SOMETM . This is
hardly surprising, since SOMETM is essentially NONETM (there are some subtle difference when
it comes to inputs <M’> where <M’> is not a well-formed TM description, but we’ll ignore that
detail for now.)
Note that because of this relationship, knowing from above that NONETM is undecidable, we
could also prove the undecidability of SOMETM by a reduction of NONETM to SOMETM ; in
mapping reduction terminology we have:
NONETM ≤m SOMETM
12
where the overbar is necessary to reflect the negation (flipping of outputs) in the following
diagram:
<M>
MSOME
reject
accept
MNONE
accept
reject
♥ ALLTM: This is also very similar to the NONETM and SOMETM cases. We show how to
reduce ACCEPTTM to ALLTM. Since ACCEPTTM is undecidable, ALLTM must also be
undecidable. Suppose there is a TM MALL that decides ALLTM. Then we could define a TM
MACCEPT that decides ACCEPTTM as follows:
MACCEPT ((<M>,w)) =
let M’(x) = M(w)
in MALL (<M’>)
end
Note that MALL accepts M’iff M accepts w (because in this case L(M’) = Σ*), so that MACCEPT
accepts <<M>,w> iff M accepts w. Thus, MACCEPT accepts exactly the language ACCEPTTM.
Here is the reduction expressed in picture form:
(<M>,w)
M’(x) = ignore x
and run M on w.
<M’>
MALL
accept
reject
MACCEPT
accept
reject
In the mapping reducibility terminology of Sipser, we write ACCEPTTM ≤m ALLTM.
13
♥ NONEMPTYDIFFTM: We show how to reduce SOMETM to NONEMPTYDIFFTM. Since we
showed above that SOMETM is undecidable, NONEMPTYDIFFTM must also be undecidable.
Suppose there is a TM MNONEMPTYDIFF that decides NONEMPTYDIFFTM. Then we could define a
TM MSOME that decides SOMETM as follows:
MSOME (<M>) =
let MEMPTY (y) = reject
in MNONEMPTYDIFF (<M>, <MEMPTY>)
end
Here MEMPTY is a Turing machine that rejects all inputs and so has an empty language. So L(M) –
L(MEMPTY) = L(M), and MNONEMPTYDIFF accepts (<M>, <MEMPTY>)iff the language of M is
nonempty. Thus, MSOME accepts exactly the language SOMETM. Here is the reduction expressed
in picture form:
<M>
<MEMPTY>
MNONEMPTYDIFF
accept
reject
MSOME
accept
reject
In the mapping reducibility terminology of Sipser, we write SOMETM ≤m NONEMPTYDIFFTM.
An alternative approach is to use the reduction ALLTM ≤m NONEMPTYDIFFTM shown below,
where MSIGMA* is a Turing machine that accepts all inputs and so has the language Σ*.
<M>
<MSIGMA*>
MNONEMPTYDIFF
accept
reject
MALL
accept
reject
14
Subpart ii For each of the above four languages:
• If it is in RE (i.e., is semi-decidable+ = an undecidable language that is Turingacceptable/recursively enumerable), then sketch an algorithm for accepting the strings in the
language.
• If it is in co-RE (i.e., is semi-decidable- = an undecidable language whose complement is Turingacceptable/recursively enumerable), then sketch an algorithm for accepting strings in the
complement of the language.
• If it is neither in RE nor co-RE (i.e., is not-even-semidecidable) then (1) informall explain the
intuition why no algorithm could accept all strings in the language or accept all strings in the
complement of the language and (2) formally prove the fact by showing for this language L that
there are semi-decidable+ languages SDP1 and SDP2 s.t. SDP1 ≤m L and SDP2 ≤m L.
Recall that a language is in RE iff we can describe a Turing machine that accepts every string in
the language (but might loop on some strings that are not in the language). If all Turing
machines that recognize an RE language must loop on some input (not necessarily the same
input for each machine), the language is undecidable and we say the language is semidecidable+. Note that ACCEPTTM and HALTTM are the standard examples of semi-decidable+
languages.
♥ SOMETM is semi-decidable+: Recall that a language is semi-decdable+ (in RE) iff we can
describe a Turing machine that accepts every string in the language (but might loop on some
strings that are not in the language). If all Turing machines that recognize an RE language must
loop on some input (not necessarily the same input for each machine), the language is
undecidable and we say the language is semi-decidable+. Note that ACCEPTTM and HALTTM
are the standard examples of semi-decidable+ languages.
SOMETM is semi-decidable+ because it is recognized by the following machine:
MSOME (<M>) =
If <M> is not a Turing Machine encoding, reject it.
Otherwise, enumerate all strings in input-alphabet(M) in parallel and
simulate <M> on these strings in parallel. If M accepts one such
string, accept <M>.
MSOME accepts the description <M> of a Turing machine with a nonempty language because it
will eventually accept a string in L(M).
Note that MSOME can never reject an input <M>, since doing so would require showing that M
does not accept any string in the input alphabet. There are two problems with this. (1) Since
there are an infinite number of strings in the input alphabet, we can’t check all of them in finite
time. (2) M might loop (rather than reject) some of its inputs, so we can never tell definitively
that it rejects even a finite number of inputs.
♥ NONETM is semi-decidable−: Recall that a language is in co-RE iff its complement is in RE
and that a language is semi-decidable− iff its complement is semi-decidable+.
We just showed that SOMETM is semi-decidable+. The complement of SOMETM is NONETM
(ignoring the case of ill-formed inputs) so NONETM must be semi-decidable−.
15
♥ ALLTM is not-even-semidecidable: Recall that a language is not-even-semidecidable iff it is
neither in RE nor in co-RE. Note that if a language is not-even-semidecidable, then so is its
complement.
Informally, we can argue that neither ALLTM nor its complement are in RE, so it is not-evensemi-decidable.
o There is no Turing Machine that accepts ALLTM.. Such a machine could only accept an
input <M> iff it could prove that M accepts all inputs strings over the input alphabet of
M. There are an infinite number of such inputs, and it is impossible to check all of them
in a finite time. Moreover, if M looped on any input, we would never know whether M
accepted or rejected that input.
o There is no Turing Machine that accepts ALLTM.. Such a machine could only accept an
input <M> iff it could prove that M does not accept every input string over the input
alphabet of M. That is, it would have to prove that there is at least one string s that is not
accepted by M. But M might loop on s rather than rejecting it, which means that we can
never know whether s is not accepted by M.
Formally, we can prove that a language L is not-even-semi-decidable if we can show both of the
following mapping reductions for any semi-decidable+ languages SDP1 and SDP2:
•
•
SDP1 ≤m L
SDP2 ≤m L
This is explained in Sipser 5.3 and the slides from lecture #31, which also show that EQTM and
its complement are standard examples of not-even-semi-decidable languages.
We can formally prove that ALLTM is not-even-semi-decidable by demonstrating mapping
reductions from semi-decidable+ languages to (a) ALLTM and (b) ALLTM . Above, we showed (a)
via the reduction ACCEPTTM ≤m ALLTM. To show (b) we exhibit a reduction ACCEPTTM ≤m
ALLTM. Suppose there is a machine MALL that decides ALLTM . Then we could define a TM
MACCEPT that decides ACCEPTTM as follows:
MACCEPT ((<M>, w)) =
let M’(x) = if M does not accept w in ≤ |x| steps
then accept x
else reject x
in MALL <M’>
end
Note that if w ∉ L(M), then L(M’) = Σ*, and if w ∈ L(M), then L(M’) ≠ Σ*, so MALL accepts M’
iff w∈ L(M).
It’s worth underscoring the importance of “in ≤ |x| steps” in the above reduction. Suppose we
instead try the following alternative reduction:
MACCEPT ((<M>, w)) =
let M’’(x) = if M does not accept w
then accept x
else reject x
16
in MALL <M’’>
end
This works find if M accepts or rejects w. The problem here is that if M does not accept w by
looping, then M’’ loops on x (and thus doesn’t accept x) if M loops on w. So we have:
L(M’’) = Σ* if M rejects w
L(M’’) = ∅ if M accepts w or if M loops on w
So nonacceptance by rejection and nonacceptance by looping are treated differently, which
shouldn’t be the case.
♥ NONEMPTYDIFFTM is is not-even-semidecidable. Informally, we can argue that neither
NONEMPTYDIFFTM nor its complement are in RE, so it is not-even-semi-decidable.
o There is no Turing Machine that accepts NONEMPTYDIFFTM. Such a machine could
only accept an input pair (<M1>,<M2>) iff it could prove that M1 accepts some string
that M2 does not. While it is possible to enumerate all strings in input-alphabet(M1)∩
input-alphabet(M2) in parallel and simulate M1 and M2 on these strings in parallel, in
general it is not possible to determine if there is a string s that M1 accepts and M2 does
not. The reason is that M2 might not accept s by looping on it, which cannot be decided.
o There is no Turing Machine that accepts NONEMPTYDIFFTM. Such a machine could only
accept an input pair (<M1>,<M2>) iff it could prove that (<M1>,<M2>) is not a legal
encoding of Turing Machine pairs (easy) or M2 accepts at least every string that M1 does.
Since the latter is the same as SUBSETTM, a known undecidable language, this is not
possible.
Indeed, since SUBSETTM is a not-even-semidecidable language (PS10), the second bullet is
already a proof that NONEMPTYDIFFTM is not-even-semidecidable. But here’s an alternative
proof:
(1) SOMETM ≤m NONEMPTYDIFFTM, as shown earlier in Subpart i.
(2) ACCEPTTM ≤m NONEMPTYDIFFTM. Suppose there is a TM MNONEMPTYDIFF that decides the
complement of NONEMPTYDIFFTM. Then we could define a TM MACCEPT that decides
ACCEPTTM as follows:
MACCEPT ((<M>,w)) =
let MEVERY = accept
M’(x) = M(w)
in M NONEMPTYDIFF (<MEVERY>,<M’>)
end
Note that L(MEVERY) = Σ*, so M NONEMPTYDIFF (<MEVERY>,<M’>) accepts when L(M) = Σ* (i.e., when
w in L(M)) and rejects when L(M) = ∅ (i.e., when w is not in L(M)). Thus, MACCEPT accepts
exactly the language ACCEPTTM.
17
Problem 5 Classifying Languages
On the next page is a Venn diagram showing the relationship of various classes of languages that
we have studied this semester. For each of the following languages, show where it belongs in the
Venn diagram by writing the name of the language in the appropriate area of the diagram. You
need not explain your answers (but are welcome to write comments next to the languages below).
ABC1 = { aibjck | i,j,k in Nat}
ABC2 = { aibjci | i,j in Nat}
ABC3 = { aibici | i in Nat}
DUP = {ww | w in {a,b}* }
NODUP = DUP
EQFA = {(<FA1>,<FA2>) | FA1, FA2 are Finite Automata and L(FA1) = L(FA2) }
NOT_EQFA = {(<FA1>,<FA2>) | FA1, FA2 are Finite Automata and L(FA1) ≠ L(FA2) }
EQTM = {(<M1>,<M2>) | M1 and M2 are Turing Machines and L(M1) = L(M2) }
NOT_EQTM = {(<M1>,<M2>) | M1 and M2 are Turing Machines and L(M1) ≠ L(M2) }
CONTAINS_AFA = {<FA> | FA is a finite automaton and a ∈ L(FA) }
NOT_CONTAINS_AFA = {<FA> | FA is a finite automaton and a ∉ L(FA) }
CONTAINS_ACFG = {<CFG> | CFG is a context-free grammar and a ∈ L(CFG) }
NOT_CONTAINS_ACFG = {<CFG> | CFG is a context-free grammar and a ∉ L(CFG) }
CONTAINS_ATM = {<M> | M is a Turing Machine and a ∈ L(M) }
NOT_CONTAINS_ATM = {<M> | M is a Turing Machine and a ∉ L(M) }
INFINITETM = { <M> | M is a Turing Machine and L(M) is infinite }
FINITETM = { <M> | M is a Turing Machine and L(M) is finite }
GRAM1 = The language of the grammar S → aS | T ; T → % | bT
GRAM2 = The language of the grammar S → % | aT ; T → Sb
GRAM3 = The LL(1) grammar for SLiP-- presented in class
18
Here is the fleshed out map. See the next page for some explanations
Lan (All Languages)
• EQTM
• NOT_ EQTM
• INFINITETM
•
FINITETM
RE (Recursively Enumerable Languages)
• CONTAINS_ATM
Dec (Decidable Languages) = RE ∩ Co-RE
• ABC3
• DUP
• EQFA
• NOT_ EQFA
• CONTAINS_AFA
• NOT_CONTAINS_AFA
• CONTAINS_ACFG
• NOT_CONTAINS_ACFG
CFL (Context-Free Languages)
• ABC2
• GRAM2
• GRAM3
• NODUP
Reg (Regular Languages)
• ABC1
• GRAM1
• NOT_CONTAINS_ATM
co-RE (co-recursively enumerable languages)
19
20
Here are some notes about the languages in the map:
ABC1 is equivalent to a*b*c* and so is regular.
ABC2 is generated by the grammar S -> aSc | T, T -> % | bT and so is context-free. But it
isn’t regular because it requires counting to ensure the number of as and cs are equal.
ABC3 is decidable (Lec #28) but not context-free (Lec #25).
DUP is decidable (Lec #28) but not context-free (Lec #25)
NODUP (surprisingly) is context-free (Lec #25)
EQFA is decidable (Lec #14)
NOT_EQFA is decidable because it’s the complement of a decidable language.
EQTM is not-even-semi-decidable (Lec #32)
NOT_EQTM is not-even-semi-decidable (Lec #32)
CONTAINS_AFA is decidable: can just simulate the FA on input “a”.
NOT_CONTAINS_AFA is decidable as the complement of a decidable language (or can just
simulate the FA on input “a”).
CONTAINS_ACFG is decidable. One approach is to run the enzyme algorithm with the CFG
on input “a”. Another is to intersect CFG with the regular language “a” and see if the
resulting CFG is nonempty.
NOT_CONTAINS_ACFG is decidable by tweaks to the algorithms for CONTAINS_ACFG.
CONTAINS_ATM is RE (accept <M> if simulating M on input “a” accepts it) but not
decidable (can reduce ACCEPT_ATM to CONTAINS_ATM by the standard
construction in which M’(x) = M(w)).
NOT_CONTAINS_ATM is in (co-RE – Dec) because CONTAINS_ATM is in (RE – Dec).
INFINITETM & FINITETM are both not-even-semi-decidable by the following reductions:
ACCEPTTM ≤m INFINITETM
ACCEPTTM ≤m FINITETM
MACCEPT ((<M>,w)) =
let M’(x) = M(w)
in MINFINITE (<M’>)
end
MACCEPT ((<M>, w)) =
let M’(x) = if M does not accept w in ≤ |x| steps
then accept x
else reject x
in MFINITE <M’>
end
GRAM1 is regular since it’s right-linear; it’s equivalent to a*b*.
GRAM2 is equivalent to S -> % | aSb, which generates { anbn | n in Nat}, so is context-free
but not regular. Mixing left-linear and right-linear grammars may not yield a regular
language!
GRAM3 is context-free – all LL(k) and LR(k) languages are generated by CFGs and so are
context free.
21
Problem 5 Closure Properties
In the following table, if a language is closed under the given property, write Y for YES and
otherwise write N for No.
Complement Union Intersection Concatenation Kleene
Star
Reg, the set of regular languages
Y
Y
Y
Y
Y
CFL, the set of context-free
N
Y
N
Y
Y
languages
Dec, the set of recursive (TuringY
Y
Y
Y
Y
decidable) languages
RE, the set of recursively
N
Y
Y
Y
Y
enumerable (Turing-acceptable)
languages
22
Problem 6: Countable or Uncountable?
For each of the following sets, circle one letter to indicate whether it is countable (C) or
uncountable (U). You need not explain your answers.
Nat stands for the set of natural numbers and Sign stands for the set {-, 0, +}.
The set Sign
C
U
The set Nat
C
U
The set Sign x Sign
C
U
The set Sign x Nat
C
U
The set Nat x Nat
C
U
The set Sign → Sign
C
U
The set Sign → Nat
C
U
The set Nat → Sign
C
U
The set Nat → Nat
C
U
The set P(Sign)
C
U
The set P(Nat)
C
U
The set Reg of context-free languages
C
U
The set of strings described by the regular expression
(a + b)*(c+d)*
C
U
The set CFL of context-free languages
C
U
The set of all Turing Machines accepting a nonempty language
C
U
The set RE of recursively enumerable languages
C
U
The set co-RE of co-recursively enumerable languages
C
U
The set of all SML programs
C
U
The set of all undecidable languages
C
U
23