knuth-morris-pratt pattern matching algorithm instructor : prof. jyh-shing roger jang designer :...

17
Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “ and “ 名名名名名名 - 名名 C 名名 ( 名名名 )”.

Upload: bryce-ballon

Post on 31-Mar-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Instructor : Prof. Jyh-Shing Roger Jang

Designer : Shao-Huan WangThe ideas are reference to the textbook “Fundamentals of Data Structures in C “ and “名題精選百則 - 使用 C 語言 ( 技巧篇 )”.

Page 2: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j)

Page 3: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1

First, initial f(0) = -1

Page 4: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

-1

Page 5: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

0 1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1

Page 6: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1 0 1 -1Not equal!

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j]

Page 7: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1 0 1 -1 0 1 2 3

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j]

Page 8: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1 0 1 -1 0 1 2 3Not equal!

If p[i+1] == p[j], put i+1

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1

Equal!2

If i == -1 compare p[0] and p[j]

Page 9: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1 0 1 -1 0 1 2 3 2

If p[i+1] == p[j], put i+1

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1

Not equal!

If i == -1 compare p[0] and p[j]

If i == -1 and p[0] == p[j], put i+1

0

Page 10: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function

j 0 1 2 3 4 5 6 7 8 9 10

p[] a b a b b a b a b a a

f(j) -1 -1 0 1 -1 0 1 2 3 2 0

If p[i+1] == p[j], put i+1

First, initial f(0) = -1

j++, i = f(j-1), then compare p[i+1] and p[j]

If p[i+1] != p[j] and f(i-1) == -1, put -1

Else if p[i+1] == p[j], put i+1

Else if p[i+1] != p[j] and i != -1, let i = f(i),until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j]

If i == -1 and p[0] == p[j], put i+1

Page 11: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Page 12: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++

Page 13: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++

Page 14: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++

Page 15: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++

Page 16: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++

Page 17: Knuth-Morris-Pratt Pattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook

Knuth-Morris-PrattPattern Matching Algorithm

Use failure function Compare with a string s[] :

a b a a b a b a b b a b a b a a

p[]

a b a b b a b a b a a

f(j)

-1 -1 0 1 -1 0 1 2 3 2 0

j 0 1 2 3 4 5 6 7 8 9 10

If s[i] != p[j] and j != 0, j = f(j-1)+1Compare with s[i] and p[j], start with i = j = 0

Else if s[i] != p[j] and j == 0, i++Following the rules and you can finish this comparison