string matching algorithms

Click here to load reader

Upload: ashikapokiya12345

Post on 20-Jan-2017

1.029 views

Category:

Data & Analytics


0 download

TRANSCRIPT

STRING MATCHING ALGORITHMS

Presented By:-Ashika Pokiya(12TI083)Guide by:-Nehal PatelSTRING MATCHING ALGORITHMS

WHAT IS STRING MATCHINGIncomputer science,string searching algorithms, sometimes calledstring matching algorithms, that try to find a place where one or severalstring (also calledpattern) are found within a larger string or text.

EXAMPLE

STRING MATCHING PROBLEM

ABCABAACAB

ABAA

TEXTPATTERNSHIFT=3

STRING MATCHING ALGORITHMSThere are many types of String MatchingAlgorithms like:-1)The Naive string-matching algorithm2)The Rabin-Krap algorithm3)String matching with finite automata4)The Knuth-Morris-Pratt algorithmBut we discuss about 2 types of string matching algorithms.1)The Naive string-matching algorithm2)The Rabin-Krap algorithm

THE NAIVE ALGORITHM The naive algorithm finds all valid shifts using a loop that checks the condition P[1.m]=T[s+1. s+m] for each of the n-m+1possible values of s.(P=pattern , T=text/string , s=shift)NAIVE-STRING-MATCHER(T,P)n = T.length m = P.lengthfor s=0 to n-m if P[1m]==T[s+1.s+m] printf Pattern occurs with shift s

EXAMPLESUPPOSE,T=1011101110P=111 FIND ALL VALID SHIFT

1011101110

T=Text111

P=Pattern

S=0

1011101110

111

S=1

1011101110

111

S=2So, S=2 is a valid shift

1011101110

111

S=3

1011101110

111

S=4

1011101110

1 11

S=5

1011101110

111

S=6So, S=6 is a valid shift

1011101110

111

S=7

THE RABIN-KARP ALGORITHMRabin and Karp proposed a string matching algorithm that performs well in practice and that also generalizes to other algorithms for related problems, such as two-dimentional pattern matching.

ALGORITHMRABIN-KARP-MATCHER(T,P,d,q)n = T.lengthm = P.lengthh = d^(m-1) mod qp = 0t = 0for i =1 to m //pre-processing p = (dp + P[i]) mod q t = (d t + T[i]) mod qfor s = 0 to n m //matching if p == t if P[1m] == T[s+1. s+m] printf Pattern occurs with shift s if s< n-m t+1 = (d(t- T[s+1]h)+ T[s+m+1]) mod q

EXAMPLEPattern P=26, how many spurious hits does the RabinKarp matcher in the text T=3 1 4 1 5 9 2 6 5 3 5T = 3 1 4 1 5 9 2 6 5 3 5P = 2 6Here T.length=11 so Q=11and P mod Q = 26 mod 11 = 4Now find the exact match of P mod Q

31415926535

31415926535

3 1 mod 1 1 = 9 not equal to 4 1 4 mod 1 1 = 3 not equal to 4 4 1 mod 1 1 = 8 not equal to 4 31415926535

S=1S=0S=2

31415926535

31415926535

31415926535

1 5 mod 1 1 = 4 equal to 4 SPURIOUS HIT 5 9 mod 1 1 = 4 equal to 4 SPURIOUS HIT 9 2 mod 1 1 = 4 equal to 4 SPURIOUS HIT S=3S=4S=5

Spurious hit is when we have a match but it isnt an actual match to the pattern. When this happen, further testing is done.

18

31415926535

31415926535

31415926535

2 6 mod 1 1 = 4 EXACT MATCH 6 5 mod 1 1 = 10 not equal to 4 5 3 mod 1 1 = 9 not equal to 4 S=7S=6S=8

31415926535

3 5 mod 1 1 = 2 not equal to 4 S=9Pattern occurs with shift 6

COMPARISSION TheNaive String Matchingalgorithm 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.

THANK YOU