algorithm - When would you use KMP over BOYER-MOORE -
i learning pattern matching algorithms , have come across these 2 algorithms. have following general ideas:
kmp
- compares text left-to-right
- uses failure array shift intelligently
- takes o(m), m length of pattern, compute failure array
- takes o(m), space
- takes o(n), time search string
bm
- compares pattern last character
- uses bad character jumps , suffix jumps
- takes o(m + size of alphabet) compute tables
- takes o(m + size of alphabet), space
- takes o(n), better search
i came across following question triggered question(true or false):
the knuth-morris-pratt (kmp) algorithm choice if want search same pattern repeatedly in many different texts.
so believe answer true because assumption every time run algorithm on different text preprocessing o(n) bm o(n + size of alphabet). however, not sure if making correct assumption every time algorithm rerun new table recomputed. because text falls in alphabet of english. need compute table once , reuse table. @ end of day, answer question dependent on fact algorithms being run on text contained in same alphabet or there other factor may affect it?
in theory, both algorithms have "similar" performance; kmp 2n comparisons in searching phase , boyer-moore 3n comparisons in searching phase in worst case. in neither case need repeat preprocessing when new text.
but real answer shouldn't use either 1 in practice.
the linear auxiliary storage needed both algorithms leads considerably...rougher performance on modern architectures because of of memory accesses.
however, ideas behind boyer-moore , kmp underpin fast string matching algorithms. kmp's "failure function" idea used every practically effective string matching algorithm know of; turns out can compute suboptimal "failure function" pattern on-the-fly still gives linear time matching while needing constant additional space. boyer-moore faster linear in "average case" of matching fixed pattern against random noise, , bears out in many practical situations.
Comments
Post a Comment