24MTCS003HY KMP Algorithm Presentation
24MTCS003HY KMP Algorithm Presentation
Morris-
Pratt
Algorithm
• Definition
• History
• Components of KMP
• Algorithm
• Example
• Run-Time Analysis
• Advantages and Disadvantages
Definition
• Motivation: To optimize
string matching.
• Context: Part of
foundational algorithms in
text processing.
Components of
KMP
• 1. Construct the LPS (Longest Prefix Suffix)
array for the pattern.
Algorithm
• 2. Use the LPS array to skip unmatched
characters in the text.
• 3. Continue until the pattern is found or the
text is fully scanned.
Example: LPS Array
Text: ABABABCAB
Pattern: ABABC
Advantages:
• Efficient for large texts.
• Skips unnecessary re-evaluation
of characters.
Disadvantages:
• Requires extra space for LPS
array.
• Slightly complex for beginners.