0% found this document useful (0 votes)
18 views33 pages

26-CYK Algorithm-20-02-2024

Uploaded by

chandan shekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views33 pages

26-CYK Algorithm-20-02-2024

Uploaded by

chandan shekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

The CYK Algorithm

Table Filling Algorithm or


Membership Algorithm
The CYK Algorithm
• The membership problem:
– Problem:
• Given a context-free grammar G and a string w
– G = (V, ∑ ,P , S) where
» V finite set of variables
» ∑ (the alphabet) finite set of terminal symbols
» P finite set of rules
» S start symbol (distinguished element of V)
» V and ∑ are assumed to be disjoint
– G is used to generate the string of a language
– Question:
• Is w in L(G)?
The CYK Algorithm
• J. Cocke
• D. Younger,
• T. Kasami

– Independently developed an algorithm to answer


this question.
The CYK Algorithm Basics

– In CYK the CFG grammar used follows a Structure of


production rules which is in Chomsky Normal Form
Grammar(CNF)

– Uses a “dynamic programming” or “table-filling


algorithm”
• CNF (Normal form used to derive a string which takes
exponential in |x| or even less.
• Where as CYK algorithm is based on the idea of Dynamic
Programming which is also known as table filling
algorithm.
• To apply CYK algorithm convert the given Grammar into
CNF form.
• The input to the algorithm is w .
• Time taken to construct the table is O(n3)
• CNF is Binary tree where if length of W is n then there
will be exactly 2n-1 nodes labelled by variables in the
tree.
• The no:of possible trees and node-labelling is thus “only
exponential in n.
• CYK Algorithm
Chomsky Normal Form(CNF)
• Normal Form is described by a set of conditions
that each production rule in the grammar must
satisfy.
• Context-free grammar is in CNF if each
production rule has one of the following forms:
A  BC 2 Non Terminal symbols on RHS
or
Aa terminal symbol on RHS
where B, C Є V – {S}
Construct a Triangular Table
• In a Table Each row corresponds to one length of
substrings
– Bottom Row – Strings of length 1
– Second from Bottom Row – Strings of length 2
…………………………….

Top Row – string ‘w’


Construct a Triangular Table
• Xi, i is the set of variables A such that
A  wi is a production of G A a (CNF)
• Compare at most n pairs of previously computed sets:
Here productions of the form
A BC considered (CNF)
Compute Xi, j (j>i)
(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
Example :
X1 , 2 = (Xi ,i, Xi+1 , j) = (X1 , 1 , X2 , 2)
Construct a Triangular Table
X1, 5
X1, 4 X2, 5
X1, 3 X2, 4 X3, 5
X1, 2 X2, 3 X3, 4 X4, 5
X1, 1 X2, 2 X3, 3 X4, 4 X5, 5

Table for W1 string ‘w=w


W2w w w w ’ W
1 2 3 4 5 that 3 has length W4 5 W5

i.e., if w =baaba Xi, j (j>i)= (Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
S  AB | BC X1, 3 = (X1 , 1 , X2 , 3) , (X1 , 2 , X3 , 3)
A  BA | a
B  CC | b
C  AB | a
Construct a Triangular Table
X1, 5
X1, 4 X2, 5
X1, 3 X2, 4 X3, 5
X1, 2 X2, 3 X3, 4 X4, 5
X1, 1 X2, 2 X3, 3 X4, 4 X5, 5
w1 w2 w3 w4 w5

Looking for pairs to compare


Example CYK Algorithm
• Show the CYK Algorithm with the following
example:
– CNF grammar G
S  AB | BC
A  BA | a
B  CC | b
C  AB | a
– w is baaba
– Question Is baaba in L(G)?
Constructing The Triangular Table
S  AB | BC
A  BA | a
B  CC | b
C  AB | a

{B} {A, C} {A, C} {B} {A, C}


b a a b a

Calculating the Bottom ROW


Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
• X1 , 2 = (Xi , i ,Xi+1 , j) = (X1 , 1 , X2 , 2)
•  {B}{A,C} = {BA, BC}
• Steps:
–Look for production rules to generate BA or BC
S  AB | BC
–There are two: S and A A  BA | a
–X1 , 2 = {S, A} B  CC | b
C  AB | a
Constructing The Triangular Table

{S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j ) Xi,j
for ( j> i)

•X2 , 3 = (Xi , i ,Xi+1 , j) = (X2 , 2 , X3 , 3)


• {A, C}{A,C} = {AA, AC, CA, CC} = Y
•Steps:
– Look for production rules to generate Y S  AB | BC
A  BA | a
– There is one: B B  CC | b
C  AB | a
– X2 , 3 = {B}
Constructing The Triangular Table

{S, A} {B}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X3 , 4 = (Xi , i ,Xi+1 , j) = (X3 , 3 , X4 , 4)
• {A, C}{B} = {AB, CB} = Y
•Steps:
– Look for production rules to generate YS  AB | BC
– There are two: S and C A  BA | a
B  CC | b
– X3 , 4 = {S, C} C  AB | a
Constructing The Triangular Table

{S, A} {B} {S, C}


{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X4 , 5 = (Xi , i ,Xi+1 , j) = (X4 , 4 , X5 , 5)
• {B}{A, C} = {BA, BC} = Y
•Steps:
– Look for production rules to generate YS  AB | BC
– There are two: S and A A  BA | a
B  CC | b
– X4 , 5 = {S, A} C  AB | a
Constructing The Triangular Table

{S, A} {B} {S, C} {S, A}


{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X1 , 3 = (Xi , i ,Xi+1 , j) (Xi , i+1 ,Xi+2 , j)
= (X1 , 1 , X2 , 3) , (X1 , 2 , X3 , 3)
 {B}{B} U {S, A}{A, C}= {BB, SA, SC, AA, AC} = Ø
•Steps: S  AB | BC
– Look for production rules to generate Y A  BA | a
B  CC | b
– There are NONE: S and A C  AB | a

– X1 , 3 = Ø
– no elements in this set (empty set)
Constructing The Triangular Table

Ø
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X2 , 4 = (Xi , i ,Xi+1 , j) (Xi , i+1 ,Xi+2 , j)
= (X2 , 2 , X3 , 4) , (X2 , 3 , X4 , 4)
• {A, C}{S, C} U {B}{B}= {AS, AC, CS, CC, BB} = B
•Steps: S  AB | BC
A  BA | a
– Look for production rules to generate Y B  CC | b
– There is one: B C  AB | a

– X2 , 4 = {B}
Constructing The Triangular Table

Ø {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X3 , 5 = (Xi , i ,Xi+1 , j) (Xi , i+1 ,Xi+2 , j)
= (X3 , 3 , X4 , 5) , (X3 , 4 , X5 , 5)
• {A,C}{S,A} U {S,C}{A,C}
= {AS, AA, CS, CA, SA, SC, CA, CC} = B
•Steps: S  AB | BC
– Look for production rules to generate Y A  BA | a
B  CC | b
– There is one: B C  AB | a
– X3 , 5 = {B}
Constructing The Triangular Table

Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X1 , 4 = (Xi , i ,Xi+1 , j) (Xi , i+1 ,Xi+2 , j) (Xi , i+2 ,Xi+3, j )
= (X1 , 1 , X2 , 4) , (X1 , 2 , X3 , 4) (X1 , 3 , X4, 4 )
• {B}{B} U {S,A} {S,C} U {Ø } { B}
= {BB,SS,SC,AS,AC, Ø } = Y = Ø
•Steps: S  AB | BC
– Look for production rules to generate Y A  BA | a
B  CC | b
– There is one: B C  AB | a
– X1, 4 = {Ø }
Constructing The Triangular Table
Xi, j (j>i)=(Xi, i , Xi+1, j ), (Xi, i+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )
•X1 , 5 = (Xi , i ,Xi+1 , j) (Xi , i+1 ,Xi+2 , j) (Xi , i+2 ,Xi+3, j ) (Xi , i+3 ,Xi+4, j )
= (X1 , 1 , X2 , 5) , (X1 , 2 , X3 , 5) (X1 , 3 , X4, 5 ) (X1 , 4 , X5, 5 )
• {B}{S,A,C} U {S,A} {B} U {Ø } { S,A} U{Ø} {A,C}
= {BS,BA,BC,SB,AB, Ø, Ø } = Y = {S,A,C}
•Steps:
S  AB | BC
– Look for production rules to generate Y A  BA | a
– There is one: B B  CC | b
C  AB | a
– X1, 5 = {S,A,C }
Final Triangular Table
{S, A, C}  X1, 5
Ø {S, A, C}
Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a

- Table for string ‘w’ that has length 5


- The algorithm populates the triangular table
Example (Result)
• Is baaba in L(G)?

Yes

We can see the S in the set X1n where ‘n’ = 5


We can see the table
the cell X15 = (S, A, C) then
if S Є X15 then baaba Є L(G)
Theorem

• The CYK Algorithm correctly computes X i j for


all i and j; thus w is in L(G) if and only if S is in
X1n.
• The running time of the algorithm is O(n3).
References
• J. E. Hopcroft, R. Motwani, J. D. Ullman,
Introduction to Automata Theory, Languages
and Computation, Second Edition, Addison
Wesley, 2001
• T.A. Sudkamp, An Introduction to the Theory
of Computer Science Languages and
Machines, Third Edition, Addison Wesley,
2006

You might also like