2

Does the KMP (Knuth–Morris–Pratt) algorithm perform fewer comparisons than the simplified Boyer-Moore algorithm?

user207421
  • 305,947
  • 44
  • 307
  • 483
fmunshi
  • 415
  • 2
  • 8
  • 15

1 Answers1

3

The Boyers Moore algorithm should usually perform with less comparisons to quote from here

It should be reasonably clear that, if it is normally the case that a given letter doesnt appear at all in the search string, then this algorithm only requires approx N/M character comparisons (N=length(s1), M=length(s2)) - a big improvement on the KMP algorithm, which still requires N. However, if this is not the case then we may need up to N+M comparisons again (with the full version of the algorithm). Fortunately, for many applications we get close to the N/M performance. If the search string is very large, then it is likely that a given character WILL appear in it, but we still get a good improvement compared with the other algorithms (approx N*2/alphabet_size if characters are randomly distributed in a string).

hhafez
  • 38,949
  • 39
  • 113
  • 143