What does KMP stand for in the KMP Pattern Matching Algorithm?
KMP stands for Knuth-Morris-Pratt, the last names of the three computer scientists who created this efficient pattern matching algorithm.
Click to reveal answer
beginner
What is the main advantage of the KMP algorithm over the naive pattern matching approach?
KMP avoids rechecking characters that have already been matched by using a precomputed table, making it faster especially for large texts and patterns.
Click to reveal answer
intermediate
What is the purpose of the 'Longest Prefix Suffix' (LPS) array in KMP?
The LPS array stores the length of the longest proper prefix of the pattern that is also a suffix for each prefix of the pattern. It helps decide where to continue matching after a mismatch.
Click to reveal answer
intermediate
In KMP, what happens when a mismatch occurs after some characters have matched?
Instead of restarting from the next character in the text, KMP uses the LPS array to shift the pattern to the right position, skipping unnecessary comparisons.
Click to reveal answer
intermediate
What is the time complexity of the KMP Pattern Matching Algorithm?
The time complexity is O(n + m), where n is the length of the text and m is the length of the pattern, because each character is processed at most twice.
Click to reveal answer
What does the LPS array in KMP represent?
ALength of the pattern
BPositions of pattern matches in text
CNumber of mismatches found
DLength of longest prefix which is also suffix for pattern prefixes
✗ Incorrect
The LPS array stores the length of the longest proper prefix which is also a suffix for each prefix of the pattern.
Why is KMP faster than naive pattern matching?
AIt uses a precomputed table to avoid rechecking characters
BIt checks all characters twice
CIt uses random guessing
DIt only works on small texts
✗ Incorrect
KMP uses the LPS array to skip unnecessary comparisons, avoiding rechecking characters.
What is the worst-case time complexity of KMP?
AO(n + m)
BO(n * m)
CO(n^2)
DO(m^2)
✗ Incorrect
KMP runs in linear time relative to the sum of text and pattern lengths, O(n + m).
When a mismatch occurs in KMP, what does the algorithm do?
AStops the search
BRestarts matching from the next character in text
CUses LPS array to shift pattern without rechecking matched characters
DSkips the entire pattern
✗ Incorrect
KMP uses the LPS array to shift the pattern smartly, avoiding rechecking matched characters.
What is the first step in the KMP algorithm?
AReverse the pattern
BCompute the LPS array for the pattern
CSearch the pattern in the text directly
DSort the text
✗ Incorrect
KMP first computes the LPS array to know how to shift the pattern during matching.
Explain how the LPS array is constructed in the KMP algorithm and why it is important.
Think about matching prefixes and suffixes within the pattern itself.
You got /3 concepts.
Describe the process of pattern matching in KMP when a mismatch occurs after some characters matched.
Focus on how KMP shifts the pattern smartly instead of naive restarting.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the KMP (Knuth-Morris-Pratt) pattern matching algorithm?
easy
A. To calculate the factorial of a number recursively
B. To sort an array of numbers in ascending order
C. To reverse a string in linear time
D. To find occurrences of a pattern string within a text efficiently
Solution
Step 1: Understand the goal of KMP algorithm
KMP is designed to search for a pattern inside a larger text efficiently without rechecking characters unnecessarily.
Step 2: Compare with other options
Sorting, reversing strings, and factorial calculations are unrelated to pattern searching.
Final Answer:
To find occurrences of a pattern string within a text efficiently -> Option D
Quick Check:
KMP = Pattern search [OK]
Hint: KMP is about fast pattern search in text [OK]
Common Mistakes:
Confusing KMP with sorting algorithms
Thinking KMP reverses strings
Assuming KMP calculates mathematical functions
2. Which of the following is the correct way to initialize the prefix table (lps array) in KMP algorithm in Python?
easy
A. lps = [0] * len(pattern)
B. lps = []
C. lps = [1] * len(pattern)
D. lps = None
Solution
Step 1: Understand the prefix table initialization
The prefix table (lps array) stores lengths of longest proper prefix which is also suffix for each prefix of the pattern. It must be initialized with zeros for all positions.
Step 2: Check each option
lps = [0] * len(pattern) correctly initializes lps with zeros for the entire pattern length. lps = [] is empty list, which is incomplete. lps = [1] * len(pattern) initializes with ones, which is incorrect. lps = None sets None, which is invalid.
Final Answer:
lps = [0] * len(pattern) -> Option A
Quick Check:
Prefix table starts with zeros [OK]
Hint: Prefix table starts with zeros for all pattern chars [OK]
Common Mistakes:
Initializing lps with ones instead of zeros
Leaving lps empty
Assigning None to lps
3. What will be the output of the following Python code snippet using KMP algorithm?
def compute_lps(pattern):
lps = [0] * len(pattern)
length = 0
i = 1
while i < len(pattern):
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
return lps
pattern = "ABABC"
lps = compute_lps(pattern)
print(lps)