P Zeph Grunschlag 1

advertisement
The class P
Zeph Grunschlag
1
Agenda
Running time
Big-O review
Time complexity classes

The class P
Converting RAM programs to TM programs
with polynomial loss of efficiency
Decision problems encoding I/O problems

Factoring numbers
Context free languages are in P

CYK algorithm
2
Polynomial Time vs.
Exponential Time
Experience shows that the dividing line
between problems solvable in polynomial
time vs. those requiring exponential time
is fundamental. Let’s define the running
time of TM’s:
DEF: The running time of a Turing
machine M is the function f (n) which is
the maximum number of transitions that
M takes to halt when given an arbitrary
input of length n.
NOTE: A crash is considered as halting. 3
Running Time
0R
rej
acc
R
•
0
2
1R
1R
0R
1
0R
1R
Q: What the running time of the above?
4
Running Time
0R
rej
acc
R
•
0
2
1R
1R
0R
1
0R
1R
A: Assuming S = {0,1}, the running time is
T (n)=n+1` as can be seen on any input
starting with 1. Even though the TM
runs faster on 0{0,1}*, running time is
defined in terms of the worst case.
Crashes don’t affect this.
5
Polynomial Time Complexity
DEF: M is of polynomial time complexity if
there is a polynomial p(n) for with the running
time f (n) of M satisfies:
f (n)  p(n) .
Problem: Nobody uses Turing machines in the
real world so is this really justified.
Resolution: Investigate the efficiency of Turing
machines vs. actual RAM machines:
Strong Church-Turing Thesis: Any computing
device can be simulated by TM’s with at worst a
polynomial time slow-down.
6
Strong Church-Turing Thesis?
As opposed to the standard Church-Turing
thesis for which there is overwhelming
evidence, there is a great deal of current
research trying to show that the strong
version is false. In particular:
Possible Counterexample: Many conjecture
that no polynomial time algorithm exists for
factoring numbers using a TM. But a
polynomial time quantum algorithm exists
(Peter Shor). Would mean that quantum
computers invalidate Strong C-T thesis!
7
Big-O Notation
Nothing to say here.
Assume you learned in Discrete Math and
Data Structures.
8
Big-O Notation
OK… here’s a refresher:
DEF: Let f and g be functions with
domain R0 or N and codomain R. If
there are constants C and k such
 x > k, |f (x )|  C  |g (x )|
i.e., past k, f is less than or equal to a
multiple of g, then we write:
f (x ) = O ( g (x ) )
9
Big- and Big-
Big- is just the reverse of big-O. I.e.
f (x ) = (g (x ))  g (x ) = O (f (x ))
So big- says that asymptotically f (x )
dominates g (x ).
Big- says that both functions dominate eachother so are asymptotically equivalent. I.e.
f (x ) = (g (x ))

f (x ) = O (g (x ))  f (x ) = (g (x ))
Synonym for f = (g): “f is of order g ”
10
Useful facts
Any polynomial is big- of its largest
term

EG: x 4/100000 + 3x 3 + 5x 2 – 9 = (x 4)
The sum of two functions is big-O of
the biggest

EG: x 4 ln(x ) + x 5 = O (x 5)
Non-zero constants are irrelevent:

EG: 17x 4 ln(x ) = O (x 4 ln(x ))
11
Big-O, Big-, Big-.
Examples
Q: Order the following from smallest to
largest asymptotically. Group together
all functions which are big- of each
other:
1
1
x  sin x, ln x, x  x , ,13  ,13  x, e x , x e , x x
x
x
20
2
( x  sin x)( x  102), x ln x, x(ln x) , lg 2 x
12
Big-O, Big-, Big-.
Examples
A:
1.1 x
2.13  1 x
3. ln x, lg 2 x (change of base formula)
4. x  sin x, x  x ,13  x
5. x ln x
2
6. x(ln x)
e
7. x
8. ( x  sin x)( x 20  102)
x
9. e
10. x x
13
Time Complexity Classes
Time complexity classes are the next
languages classes that we’ll study:
DEF: Suppose that g (n) is a real function.
The time complexity class TIME(g (n) )
consists of all languages which are decided
by some TM with running time O(g (n)).
Any such language is said to by of time
complexity g (n).
14
Time Complexity
0R
rej
acc
R
•
0
1R
2
0R
1
0R
1R
Q: For which g (n) is the language accepted
above of time complexity g (n) ?
1. g (n) = n +2
2. g (n) = n/2
3. g (n) = n2
4. g (n) = 1
15
Time Complexity
0R
rej
acc
R
•
0
1R
2
0R
1
0R
1R
A: ALL! This particular decider for the
language has running time f (n) = n+2
which is big-O of no.’s 1, 2 and 3. But
the definition doesn’t say that must stick
to any single implementation:
Q: How would you modify the TM to see
that accepted language is in TIME(1) ?16
Time Complexity
A: Accepted language is 10{0,1}*. So
just need to check first two bits as
pictured below:
R
•
0
1R
1
0R
2
0R
1R
acc
So now we see that the language is
accepted by a TM which runs in
constant time.
17
The Class P
P is the class of languages that are
decided by a TM of time complexity
some polynomial. IE:

DEF:
P   TIME (n )
k
k 0
18
From RAM to TM
in Polynomial Time
THM 1: Any RAM program which runs in O (f (n))
time can be simulated by a multi-tape TM which
runs in O (f (n)3 ) time.
THM 2: Any multi-tape TM which runs in O (g (n))
time can be simulated by a one-tape TM which
runs in O (g (n)2 ) time.
COR: Any RAM program which runs in O (f (n))
time can be simulated by a one-tape TM which
runs in O (f (n)6 ) time.
Consequently, any algorithm that runs in polynomial
time on your desktop, can be made to run in
polynomial time on a TM.
19
From RAM to TM
in Polynomial Time
No proofs. Rather, give brief explanation:
A RAM consists of a “goto” style program and a
memory made up of arbitrarily many integers
called registers and each having an integer
address. A single step of a RAM consists of doing
something to one of the registers1. After k steps,
a RAM may have at most O (k) active registers
each containing an integer of length O (k). If we
keep these contents on one of the TM tapes,
changing the state of the simulated RAM requires
reading all O (k) registers each of size O (k), so
O (k 2) tape cells. Since k = O (f (n) ), and there
are f (n) steps to simulate, g(n) = O (f (n)3 ). 20
From RAM to TM
in Polynomial Time
On the other hand, recall conversion from a
multi-tape to a 1-tape TM. Converted to a multitrack machine first. The multi-track machine
needed to sweep back and forth once across
whole tape to read the current active cells. So on
multitape’s k ’th step, contents are O (k) so
sweeping back and forth is O(k ). Since
k = O (g (n) ), and there are g (n) steps to
simulate, h(n) = O (g (n)2 ).
Now go from multitrack to one-tape machine.
Recall that all we did was rename alphabet and
transitions so running time was identical to that
of 1-tape simulator and to that of k-tape TM! 21
RAM’s are Polynomially
Equivalent to TM’s
Other direction valid as well: Can simulate
any TM in the same asymptotic time
complexity in Java.
Conclude: The belonging of a given
language to the class P does not depend
on the computing paradigm chosen.
Thus pseudocode is useful in analyzing
computational complexity.
22
Turning Decision Problems
into I/O Problems
Up till now, point of view of course has
been to study computability of
algorithmic problems by looking at
decision problem versions.
However, in practice, decision problems
aren’t always interesting.
EG: If you want to break the RSA
encryption algorithm knowing that a
number N is composite isn’t good
enough. Would want to output the
prime factors as well!
23
Encoding FACTORING by a
Decision Problem
It turns out, that usually I/0 problems can
easily be encoded as decision problems
which can yield solutions to the original
I/O problem with only polynomial loss of
efficiency.
EG: Consider I/O problem FACTORING:
Input: a number n
Output: a set of prime numbers p1,p2,…,pt
whose product is the number n.
24
Encoding FACTORING by a
Decision Problem
Encode FACTORING by the decision problem
BOUNDED_FACTOR:
Given: a number pair (n,k)
Decide: is there a number d strictly between 1
and k which divides n ?
Q: Which of the following are in
BOUNDED_FACTOR:
1. (10,5)
2. (7,8)
3. (7,7)
25
Encoding FACTORING by a
Decision Problem
A: Which of the following are in
BOUNDED_FACTOR:
1. (10,5) Yes, because 2 divides 10 and
1<2<5.
2. (7,8) Yes, because 7 divides itself and
1<7<8
3. (7,7) No, because 7 is prime so does
not contain any factors between 1 & 7.
Q: How can PRIME be reduced to
BOUNDED_FACTOR ?
26
Encoding FACTORING by a
Decision Problem
A: Use the co-mapping reduction
f : PRIME  BOUNDED_FACTOR
defined by f (n) = (n,n).
Now let’s see how to turn a solution to
BOUNDED_FACTOR into a factoring
algorithm with polynomial loss of
efficiency. The idea is to use a binary
search to look for a prime factor. Then
divide out by the factor and repeat:
27
Encoding FACTORING by a
Decision Problem
m = n, i = 0
while(m > 1)
i = i + 1
k = 0, l = m + 1
while( k < l - 1 )
if( BOUNDED_FACTOR(m,(k+l)/2) )
l = (k+l)/2
else
k = (k+l)/2
pi = k
// the smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
28
Encoding FACTORING by a
Decision Problem
m = n, i = 0
Let’s see how
while(m > 1)
the algorithm
i = i+1
works on
k = 0, l = m+1
input n = 12
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
29
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
l
m = n, i = 0
while(m > 1)
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
30
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
l
12
0
m = n, i = 0
while(m > 1)
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
31
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
l
12
m = n, i = 0
while(m > 1)
1
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
32
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
l
13
33
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
l
6
34
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
l
3
35
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
1
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
l
3
36
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
l
3
37
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
12
m = n, i = 0
while(m > 1)
1
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
38
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
1
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
39
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
40
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
7
41
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
42
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
1
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
43
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2
l
3
44
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
6
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
3
45
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
2
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
3
46
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
3
47
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
0
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
4
48
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
2
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
4
49
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
3
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2
l
4
50
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
3
k = (k+l)/2
pi=k //smallest prime factor of m
3
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2, 3
l
4
51
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
3
k = (k+l)/2
pi=k //smallest prime factor of m
1
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2, 3
l
4
52
Encoding FACTORING by a
Decision Problem EG n=12 m
i
k
m = n, i = 0
while(m > 1)
3
i = i+1
k = 0, l = m+1
while( k < l-1 )
if(BOUNDED_FACTOR(m,(k+l)/2))
l = (k+l)/2
else
3
k = (k+l)/2
pi=k //smallest prime factor of m
1
m = m/k
return (p1, p2 , … , pi)
Prime factors:
2, 2, 3 HALT!
l
4
53
Encoding FACTORING by a
Decision Problem
Running Time:
m = n, i = 0
O (# of prime factors)
while(m > 1)
O (log n)
i = i+1
O (log n)
k = 0, l = m+1
O (log n)
while( k < l-1 )
O (log n)
if(BOUNDED_FACTOR(m,(k+l)/2))
O ( f (n) )
l = (k+l)/2
O (log n)
else
k = (k+l)/2
O (log n)
pi=k //smallest prime factor of m
O (log n)
m = m/k
O (log n)
return (p1, p2 , … , pi)
O (1)
54
Encoding FACTORING by a
Decision Problem
As complexities of nested loops multiply, the time
complexity of FACTORING is
O (# of prime factors of n · log n · f (n ) · log n )
CLAIM: The number of prime factors of n is no
greater than log n (base 2).
Proof. Each prime factor pi  2 so
n = p1·p2·…·pt  2t where t is the number of
factors. Take the log of both sides to conclude
that logn  t.
•
Consequently, FACTORING is
O ( (log n )3 · f (n ) )
so that if f (n) is a polynomial, then FACTORING55is.
CFL  P
Let’s show that every context free language
admits a polynomial time solution for its
acceptance problem.
NOTE: Since the complexity of the algorithm is
measured in terms of the strings being tested,
and not the representation of the context free
language, we may assume any form of
representing context free languages, as we find
convenient! So let’s assume that the language is
represented by a Chomsky normal form
grammar.
56
CYK Algorithm for Deciding
Context Free Languages
IDEA: For each substring of a given input
x, find all variables which can derive the
substring. Once these have been
found, telling which variables generate
x becomes a simple matter of looking at
the grammar, since it’s in Chomsky
normal form
57
CYK Algorithm for Deciding
Context Free Languages
Q: Consider the grammar G given by
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
1. Is x = aaabb in L(G )
2. Is x = aaabbb in L(G )
58
CYK Algorithm for Deciding
Context Free Languages
The algorithm is “bottom-up” in that we start
with bottom of derivation tree. I’ve inverted
everything:
a a a b b
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
59
CYK Algorithm for Deciding
Context Free Languages
1) Write variables for all length 1 substrings
S  e | AB | XB
T  AB | XB
X  AT
a
a
a
b
b
A
A
A
B
B
Aa
Bb
60
CYK Algorithm for Deciding
Context Free Languages
2) Write variables for all length 2 substrings
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
a
a
b
b
A
A
A
B
B
S,T
T
61
CYK Algorithm for Deciding
Context Free Languages
3) Write variables for all length 3 substrings
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
a
a
b
b
A
A
A
B
B
S,T
T
X
62
CYK Algorithm for Deciding
Context Free Languages
4) Write variables for all length 4 substrings
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
a
a
b
b
A
A
A
B
B
S,T
T
X
S,T
63
CYK Algorithm for Deciding
Context Free Languages
5) Write variables for all length 5 substrings.
X is the only generator!
S  e | AB | XB
T  AB | XB
X  AT
a
a
a
b
b
A
A
A
B
B
Aa
Bb
REJECT!
S,T
T
X
S,T
X
64
CYK Algorithm for Deciding
Context Free Languages
Now look at aaabbb :
S  e | AB | XB
T  AB | XB
a
a
a
b
b
b
X  AT
Aa
Bb
65
CYK Algorithm for Deciding
Context Free Languages
1) Write variables for all length 1 substrings.
S  e | AB | XB
T  AB | XB
X  AT
a
A
a
A
a
A
b
B
b
B
b
B
Aa
Bb
66
CYK Algorithm for Deciding
Context Free Languages
2) Write variables for all length 2 substrings.
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
A
a
A
a b
A B
S,T
b
B
b
B
67
CYK Algorithm for Deciding
Context Free Languages
3) Write variables for all length 4 substrings.
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
A
a
A
a b
A B
S,T
T
b
B
b
B
X
68
CYK Algorithm for Deciding
Context Free Languages
4) Write variables for all length 4 substrings.
S  e | AB | XB
T  AB | XB
a
A
a
A
a b
A B
S,T
T
X  AT
Aa
Bb
b
B
b
B
X
S,T
69
CYK Algorithm for Deciding
Context Free Languages
5) Write variables for all length 5 substrings.
S  e | AB | XB
T  AB | XB
X  AT
a
A
a
A
Aa
Bb
a b
A B
S,T
T
b
B
b
B
X
S,T
X
70
CYK Algorithm for Deciding
Context Free Languages
6) Write variables for all length 6 substrings.
S  e | AB | XB
T  AB | XB
X  AT
Aa
Bb
a
A
a
A
a b
A B
S,T
T
b
B
b
B
X
S,T
S is included so
aaabbb accepted!
X
S,T
71
CYK Algorithm for Deciding
Context Free Languages
Can also use a table for same purpose.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
4:
5:
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
72
CYK Algorithm for Deciding
Context Free Languages
1. Variables for length 1 substrings.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
4:
5:
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
A
A
B
B
B
73
CYK Algorithm for Deciding
Context Free Languages
2. Variables for length 2 substrings.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
A
-
3:
4:
5:
S,T
B
-
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
-
A
B
-
B
74
CYK Algorithm for Deciding
Context Free Languages
3. Variables for length 3 substrings.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
A
-
-
4:
5:
X
S,T
B
-
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
A
B
-
B
75
CYK Algorithm for Deciding
Context Free Languages
4. Variables for length 4 substrings.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
4:
A
-
-
-
5:
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
A
X
S,T
B
S,T
-
B
-
B
76
CYK Algorithm for Deciding
Context Free Languages
5. Variables for length 5 substrings.
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
4:
5:
A
-
-
-
X
S,T
6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
A
X
S,T
B
-
B
-
B
77
CYK Algorithm for Deciding
Context Free Languages
6. Variables for aaabbb. ACCEPTED!
end at
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
1:
2:
3:
4:
5:
6:
A
-
-
-
X
S,T
S,T
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
A
A
X
S,T
B
-
B
-
B
78
CYK Pseudocode
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
79
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (1)
80
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (n)
O (1)
81
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (n2)
O (n)
O (1)
82
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (n3 )
O (n2)
O (n)
O (1)
83
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (1)
O (n3 )
O (n2)
O (n)
O (1)
84
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (n)
O (1)
O (n3 )
O (n2)
O (n)
O (1)
85
CYK Pseudocode
Complexity
On input x = x1x2 … xn :
for (i = 1 to n) //create middle diagonal
for (each var. A)
if(Axi)
add A to table[i-1][i]
for (d = 2 to n) // d’th diagonal
for (i = 0 to n-d)
for (k = i+1 to i+d-1)
for (each var. A)
for(each var. B in table[i][k])
for(each var. C in table[k][k+d])
if(ABC)
add A to table[i][k+d]
return Stable[0][n] ? ACCEPT : REJECT
O (n3)
O (n)
O (1)
O (n3 )
O (n2)
O (n)
O (1)
86
CYK Pseudocode
Complexity
Therefore, the class of context free
languages is contained in TIME(n3) so
all context free languages are in P.
NOTE: There is a way of proving this
fact, without resorting to Chomsky
normal form. Earley’s algorithm parses
any CFG in cubic time, and this is the
algorithm used in JavaCFG.
87
Download