Posts

Showing posts from September, 2020

Knuth-Morris-Pratt Algorithm

Image
 Knuth-Morris-Pratt Algorithm              Knuth Morris Pratt (KMP) is an algorithm, which checks the characters from left to right. When a pattern has a sub-pattern appears more than one in the sub-pattern, it uses that property to improve the time complexity, also for in the worst case.            The KMP matching algorithm uses degenerating property (pattern having same sub-patterns appearing more than once in the pattern) of the pattern and improves the worst case complexity to O(n). The basic idea behind KMP’s algorithm is: whenever we detect a mismatch (after some matches), we already know some of the characters in the text of the next window. We take advantage of this information to avoid matching the characters that we know will anyway match.  Algorithm: findPrefix(pattern, m, prefArray) Begin    length := 0    prefArray[0] := 0    for all character index ‘i’ of pat...