forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabinKarp.py
More file actions
120 lines (101 loc) · 2.56 KB
/
RabinKarp.py
File metadata and controls
120 lines (101 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
~~~ RABIN KARP ALGORITHM: ~~~
This algorthm is used to search a pattern
in the given text.
Time complexity of the algorithm:
best case: O(n+m)
worst case: O(nm)
where n and m are lengths of text and pattern
'''
def hash(text, pattern, d, q):
'''
function to calculate hash values of text
and pattern
parameters: text, pattern, base value,
large enough prime number
returns: a tuple containing hash of text
and pattern
'''
p = 0
t = 0
m = len(pattern)
# calculate hash for each window of size m
# from both text and pattern
for i in range(m):
p = (d*p + ord(pattern[i])) % q
t = (d*t + ord(text[i])) % q
return (p,t)
def rabinKarp(pattern, text):
'''
function to perform search using rabin karp
algorithm
parameters: pattern and text
returns: a list containing positions of matching
sequences
(or) -1 if not found
'''
m = len(pattern) #length of pattern
n = len(text) #length of text
# base of the numerical system
d = 10
# large enough prime number
q = 101
# list to store search results
arr = []
p = 0
t = 0
h = 1
i = 0
j = 0
flag = False
# check if length of pattern is greater than text
# if yes then return
if(m > n):
arr.append(-1)
return arr
for i in range(m-1):
h = (h*d) % q
# calculate hash value for pattern and text
(p, t) = hash(text, pattern, d, q)
# find the match
for i in range(n-m+1):
if p == t:
for j in range(m):
if text[i+j] != pattern[j]:
break
j += 1
if j == m:
# pattern found at index i
# change flag status and append to list
flag = True
arr.append(i+1)
if i < n-m:
t = (d*(t-ord(text[i])*h) + ord(text[i+m])) % q
if t < 0:
t = t+q
if( not flag):
# pattern not found...append -1 to list and return
arr.append(-1)
return arr
def main():
text = input("Text:")
pattern = input("Pattern:")
listing = rabinKarp(pattern,text)
if(listing == [-1]):
print("Pattern not found")
else:
print (f"Pattern found at positions {listing}")
if __name__ == "__main__":
main()
'''
Sample Input/Output:
Text:abcdhrabds
Pattern:ab
Pattern found at positions [1, 7]
Text:abcabcabc
Pattern:xyz
Pattern not found
Text:abababab
Pattern:aba
Pattern found at positions [1, 3, 5]
'''