Download presentation
Presentation is loading. Please wait.
1
Princeton University COS 226 Algorithms and Data Structures Spring 2004 Kevin Wayne http://www.Princeton.EDU/~cos226 DFA Construction for KMP Reference: Chapter 19, Algorithms in C, 2 nd Edition, Robert Sedgewick.
2
2 DFA Construction for KMP 0 b a aabaaabb Search Pattern Xpattern[1..j]jnext
3
3 DFA Construction for KMP 01 a b b0 a1 0 aabaaabb Search Pattern X 00 pattern[1..j]jnext 0
4
4 DFA Construction for KMP 01 aa 2 b b b0 a1 01 0 2 aabaaabb Search Pattern X 0 a11 0 pattern[1..j]jnext 0 0
5
5 DFA Construction for KMP 301 aa 2 b b b a b0 a1 012 0 2 3 2 aabaaabb Search Pattern X 0 ab0 a1 2 1 0 pattern[1..j]jnext 0 2 0
6
6 DFA Construction for KMP 34 a 01 aa 2 b b b b a b0 a1 0123 0 2 3 2 0 4 aabaaabb Search Pattern X 0 ab0 aba1 a1 2 1 3 0 pattern[1..j]jnext 0 2 0 0
7
7 DFA Construction for KMP 34 aa 5 01 aa 2 b b b b b a b0 a1 01234 0 2 3 2 0 4 0 5 aabaaabb Search Pattern abaa X 2 0 ab0 aba1 a1 2 1 4 3 0 pattern[1..j]jnext 0 0 2 0 0
8
8 DFA Construction for KMP 34 aa 56 a 01 aa 2 b b b b b b a b0 a1 012345 0 2 3 2 0 4 0 5 3 6 aabaaabb Search Pattern abaa X 2 abaaa2 0 ab0 aba1 a1 2 1 4 3 5 0 pattern[1..j]jnext 0 3 0 2 0 0
9
9 DFA Construction for KMP 34 aa 56 a 01 aa 2 b b 7 b b b b a b a b0 a1 0123456 0 2 3 2 0 4 0 5 3 6 7 2 aabaaabb Search Pattern abaa X 2 abaaa2 abaaab3 0 ab0 aba1 a1 2 1 4 3 5 0 6 pattern[1..j]jnext 0 3 2 0 2 0 0
10
10 DFA Construction for KMP 34 aa 56 a 01 aa 2 b b 7 8 b b b a b b a b a b0 a1 01234567 0 2 3 2 0 4 0 5 3 6 7 2 8 4 abaa X 2 abaaa2 abaaab3 0 ab0 aba1 a1 2 1 4 3 5 0 6 abaaabb07 aabaaabb Search Pattern pattern[1..j]jnext 0 3 2 0 2 0 0 4
11
11 DFA Construction for KMP: Implementation Build DFA for KMP. n Takes O(M) time. Requires O(M) extra space to store next[] table. int X = 0; int[] next = new int[M]; for (int j = 1; j < M; j++) { if (p.charAt(X) == p.charAt(j)) { // char match next[j] = next[X]; X = X + 1; } else { // char mismatch next[j] = X + 1; X = next[X]; } DFA Construction for KMP (assumes binary alphabet)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.