Skip to content

Commit 7c3ef98

Browse files
mahbubcsejucclauss
authored andcommitted
Implement ruling hash to appropriate complexity of Rabin Karp (TheAlgorithms#1066)
* Added matrix exponentiation approach for finding fibonacci number. * Implemented the way of finding nth fibonacci. * Complexity is about O(log(n)*8) * Updated the matrix exponentiation approach of finding nth fibonacci. - Removed some extra spaces - Added the complexity of bruteforce algorithm - Removed unused function called zerro() - Added some docktest based on request * Updated the matrix exponentiation approach of finding nth fibonacci. - Removed some extra spaces - Added the complexity of bruteforce algorithm - Removed unused function called zerro() - Added some docktest based on request * Updated Rabin Karp algorithm. - Previous solution is based on the hash function of python. - Implemented ruling hash to get the appropriate complexity of rabin karp. * Updated Rabin Karp algorithm. - Previous solution is based on the hash function of python. - Implemented ruling hash to get the appropriate complexity of rabin karp. * Implemented ruling hash to appropriate complexity of Rabin Karp Added unit pattern testing
1 parent b2ed8d4 commit 7c3ef98

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

strings/rabin_karp.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
# Numbers of alphabet which we call base
2+
alphabet_size = 256
3+
# Modulus to hash a string
4+
modulus = 1000003
5+
6+
17
def rabin_karp(pattern, text):
28
"""
3-
49
The Rabin-Karp Algorithm for finding a pattern within a piece of text
510
with complexity O(nm), most efficient when it is used with multiple patterns
611
as it is able to check if any of a set of patterns match a section of text in o(1) given the precomputed hashes.
@@ -12,22 +17,42 @@ def rabin_karp(pattern, text):
1217
2) Step through the text one character at a time passing a window with the same length as the pattern
1318
calculating the hash of the text within the window compare it with the hash of the pattern. Only testing
1419
equality if the hashes match
15-
1620
"""
1721
p_len = len(pattern)
18-
p_hash = hash(pattern)
22+
t_len = len(text)
23+
if p_len > t_len:
24+
return False
25+
26+
p_hash = 0
27+
text_hash = 0
28+
modulus_power = 1
1929

20-
for i in range(0, len(text) - (p_len - 1)):
30+
# Calculating the hash of pattern and substring of text
31+
for i in range(p_len):
32+
p_hash = (ord(pattern[i]) + p_hash * alphabet_size) % modulus
33+
text_hash = (ord(text[i]) + text_hash * alphabet_size) % modulus
34+
if i == p_len - 1:
35+
continue
36+
modulus_power = (modulus_power * alphabet_size) % modulus
2137

22-
# written like this t
23-
text_hash = hash(text[i:i + p_len])
24-
if text_hash == p_hash and \
25-
text[i:i + p_len] == pattern:
38+
for i in range(0, t_len - p_len + 1):
39+
if text_hash == p_hash and text[i : i + p_len] == pattern:
2640
return True
41+
if i == t_len - p_len:
42+
continue
43+
# Calculating the ruling hash
44+
text_hash = (
45+
(text_hash - ord(text[i]) * modulus_power) * alphabet_size
46+
+ ord(text[i + p_len])
47+
) % modulus
2748
return False
2849

2950

30-
if __name__ == '__main__':
51+
def test_rabin_karp():
52+
"""
53+
>>> test_rabin_karp()
54+
Success.
55+
"""
3156
# Test 1)
3257
pattern = "abc1abc12"
3358
text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc"
@@ -48,3 +73,8 @@ def rabin_karp(pattern, text):
4873
pattern = "abcdabcy"
4974
text = "abcxabcdabxabcdabcdabcy"
5075
assert rabin_karp(pattern, text)
76+
print("Success.")
77+
78+
79+
if __name__ == "__main__":
80+
test_rabin_karp()

0 commit comments

Comments
 (0)