java - Is this KMP implementation based on a DFA more efficient than a standard implementation? -
what complexity of deterministic finite state automaton based kmp algorithm? more efficient standard,non-automaton version of kmp algorithm?
class kmp { private final int r; private int[][] dfa; private string pat; public kmp(string pat) { this.r = 256; this.pat = pat; int m = pat.length(); dfa = new int[r][m]; dfa[pat.charat(0)][0] = 1; (int x = 0, j = 1; j < m; j++) { (int c = 0; c < r; c++) dfa[c][j] = dfa[c][x]; dfa[pat.charat(j)][j] = j+1; x = dfa[pat.charat(j)][x]; } } public int search(string txt) { int m = pat.length(); int n = txt.length(); int i, j; (i = 0, j = 0; < n && j < m; i++) { j = dfa[txt.charat(i)][j]; } if (j == m) return - m; return -1; } } test:
// test kmp dfa kmp p = new kmp("abacab"); system.out.println("kmpdfa: " + p.search("ababbadabacabcbabac")); output: 7
i believe standard version of kmp more efficient since uses less memory dfa version. dfa array can become quite large if have large alphabet , large pattern.
an implementation of both versions can found in flowing links quite documentation how work in related course pages (note in given links kmpplus standard version).
http://algs4.cs.princeton.edu/53substring/kmp.java.html http://algs4.cs.princeton.edu/53substring/kmpplus.java.html
Comments
Post a Comment