Knuth–Morris–Pratt algorithm
Builds on AlphabetStringString-searching algorithm
Definition
Let and be strings over a finite alphabet, with . The failure function of is the map such that is the largest with . The Knuth–Morris–Pratt algorithm is the string-searching algorithm that computes and then runs these steps.
- Set .
- For : while and , set .
- Still for this : if , increase by .
- Still for this : if , report an occurrence at position and set .
After step 3 for index , is the length of the longest prefix of that is a suffix of .
Remarks
Example: for the failure function is , , , . For instance because is both a prefix and a suffix of , while is not a suffix of it. On the text the algorithm reports positions and .
Non-example: the value is excluded in the failure function. Allowing it would give , and step 2 would never move the pattern.
The failure function itself is computed by the same scan applied to against itself, in time . Each step 2 loop lowers , and rises by at most per character, so the search takes time and the whole algorithm .
Reading path
To understand this page from the ground up, read these 4 pages in order. The foundations come first.
Show the path as a graph
- no evidence level
- Alphabet concept
- String concept
- String-searching algorithm concept
- Knuth–Morris–Pratt algorithm definition
Further reading
About this page
| Tag | XADN5F6SCP |
|---|---|
| URI | m2://XADN5F6SCP |
| Kind | definition |
| Writing | W0 stub |
| MSC | 68W32, 68R15, 68W40 |
| Defines | Knuth–Morris–Pratt algorithm |
| Uses | Alphabet, String, String-searching algorithm |
| Changed | 3 November 2026 |
Markdown · JSON · Edit on GitHub
What this page needs for W1
- an inventory row: the row of the inventory that the node accounts for (0 of 1)
- a source with a locator (0 of 1)
The writing levels say how complete the text of a page is. They do not measure its truth.
What links here
No page links to this page yet.