Overview

5 Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt is presented as a smarter alternative to naive string searching, one that avoids the costly backtracking that makes simple pattern matching slow on repetitive inputs. Instead of treating a mismatch as a dead end, the algorithm uses the information gained from already matched characters to continue searching without rechecking the text from the beginning. This shift in thinking is the key idea behind its efficiency and is what separates it from hash-based methods like Rabin-Karp.

The chapter explains that KMP works in two stages: first, it preprocesses the pattern to build the LPS array, which records the longest proper prefix that is also a suffix for each position in the pattern. This array can be constructed in linear time, O(m), and serves as a lookup table for how far the pattern can shift after a mismatch. During the actual search, two pointers move through the text and pattern, but the text pointer never moves backward, which guarantees a linear scan of the text and overall O(n + m) time.

Through worked examples, the text shows how the LPS array is built and then used to skip redundant comparisons while matching patterns in longer texts. It also emphasizes practical uses such as log analysis, real-time streaming, and intrusion detection systems, where forward-only scanning is especially valuable. At the same time, it notes the trade-off: KMP requires extra preprocessing time and memory, and if the pattern has little or no repetition, the benefit can disappear and the method may offer little advantage over simpler approaches.

To create the LPS array, we start with an empty array of the same length as that of the pattern. We initialize a variable length as 0, and an iterator i starts with 1, which will track the current character.
The KMP match starts with two pointers, i and j, starting at 0. If it’s a match with text and pattern, we increment i and j by 1 and so on. At index 6, enclosed with a box there is a mismatch of character ‘e’ with ‘c’
The pattern shifts to the right, and the comparison now takes place with ‘e’ and ‘a’. A mismatch occurs again, but the value of j is already at 0, looking at the LPS array. So now i is incremented by 1 to 6.
The text and the pattern match perfectly till i = 3. At i = 3, the character is ‘x’ and j = 3 is ‘d’, a mismatch occurs. In the LPS array, we look at the previous LPS[j-1] and set that value to j. The right shift of the pattern can be calculated as j - LPS[j-1], which is 3 - 0 spaces towards the right.
The shift by 3 characters is completed. Now i = 3 and j = 0, a mismatch occurs, but j is already 0, so increment i by 1, and j is still 0. The next comparison is with i = 4 and j = 0, which is a match. Proceeding further, i increments until 10 and j increments until 6, a mismatch between ‘x’ and ‘c’ occurs. Previous LPS[6-1] is set to j. The visual shift is going to be j - LPS[j-1], which is 6 -2 = 4 spaces towards the right.
A comparison is done with ‘x’ and 'c', which is a mismatch. Since, LPS[1] is 0, we increment i by 1. The pointer i = 11 and j = 0. The pointer i increases up to 18 for character ‘d’, and pointer j increments till 7 for character ‘y’. Mismatch occurs here, and LPS[7-1] = 3 is set to the pointer j. Ths shift is 7 - 3 = 4 spaces towards the right.
This is the final check where the full match occurs. We have moved 4 spaces towards the right, matching the pointer and j = 3. The sequence a b c is already compared, and pointer i = 18 with j = 3, so the comparison starts again. The comparison ends with all characters of the text matched with the pattern. The pattern is found from pointer i = 15 to 22 in the text.

Summary

  • The Knuth-Morris-Pratt Algorithm (KMP) introduces the concept where a mismatch is not a failure but a source of information. Naive and brute force algorithms reset the text pointer to the start when a mismatch occurs
  • The time complexity without KMP is O(nm), where n is the length of the text and m is the length of the pattern.
  • KMP makes sure the text pointer only moves forward in linear time.
  • The search pattern is preprocessed and stored in auxiliary space called the Longest Prefix which is also the Suffix (LPS) array. This operation is done in O(m) time.
  • For every index in the pattern, the LPS array calculates the length of the longest contiguous sequence of characters at the beginning of the string that perfectly matches the sequence at the end.
  • During a mismatch at pattern index j, the algorithm avoids a full reset. It consults the LPS array (LPS[j-1]) to determine exactly how many characters of the prefix are guaranteed to still match the text. This makes it deterministic.
  • The physical distance the pattern slides to the right during a mismatch is mathematically defined by, j - LPS[j-1]
  • By decoupling the pattern analysis from the text scanning, the actual search phase executes in strictly O(n) time, bringing the total algorithmic time complexity to O(n + m).
  • KMP is the foundational algorithm for continuous, real-time data streaming, live log analysis, and line-speed network Intrusion Detection Systems (IDS).
  • KMP derives its power exclusively from pattern repetition. If there is no repetition in the pattern or the text, the LPS array is not useful, and the approach falls to the naive approach with worse time complexity. This is because the LPS array without repetition consists of only zeroes. Every mismatch reset the pointer to 0, making it naive.
  1. The original paper of this algorithm is https://www.cs.jhu.edu/~misha/Spring23/Knuth77.pdf.
  2. Practice Problems of string matching specifically for bioinformatics here - https://rosalind.info/problems/list-view/
  3. YouTube video - https://www.youtube.com/playlist?list=PL_drzcX_ki75Hif00uAhycjlqDNi0ZVlC

FAQ

What is the Knuth-Morris-Pratt (KMP) algorithm used for?KMP is a string-matching algorithm used to find a pattern inside a text efficiently. It avoids re-checking characters that were already matched by using preprocessing on the pattern, making search linear in the size of the text.
Why is KMP faster than the naive string-search approach?The naive approach backtracks in the text after a mismatch, which causes many repeated comparisons and can lead to O(nm) time. KMP avoids this by never moving the text pointer backward, so it runs in O(n + m) time.
What does “backtracking” mean in the context of string matching?Backtracking is when a search algorithm moves its pointer backward in the text after a mismatch and re-evaluates characters it already scanned. This wastes time and is the main performance problem KMP is designed to solve.
What is the LPS array in KMP?LPS stands for Longest Proper Prefix which is also a Suffix. It stores, for each position in the pattern, the length of the longest prefix that is also a suffix for the substring ending at that position.
Why is the LPS array important in KMP?The LPS array tells the algorithm how far the pattern can shift after a mismatch without restarting from the beginning. This lets KMP reuse previous matches and skip redundant comparisons.
How is the LPS array built in KMP?The LPS array is built in O(m) time using two pointers: one for the current position and one for the length of the current prefix-suffix match. If characters match, the length increases; if they mismatch, the algorithm falls back using earlier LPS values.
Why is LPS[0] always 0?A single character cannot have a proper prefix or proper suffix, since a proper prefix/suffix must be strictly shorter than the whole string. Therefore, the first LPS value is always 0.
How does KMP search the text after building the LPS array?KMP uses two pointers: one for the text and one for the pattern. When characters match, both pointers advance; when a mismatch occurs, the pattern pointer jumps to the position indicated by the LPS array, while the text pointer does not move backward.
What is the time complexity of KMP?KMP takes O(m) time to build the LPS array and O(n) time to search the text, giving a total time complexity of O(n + m). This makes it strictly linear with respect to the text and pattern sizes.
What are the real-world uses of KMP?KMP is useful in log analysis, real-time data streaming, and intrusion detection systems where data arrives continuously and backtracking is expensive or impossible. It is especially effective when searching repetitive patterns in streaming or memory-sensitive environments.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free