Knuth–Morris–Pratt algorithm

E94984

The Knuth–Morris–Pratt algorithm is a classic linear-time string-searching algorithm that efficiently finds occurrences of a pattern within a text by precomputing a prefix function to avoid redundant comparisons.

All labels observed (2)

How this entity was disambiguated

Statements (50)

Predicate Object
instanceOf deterministic algorithm
linear-time algorithm
pattern-matching algorithm
string-search algorithm
application pattern matching in compilers
search in DNA or protein sequences
search in network intrusion detection systems
text editors search functionality
authorsOfOriginalPaper Donald E. Knuth
James H. Morris
surface form: James H. Morris Jr.

Vaughan Pratt
surface form: Vaughan R. Pratt
avoids backtracking in the text
re-examining characters in the text
category exact string matching algorithms
comparedWith Boyer–Moore string-search algorithm
surface form: Boyer–Moore algorithm

Rabin–Karp algorithm
naive string-search algorithm
describedIn Knuth–Morris–Pratt algorithm self-linksurface differs
surface form: "Fast Pattern Matching in Strings"
field algorithms
computer science
stringology
hasPhase preprocessing phase
search phase
input pattern string
text string
introducedIn 1977
keyIdea avoid redundant comparisons by reusing previous match information
precompute longest proper prefix which is also suffix for each pattern position
namedAfter Donald E. Knuth
surface form: Donald Knuth

James H. Morris
Vaughan Pratt
output starting indices of pattern occurrences in text
preprocessingPhaseComplexity O(m)
property guaranteed worst-case linear time
runs in time linear in the length of the text plus pattern
stable performance independent of alphabet size
purpose find occurrences of a pattern in a text
searchPhaseComplexity O(n)
spaceComplexity O(m)
taughtIn data structures and algorithms curricula
undergraduate algorithms courses
timeComplexity O(n + m)
timeComplexityBestCase O(n)
timeComplexityWorstCase O(n + m)
typicalRepresentation pseudocode in algorithm textbooks
usesConcept border of a string
failure function
prefix function
proper prefix
proper suffix

How these facts were elicited

Referenced by (3)

Full triples — surface form annotated when it differs from this entity's canonical label.

Donald E. Knuth knownFor Knuth–Morris–Pratt algorithm
Knuth–Morris–Pratt algorithm describedIn Knuth–Morris–Pratt algorithm self-linksurface differs
this entity surface form: "Fast Pattern Matching in Strings"
Boyer–Moore string-search algorithm comparedWith Knuth–Morris–Pratt algorithm