120 likes | 269 Views
DFA Construccion para KMP. Reference: Chapter 19, Algorithms in C, 2 nd Edition, Robert Sedgewick. DFA Construction for KMP. Search Pattern. j. pattern[1..j]. X. next. a. a. b. a. a. a. b. b. a. b. 0. DFA Construction for KMP. Search Pattern. j. pattern[1..j]. X. next. a.
E N D
DFA Construccion para KMP Reference: Chapter 19, Algorithms in C, 2nd Edition, Robert Sedgewick.
DFA Construction for KMP Search Pattern j pattern[1..j] X next a a b a a a b b a b 0
DFA 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
DFA 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
DFA 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
DFA 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
DFA 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
DFA 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
DFA 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
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 DFA 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
DFA Construction for KMP: Implementation • Build DFA for KMP. • Takes O(M) time. • Requires O(M) extra space to store next[] table. int X =0; int[] next =newint[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)