COMS W3261
Computer Science Theory
Lecture 16: November 4, 2009
Decision Properties of CFL's
Midterm 2 will be in class, Wed, Nov 11. It will cover
context-free languages (HMU Chs. 5-7). Closed book, no aids.
1. Outline
- Review
- Time complexity of an algorithm
- Decision properties of CFL's
- Undecidable problems for CFL's
2. Review
- Pumping lemma for CFL's
- Closure properties of CFL's
- Nonclosure properties of CFL's
3. Time Compexity of an Algorithm
- Given an algorithm A to solve a problem P,
we say A has time complexity T(n),
if A can solve any instance of problem P
of size n in O(T(n)) steps.
- The time complexity of an algorithm is its worst-case running time
on any problem of size n.
- We say T(n) is
- linear if T(n) ≤ cn for some constant c
- quadratic if T(n) ≤ cn2 for some
constant c
- exponential if T(n) ≤ c2n
for some constant c
4. Testing Emptiness of a CFG
- Problem: Given a CFG G, is L(G) empty?
- Emptiness problem is decidable: determine whether the
start symbol of G is generating.
- Naive algorithm has O(n2) time complexity where n
is the size of G
(sum of the lengths of the productions).
- With a more sophisticated list-processing algorithm, emptiness
problem can be solved in linear time. See HMU, p. 302.
5. Cocke-Younger-Kasami Algorithm for Testing Membership in a CFL
- Input: a Chomsky normal form CFG G = (V, T, P, S) and a string w =
a1a2 ... an
in T*.
- Output: "yes" if w is in L(G), "no" otherwise.
- Method: The CYK algorithm is a dynamic programming algorithm that fills in
a triangular table
Xij with nonterminals A
such that A ⇒*
aiai+1 ...
aj.
for i = 1 to n do
if A → ai is in P then
add A to Xii
for row = 2 to n do
for col = 1 to row - 1 do
for k = col to row - 1 do {
i = col
j = col + row - 1
if (A → BC is in P) and
(B is in Xik) and (C is in Xk+1,j) then
add A to Xij
}
if S is in X1n then
output "yes"
else
output "no"
The algorithm adds nonterminal A to Xij iff there is a
production A → BC in P where B ⇒*
aiai+1 ... ak
and C ⇒*
ak+1ak+2 ... aj.
To compute entry Xij, we examine at most
n pairs of entries:
(Xii, Xi+1,j),
(Xi,i+1, Xi+2,j),
and so on until
(Xi,j-1, Xj,j).
Thus, the running time of the CYK algorithm is O(n3).
6. Undecidable Problems for CFL's
- If a problem cannot be solved by any algorithm,
then the problem is said to be undecidable.
- The following problems are undecidable:
- Is a given CFG ambiguous?
- Is a given CFL inherently ambiguous?
- Is the intersection of two CFL's empty?
- Given two CFG's G1 and G2,
is L(G1) = L(G2)?
- Given a CFG G = (V, T, P, S), is L(G) = T*?
7. Practice Problems
- HMU, Exercise 7.4.1(a).
- HMU, Exercise 7.4.3(a).
- Augment the CYK algorithm to construct all parse trees
for the given input.
What is the time complexity of your algorithm?
Show how your algorithm works on Exercise 7.4.3(a).
- (Hard) HMU, Exercise 7.4.1(c).
8. Reading Assignment
aho@cs.columbia.edu