Lab 11 Solutions

CS 235 Spring 2015
Lab 11 Key
Rhys
April 5, 2015
Introduction
Undecidability, huh? My partner Daphne Duck and I are undecided as
to how we like this lab.
1
Exercise 1
(Sipser 5.19, both editions) Show that the Silly PCP (where all the pairs
have strings of the same length top and bottom) is decidable.
There is a solution to the SPCP if and only if it begins with a single
domino that has identical strings top and bottom. So here’s the decider:
boolean sillyPCPDecider(DominoSet ds) {
for each Domino d in ds {
if (d.topString.equals(d.bottomString)) return true;
}
return false;
}
2
Exercise 2
(Sipser 5.21, both editions) Use PCP to show that the ambiguity of a CFG
is undecidable.
The hint given by Sipser is crucial to this proof.
Assume, to get a contradiction that AMBIG CF G is decidable, i.e. that
there is a guaranteed halting correct TM decider
boolean AmbigCFGDecider(CFG G) {
// returns true if there is a string w in L(G) with two leftmost derivations
// returns false otherwise
}
We will reduce PCP (which we know is undecidable) to AMBIG CF G and
thus show that AMBIG CF G is undecidable. To do this we will create a
(contradictory since it cannot exist) decider for PCP .
1
nh i h i
h io
tk
t1
t2
We may write P =
.
,
,
·
·
·
b1
b2
bk
We may now define a context-free grammar G by:
S → T |B
T
→ t1 T a1 | t2 T a2 | · · · | tk T ak t1 a1 | t2 a2 | · · · | tk ak
B → b1 Ba1 | b2 Ba2 | · · · | bk Bak b1 a1 | b2 a2 | · · · | bk ak
where a1 , a2 , · · · , ak are new terminal symbols.
We will show that the PCP P has a solution if and only if this CFG is
ambiguous. Then we will have our contradictory:
boolean PCPDecider(PCPInstance P) {
return AmbigCFGDecider(G);
}
One direction is easy: If P has a match ti1 ti2 · · · tin = bi1 bi2 · · · bin then
we have a string that has two leftmost derivations in G. That string is
ti1 ti2 · · · tin ai1 ai2 · · · ain = bi1 bi2 · · · bin ai1 ai2 · · · ain
which has one derivation via T and another via B. So G is ambiguous.
Conversely, it remains to prove that if G is ambiguous then the PCP P
has a solution.
So suppose a string w has two distinct leftmost derivations. We remark
that every string in L(G) is of the form xaim aim−1 · · · ai1 where the string
x is composed entirely of symbols that appear on the dominos of PCP P .
Furthermore, every derivation in G must begin with S → T or S → B.
If the derivation begins S → T then the rest of the derivation of xai1 ai2 · · · aim
is determined completely by the sequence i1 i2 · · · im . In fact the derivation
is S → T → ti1 T ai1 ⇒ ti1 ti2 T ai2 ai1 ⇒ ti1 ti2 · · · tim aim · · · ai2 ai1
The only candidate for a second derivation for w will begin S → B and
the rest will again be completely determined by the sequence i1 i2 · · · im . In
fact the derivation is S → B → bi1 Bai1 ⇒ bi1 bi2 Bai2 ai1 ⇒ bi1 bi2 · · · bim aim · · · ai2 ai1
Since w is ambiguous we have
ti1 ti2 · · · tim aim · · · ai2 ai1 = bi1 bi2 · · · bim aim · · · ai2 ai1
and so ti1 ti2 · · · tim = bi1 bi2 · · · bim .
And this is a solution to the PCP P .
3
Exercise 3
Sipser 5.30.
1. Prove that INFINITE T M = {hM i | M is a TM and L is an infinite language}
is undecidable.
INFINITETM is nontrivial because some TMs have infinite languages
and other TMs have finite languages. Secondly, it is a language property rather than a machine property because if there are two TMs
2
M1 and M2 with L(M1 ) = L(M2 ) then either both hM1 i and hM1 i ∈
INFINITETM or both hM1 i and hM1 i ∈
/ INFINITETM .
So by Rice’s Theorem, we conclude that INFINITE T M is undecidable.
2. Prove that {hM i | M is a TM and 1011 ∈ L(M )} is undecidable.
Again we need show only that it’s a non-trivial language property.
It’s non-trivial because some TMs accept 1011 and others don’t. It’s a
language rather than a machine property because any time two TMs
recognize the same language, either both have descriptions in this set
or neither do.
3. ALLTM = {hM i | M is a TM and L(M ) = Σ∗ }.
This is nontrivial because there are some TMs that recognize Σ∗ and
there are others that do not. Secondly, it’s a language property because
if two TMs have the same language, then either both have descriptions
in ALLTM or neither does.
4
Exercise 4
Prove that ALLCFG is undecidable.
Notice that LP AL and LP CP for a given instance of PCP are both deterministic context-free languages. Since DCFLs are closed under complement
and CFLs are closed under union, we conclude that LP AL ∪ LP C is contextfree.
Now LP AL ∪ LP C = Σ∗ if and only if
LP AL ∪ LP C = ∅
if and only if
LP AL ∩ LP C = ∅
which we know is undecidable. Hence it is undecidable if the language
of a context free grammar is all of Σ∗ .
5
Exercise 5
Say a variable A in a CFG G is necessary if it appears in every derivation
of some string w. Let NECESSARYCFG be the language of pairs {hG, Ai |
A is a necessary variable in G}.
Show that NECESSARYCFG is Turing recognizable and undecidable.
In other words, we are asked to provide a recognizer:
boolean NecessaryRecognizer(CFG G, Variable A) {
// return true if there is a string w in L(G)
// and A appears in every derivation of w
}
and prove that there does not exist a
3
boolean NecessaryDecider(CFG G, Variable A) {
// return true if there is a string w in L(G)
// and A appears in every derivation of w
// return false if every string w has a derivation without A
}
Let’s begin by showing that it’s decidable if a variable A is necessary in
the derivation of a particular string w. Specifically:
boolean NecessaryForDerivationOf(CFG G, Variable A, String w) {
Let the CFG G’ be the same as G but with every reference to A removed
return w in L(G’) -- which we know is decidable
}
Now use that decider to show that NECESSARYCFG is Turing recognizable:
boolean NecessaryRecognizer(CFG G, Variable A) {
// return true if there is a string w in L(G)
// and A appears in every derivation of w
Let w1, w2, .. be an enumeration of Sigma*
for w = w1, w2, ... {
if (NecessaryForDerivationOf(G,A,w)) return true;
}
}
We will prove NECESSARYCFG is undecidable by reducing ALLCFG to
NECESSARYCFG . So assume, to get a contradiction, that a decider
boolean NecessaryDecider(CFG G, Variable A) {
// return true if there is a string w in L(G)
// and A appears in every derivation of w
// return false if every string w has a derivation without A
}
has been written. We will use it to make:
boolean AllCFGDecider(CFG G) {
// We know this should be impossible
// because the problem is undecidable!
Construct a new CFG G’ which has all the rules of G together with
S0 -> S | T
T -> aT | epsilon for all a in Sigma
where S0 is the new start symbol, S is the old start symbol and T is new
return(not(NecessaryDecider(G’,T)))
}
This works because, if L(G) = Σ∗ then also L(G0 ) = Σ∗ because G0
can do anything G can do. Furthermore, anything G can do G0 can also do
without using T . So if L(G) = Σ∗ then T is not necessary in G0 . However,
if L(G) 6= Sigma∗ , that means there is a string w ∈ Σ∗ but w ∈
/ L(G).
Notice however that w ∈ L(G0 ) but that any derivation of w in G0 requires
T . Hence hGi ∈ ALLCFG if and only if hG0 , T i ∈
/ NECESSARYCFG .
4