Cocke–Younger–Kasami algorithm (CYK) | |
Class: | Parsing with context-free grammars |
Data: | String |
Time: | l{O}\left(n3 ⋅ \left |
In computer science, the Cocke–Younger–Kasami algorithm (alternatively called CYK, or CKY) is a parsing algorithm for context-free grammars published by Itiroo Sakai in 1961.[1] [2] The algorithm is named after some of its rediscoverers: John Cocke, Daniel Younger, Tadao Kasami, and Jacob T. Schwartz. It employs bottom-up parsing and dynamic programming.
The standard version of CYK operates only on context-free grammars given in Chomsky normal form (CNF). However any context-free grammar may be algorithmically transformed into a CNF grammar expressing the same language .
The importance of the CYK algorithm stems from its high efficiency in certain situations. Using big O notation, the worst case running time of CYK is
l{O}\left(n3 ⋅ \left|G\right|\right)
n
\left|G\right|
G
The dynamic programming algorithm requires the context-free grammar to be rendered into Chomsky normal form (CNF), because it tests for possibilities to split the current sequence into two smaller sequences. Any context-free grammar that does not generate the empty string can be represented in CNF using only production rules of the forms
A → \alpha
A → BC
S\to\varepsilon
S
The algorithm in pseudocode is as follows:
let the input be a string I consisting of n characters: a1 ... an. let the grammar contain r nonterminal symbols R1 ... Rr, with start symbol R1. let P[''n'',''n'',''r''] be an array of booleans. Initialize all elements of P to false. let back[''n'',''n'',''r''] be an array of lists of backpointing triples. Initialize all elements of back to the empty list. for each s = 1 to n for each unit production Rv → as set P[''1'',''s'',''v''] = true for each l = 2 to n -- Length of span for each s = 1 to n-l+1 -- Start of span for each p = 1 to l-1 -- Partition of span for each production Ra → Rb Rc if P[''p'',''s'',''b''] and P[''l''-''p'',''s''+''p'',''c''] then set P[''l'',''s'',''a''] = true, append
to back[''l'',''s'',''a''] if P[n,''1'',''1''] is true then I is member of language return back -- by retracing the steps through back, one can easily construct all possible parse trees of the string. else return "not a member of language"
Allows to recover the most probable parse given the probabilities of all productions.
let the input be a string I consisting of n characters: a1 ... an. let the grammar contain r nonterminal symbols R1 ... Rr, with start symbol R1. let P[''n'',''n'',''r''] be an array of real numbers. Initialize all elements of P to zero. let back[''n'',''n'',''r''] be an array of backpointing triples. for each s = 1 to n for each unit production Rv →as set P[''1'',''s'',''v''] = Pr(Rv →as) for each l = 2 to n -- Length of span for each s = 1 to n-l+1 -- Start of span for each p = 1 to l-1 -- Partition of span for each production Ra → Rb Rc prob_splitting = Pr(Ra →Rb Rc) * P[''p'',''s'',''b''] * P[''l''-''p'',''s''+''p'',''c''] if prob_splitting > P[''l'',''s'',''a''] then set P[''l'',''s'',''a''] = prob_splitting set back[''l'',''s'',''a''] =
if P[n,''1'',''1''] > 0 then find the parse tree by retracing through back return the parse tree else return "not a member of language"
In informal terms, this algorithm considers every possible substring of the input string and sets
P[l,s,v]
l
s
Rv
A\toB C
B
C
A
This is an example grammar:
Now the sentence she eats a fish with a fork is analyzed using the CYK algorithm. In the following table, in
P[i,j,k]
S | |||||||
VP | |||||||
S | |||||||
VP | PP | ||||||
S | NP | NP | |||||
NP | V, VP | Det. | N | P | Det | N | |
she | eats | a | fish | with | a | fork |
For readability, the CYK table for P is represented here as a 2-dimensional matrix M containing a set of non-terminal symbols, such that is in if, and only if, .In the above example, since a start symbol S is in, the sentence can be generated by the grammar.
The above algorithm is a recognizer that will only determine if a sentence is in the language. It is simple to extend it into a parser that also constructs a parse tree, by storing parse tree nodes as elements of the array, instead of the boolean 1. The node is linked to the array elements that were used to produce it, so as to build the tree structure. Only one such node in each array element is needed if only one parse tree is to be produced. However, if all parse trees of an ambiguous sentence are to be kept, it is necessary to store in the array element a list of all the ways the corresponding node can be obtained in the parsing process. This is sometimes done with a second table B[n,n,r] of so-called backpointers.The end result is then a shared-forest of possible parse trees, where common trees parts are factored between the various parses. This shared forest can conveniently be read as an ambiguous grammar generating only the sentence parsed, but with the same ambiguity as the original grammar, and the same parse trees up to a very simple renaming of non-terminals, as shown by .
As pointed out by, the drawback of all known transformations into Chomsky normal form is that they can lead to an undesirable bloat in grammar size. The size of a grammar is the sum of the sizes of its production rules, where the size of a rule is one plus the length of its right-hand side. Using
g
g2
22
It is also possible to extend the CYK algorithm to parse strings using weighted and stochastic context-free grammars. Weights (probabilities) are then stored in the table P instead of booleans, so P[i,j,A] will contain the minimum weight (maximum probability) that the substring from i to j can be derived from A. Further extensions of the algorithm allow all parses of a string to be enumerated from lowest to highest weight (highest to lowest probability).
When the probabilistic CYK algorithm is applied to a long string, the splitting probability can become very small due to multiplying many probabilities together. This can be dealt with by summing log-probability instead of multiplying probabilities.
The worst case running time of CYK is
\Theta(n3 ⋅ |G|)
Using the Coppersmith–Winograd algorithm for multiplying these matrices, this gives an asymptotic worst-case running time of
O(n2.38 ⋅ |G|)
O(n3-\varepsilon ⋅ |G|)
(n x n)
O(n3)