String matching Algorithm by Foysal
-
Upload
foysal-mahmud -
Category
Education
-
view
17 -
download
0
Transcript of String matching Algorithm by Foysal
![Page 1: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/1.jpg)
05/02/2023
1
STRING MATCHING ALGORITHMS
Presented By:-Md. FoysaL
MahmudUniversity of
Barisal
![Page 2: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/2.jpg)
05/02/2023
2
Index
What is String? What is String Matching? Definition of Algorithm. String Matching Algorithms. String Matching Algorithms with Example.
![Page 3: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/3.jpg)
05/02/2023
3
What is String?
In computer programming, a string is traditionally a sequence
of characters, either as
constant or as some kind of variable.
E.g. Foysal or
14CSE028
![Page 4: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/4.jpg)
05/02/2023
4
What is String?
String may be applied in Bioinformatics to describe DNA strand composed of nitrogenous bases
![Page 5: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/5.jpg)
05/02/2023
5
What is String matching?
In computer science, string searching algorithms, sometimes called string matching algorithms, that try to find a place where one or several string (also called pattern) are found within a larger string or text.
Example: We have a string “Abcdefgh” and the pattern to be searched is “Def”. Now finding “def” in the string “Abcdefgh” is string matching.
![Page 6: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/6.jpg)
05/02/2023
6
EXAMPLE
STRING MATCHING PROBLEM
A B C A B A A C A B
A B A A
TEXT
PATTERN
SHIFT=3
![Page 7: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/7.jpg)
05/02/2023
7
STRING MATCHING ALGORITHMS
There 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 algorithm
![Page 8: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/8.jpg)
05/02/2023
8
Naïve String Matching Algorithm
![Page 9: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/9.jpg)
05/02/2023
9
EXAMPLE SUPPOSE,
T=1011101110P=111 FIND ALL VALID SHIFT……
1 0 1 1 1 0 1 1 1 0
1 1 1P=Pattern
S=0
![Page 10: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/10.jpg)
05/02/2023
10
1 0 1 1 1 0 1 1 1 0
1 1 1S=1
![Page 11: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/11.jpg)
05/02/2023
11
1 0 1 1 1 0 1 1 1 0
1 1 1S=2
So, S=2 is a valid shift…
![Page 12: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/12.jpg)
05/02/2023
12
1 0 1 1 1 0 1 1 1 0
1 1 1S=3
![Page 13: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/13.jpg)
05/02/2023
13
1 0 1 1 1 0 1 1 1 0
1 1 1S=4
![Page 14: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/14.jpg)
05/02/2023
14
1 0 1 1 1 0 1 1 1 0
1 1 1S=5
![Page 15: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/15.jpg)
05/02/2023
15
1 0 1 1 1 0 1 1 1 0
1 1 1S=6
So, S=6 is a valid shift…
![Page 16: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/16.jpg)
05/02/2023
16
1 0 1 1 1 0 1 1 1 0
1 1 1S=7
![Page 17: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/17.jpg)
05/02/2023
17
Naïve String Matching Algorithmvoid search_pattern(string ptr,string txt){
int p=ptr.size();
int t=txt.size();
for(int i=0;i<=t-p;i++) {
int j;
for(j=0;j<p;j++){
if(txt[i+j]!=ptr[j])
break; }
if(j==p)
“Pattern Found”;
}
![Page 18: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/18.jpg)
05/02/2023
18
THE RABIN-KARP ALGORITHM
Rabin 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.
Its complexity O(mn)
![Page 19: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/19.jpg)
05/02/2023
19
Formula:
First select a prime number,like prime=101.
Then find the hash value of Pattern.
Here, Text=“abcdabc”
Pattern=“cda”
*hash value of pattern=
99 + (100*101) + (97*(101)^2)
= 999696
Now apply the following steps:
1. X=old hash – Value (old char)
2. X= x/prime .
3. New hash = x + (prime)^(p-1) * value(new char)
![Page 20: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/20.jpg)
05/02/2023
20
Text = abcdabcabc = 97+98*101+99*(101)^2
= 1019894 != 999696
Text = abcdabcbcd = old hash – Value (old char)
= 1019894 – 97
= 1019797 / 101
= 10097 + 100*(101)^2 =1030197 != 999696
![Page 21: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/21.jpg)
05/02/2023
21
Text = abcdabc cda = 1030197 – 98 = 1030099 / 101
= 10199 + 97*(101)^2 = 999696 == 999696 (Pattern match)
Text = abcdabcdab = 999696 – 99 = 999597/101= 9897 + 98*(101)^2 = 1009595 != 999696
Text = abcdabcabc = 1009595 – 100= 1009495 / 101 = 9995 + 99*(101)^2= 1019894 != 999696
![Page 22: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/22.jpg)
05/02/2023
22
So Pattern found in that text.
Text = ABCDABCPattern = CDA
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 the Pattern is found in the Text.
![Page 23: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/23.jpg)
05/02/2023
23
Coding :int prime=101;string pattern,text; int p=pattern.size(); int t=text.size(); int val=text[0]-'0'; int pattern_value= (pattern[0]-'0')+((pattern[1]-'0')*prime)+
((pattern[2]-'0')*pow(prime,2)); int check; for(int i=0;i<p;i++){ check=(text[0]-'0')+((text[1]-'0')*prime)+((text[2]-'0')*pow(prime,2)); }
if(check==pattern_value) “Pattern Found”
![Page 24: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/24.jpg)
05/02/2023
24
int check_temp=check; for(int j=1;j<t;j++) { int i=j-1; int temp,check2; check2=check_temp; temp=check2-(text[i]-'0'); temp=temp/prime; check_temp=temp+((text[j+2]-'0')*pow(prime,2)); if(check_temp==pattern_value){ “Pattern Found at (j+1) index”;
break; } }
![Page 25: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/25.jpg)
05/02/2023
25
Knuth-Morris-Pratt
AlgorithmKnuth-Morris-Pratt Algorithm has 2 stage:
1. Prefix Function.2. String Matching.
![Page 26: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/26.jpg)
05/02/2023
26
Text = abxabcabcabyPattern = abcabyNow Find Pattern Index:
j ia b c a b y
Here j!=i , So index will be 0.
0 0
![Page 27: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/27.jpg)
05/02/2023
27
Now i is increase… i++;
j ia b c a b
y
Here j!=i , So index will be 0.
0 0 0
![Page 28: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/28.jpg)
05/02/2023
28
Now i is increase…. i++;
j ia b c a b y
Now j==i then index = j+1 = 0+1 = 1
0 0 0 1
![Page 29: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/29.jpg)
05/02/2023
29
Now both i and j will be increase. i++,j++;
j i a b c a b
y
Now j==i then index = j+1 =
1+1 = 2
0 0 0 1 2
![Page 30: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/30.jpg)
05/02/2023
30
Now both i and j will be increase. i++,j++;
j i a b c a b y
Now j!=i, So look previous index value.And Check the index number while represent the value.
0 0 0 1 2
![Page 31: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/31.jpg)
05/02/2023
31
j i
a b c a b y
Now start checking from ‘a’.
0 0 0 1 2
![Page 32: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/32.jpg)
05/02/2023
32
j i a b c a b y
Now j!=i , So index will be 0.
0 0 0 1 2 0
![Page 33: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/33.jpg)
05/02/2023
33
String MatchingText = abxabcabcabyPattern = abcaby
a b x a b c a b c a b y a b c a b y 0 0 0 1 2 0
![Page 34: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/34.jpg)
05/02/2023
34
Here c!=x , So it will go pattern index table previous character value.b = 0;So it will start matching from 0 index of the pattern. a b x a b c a b c a b y a b c a b y
![Page 35: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/35.jpg)
05/02/2023
35
a b x a b c a b c a b y
a b c a b yPattern index:0 1 2 3 4 5Here y!=c , So it will go pattern index table previous character value.b = 2;So it will start matching from 2 index of the pattern.
![Page 36: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/36.jpg)
05/02/2023
36
a b x a b c a b c a b y
a b c a b y
Now Pattern is found in the Text…..
That’s way KMP algorithm works.Its complexity O(m+n)
![Page 37: String matching Algorithm by Foysal](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ac5b760da3da068b8d2f/html5/thumbnails/37.jpg)
05/02/2023
37
THANK YOU…