0% found this document useful (0 votes)
19 views

21 DynamicProgramming (HR 2016) CYK ProjDef Finalized

Uploaded by

amiralifm1407
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)
19 views

21 DynamicProgramming (HR 2016) CYK ProjDef Finalized

Uploaded by

amiralifm1407
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/ 35

An Introduction to

Algorithms
By
Hossein Rahmani
[email protected]
http://webpages.iust.ac.ir/h_rahmani/

1
Intro

Complexity Data Structure Trees

Dynamic
Programming Sorting Hash Functions

Greedy Algorithm Misc Graph/Tree


Algorithms

2
Languages

3
Languages
A language is a set of strings

String: A sequence of letters

Examples: “cat”, “dog”, “house”, …

Defined over an alphabet:


  a, b, c,, z 4
Alphabets and Strings
We will use small alphabets:   a, b
a
Strings
ab u  ab
abba v  bbbaaa
baba w  abba
aaabbbaabab
5
Grammars

6
Grammars
Grammars express languages

Example: the English language

sentence  noun _ phrase predicate

noun _ phrase  article noun


7
article  a
article  the

noun  boy
noun  dog

verb  runs
verb  walks 8
A derivation of “the boy walks”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the boy verb
 the boy walks
9
A derivation of “a dog runs”:
sentence  noun _ phrase predicate
 noun _ phrase verb
 article noun verb
 a noun verb
 a dog verb
 a dog runs
10
Language of the grammar:

L = { “a boy runs”,
“a boy walks”,
“the boy runs”,
“the boy walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
11
Notation
noun  boy
noun  dog

Variable Terminal
Production
or
rule
Non-terminal

12
Another Example
S  aSb
Grammar: S 

Derivation of sentence :
ab

S  aSb  ab

S  aSb S 
13
Grammar: S  aSb
S 

Derivation of sentence aabb


:

S  aSb  aaSbb  aabb

S  aSb S 
14
Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

15
Language of the grammar

S  aSb
S 

n n
L  {a b : n  0}

16
Example
S  Ab
For grammar G: A  aAb
A

L(G)  {???}

17
Example
S  Ab
For grammar G: A  aAb
A

n n
L(G )  {a b b : n  0}


n n
Since: S a b b
18
A grammar G : S  aSb
S  SS
S 

A derivation:

S  SS  aSbS  abS  abaSb  abab


19
S  aSb
S  SS
S 
L(G )  {w : na ( w)  nb ( w),
and na (v)  nb (v)
in any prefix v}

() ((( ))) (( ))
20
Chomsky Normal Form

21
Noam Chomsky: The United States, Not Iran, Poses
Greatest Threat to World Peace

22
Chomsky Normal Form
All productions have form:

A  BC and Aa

variable variable terminal

23
Examples:

S  AS S  AS
S a S  AAS
A  SA A  SA
Ab A  aa
Chomsky Not Chomsky
Normal Form Normal Form

24
The CYK Membership Algorithm
(J. Cocke, D. H. Younger, and T. Kasami)

Input:

• Grammar G in Chomsky Normal Form

• String w

Output:

find if w L(G )
25
Construction: Dynamic Programming

26
Construction: Dynamic Programming

27
The Algorithm
Input example:
• Grammar G : S  AB
A  BB
Aa
B  AB
Bb

• String w : aabbb
28
aabbb
a a b b b

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

29
S  AB
A  BB a a b b b
Aa A A B B B
B  AB aa ab bb bb
Bb
aab abb bbb

aabb abbb

aabbb 30
S  AB
A  BB a a b b b
Aa A A B B B
B  AB aa ab bb bb
Bb S,B A A
aab abb bbb

aabb abbb

aabbb 31
S  AB
a a b b b
A  BB
A A B B B
Aa
aa ab bb bb
B  AB
S,B A A
Bb aab abb bbb
S,B A S,B
aabb abbb
A S,B
aabbb
S,B
32
Therefore: aabbb  L(G )

3
Time Complexity: | w|

Observation: The CYK algorithm can be


easily converted to a parser
33
Quiz 1
• Grammar G:

Question Is ababa in L(G)? Use dynamic


programming.

34
Quiz 2

• Is input string generated by Grammar G?

35

You might also like