What are the main differences between the Knuth-Morris-Pr...
What are the main differences between the Knuth-Morris-Pr...
The main differences between the Knuth-Morris-Pratt (KMP) and Boyer-Moore (BM) string search algorithms are:
Scanning direction: KMP scans the text from left to right, while BM scans from right to left[3][5].
Preprocessing: Both algorithms preprocess the pattern, but in different ways. KMP builds a prefix table to avoid comparing characters that are known to match based on previous comparisons[1][2]. BM uses a shift table to determine how far to shift the pattern after a mismatch[1][4].
Performance: In the worst case, KMP has a guaranteed linear time complexity of O(m+n), where m is the pattern length and n is the text length[3]. BM can be faster than KMP on certain inputs that allow skipping many characters, but it has a worst-case complexity of O(nm)[3][4]. So KMP is more reliable, while BM can be faster or slower depending on the input[3].
Alphabet size: KMP works well with small alphabets (e.g., DNA bases) because...
expert
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào