- Notifications
You must be signed in to change notification settings - Fork8
#44: add rabin karp algorithm#304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
hamidgasmi wants to merge1 commit intomasterChoose a base branch from44-rabin-karp-algo
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Empty file removed10-algo-ds-implementations/string_pattern_matching_algorithms.py
Empty file.
71 changes: 71 additions & 0 deletions10-algo-ds-implementations/string_pattern_matching_rabin_karp.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
''' | ||
Problem: Exact pattern matching | ||
- Input: a text (T), a pattern (P) | ||
- Output: 1 or all occurences of P in T | ||
Algorithm: | ||
- Hashing function: H(S: str, n: int, m: int) = Sum(S[i] * x^i-n % p) for n <= i < m | ||
- Compute H(P, 0, |P|) | ||
- Compute H(T, 0, |P|) | ||
- H(S, n + 1, m + 1) = (H(S, n, m) - S[n]) / x + S[m] * x^m-n-1 % p | ||
- Loop through T: | ||
- Compute H(T, i + 1, i + 1 + |P|) by using H(T, i, i + |P|) in O(1) time complexity | ||
- Compare it with H(P) | ||
Questions: | ||
- How to choose the multiplier and the prime numbers? | ||
- How to make the trade-off between collision numbers and performance of recurrence calculation for H[i]? | ||
- More the prime is bigger, less false positive we gets. This is supposed to make our code faster. | ||
- In the other hand, more the prime is bigger, the related division and multiplication operations get slower. | ||
''' | ||
class Rabin_Karp: | ||
def __init__(self): | ||
self.__multiplier = 31 | ||
self._prime = 1000000007 | ||
self.__multiplier_power_prime = self.__multiplier ** self._prime % self._prime | ||
def __abs_hash__(self, s: str, length: int) -> int: | ||
assert(length < len(s)) | ||
hash, power_x = 0, 1 | ||
for idx in range(length): | ||
hash += ord(s[idx]) * power_x | ||
hash %= self.__prime | ||
power_x *= self.__multiplier | ||
return hash | ||
def __rel_hash(self, s: str, prev_hash: int, idx: int, length: int) -> int: | ||
assert(0 < idx < len(s)) | ||
return (prev_hash - s[idx - 1]) // self.__multiplier + s[idx + length - 1] * self.__multiplier | ||
def find(self, t: str, p: str) -> List[int]: | ||
len_t = len(t) | ||
len_p = len(p) | ||
if len_p > len_t: | ||
return [] | ||
# 1. find hash functions for t and p | ||
hash_p = self.__abs_hash__(p, len_p) | ||
hash_t = self.__abs_hash__(t, len_p) | ||
# 2. find all occurences | ||
occurences = [] | ||
if hash_p == hash_t: | ||
occurences.append(0) | ||
for idx in range(1, len_t): | ||
hash_t = self.__rel_hash(t, hash_t, idx, len_p) | ||
if hash_p == hash_t and p == t[idx:idx+len_p]: | ||
occurences.append(idx) | ||
return occurences | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.