What are the main differences between the Knuth-Morris-Pratt and Boyer-Moore search algorithms?

后端 未结 3 1278
鱼传尺愫
鱼传尺愫 2021-01-29 20:36

What are the main differences between the Knuth-Morris-Pratt search algorithm and the Boyer-Moore search algorithm?

I know KMP<

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 21:22

    In an rough explanation

    Boyer-Moore's approach is to try to match the last character of the pattern instead of the first one with the assumption that if there's not match at the end no need to try to match at the beginning. This allows for "big jumps" therefore BM works better when the pattern and the text you are searching resemble "natural text" (i.e. English)

    Knuth-Morris-Pratt searches for occurrences of a "word" W within a main "text string" S by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters. (Source: Wiki)

    This means KMP is better suited for small sets like DNA (ACTG)

提交回复
热议问题