Algo Lab Project

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

UNIVERSITY OF ENGINEERING AND

MANAGEMENT, KOLKATA

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING(CSE)
SUBJECT NAME: ALGORITHM PRACTICAL LAB
COPY
SUBJECT CODE: CS591

NAME- SAGNIK CHATTOPADHYAY


ENROLLMENT NO- 3312016009001479
ROLL NO- 95
STREAM- CSE
INTRODUCTION

In computer science, the Rabin–Karp algorithm or Karp–Rabin algorithm is a string-


searching algorithm created by Richard M. Karp and Michael O. Rabin (1987) that
uses hashing to find an exact match of a pattern string in a text. It uses a rolling hash to
quickly filter out positions of the text that cannot match the pattern, and then checks
for a match at the remaining positions. Generalizations of the same idea can be used to
find more than one match of a single pattern, or to find matches for more than one
pattern. Rabin-Karp is another pattern searching algorithm to find the pattern in a
more efficient way. It also checks the pattern by moving window one by one, but
without checking all characters for all cases, it finds the hash value. When the
hash value is matched, then only it tries to check each character. This procedure
makes the algorithm more efficient.
Moving a level above the naive approach to string matching
can be done by making use of the comparisons being done
during each iteration and using it for the next iteration . In the
brute force approach , say we compare T[a…b] with the Pattern
P in an iteration and in the next iteration we compare
T[a+1….b+1] . So, we are comparing the same characters
again and again , which leads to in-efficiency .

The Rabin Karp Algorithm makes a better attempt in solving the


above problem . Before giving the implementation we can
define a few steps to easily comprehend the Algorithm
implementation.

Algorithm

rabinKarpSearch(text, pattern, prime)


Input: The main text and the pattern. Another prime number of find hash location
Output: location where patterns are found
Begin
patLen := pattern Length
strLen := string Length
patHash := 0 and strHash := 0, h := 1
maxChar := total number of characters in character set

for index i of all character in pattern, do


h := (h*maxChar) mod prime
done

for all character index i of pattern, do


patHash := (maxChar*patHash + pattern[i]) mod prime
strHash := (maxChar*strHash + text[i]) mod prime
done

for i := 0 to (strLen - patLen), do


if patHash = strHash, then
for charIndex := 0 to patLen -1, do
if text[i+charIndex] ≠ pattern[charIndex], then
break the loop
done
if charIndex = patLen, then
print the location i as pattern found at i position.
if i < (strLen - patLen), then
strHash := (maxChar*(strHash – text[i]*h)+text[i+patLen]) mod
prime, then
if strHash < 0, then
strHash := strHash + prime
done
End

Code (in C language)

#include<stdio.h>
#include<string.h>

int main()
{
printf("\t\tRABIN KARP ALGORITHM\n");
char a[1000],b[1000];

printf("Enter the main text :\n");


gets(a);
printf("Enter the text to be searched :\n");
gets(b);
int la = strlen(a);
int lb = strlen(b);
int d = 100,p=0,t=0,h=1,i,j,mod = 101;

for(i=0;i<lb-1;i++)
h=(d*h)%mod;

for(i=0;i<lb;i++)
{
p = (d*p + b[i])%mod;
t = (d*t + a[i])%mod;
}

for(i=0;i<=la-lb;i++)
{ if(p==t)
{
for(j=0;j<lb;j++)
{
if(a[i+j]!=b[j])
break;
}
if(j==lb)
printf("pattern found at %d",i+1);
}
if(i<la-lb)
{
t = (d*(t - a[i]*h) + a[i+lb])%mod;

if(t < 0)
t = (t + mod);
}
}
return 0;
}

Sample inputs and outputs


Applications

A practical application of the algorithm is detecting plagiarism. Given


source material, the algorithm can rapidly search through a paper for
instances of sentences from the source material, ignoring details such
as case and punctuation. Because of the abundance of the sought
strings, single-string searching algorithms are impractical.
Complexity analysis

The running time of RABIN-KARP-MATCHER in the worst


case scenario O ((n-m+1) m but it has a good average case
running time. If the expected number of strong shifts is small O
(1) and prime q is chosen to be quite large, then the Rabin-
Karp algorithm can be expected to run in time O (n+m) plus the
time to require to process spurious hits.

------------------*------------------

You might also like