Knuth-Morris-Pratt (KMP) Algorithm

A linear-time string-matching algorithm that avoids backtracking the text pointer using a prefix function (LPS array).

Method

Precompute the longest proper prefix that is also a suffix; on mismatch, use the LPS array to shift the pattern pointer.

Applications

Software engineering (text search), education (algorithm teaching), bioinformatics.

Advantages

Strict O(n+m); deterministic; easy to prove correct.

Limitations

Needs O(m) pattern preprocessing and storage; a single-pattern algorithm. Practical relative speed depends on inputs and implementation.

Alternatives

Boyer-Moore, Rabin-Karp, Z-algorithm.

Editorial clarification

Source description reviewed against the method-specific reference. Volume ordering is editorial, not a measured ranking; advantages are conditional rather than benchmark results.

Sources

Educational reference only, not professional advice.

All algorithms