Implementing the KMP algorithm

The Python implementation of the KMP algorithm is explained here. We start by implementing the prefix function for the given pattern. For this, first, we compute the length of the pattern by using the len() function, and then we initialize a list to store the computed values by the prefix function. 

Next, we start the loop that executes from 2 to the length of the pattern. Then, we have a nested loop that is executed until we have processed the whole pattern. The variable k is initialized to 0, which is the prefix function for the first element of the pattern.  If the kth element of the pattern is equal to the qth element, then we increment the value of k by 1.

The value of k is the computed value by the prefix function, and so we assign it at the index position of the q of the pattern. Finally, we return the list of the prefix function that has the computed value for each character of the pattern. The code for the prefix function is shown as follows:

def pfun(pattern): # function to generate prefix function for the given pattern
n = len(pattern) # length of the pattern
prefix_fun = [0]*(n) # initialize all elements of the list to 0
k = 0
for q in range(2,n):
while k>0 and pattern[k+1] != pattern[q]:
k = prefix_fun[k]
if pattern[k+1] == pattern[q]: # If the kth element of the pattern is equal to the qth element
k += 1 # update k accordingly
prefix_fun[q] = k
return prefix_fun # return the prefix function

Once we have created the prefix function, we implement the main KMP matching algorithm. We start by computing the length of the text string and the pattern, which are stored in the variables m and n, respectively. The following code shows this in detail:


def KMP_Matcher(text,pattern):
m = len(text)
n = len(pattern)
flag = False
text = '-' + text # append dummy character to make it 1-based indexing
pattern = '-' + pattern # append dummy character to the pattern also
prefix_fun = pfun(pattern) # generate prefix function for the pattern
q = 0
for i in range(1,m+1):
while q>0 and pattern[q+1] != text[i]:
# while pattern and text are not equal, decrement the value of q if it is > 0
q = prefix_fun[q]
if pattern[q+1] == text[i]: # if pattern and text are equal, update value of q
q += 1
if q == n: # if q is equal to the length of the pattern, it means that the pattern has been found.
print("Pattern occours with shift",i-n) # print the index,

where first match occours.
flag = True
q = prefix_fun[q]
if not flag:
print(' No match found')


KMP_Matcher('aabaacaadaabaaba','abaac') #function call
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset