Advertisement
avisrivastava254084

Untitled

Nov 18th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. # Python program for KMP Algorithm
  2. from time import time
  3.  
  4.  
  5. def KMPSearch(pat, txt):
  6.     M = len(pat)
  7.     N = len(txt)
  8.     # create lps[] that will hold the longest prefix suffix
  9.     # values for pattern
  10.     lps = [0]*M
  11.     j = 0  # index for pat[]
  12.     # Preprocess the pattern (calculate lps[] array)
  13.     computeLPSArray(pat, M, lps)
  14.     i = 0  # index for txt[]
  15.     loop_counter = 0
  16.     while i < N:
  17.         loop_counter += 1
  18.         if pat[j] == txt[i]:
  19.             i += 1
  20.             j += 1
  21.  
  22.         if j == M:
  23.             print("Found pattern at index " + str(i-j))
  24.             j = lps[j-1]
  25.         # mismatch after j matches
  26.         elif i < N and pat[j] != txt[i]:
  27.             # Do not match lps[0..lps[j-1]] characters,
  28.             # they will match anyway
  29.             if j != 0:
  30.                 j = lps[j-1]
  31.             else:
  32.                 i += 1
  33.     print('Loop counter for complete algo is: {}'.format(loop_counter))
  34.  
  35.  
  36. def computeLPSArray(pat, M, lps):
  37.     len = 0  # length of the previous longest prefix suffix
  38.     lps[0]  # lps[0] is always 0
  39.     i = 1
  40.     # the loop calculates lps[i] for i = 1 to M-1
  41.     loop_counter = 0
  42.     while i < M:
  43.         loop_counter += 1
  44.         if pat[i] == pat[len]:
  45.             len += 1
  46.             lps[i] = len
  47.             i += 1
  48.         else:
  49.             # This is tricky. Consider the example.
  50.             # AAACAAAA and i = 7. The idea is similar
  51.             # to search step.
  52.             if len != 0:
  53.                 len = lps[len-1]
  54.  
  55.                 # Also, note that we do not increment i here
  56.             else:
  57.                 lps[i] = 0
  58.                 i += 1
  59.     print('loop_counter for computeLPSArray is: {}'.format(loop_counter))
  60.  
  61.  
  62. def search(pat, txt):
  63.     M = len(pat)
  64.     N = len(txt)
  65.     loop_counter = 0
  66.     # A loop to slide pat[] one by one */
  67.     for i in range(N - M + 1):
  68.         j = 0
  69.  
  70.         # For current index i, check
  71.         # for pattern match */
  72.         while(j < M):
  73.             loop_counter += 1
  74.             if (txt[i + j] != pat[j]):
  75.                 break
  76.             j += 1
  77.  
  78.         if (j == M):
  79.             print("Pattern found at index ", i)
  80.     print('In brute force, number of iterations were {}'.format(loop_counter))
  81.  
  82.  
  83. txt = "gcagtacgcagagagtatacagtacg"
  84. pat = "agtacg"
  85. KMPSearch(pat, txt)
  86. print(len(txt))
  87. print(len(pat))
  88.  
  89. print('applying brute force')
  90. search(pat, txt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement