Download presentation

Presentation is loading. Please wait.

Published byBryce Ballon Modified over 3 years ago

1
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 語言 ( 技巧篇 )”.

2
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j)

3
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) First, initial f(0) = -1

4
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 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

5
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 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 01 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1

6
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 01 Not 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]

7
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 01 0123 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]

8
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 01 0123 Not 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]

9
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 01 01232 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

10
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function j012345678910 p[]ababbababaa f(j) 01 012320 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

11
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0

12
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0 Else if s[i] != p[j] and j == 0, i++

13
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0 Else if s[i] != p[j] and j == 0, i++

14
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0 Else if s[i] != p[j] and j == 0, i++

15
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0 Else if s[i] != p[j] and j == 0, i++

16
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare with s[i] and p[j], start with i = j = 0 Else if s[i] != p[j] and j == 0, i++

17
Knuth-Morris-Pratt Pattern Matching Algorithm Use failure function Compare with a string s[] : abaabababbababaa p[]ababbababaa f(j) 01 012320 j012345678910 If s[i] != p[j] and j != 0, j = f(j-1)+1 Compare 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

Similar presentations

OK

Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.

Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on grease lubrication in rolling Ppt on power quality problems Ppt on network switching modes Ppt on weapons of mass destruction robin File type ppt online voting system Ppt on nepal earthquake Ppt on regular expression validator Ppt on artificial intelligence in power station Ppt on film industry bollywood life Ppt on rc coupled amplifiers