โ† Wiki Index / Computation & Algorithms / CYK Algorithm
๐Ÿ’ป Computation & Algorithms

CYK Algorithm

The CYK algorithm is a classic bottom-up dynamic programming method for determining whether a string belongs to a context-free language and constructing its parse trees in cubic time.

The CYK algorithm (named after its independent co-inventors John Cocke, Daniel Younger, and Tadao Kasami) is a bottom-up dynamic programming algorithm that decides membership and constructs parse trees for any context-free grammar (CFG) expressed in Chomsky Normal Form (CNF). Developed during the 1960s (Younger, 1967), the algorithm evaluates all contiguous substrings of length 1 through n in a triangular parse table, achieving a worst-case time complexity of O(nยณ ยท |G|) and space complexity of O(nยฒ ยท |G|), where n is the length of the input string and |G| is the size of the grammar.

Formal Grammar Prerequisites: Chomsky Normal Form

Before running the CYK algorithm, an arbitrary context-free grammar G = (V, ฮฃ, R, S) must be converted into Chomsky Normal Form (Chomsky, 1959), in which every production rule takes one of two restricted shapes:

  • Binary non-terminal rules: A โ†’ BC, where A, B, and C are non-terminal symbols in V (and B, C are not the start symbol S).
  • Terminal rules: A โ†’ a, where a is a single terminal symbol in ฮฃ (with S โ†’ ฮต permitted only if the empty string belongs to the language).

Converting an arbitrary CFG to CNF eliminates ฮต-productions, unit rules (A โ†’ B), and long right-hand sides by introducing auxiliary non-terminals, increasing grammar size by at most a polynomial factor while preserving the exact generated language.

Dynamic Programming Table and Recurrence

Bottom-Up Substring Indexing

Given an input string w = aโ‚aโ‚‚ยทยทยทaโ‚™ of length n, the CYK algorithm maintains a two-dimensional triangular table P[i, l], where i โˆˆ {1, ..., n} denotes the starting position and l โˆˆ {1, ..., n โˆ’ i + 1} denotes the substring length. Each entry P[i, l] stores the set of non-terminals A โˆˆ V that can derive the substring w[i .. i + l โˆ’ 1].

Base Case and Inductive Step

  1. Length l = 1 (Base Case): For each position i from 1 to n, populate P[i, 1] with all non-terminals A such that A โ†’ aแตข is a production rule in R.
  2. Lengths l = 2 to n (Inductive Step): For each starting index i from 1 to n โˆ’ l + 1 and each split point k from 1 to l โˆ’ 1, inspect pairs (B, C) such that B โˆˆ P[i, k] and C โˆˆ P[i + k, l โˆ’ k]. Whenever a binary rule A โ†’ BC exists in R, insert A into P[i, l].
  3. Acceptance: The input string w belongs to the language L(G) if and only if the start symbol S appears in the top cell P[1, n].

By recording backpointers alongside each non-terminal added to P[i, l], the algorithm reconstructs one or all valid parse treesโ€”or a compact shared packed parse forest (SPPF) for ambiguous grammarsโ€”in polynomial space.

Complexity Analysis and Probabilistic Extensions

Worst-Case Bounds and Matrix Multiplication Reduction

Because the triple nested loop iterates over substring length l (n steps), starting index i (O(n) steps), and split position k (O(n) steps), standard CYK executes in ฮ˜(nยณ ยท |G|) time for general CNF grammars. In 1975, Leslie Valiant proved that general context-free recognition can be reduced to Boolean matrix multiplication, improving the theoretical asymptotic bound in n to O(nฯ‰), where ฯ‰ < 2.372 is the matrix-multiplication exponent, though standard cubic CYK remains faster in practice for moderate string lengths due to low constant factors.

FeatureCYK AlgorithmEarley ParserLL / LR Parsers
Grammar ClassArbitrary CFG (in CNF)Arbitrary CFG (no CNF needed)Deterministic subclasses (LL(k), LR(k))
StrategyBottom-up dynamic programmingTop-down chart parsing with predictionPredictive / shift-reduce deterministic
Worst-Case TimeO(nยณ ยทG)O(nยณ) general; O(nยฒ) unambiguousO(n) linear time
Natural Language UseProbabilistic CFG (PCFG) parsingAmbiguous chart parsingCompiler front-ends (rare in NLP)

Probabilistic CYK in Computational Linguistics and Bioinformatics

In natural language processing, Probabilistic CYK extends each table entry P[i, l] to store the maximum-probability derivation (Viterbi-style maximization) or total inside probability for each non-terminal under a Probabilistic Context-Free Grammar (PCFG). In computational biology, the same cubic dynamic-programming structure underlies RNA secondary-structure prediction and stochastic context-free grammar (SCFG) covariance models, where base-paired stems correspond to nested context-free productions folded across nucleic-acid sequences to identify conserved non-coding RNAs and riboswitches across microbial, plant, and eukaryotic genomes worldwide in comparative genomics.

Worked Triangular Table Example

Consider a CNF grammar G with start symbol S and rules S โ†’ AB | BC, A โ†’ BA | a, B โ†’ CC | b, C โ†’ AB | a, tested against input string w = baaba (n = 5):

  • Row l = 1 (Terminal Base Case): The five single-character cells are populated directly from terminal productions: P[1, 1] = {B}, P[2, 1] = {A, C}, P[3, 1] = {A, C}, P[4, 1] = {B}, and P[5, 1] = {A, C}.
  • Row l = 2 (Pairs): For w[1..2] = ba, combining P[1, 1] = {B} and P[2, 1] = {A, C} matches A โ†’ BA and S โ†’ BC, giving P[1, 2] = {S, A}. Similarly, P[2, 2] = {B} (from CC), P[3, 2] = {S, C} (from AB and CB), and P[4, 2] = {S, A} (from BA and BC).
  • Rows l = 3 to 5 (Bottom-Up Completion): Testing every split k โˆˆ {1, ..., l โˆ’ 1} populates P[1, 3] = โˆ…, P[2, 3] = {S, B}, P[3, 3] = {B}, then P[1, 4] = {S, A}, P[2, 4] = {S, A, C}, and finally the apex cell P[1, 5] = {S, A, C}. Because S โˆˆ P[1, 5], w = baaba belongs to L(G), and following stored split indices k reconstructs every valid binary parse tree.

Frequently Asked Questions

Why does the CYK algorithm require Chomsky Normal Form?

Every inductive step splits a substring of length l into exactly two adjacent non-empty substrings (w[i..i+kโˆ’1] and w[i+k..i+lโˆ’1]), which corresponds directly to binary rules A โ†’ BC. Eliminating ฮต-rules and unary chain rules A โ†’ B ensures that every subproblem references strictly shorter substrings, preventing cyclic dependencies inside each table cell.

How can Earley's algorithm parse arbitrary CFGs in the same cubic worst-case time without CNF conversion?

Earley's parser maintains dotted production items along a chart and handles unary rules, ฮต-productions, and arbitrary right-hand-side lengths dynamically via Predictor, Scanner, and Completer operations, achieving O(nยณ) worst-case time (and O(nยฒ) on unambiguous grammars) without expanding the grammar into Chomsky Normal Form beforehand.

References

  1. Younger, D. H. (Feb 1967). Recognition and parsing of context-free languages in time nยณ. Information and Control, 10(2), 189โ€“208. https://doi.org/10.1016/S0019-9958(67)80007-X
  2. Chomsky, N. (Jun 1959). On certain formal properties of grammars. Information and Control, 2(2), 137โ€“167. https://doi.org/10.1016/S0019-9958(59)90362-6
  3. Valiant, L. G. (Apr 1975). General context-free recognition in less than cubic time. Journal of Computer and System Sciences, 10(2), 308โ€“315. https://doi.org/10.1016/S0022-0000(75)80046-8
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision