|
| 1 | +"""KMP Algorithm - Pattern Searching Algorithm |
| 2 | +
|
| 3 | +KMP Algorithm is also called as Knuth, Morris, and Pratt string searching algorithm |
| 4 | +This algorithm uses the previous comparison data. |
| 5 | +It uses a partial match table to analyze the pattern structure. |
| 6 | +The goal of the table is to allow the algorithm not to match any character of pattern more than once. |
| 7 | +The basic idea behind KMP’s algorithm is: |
| 8 | +whenever we detect a mismatch (after some matches), we already know some of the characters in the text of the next window. |
| 9 | +We take advantage of this information to avoid matching the characters that we know will anyway match. |
| 10 | +We need to know about proper prefixes and proper suffixes first. |
| 11 | +
|
| 12 | +Proper prefix - All the characters in a string, with one or more cut off the end. |
| 13 | +“C”, “Co”, “Cod”, and “Codi” are all the proper prefixes of “Coding”. |
| 14 | +
|
| 15 | +Profer suffix - All the characters in a string, with one or more cut off the beginning. |
| 16 | +“adrid”, “drid”, “rid”, “id”, and “d” are all proper suffixes of “Madrid”. |
| 17 | +
|
| 18 | +The value of the partial table is the "length of the longest proper prefix that matches a proper suffix". |
| 19 | +
|
| 20 | +Pseudocode - |
| 21 | +if table[partial_match_length] > 1: |
| 22 | + skip ahead by partial_match_length - table[partial_match_length - 1] characters |
| 23 | +else: |
| 24 | + don’t get to skip ahead by partial_match_length - table[partial_match_length - 1] characters. See next partial match. |
| 25 | +
|
| 26 | +Let’s say we’re matching the pattern “abababca” against the text “bacbababaabcbab”. |
| 27 | +Here’s our partial match table again for easy reference |
| 28 | +
|
| 29 | +char: | a | b | a | b | a | b | c | a | |
| 30 | +index: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| 31 | +value: | 0 | 0 | 1 | 2 | 3 | 4 | 0 | 1 | |
| 32 | +
|
| 33 | +Example - |
| 34 | +1. The first match is at index 1. |
| 35 | +bacbababaabcbab |
| 36 | + | |
| 37 | + abababca |
| 38 | +Hence partial_match_length = 1 |
| 39 | +See the next partial match and so on. |
| 40 | +Repeat the steps till the last partial match is found. |
| 41 | +
|
| 42 | +Time Complexity : |
| 43 | +Assuming n is the length of text and m is the length of pattern. |
| 44 | +It can search for a pattern in O(n) time as it never re-compares a text symbol that has matched a pattern symbol. |
| 45 | +Construction of a partial match table takes O(m) time. |
| 46 | +Therefore, the overall time complexity of the KMP algorithm is O(m + n). |
| 47 | +""" |
| 48 | + |
| 49 | +# Python program for KMP Algorithm |
| 50 | +def KMPSearch(pat, txt): |
| 51 | + plen = len(pat) |
| 52 | + tlen = len(txt) |
| 53 | + |
| 54 | + # create lps[] that will hold the longest prefix suffix |
| 55 | + # values for pattern |
| 56 | + lps = [0]*plen |
| 57 | + j = 0 # index for pat[] |
| 58 | + |
| 59 | + alen = 0 # length of the previous longest prefix suffix |
| 60 | + |
| 61 | + lps[0] # lps[0] is always 0 |
| 62 | + i = 1 |
| 63 | + |
| 64 | + # the loop calculates lps[i] for i = 1 to M-1 |
| 65 | + while i < plen: |
| 66 | + if pat[i]== pat[alen]: |
| 67 | + alen += 1 |
| 68 | + lps[i] = alen |
| 69 | + i += 1 |
| 70 | + else: |
| 71 | + if len != 0: |
| 72 | + len = lps[alen-1] |
| 73 | + # Also, note that we do not increment i here |
| 74 | + else: |
| 75 | + lps[i] = 0 |
| 76 | + i += 1 |
| 77 | + |
| 78 | + i = 0 # index for txt[] |
| 79 | + while i < tlen: |
| 80 | + if pat[j] == txt[i]: |
| 81 | + i += 1 |
| 82 | + j += 1 |
| 83 | + |
| 84 | + if j == plen: |
| 85 | + print ("Found pattern at index " + str(i-j)) |
| 86 | + j = lps[j-1] |
| 87 | + |
| 88 | + # mismatch after j matches |
| 89 | + elif i < tlen and pat[j] != txt[i]: |
| 90 | + # Do not match lps[0..lps[j-1]] characters, |
| 91 | + # they will match anyway |
| 92 | + if j != 0: |
| 93 | + j = lps[j-1] |
| 94 | + else: |
| 95 | + i += 1 |
| 96 | + |
| 97 | +print("enter text: ") |
| 98 | +txt=input() |
| 99 | +print("enter pattern; ") |
| 100 | +pat=input() |
| 101 | +KMPSearch(pat, txt) |
0 commit comments