NIST

Knuth-Morris-Pratt algorithm

(algorithm)

Definition: A string matching algorithm that turns the search string into a finite state machine, then runs the machine with the string to be searched as the input string. Execution time is O(m+n), where m is the length of the search string, and n is the length of the string to be searched.

Also known as KMP.

Author: SB

Implementation

Christian Charras' and Thierry Lecroq's Exact String Matching Algorithms (C). Unix strstr implemented with KMP (C). (C and Pascal) which uses Boyer-Moore preprocessing (C)

More information

Series of pages explaining how Knuth-Morris-Pratt works.

Donald E. Knuth, James H. Morris, and Vaughan R. Pratt, Fast Pattern Matching in Strings, SIAM Journal on Computing, 6(2):323-350, 1977.


Go to the Dictionary of Algorithms and Data Structures home page.

If you have suggestions, corrections, or comments, please get in touch with Paul E. Black.

Entry modified 4 October 2010.
HTML page formatted Tue Dec 6 16:16:32 2011.

Cite this as:
Sterling Bates, "Knuth-Morris-Pratt algorithm", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed., U.S. National Institute of Standards and Technology. 4 October 2010. (accessed TODAY) Available from: http://www.nist.gov/dads/HTML/knuthMorrisPratt.html

to NIST home page