1 / 60

CYK Algorithm

CYK Algorithm. Introduction. Problem: Given a context free grammar and a string s is it possible to decide whether s can be generated by the grammar or not? If the grammar is not in a very special form this is not so efficient.

bianca
Télécharger la présentation

CYK Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CYK Algorithm

  2. Introduction • Problem: Given a context free grammar and a string s is it possible to decide whether s can be generated by the grammar or not? • If the grammar is not in a very special form this is not so efficient. • If the grammar is in Chomsky Normal Form, we have an elegant algorithm for testing this, the CYK algorithm.

  3. The CYK algorithm • Suppose that we are given a grammar in Chomsky Normal form S → AB A → BB | 0 B → AA |1 • We would like to see if 10110 is generated by this grammar or not.

  4. Substrings of length 1 • Since the only way to produce terminals is by following the rules A → a, just replace every terminal with the variables that produce it. 1 0 1 1 0 B A B B A

  5. Substrings of length 2 Suppose now that we want to see how every substring of length 2 can be generated. This is equivalent with finding ways to produce all the length 2 substrings where terminals are replaced with the variables that represent them. But since every rule is of the form A → BC, it suffices to replace every two consecutive variables with the variables that produce them. 1 0 1 1 0 B A B B A - S A -

  6. Substrings of length 3 • To produce the substring 101 (in 10110) we can either take 1 with 01 or 10 with 1. Here BS cannot be produced by any variable. 10 1 1 0 B A B B A - S A - -

  7. Substrings of length 3 • To produce the substring 101 (in 10110) we can either take 1 with 01 or 10 with 1. Here we don’t have a pair since 10 cannot be produced. 1 01 1 0 B A BB A - S A - -

  8. Substrings of length 3 • To produce the substring 011 (in 10110) we can either take 0 with 11 or 01 with 1. Here AA can be produced by B. 101 1 0 B A B B A - S A - - B

  9. Substrings of length 3 • To produce the substring 011 (in 10110) we can either take 0 with 11 or 01 with 1. Here SB cannot be produced by any variable 1 0 11 0 B A B B A - S A - - B

  10. Substrings of length 3 • To produce the substring 110 (in 10110) we can either take 1 with 10 or 11 with 0. Here we don’t have a pair since 10 cannot be produced by a variable. 1011 0 B A BB A - S A - - B -

  11. Substrings of length 3 • To produce the substring 110 (in 10110) we can either take 1 with 10 or 11 with 0. Here AA can be produced by B 10 110 B A B BA - S A - - B B

  12. Substrings of length 4 • To produce the substring 1011 (in 10110) we can take 1 with 011 or 10 with 11, or 101 with 1. Here BB can be produced by A. 10 1 1 0 B A B B A - S A - - BB A

  13. Substrings of length 4 • To produce the substring 1011 (in 10110) we can take 1 with 011 or 10 with 11, or 101 with 1. Here we don’t have a pair since 10 cannot be produced. 1 011 0 B A B B A - S A - - B B A

  14. Substrings of length 4 • To produce the substring 1011 (in 10110) we can take 1 with 011 or 10 with 11, or 101 with 1. Here we don’t have a pair since 101 cannot be produced. 1 0 11 0 B A B B A - S A - - B B A

  15. Substrings of length 4 • To produce the substring 0110 (in 10110) we can take 0 with 110 or 01 with 10, or 011 with 0. Here AB can be produced by S. 101 1 0 B A B B A - S A - - B B A S

  16. Substrings of length 4 • To produce the substring 0110 (in 10110) we can take 0 with 110 or 01 with 10, or 011 with 0. Here we don’t have a pair since 10 cannot be produced. 1 0110 B A B B A - S A - - B B A S

  17. Substrings of length 4 • To produce the substring 0110 (in 10110) we can take 0 with 110 or 01 with 10, or 011 with 0. Here BA cannot be produced by any variable. 1 0 1 10 B A B BA - S A - - BB A S

  18. Combine previous solutions • In order now to produce the whole string 10110 we can take 1 with 0110 or 10 with 110 or 101 with 10, or 1011 with 0. Here, BS cannot be produced. 1 0 1 1 0 B A B B A - S A - - B B A S -

  19. Combine previous solutions • In order now to produce the whole string 10110 we can take 1 with 0110 or 10 with 110 or 101 with 10, or 1011 with 0. Here we don’t have a pair. 1 0 1 1 0 B A B B A - S A - - B B A S -

  20. Combine previous solutions • In order now to produce the whole string 10110 we can take 1 with 0110 or 10 with 110 or 101 with 10, or 1011 with 0. Here we don’t have a pair. 1 01 1 0 B A B B A - S A - - B B A S -

  21. Combine previous solutions • In order now to produce the whole string 10110 we can take 1 with 0110 or 10 with 110 or 101 with 10, or 1011 with 0. Here, AA is produced by B. 1 0 1 10 B A B BA - S A - - B B A S B

  22. Answer • If the last line contains the start variable S, then there is a way to produce the string else the string cannot be generated. For our example 10110 cannot be generated.

  23. Mechanical way • Now that we show why this method works lets give an easy way to compute the table • Suppose that we are about to fill in the position with the cycle. We take the pairs that the arrows designate 10 1 1 0 B A B B A - S A - - B B A S

  24. Mechanical way • Now that we show why this method works lets give an easy way to compute the table • Suppose that we are about to fill in the position with the cycle. We take the pairs that the arrows designate 10 1 1 0 B A B B A - S A - - B B A S

  25. Mechanical way • Now that we show why this method works lets give an easy way to compute the table • Suppose that we are about to fill in the position with the cycle. We take the pairs that the arrows designate 10 1 1 0 B A B B A - S A - - B B A -

  26. Mechanical way • Now that we show why this method works lets give an easy way to compute the table • Suppose that we are about to fill in the position with the cycle. We take the pairs that the arrows designate 10 1 1 0 B A B BA - S A - - BB A -

  27. Mechanical way • So finally: 10 1 1 0 B A B B A - S A - - B B A S

  28. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1

  29. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB

  30. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB -

  31. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B ABBB - S

  32. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A BBB - S A

  33. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A A

  34. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - -

  35. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A BBB - S A - -

  36. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B

  37. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B

  38. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A BBB - S A - - B -

  39. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S

  40. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A

  41. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 BA B BB - S A - - B S A

  42. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A

  43. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A -

  44. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 BA B BB - S A - - B S A -

  45. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A A

  46. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A A -

  47. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A A -

  48. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A A -

  49. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S AA S

  50. A string that is produced • Run the CYK algorithm for the string 10111 10 1 1 1 B A B BB - S A - - B S A A S The derivation is: S

More Related