KMP: Pattern search algorithm in JAVA

For text of length N and pattern of length M, naive algorithm will match text string with pattern by incrementing index 1 by 1. Whenever a mismatch occurs after K matches, we will discard previous results and start matching with pattern from next index.

Complexity of naive will be O(m*(n-m+1)) in worst case.

In KMP (Knuth–Morris–Pratt algorithm), it uses information from last mismatch to decide next match start index and bypasses previously matched characters. To retrieve information from previous partial match, KMP does preprocessing of pattern string. It will create a partial_match[] table of size same as pattern length. Value in partial_match[] table at index i represents longest length of proper prefix which is also a proper suffix.

Let’s understand these two terms proper prefix and proper suffix.

For pattern “androidsrc”proper prefixes are “a”, “an”, “and”, “andr”, “andro”, “androi”, “android”, “androids” and “androidsr”Proper suffixes are “ndroidsrc”, “ndroidsrc”, “droidsrc”, “roidsrc”, “oidsrc”, “idsrc”, “dsrc”, “src”, “rc” and “c”.

Lets calculate partial_match[] table for pattern “ABCDABD”. For calculating partial_match at index i, we are only bothered about pattern 0 to i-1.
For sub pattern of length 1 “A” as it has no proper suffix or prefix.
partial_match[0] will always be 0.

For sub pattern of length 2 i.e. “AB” proper prefix is A and proper suffix is B. No match.
partial_match[1] = 0

For sub pattern of length 3 i.e. til index 2, “ABC” will have proper prefixes “AB” and “B”. Proper suffixes will be “BC” and “C”. No match.
partial_match[2] = 0

For sub pattern of length 4 i.e. till index 3, “ABCD” Proper prefixes will be “ABC”, “AB” and “A”. Proper suffixes will be “BCD”, “BC” and “D”. No match.
partial_match[3] = 0

For sub pattern of length 5 i.e. till index 4, “ABCDA” Proper prefixes will be “ABCD”, “ABC”, “AB” and “A”. Proper suffixes will be “BCDA”, “CDA”, “DA” and “A”.
We have a match “A” of length=1 at index 4
partial_match[4] = 1

For sub pattern of length 6 i.e. till index 5, “ABCDAB” Proper prefixes will be “ABCDA”, “ABCD”, “ABC”, “AB” and “A”. Proper suffixes will be
“BCDAB”, “CDAB”, “DAB”, “AB” and “B.” We have a match “AB” of length 2.
partial_match[5] = 2

For sub pattern of length 7 i.e. till index 6, “ABCDABD” We can see all proper suffixes will end with “D” while only one proper prefix “ABCD” will end
with D and we don’t have such proper suffix. So partial_match at index 6 will be 0.
partial_match[6] = 0

For pattern “ABCDABD”, partial_match will be {0, 0, 0, 0, 1, 2, 0}. We will use this preprocessed table to skip already matched characters.

Java code for performing computation of partial_match[] table.

Lets consider our text string as “ABC ABCDAB ABCDABCDABDE”, we will search out pattern now using KMP. textIndex is current index in text to be searched while patternIndex is current index in pattern to be matched.

First mismatch is found at textIndex = 4, we will refer partial_match[] table rather that starting pattern matching from start we will fallback our patternIndex = 4 to partial_match[patternIndex-1] i.e. partial_match[3] which is 0. In this case we can not skip as there is no proper suffix that matches
proper prefix.

Second mismatch will occur at textIndex = 10, characters matched so far in pattern are from 0 to 5 i.e. currently patternIndex = 6. For next step, we will refer partial_match[patternIndex-1] i.e. partial_match[5] which is 2 i.e. we know there is no need to match till index 2. As there exist a proper prefix “AB” of length 2 which is also a proper suffix.

In our next step, we will start with patternIndex = 2 and textIndex = 10, we will have mismatch as ” ” in text string does not match “C” in pattern. This time we can not skip as partial_table[patternIndex-1] is 0.

Next step we will start from textIndex = 11 and patternIndex = 0, mismatch will occur at index 17 after matching “ABCDAB” and patternIndex 6. partial_match[5] is 2. In next step on starting with patternIndex = 2 and textIndex = 17 we will have a match.

Java code for KMP algorithm

If we observe, KMP wisely used pattern partial_match[] table to skip index. KMP algorithm performs pattern search in linear time i.e. O(n).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *