0% found this document useful (0 votes)
38 views3 pages

CYK Algorithm For String Parsing

Uploaded by

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

CYK Algorithm For String Parsing

Uploaded by

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

CYK Algorithm for String Parsing

Exercise 4.1: Apply CYK Algorithm to String


baaba
Given the grammar rules:

S → AB | BC
A → BA | a
B → CC | b
C → AB | a
We will apply the CYK algorithm to the string baaba.

Step 1: Initialize Table for Single Characters


For each character in the string:

• b at position 1: B → b, so T [1, 1] = {B}


• a at position 2: A → a and C → a, so T [2, 2] = {A, C}
• a at position 3: A → a and C → a, so T [3, 3] = {A, C}

• b at position 4: B → b, so T [4, 4] = {B}


• a at position 5: A → a and C → a, so T [5, 5] = {A, C}

Step 2: Fill the Table for Substrings of Length 2 (l = 2)


• Substring ba (positions 1-2):

T [1, 2] = {A} because B → BA (with T [2, 2] = {A})

• Substring aa (positions 2-3):

T [2, 3] = {A, C} because A → BA and C → AB

1
• Substring ab (positions 3-4):
T [3, 4] = {S} because S → AB

• Substring ba (positions 4-5):


T [4, 5] = {A} because B → BA

Step 3: Fill the Table for Substrings of Length 3 (l = 3)


• Substring baa (positions 1-3):
T [1, 3] = {A, S} because B → BA and S → AB

• Substring aab (positions 2-4):


T [2, 4] = {S} because S → AB

• Substring aba (positions 3-5):


T [3, 5] = {S} because S → AB

Step 4: Fill the Table for Substrings of Length 4 (l = 4)


• Substring baab (positions 1-4):
T [1, 4] = {S, B} because S → AB

• Substring aaba (positions 2-5):


T [2, 5] = {S} because S → AB

Step 5: Fill the Table for the Full String (l = 5)


Substring baaba (positions 1-5):
T [1, 5] = {S} because S → AB
Since S is in the top-right cell T [1, 5], the string baaba is accepted by the
grammar.

Exercise 4.2: Building the Algorithm


The algorithm follows these steps:
1. Initialize a table T [i, j] of size n × n, where n is the length of the input
string.
2. For each diagonal entry, check if a single character can be derived from a
terminal symbol. Populate the diagonal entries accordingly.
3. For each substring of increasing length l = 2 to n, fill in the table by
checking all possible split points and applying the grammar rules.
4. After filling the table, check if S is in the top-right cell T [1, n].

2
Exercise 4.3: Time Complexity
The time complexity of the CYK algorithm is O(n3 |G|), where:

• n is the length of the input string,


• |G| is the number of rules in the grammar.

The reasoning is as follows:

• The algorithm processes all substrings of length 1 to n, leading to O(n3 )


complexity due to the three nested loops: one for the substring length,
one for the starting point, and one for the split point.
• For each substring, all grammar rules must be checked, adding a factor of
|G|.

Thus, the total time complexity is O(n3 |G|).

You might also like