Appendix F. CYK Algorithm
Appendix F. CYK Algorithm
1
CYK Algorithm
wij = ai ..... aj
j
w = a1 a2 a3 a4 a5 a6
V11 V22 V33 V44 V55 V66
V12 V23 V34 V45 V56
V13 V24 V35 V46
V14 V25 V36 Clearly, by definition if
i S V16 , then string
V15 V26
w L(G).
V16
2
CYK Algorithm
The entries Vij can be computed with the entries in the i-th diagonal and those in the
j-th column, going along the direction indicated by the two arrows in the following
figure. If A Vii (which implies A can derive ai ), B V(i+1)j (implying B can derive
ai+1. . . aj ) and C AB, then put C in the set Vij . If D Vi(i+1) (which implies D can
derive aiai+1), E V(i+2)j (implying E can derive ai+2. . . aj ) and F DE, then put F in
the set Vij , and so on.
V(i+2)j
Vi(j-1) V(i+1)j
Vij
3
CYK Algorithm
w = a1 a2 a3 a4 a5 a6
V11 V22 V33 V44 V55 V66
V12 V23 V34 V45 V56
V13 V24 V35 V46
A
V14 V25 V36 N
Let A, B and C be nonterminals of G. V15 V26 I
V25 = { A | B V22 , C V35 , and A BC } V16
{ B | C V23 , A V45 , and B CA }
{ C | B V24 , A V55 , and C BA }
.....
(Recall that G is in CNF.)
4
CYK Algorithm
wij = ai ai+1 . . . . . aj
Vii Vjj
Vi(i+1)
V(i+2)j
Vi(j-1) V(i+1)j
Vij
5
CYK Algorithm
Example:
w = a a a a b b
{S,B,C}
CNF CFG {S,C} SAC,CDB
B SB
S AB | AC Aa B SB Bb
{S,B,C}
SAB,S AC
C DB D AD | a CDB, BSB
The number of sets Vij is O(n2), and it takes O(n) steps to compute each vij. Thus
the time complexity of the algorithm is O(n3).