FAST PATTERN MATCHING IN STRINGS Dr. DONALD E. KNUTH, Dr. JAMES H. MORRIS JR., VAUGHAN R. PRATT Presented by: Mangesh Sakordekar OVERVIEW • Goal • Intuition • Algorithm & Implementation • Run Time Analysis • Conclusion GOAL • Improve run time of pattern matching algorithm to find a pattern(M) in a given text(N) • Naive approach run time : O(N*M) INTUITION • Process each character only once. text = A A A X pattern = A A A A • Intelligently move the pattern across the string. A ALGORITHM & IMPLEMENTATION PART 1: LONGEST PREFIX SUFFIX (LPS) TABLE The LPS table stores the length of the longest matching suffix and prefix formed by the substrings of the pattern. Code: pattern = "aaaba" lps = [0] * len(pattern) prevLPS, i = 0, 1 while i < len(pattern): if needle[i] == needle[prevLPS]: lps[i] = prevLPS + 1 prevLPS += 1 i += 1 elif prevLPS == 0: lps[i] = 0 i += 1 else: prevLPS = lps[prevLPS - 1] pattern = LPS = A A A B A 0 1 2 0 1 ALGORITHM & IMPLEMENTATION PART 2: KMP ALGORITHM Unlike the naive algorithm, we can now use the LPS table to intelligently match the strings instead of searching for the pattern from each index. Code: i = 0 # ptr for text j = 0 # ptr for pattern while i < len(text): if text[i] == pattern[j]: i, j = i + 1, j + 1 else: if j == 0: i += 1 else: j = lps[j - 1] if j == len(pattern): print(“Pattern found at :” + str(i - len(pattern))) j = lps[j - 1] text = A A A B A A A B pattern = A A A B A LPS = 0 1 2 0 1 A RUNTIME ANALYSIS • Consider length of the text as N and length of the pattern as M. • Time complexity of generating LPS Table = O(M) • Time complexity of pattern matching = O(N) • Total Time Complexity = O(M+N) • Total Space Complexity = O(M) CONCLUSION a time complexity of O(M+N) • Achieved • Successfully beat Boyer-Moore algorithm. REFERENCES 1. K n u t h , D . E . , M o r r i s , J . H . , J R , & P r a t t , V. R . ( 1 9 7 7 ) . F a s t P a t t e r n M a t c h i n g i n S t r i n g s . S I A M J o u r n a l o n C o m p u t i n g . h t t p s : / / d o i . o r g / 1 0 . 11 3 7 / 0 2 0 6 0 2 4 2. NeetCode. “Knuth–Morris–Pratt KMP - Implement Strstr() - Leetcode 28 - Python.” YouTube, YouTube, 14 Nov. 2021, https://www.youtube.com/watch?v=JoF0Z7nVSrA&ab_channel=NeetCode. 3. “KMP Algorithm for Pattern Searching.” GeeksforGeeks, 30 Sept. 2022, h t t p s : / / w w w. g e e k s f o r g e e k s . o r g / k m p - a l g o r i t h m - f o r - p a t t e r n - s e a r c h i n g / . THANK YOU QUESTIONS ?