Bizda quyidagi matn bor bo’lsin


Rabin-Karp algoritmining murakkabligi


Download 412.49 Kb.
bet13/21
Sana16.02.2023
Hajmi412.49 Kb.
#1203577
1   ...   9   10   11   12   13   14   15   16   ...   21
Bog'liq
kurs iwi malumotlar

Rabin-Karp algoritmining murakkabligi


Rabin-Karp algoritmining o'rtacha va eng yaxshi holatlar murakkabligi va O(m + n)eng yomon holatlarning murakkabligiO(mn).
Eng yomon murakkablik, barcha oynalar uchun bir qator soxta zarbalar sodir bo'lganda yuzaga keladi.

Rabin-Karp algoritmi ilovalari


  • Shaklni moslashtirish uchun

  • Kattaroq matnda qatorni qidirish uchun

// Rabin-Karp algorithm in C++


#include


#include
using namespace std;


#define d 10


void rabinKarp(char pattern[], char text[], int q) {
int m = strlen(pattern);
int n = strlen(text);
int i, j;
int p = 0;
int t = 0;
int h = 1;


for (i = 0; i < m - 1; i++)
h = (h * d) % q;


// Calculate hash value for pattern and text
for (i = 0; i < m; i++) {
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}


// Find the match
for (i = 0; i <= n - m; i++) {
if (p == t) {
for (j = 0; j < m; j++) {
if (text[i + j] != pattern[j])
break;
}


if (j == m)
cout << "Pattern is found at position: " << i + 1 << endl;
}


if (i < n - m) {
t = (d * (t - text[i] * h) + text[i + m]) % q;


if (t < 0)
t = (t + q);
}
}
}


int main() {
char text[] = "ABCCDDAEFG";
char pattern[] = "CDD";
int q = 13;
rabinKarp(pattern, text, q);
}


DESCRIPTION
:


String Matching Algorithm is also called "String Searching Algorithm." This is a vital class of string algorithm is declared as "this is the method to find a place where one is several strings are found within the larger string."



Given a text array, T [1.....n], of n character and a pattern array, P [1......m], of m characters. The problems are to find an integer s, called valid shift where 0 <= s < n-m and T [s+1......s+m] = P [1......m]. In other words, to find even if P in T, i.e., where P is a substring of T. The item of P and T are character drawn from some finite alphabet such as {0, 1} or {A, B .....Z, a, b..... z}.Given a string T [1......n], the substrings are represented as T [i......j] for some 0 <= i <= j <= n-1, the string formed by the characters in T from index i to index j, inclusive. This process that a string is a substring of itself (take i = 0 and j =m). 

The proper substring of string T [1......n] is T [1......j] for some 00 or j < m-1.

There are various String Matching Algorithms but the most commonly used among them.




The Naive String Matching algorithm slides the pattern one by one. After each slide, it one by one checks characters at the current shift and if all characters match then prints the match. Like the Naive Algorithm, Rabin-Karp algorithm also slides the pattern one by one. But unlike the Naive algorithm, Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of text, and if the hash values match then only it starts matching individual characters. 


So Rabin Karp algorithm needs to calculate hash values for following strings:
1) Pattern itself.
2) All the substrings of text of length m.
Since we need to efficiently calculate hash values for all the substrings of size m of text, we must have a hash function which has following property.
Hash at the next shift must be efficiently computable from the current hash value and next character in text or we can say hash(txt[s+1 .. s+m]) must be efficiently computable from hash(txt[s .. s+m-1]) and txt[s+m] i.e., hash(txt[s+1 .. s+m])rehash(txt[s+m], hash(txt[s .. s+m-1])) and rehash must be O(1) operation.
The hash function suggested by Rabin and Karp calculates an integer value. The integer value for a string is numeric value of a string. For example, if all possible characters are from 1 to 10, the numeric value of "122" will be 122. The number of possible characters is higher than 10 (256 in general) and pattern length can be large. So the numeric values cannot be practically stored as an integer. Therefore, the numeric value is calculated using modular arithmetic to make sure that the hash values can be stored in an integer variable (can fit in memory words). To do rehashing, we need to take off the most significant digit and add the new least significant digit for in hash value. Rehashing is done using the following formula.
hash( txt[s+1 .. s+m] ) = ( d ( hash( txt[s .. s+m-1]) - txt[s]*h ) + txt[s + m] ) mod q
hash( txt[s .. s+m-1] ) : Hash value at shift s.
hash( txt[s+1 .. s+m] ) : Hash value at next shift (or shift s+1)
d: Number of characters in the alphabet
q: A prime number
h: d^(m-1)
Working of above formula: 
This is simple mathematics, we compute decimal value of current window from previous window.
For example pattern length is 3 and string is %u201C23456%u201D
You compute the value of first window (which is %u201C234%u201D) as 234.
How how will you compute value of next window %u201C345%u201D? You will do (234 %u2013 2*100)*10 + 5 and get 345.







Download 412.49 Kb.

Do'stlaringiz bilan baham:
1   ...   9   10   11   12   13   14   15   16   ...   21




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling