Lab7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

22103091

Week-7
Program 10: Implement KMP pattern search algorithm.
Description:
the Knuth–Morris–Pratt algorithm (or KMP algorithm) is a string-searching algorithm that
searches for occurrences of a "word" W within a main "text string" S by employing the
observation that when a mismatch occurs, the word itself embodies sufficient information to
determine where the next match could begin, thus bypassing re-examination of previously
matched characters.
ALGORITHM:
KMP_Search(text, pattern):
n = length of text
m = length of pattern
lps = computeLPSArray(pattern, m)
i = 0 // index for text[]
j = 0 // index for pattern[]
while i < n:
if pattern[j] = text[i]:
i++
j++
if j = m:
print "Pattern found at index " + (i - j)
j = lps[j - 1]
else if i < n and pattern[j] ≠ text[i]:
if j ≠ 0:
j = lps[j - 1]
else:
i++

computeLPSArray(pattern, m):
length = 0
lps[0] = 0
i=1

while i < m:
if pattern[i] = pattern[length]:
length++
lps[i] = length
i++
else:
if length ≠ 0:
length = lps[length - 1]
else:
lps[i] = 0

41
22103091

i++
return lps

Time Complexity:

The time complexity of the Knuth-Morris-Pratt (KMP) pattern search algorithm is O(n + m),
where n is the length of the text and m is the length of the pattern being searched for.
• The preprocessing step of the KMP algorithm takes O(m) time to construct the prefix
function, which helps in skipping unnecessary comparisons during the search phase.
• The search phase iterates through the text only once, comparing characters of the text
with characters of the pattern. Since each comparison takes constant time, the search
phase also contributes O(n) time complexity.

Combining the preprocessing and search phases, the overall time complexity of the KMP
algorithm is O(n + m).

42
22103091

SOURCE CODE:

#include <bits/stdc++.h>

void computeLPSArray(char* pat, int M, int* lps);

void KMPSearch(char* pat, char* txt)


{
int M = strlen(pat);
int N = strlen(txt);
int lps[M];

computeLPSArray(pat, M, lps);

int i = 0;
int j = 0;
while ((N - i) >= (M - j)) {
if (pat[j] == txt[i]) {
j++;
i++;
}

if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
}

else if (i < N && pat[j] != txt[i]) {


if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}

void computeLPSArray(char* pat, int M, int* lps)


{

int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;

43
22103091

}
else
{

if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
int main()
{
char txt[] = "ABABDABACDABABCABAB";
char pat[] = "ABABCABAB";
KMPSearch(pat, txt);
return 0;
}

44
22103091

OUTPUT:

45

You might also like