开发者

Is this KMP implementation based on a DFA more efficient than a standard implementation?

What is the complexity of this deterministic finite state automaton based KMP algorithm? Is it more efficient than the 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; 
    for (int X = 0, j = 1; j < M; j++) {
        for (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;
    for (i = 0, j = 0; i < N && j < M; i++) {
        j = dfa[txt.charAt(i)][j];
    }
    if (j == M) return i - M;    
    return -1;                   
  }
}

test:

// test KMP DFA
KMP p = new KMP("abacab");
System.out.println("KMPDfa: " + p.search("ababbadabacabcbabac"));
output: 7


I believe that the standard version of KMP is more efficient since it uses less memory then the DFA version. The DFA array can become quite large if you have a large alphabet and a large pattern.

An implementation of both versions can be found in the flowing links with quite good documentation as to how they work in the related course pages (Note that in the given links KMPplus is the standard version).

http://algs4.cs.princeton.edu/53substring/KMP.java.html http://algs4.cs.princeton.edu/53substring/KMPplus.java.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜