1 / 31

Knuth-Morris-Pratt

Knuth-Morris-Pratt. Reference: Chapter 19, Algorithms in C by R. Sedgewick. Addison Wesley, 1990. Knuth-Morris-Pratt. KMP algorithm. Use knowledge of how search pattern repeats itself. Build FSA from pattern. Run FSA on text. Search Pattern. a. a. b. a. a. a. a. a. a. a. b. a.

nevin
Download Presentation

Knuth-Morris-Pratt

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. Knuth-Morris-Pratt Reference: Chapter 19, Algorithms in C by R. Sedgewick.Addison Wesley, 1990.

  2. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern a a b a a a a a a a b a 0 1 3 4 2 5 6 accept state

  3. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern a a b a a a b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  4. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  5. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  6. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  7. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  8. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  9. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  10. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  11. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  12. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  13. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a a b a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  14. Knuth-Morris-Pratt • KMP algorithm. • Use knowledge of how search pattern repeats itself. • Build FSA from pattern. • Run FSA on text. Search Pattern Search Text a a b a a a a a b a a a b a a a b b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  15. FSA Representation • FSA used in KMP has special property. • Upon character match, go forward one state. • Only need to keep track of where to go upon character mismatch. • go to state next[j] if character mismatches in state j 0 1 2 3 4 5 a 1 2 2 4 5 6 Search Pattern b 0 0 3 0 0 3 a a b a a a next 0 0 2 0 0 3 b a b a a a a b a 0 1 3 4 2 5 6 b accept state b b

  16. int kmpsearch(char p[], char t[], int next[]) { int i, j = 0; int M = strlen(p); // pattern length int N = strlen(t); // text length for (i = 0; i < N; i++) { if (t[i] == p[j]) j++; // char match else j = next[j]; // char mismatch if (j == M) return i – M + 1; // found } return N; // not found } KMP String Search KMP Algorithm • Two key differences from brute force. • Text pointer i never backs up. • Need to precompute next[] table.

  17. FSA Construction for KMP • FSA construction for KMP. • FSA builds itself! • Example. Building FSA for aabaaabb. • State 6. p[0..5] = aabaaa • assume you know state for p[1..5] = abaaa X = 2 • if next char is b (match): go forward 6 + 1 = 7 • if next char is a (mismatch): go to state for abaaaa X + 'a' = 2 • update X to state for p[1..6] = abaaab X + 'b' = 3 b a b a a a a b a 0 1 3 4 2 5 6 b b b

  18. FSA Construction for KMP • FSA construction for KMP. • FSA builds itself! • Example. Building FSA for aabaaabb. b a b a a a a b a b 0 1 3 4 7 2 5 6 b b b a

  19. FSA Construction for KMP • FSA construction for KMP. • FSA builds itself! • Example. Building FSA for aabaaabb. • State 7. p[0..6] = aabaaab • assume you know state for p[1..6] = abaaab X = 3 • if next char is b (match): go forward 7 + 1 = 8 • next char is a (mismatch): go to state for abaaaba X + 'a' = 4 • update X to state for p[1..7] = abaaabb X + 'b' = 0 b a b a a a a b a b 0 1 3 4 7 2 5 6 b b b a

  20. FSA Construction for KMP • FSA construction for KMP. • FSA builds itself! • Example. Building FSA for aabaaabb. b a a b a a a a b b a b 0 1 3 4 7 2 5 6 8 b b b a

  21. FSA Construction for KMP • FSA construction for KMP. • FSA builds itself! • Crucial insight. • To compute transitions for state n of FSA, suffices to have: • FSA for states 0 to n-1 • state X that FSA ends up in with input p[1..n-1] • To compute state X' that FSA ends up in with input p[1..n], it suffices to have: • FSA for states 0 to n-1 • state X that FSA ends up in with input p[1..n-1]

  22. FSA Construction for KMP Search Pattern j pattern[1..j] X a a b a a a b b a b b a 0 1

  23. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 0 a 1 b 0 b a 0 1

  24. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 0 1 a 1 2 b 0 0 b a a 0 1 2 b

  25. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 a 1 2 2 b 0 0 3 a b a a b 0 1 3 2 b

  26. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 3 3 a b a 1 0 a 1 2 2 4 b 0 0 3 0 a b a a a b 0 1 3 4 2 b b

  27. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 3 4 3 a b a 1 0 a 1 2 2 4 5 4 a b a a 2 0 b 0 0 3 0 0 b a b a a a a b 0 1 3 4 2 5 b b

  28. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 3 4 5 3 a b a 1 0 a 1 2 2 4 5 6 4 a b a a 2 0 b 0 0 3 0 0 3 5 a b a a a 2 3 b a b a a a a b a 0 1 3 4 2 5 6 b b b

  29. FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 3 4 5 6 3 a b a 1 0 a 1 2 2 4 5 6 2 4 a b a a 2 0 b 0 0 3 0 0 3 7 5 a b a a a 2 3 6 a b a a a b 3 2 b a b a a a a b b a 0 1 3 4 7 2 5 6 b b b a

  30. b a a b a a a a b b a b 0 1 3 4 7 2 5 6 8 b b b a FSA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b 0 0 0 1 a 1 0 2 a b 0 2 0 1 2 3 4 5 6 7 3 a b a 1 0 a 1 2 2 4 5 6 2 4 4 a b a a 2 0 b 0 0 3 0 0 3 7 8 5 a b a a a 2 3 6 a b a a a b 3 2 7 a b a a a b b 0 4

  31. void kmpinit(char p[], int next[]) { int j, X = 0, M = strlen(p); next[0] = 0; for (j = 1; j < M; j++) { if (p[X] == p[j]) { // char match next[j] = next[X]; X = X + 1; } else { // char mismatch next[j] = X + 1; X = next[X]; } } } FSA Construction for KMP FSA Construction for KMP • Code for FSA construction in KMP algorithm.

More Related