Docsity

Rabin Karp Algorithm, Schemes and Mind Maps of Pre-Calculus

Toggle sidebar
bg1
1
Rabin Karp Algorithm
Rabin Karp Algorithm
-
-Mihir Dharamshi
Mihir Dharamshi
1. Rabin Karp
1. Rabin Karp –
–Overview
Overview
2. Basics
2. Basics
3. Gruesome Internals
3. Gruesome Internals ☺
☺
4. Applications
4. Applications
5. Questions ??
5. Questions ??
Rabin Karp
Rabin Karp -
-Overview
Overview
Naïve string matching algorithm
Naïve string matching algorithm
Why compare strings as a whole??
Why compare strings as a whole??
Can we reduce the amount of
matching done ??
Representing strings as digits.
Hashing the digits further …
The Basics
The Basics
Every character in the alphabet
Every character in the alphabet
represented as a digit.
represented as a digit.
Assume alphabet set (A, C, T, G, U)
Assume alphabet set (A, C, T, G, U)
Represent them as digits (1, 2, 3, 4, 5)
Represent them as digits (1, 2, 3, 4, 5)
So, a string ACTGCTGATGG would be
So, a string ACTGCTGATGG would be
12342341344
12342341344
Text T [ 1, 2 … n]
Text T [ 1, 2 … n]
Pattern P [ 1, 2, … m ]
Pattern P [ 1, 2, … m ]
Let Pattern P map to decimal ‘p’
Let Pattern P map to decimal ‘p’
Let us create sub strings from the Text of
Let us create sub strings from the Text of
the form
the form
T[ s+1, ….. s+m ] where s = 0,1 ... n
T[ s+1, ….. s+m ] where s = 0,1 ... n-
-m
m
This would map to decimal t(s)
This would map to decimal t(s)
We will have a hit when t(s) == p
We will have a hit when t(s) == p
Suppose we are looking for pattern “GCT”
Suppose we are looking for pattern “GCT”
In the text “ACTGCTGATGG”
In the text “ACTGCTGATGG”
We have,
We have,
Text
Text -
-ACTGCTGATGG
ACTGCTGATGG
Pattern
Pattern –
–GCT maps to p = 423
GCT maps to p = 423
We match the text
We match the text 12342341344
12342341344
. . .423 Match after 3 shifts
. . .423 Match after 3 shifts
Complexity ??
Complexity ??
pf3

Partial preview of the text

Download Rabin Karp Algorithm and more Schemes and Mind Maps Pre-Calculus in PDF only on Docsity!

Rabin Karp Algorithm Rabin Karp Algorithm

- - Mihir DharamshiMihir Dharamshi

„„ 1. Rabin Karp1. Rabin Karp –– OverviewOverview

„„ 2. Basics2. Basics

„„^ 3. Gruesome Internals3. Gruesome Internals^ ☺☺

„„ 4. Applications4. Applications

„„ 5. Questions ??5. Questions ??

Rabin Karp - Rabin Karp- OverviewOverview

„„ Naïve string matching algorithmNaïve string matching algorithm

„„ Why compare strings as a whole??Why compare strings as a whole??

„ Can we reduce the amount of

matching done ??

„ Representing strings as digits.

Hashing the digits further …

The Basics The Basics

„„ Every character in the alphabetEvery character in the alphabet

represented as a digit.represented as a digit.

„„ Assume alphabet set (A, C, T, G, U)Assume alphabet set (A, C, T, G, U)

„„ Represent them as digits (1, 2, 3, 4, 5)Represent them as digits (1, 2, 3, 4, 5)

„„ So, a string ACTGCTGATGG would beSo, a string ACTGCTGATGG would be

„„ TextText T [ 1, 2 … n]T [ 1, 2 … n]

„„ Pattern P [ 1, 2, … m ]Pattern P [ 1, 2, … m ]

„„ Let Pattern P map to decimal ‘p’Let Pattern P map to decimal ‘p’

„„ Let us create sub strings from the Text ofLet us create sub strings from the Text of

the formthe form

„„^ T[ s+1, ….. s+m ] where s = 0,1 ... n-T[ s+1, ….. s+m ] where s = 0,1 ... n-mm

This would map to decimal t(s)This would map to decimal t(s)

We will have a hit when t(s) == pWe will have a hit when t(s) == p

Suppose we are looking for pattern “GCT”Suppose we are looking for pattern “GCT” In the text “ACTGCTGATGG”In the text “ACTGCTGATGG”

We have,We have, TextText - - ACTGCTGATGGACTGCTGATGG PatternPattern –– GCT maps to p = 423GCT maps to p = 423

We match the textWe match the text 1234234134412342341344

.. .423.. .423 Match after 3 shiftsMatch after 3 shifts

Complexity ??Complexity ??

ComplexityComplexity

Processing Time :Processing Time : „„ Time to compute P[ 1… m] = O( m )Time to compute P[ 1… m] = O( m )

Matching TimeMatching Time „„ Time to compute sub string valuesTime to compute sub string values Ts = O( nTs = O( n –– m + 1)m + 1)

Total matching time = O( (nTotal matching time = O( (n--m+1) m )m+1) m ) Because each substring has m digits …Because each substring has m digits … So matching m digits each time for nSo matching m digits each time for n--m+1 substrings ..m+1 substrings ..

So, what do we have here …So, what do we have here …

AnotherAnother NaïveNaïve ??????

Some Facts Some Facts

„„ In the worst case, Rabin Karp is no better than aIn the worst case, Rabin Karp is no better than a

naïve string matcher.naïve string matcher.

„„ What if we have an immensely large p, then weWhat if we have an immensely large p, then we

consequently have large Ts for every case.consequently have large Ts for every case.

„„^ However, highly suited for practical applicationsHowever, highly suited for practical applications

where substrings don’t differ by a big deal e.g.,where substrings don’t differ by a big deal e.g.,

DNA sequencingDNA sequencing

„„ We shall now see how it performs better for theWe shall now see how it performs better for the

average case for such applicationsaverage case for such applications

Some ImprovementsSome Improvements

„„ Let us compute p and all the Ts modulo aLet us compute p and all the Ts modulo a

suitable modulus q.suitable modulus q.

eg: 42456 mod 37 ,eg: 42456 mod 37 , q = 37q = 37

Now, we can compute p modulo q in O( m ) time TsNow, we can compute p modulo q in O( m ) time Ts

modulo q inmodulo q in

O( nO( n--m+1 ) time.m+1 ) time.

Now, all we do is match the new digits i.e, pNow, all we do is match the new digits i.e, p

modulo q and Ts modulo q.modulo q and Ts modulo q.

How do we save time??How do we save time??

„„ Note that earlier we were matching eachNote that earlier we were matching each

of the m digits in a pattern, so now we areof the m digits in a pattern, so now we are

only matching one digit.only matching one digit.

„„ This is the digit representing the subThis is the digit representing the sub

string.string.

„„ So, while processing time is the sameSo, while processing time is the same

O( m )O( m )

„„^ Matching time reduces from O((n-Matching time reduces from O((n-m+1)m)m+1)m)

to O( (n-to O( (n-m+1) 1 )m+1) 1 )

„„^ So, the Total matching time now isSo, the Total matching time now is

O( m ) + O( nO( m ) + O( n--m+1 ) = O( n )m+1 ) = O( n )

„„^ Better than Naïve ??Better than Naïve ??

Potential ProblemPotential Problem

„„ Finding a good hashing functionFinding a good hashing function

could be a pain. 2 strings could hashcould be a pain. 2 strings could hash to the same value.to the same value.

„„ So, we provide an additional check ifSo, we provide an additional check if

the hash matches. As you will see inthe hash matches. As you will see in the algorithm , the overhead is notthe algorithm , the overhead is not much.much.

„„ But, we still need a good hashingBut, we still need a good hashing

functionfunction

Some Math for youSome Math for you ☺☺

„„ How about the modulus function we used forHow about the modulus function we used for

hashing ?? Can you suggest an optimal one.hashing ?? Can you suggest an optimal one.

„„ The modulus operator q is chosen such that 10qThe modulus operator q is chosen such that 10q

fits into one computer word, so that all necessaryfits into one computer word, so that all necessary

computations are done with single precisioncomputations are done with single precision

arithmetic … Extra floating point arithmetic wouldarithmetic … Extra floating point arithmetic would

have its toll.have its toll.

„„ And yeah, q is typically a prime numberAnd yeah, q is typically a prime number

„„ Generalising , we haveGeneralising , we have

t(s+1) = (d( tst(s+1) = (d( ts –– T[s +1 ]h) + T[ s+m+1 ]) mod qT[s +1 ]h) + T[ s+m+1 ]) mod q