KMP algorithm
prefix table
string matching
computer science
pattern recognition

KMP prefix table

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The Knuth-Morris-Pratt (KMP) algorithm is a classic pattern matching algorithm that finds occurrences of a word within a main text string efficiently. The efficiency of KMP largely stems from its pre-processing phase, which involves the creation of a prefix table (also known as the "lps" table, where "lps" stands for "longest proper prefix which is also a suffix"). This table is key to avoiding unnecessary re-evaluation of characters when a mismatch between the pattern and the text occurs.

Understanding the KMP Prefix Table

Definition

The prefix table helps in determining the next positions within the pattern to be evaluated after a mismatch. This avoids backtracking over the text, which forms the basis for the KMP algorithm's efficiency.

Construction of the Prefix Table

To construct the prefix table for a pattern P of length m , you follow these steps:

  1. Initialize:
    • Let lps be an array of length m , initialized to zero.
    • Set length = 0 , which will keep track of the length of the current longest prefix suffix.
    • Start iterating over the pattern from index 1 (since lps[0] is always 0 ).
  2. **Iterate and Update lps **:
    • For each index i from 1 to m - 1 :
      • If P[i] matches P[length] , increment length by 1 and set lps[i] = length .
      • If P[i] does not match P[length] , and length is not zero, set length to lps[length - 1] .
      • If length is zero, simply set lps[i] = 0 .

Example

Let's construct the lps array for the pattern ABABCABAB .

IndexPatternLengthLPS Array State
0A0[0]
1AB0[0, 0]
2ABA1[0, 0, 1]
3ABAB2[0, 0, 1, 2]
4ABABC0[0, 0, 1, 2, 0]
5ABABCA1[0, 0, 1, 2, 0, 1]
6ABABCAB2[0, 0, 1, 2, 0, 1, 2]
7ABABCABA3[0, 0, 1, 2, 0, 1, 2, 3]
8ABABCABAB4[0, 0, 1, 2, 0, 1, 2, 3, 4]

Notice how the lps array provides crucial information for each prefix of the pattern.

Utilizing the lps

Table

During the search phase of the KMP algorithm, the prefix table helps to determine if and how the pattern can be shifted within the text without unnecessary shifts. When a mismatch is found after j characters matched, we use the lps table to skip comparisons rather than resetting j to zero.

Algorithm Efficiency

Time Complexity

  1. Pre-processing Phase: The prefix table creation runs in linear time, O(m)O(m), where m is the length of the pattern.
  2. Search Phase: Given the prefix table, the search phase also operates in linear time, O(n)O(n), where n is the length of the text.

Overall, the KMP algorithm runs in O(n+m)O(n + m) time, making it highly efficient for pattern matching.

Space Complexity

The KMP algorithm requires O(m)O(m) space to store the prefix table, which is relatively low compared to other algorithms that may require additional space for backtracking.

Additional Details

Comparison with Other Algorithms

  • Naive Pattern Matching: This approach has a time complexity of O(n×m)O(n \times m) due to repeated checks and backtracking.
  • Boyer-Moore Algorithm: This algorithm leverages mismatch heuristics, which can lead to sub-linear time complexity in many scenarios, making it faster than KMP in practice but more complex to implement.

Applications

  • Text editors for implementing "find" functionality.
  • DNA sequence analysis, where searching for genetic patterns is common.
  • Network intrusion detection systems to locate patterns of malicious activities.

Summary Table

TopicDetails
Algorithm NameKnuth-Morris-Pratt (KMP)
Key ComponentPrefix Table (lps table)
Prefix Table UseAvoid unnecessary character checks after mismatches
Complexity - TimeO(n+m)O(n + m)
Complexity - SpaceO(m)O(m)
StrengthsLinear time complexity, no backtracking
ComparisonOutperforms Naive, simpler than Boyer-Moore in terms of implementation
ApplicationsText search, bioinformatics, network security

The KMP algorithm and its prefix table continue to be valuable in both theoretical computer science and practical applications, providing a foundation for efficient pattern matching techniques.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.