SlideShare a Scribd company logo
1 of 37
STRING MATCHING ALGORITHMS
Presented By:-
Md. FoysaL Mahmud
University of Barisal 3/25/201
7
1
Index
 What is String?
 What is String Matching?
 Definition of Algorithm.
 String Matching Algorithms.
 String Matching Algorithms with Example.
3/25/201
7
2
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
3/25/201
7
3
What is String?
 String may be applied in Bioinformatics to describe DNA strand composed of
nitrogenous bases
3/25/201
7
4
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.
3/25/201
7
5
EXAMPLE
STRING MATCHING PROBLEM
3/25/201
7
6
A B C A B A A C A B
A B A A
TEXT
PATTERN
SHIFT=3
STRING MATCHING ALGORITHMS
There are many types of String Matching
Algorithms like:-
1) The Naive string-matching algorithm
2) The Rabin-Krap algorithm
3) String matching with finite automata
4) The Knuth-Morris-Pratt algorithm
3/25/201
7
7
Naïve String Matching Algorithm
3/25/201
7
8
EXAMPLE
 SUPPOSE,
T=1011101110
P=111
FIND ALL VALID SHIFT……
3/25/201
7
9
1 0 1 1 1 0 1 1 1 0
1 1 1P=Patter
n
S=0
1 0 1 1 1 0 1 1 1 0
3/25/201
7
10
1 1 1
S=1
1 0 1 1 1 0 1 1 1 0
1 1 1
S=2
So, S=2 is a valid shift…
3/25/201
7
11
1 0 1 1 1 0 1 1 1 0
1 1 1
S=3
3/25/201
7
12
1 0 1 1 1 0 1 1 1 0
1 1 1
S=4
3/25/201
7
13
1 0 1 1 1 0 1 1 1 0
1 1 1
S=5
3/25/201
7
14
1 0 1 1 1 0 1 1 1 0
1 1 1
S=6
So, S=6 is a valid shift…
3/25/201
7
15
1 0 1 1 1 0 1 1 1 0
1 1 1
S=7
3/25/201
7
16
Naïve String Matching Algorithm
void 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”;
}
3/25/201
7
17
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)
3/25/201
7
18
 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)
3/25/201
7
19
Text = abcdabc
abc = 97+98*101+99*(101)^2
= 1019894 != 999696
Text = abcdabc
bcd = old hash – Value (old char)
= 1019894 – 97
= 1019797 / 101
= 10097 + 100*(101)^2 =1030197 != 999696
3/25/201
7
20
Text = abcdabc
cda = 1030197 – 98 = 1030099 / 101
= 10199 + 97*(101)^2
= 999696 == 999696 (Pattern match)
Text = abcdabc
dab = 999696 – 99 = 999597/101
= 9897 + 98*(101)^2
= 1009595 != 999696
Text = abcdabc
abc = 1009595 – 100
= 1009495 / 101 = 9995 + 99*(101)^2
= 1019894 != 999696
3/25/201
7
21
So Pattern found in that text.
Text = ABCDABC
Pattern = 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.
3/25/201
7
22
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”
3/25/201
7
23
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;
}
}
3/25/201
7
24
Knuth-Morris-Pratt
Algorithm
Knuth-Morris-Pratt Algorithm has 2 stage:
1. Prefix Function.
2. String Matching.
3/25/201
7
25
Text = abxabcabcaby
Pattern = abcaby
Now Find Pattern Index:
j i
a b c a b y
Here j!=i , So index will be 0.
3/25/201
7
26
0 0
Now i is increase… i++;
j i
a b c a b y
Here j!=i , So index will be 0.
3/25/201
7
27
0 0 0
Now i is increase…. i++;
j i
a b c a b y
Now j==i then index = j+1
= 0+1 = 1
3/25/201
7
28
0 0 0 1
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
3/25/201
7
29
0 0 0 1 2
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.
3/25/201
7
30
0 0 0 1 2
j i
a b c a b y
Now start checking from ‘a’.
3/25/201
7
31
0 0 0 1 2
j i
a b c a b y
Now j!=i , So index will be 0.
3/25/201
7
32
0 0 0 1 2 0
String Matching
Text = abxabcabcaby
Pattern = abcaby
a b x a b c a b c a b y
a b c a b y
3/25/201
7
33
0 0 0 1 2 0
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
3/25/201
7
34
a b x a b c a b c a b y
a b c a b y
Pattern index:0 1 2 3 4 5
Here 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.
3/25/201
7
35
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)
3/25/201
7
36
THANK YOU…
3/25/201
7
37

More Related Content

What's hot

Caesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
Caesar Cipher , Substitution Cipher, PlayFair and Vigenere CipherCaesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
Caesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
Mona Rajput
 

What's hot (20)

String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.
 
Modern Cryptography
Modern CryptographyModern Cryptography
Modern Cryptography
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Caesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
Caesar Cipher , Substitution Cipher, PlayFair and Vigenere CipherCaesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
Caesar Cipher , Substitution Cipher, PlayFair and Vigenere Cipher
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
 
Boyer more algorithm
Boyer more algorithmBoyer more algorithm
Boyer more algorithm
 
Ch02...1
Ch02...1Ch02...1
Ch02...1
 
String matching algorithms-pattern matching.
String matching algorithms-pattern matching.String matching algorithms-pattern matching.
String matching algorithms-pattern matching.
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
KMP String Matching Algorithm
KMP String Matching AlgorithmKMP String Matching Algorithm
KMP String Matching Algorithm
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Trie Data Structure
Trie Data Structure Trie Data Structure
Trie Data Structure
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
 
Boyer moore
Boyer mooreBoyer moore
Boyer moore
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithm
 

Viewers also liked

Viewers also liked (18)

How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 
Authentication Using Hand Vein Pattern
Authentication Using Hand Vein PatternAuthentication Using Hand Vein Pattern
Authentication Using Hand Vein Pattern
 
hand vein structure authentication
hand vein structure authenticationhand vein structure authentication
hand vein structure authentication
 
Palm vein technology
Palm vein technologyPalm vein technology
Palm vein technology
 
Biometric Databases and Hadoop__HadoopSummit2010
Biometric Databases and Hadoop__HadoopSummit2010Biometric Databases and Hadoop__HadoopSummit2010
Biometric Databases and Hadoop__HadoopSummit2010
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
An Introduction to Computer Vision
An Introduction to Computer VisionAn Introduction to Computer Vision
An Introduction to Computer Vision
 
Computer Vision
Computer VisionComputer Vision
Computer Vision
 
Palm-Vein Technology
Palm-Vein TechnologyPalm-Vein Technology
Palm-Vein Technology
 
Computer Vision Crash Course
Computer Vision Crash CourseComputer Vision Crash Course
Computer Vision Crash Course
 
Computer Vision
Computer VisionComputer Vision
Computer Vision
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Major project report on
Major project report onMajor project report on
Major project report on
 
Perl - die Taschenkettensäge unter den Programmiersprachen - Vortrag 2003
Perl - die Taschenkettensäge unter den Programmiersprachen - Vortrag 2003Perl - die Taschenkettensäge unter den Programmiersprachen - Vortrag 2003
Perl - die Taschenkettensäge unter den Programmiersprachen - Vortrag 2003
 
Nigh Session Scala
Nigh Session ScalaNigh Session Scala
Nigh Session Scala
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to String matching Algorithm by Foysal

Museum Paper Rubric50 pointsRubric below is a chart form of .docx
Museum Paper Rubric50 pointsRubric below is a chart form of .docxMuseum Paper Rubric50 pointsRubric below is a chart form of .docx
Museum Paper Rubric50 pointsRubric below is a chart form of .docx
gilpinleeanna
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
 

Similar to String matching Algorithm by Foysal (20)

regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
M C6java7
M C6java7M C6java7
M C6java7
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
String_Matching_algorithm String_Matching_algorithm .pptx
String_Matching_algorithm String_Matching_algorithm .pptxString_Matching_algorithm String_Matching_algorithm .pptx
String_Matching_algorithm String_Matching_algorithm .pptx
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Strings
StringsStrings
Strings
 
String slide
String slideString slide
String slide
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
 
Matlab lecture 7 – regula falsi or false position method@taj
Matlab lecture 7 – regula falsi or false position method@tajMatlab lecture 7 – regula falsi or false position method@taj
Matlab lecture 7 – regula falsi or false position method@taj
 
Periodic pattern mining
Periodic pattern miningPeriodic pattern mining
Periodic pattern mining
 
Periodic pattern mining
Periodic pattern miningPeriodic pattern mining
Periodic pattern mining
 
Data Representation of Strings
Data Representation of StringsData Representation of Strings
Data Representation of Strings
 
Museum Paper Rubric50 pointsRubric below is a chart form of .docx
Museum Paper Rubric50 pointsRubric below is a chart form of .docxMuseum Paper Rubric50 pointsRubric below is a chart form of .docx
Museum Paper Rubric50 pointsRubric below is a chart form of .docx
 
C programming part4
C programming part4C programming part4
C programming part4
 
C programming part4
C programming part4C programming part4
C programming part4
 
Parenthesization.pptx
Parenthesization.pptxParenthesization.pptx
Parenthesization.pptx
 
14078956.ppt
14078956.ppt14078956.ppt
14078956.ppt
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
 

More from Foysal Mahmud (6)

Rs-232 by Foysal
Rs-232 by FoysalRs-232 by Foysal
Rs-232 by Foysal
 
IEEE-488 by Foysal
IEEE-488 by FoysalIEEE-488 by Foysal
IEEE-488 by Foysal
 
SRS Project Report by Foysal
SRS Project Report by FoysalSRS Project Report by Foysal
SRS Project Report by Foysal
 
Algorithm project by Foysal
Algorithm project by FoysalAlgorithm project by Foysal
Algorithm project by Foysal
 
Cache memory by Foysal
Cache memory by FoysalCache memory by Foysal
Cache memory by Foysal
 
Blood bank E-R diagram by Foysal
Blood bank E-R diagram by FoysalBlood bank E-R diagram by Foysal
Blood bank E-R diagram by Foysal
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 
Liberal & Redical Feminism presentation.pptx
Liberal & Redical Feminism presentation.pptxLiberal & Redical Feminism presentation.pptx
Liberal & Redical Feminism presentation.pptx
Rizwan Abbas
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Liberal & Redical Feminism presentation.pptx
Liberal & Redical Feminism presentation.pptxLiberal & Redical Feminism presentation.pptx
Liberal & Redical Feminism presentation.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 

String matching Algorithm by Foysal

  • 1. STRING MATCHING ALGORITHMS Presented By:- Md. FoysaL Mahmud University of Barisal 3/25/201 7 1
  • 2. Index  What is String?  What is String Matching?  Definition of Algorithm.  String Matching Algorithms.  String Matching Algorithms with Example. 3/25/201 7 2
  • 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 3/25/201 7 3
  • 4. What is String?  String may be applied in Bioinformatics to describe DNA strand composed of nitrogenous bases 3/25/201 7 4
  • 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. 3/25/201 7 5
  • 6. EXAMPLE STRING MATCHING PROBLEM 3/25/201 7 6 A B C A B A A C A B A B A A TEXT PATTERN SHIFT=3
  • 7. STRING MATCHING ALGORITHMS There are many types of String Matching Algorithms like:- 1) The Naive string-matching algorithm 2) The Rabin-Krap algorithm 3) String matching with finite automata 4) The Knuth-Morris-Pratt algorithm 3/25/201 7 7
  • 8. Naïve String Matching Algorithm 3/25/201 7 8
  • 9. EXAMPLE  SUPPOSE, T=1011101110 P=111 FIND ALL VALID SHIFT…… 3/25/201 7 9 1 0 1 1 1 0 1 1 1 0 1 1 1P=Patter n S=0
  • 10. 1 0 1 1 1 0 1 1 1 0 3/25/201 7 10 1 1 1 S=1
  • 11. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=2 So, S=2 is a valid shift… 3/25/201 7 11
  • 12. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=3 3/25/201 7 12
  • 13. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=4 3/25/201 7 13
  • 14. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=5 3/25/201 7 14
  • 15. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=6 So, S=6 is a valid shift… 3/25/201 7 15
  • 16. 1 0 1 1 1 0 1 1 1 0 1 1 1 S=7 3/25/201 7 16
  • 17. Naïve String Matching Algorithm void 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”; } 3/25/201 7 17
  • 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) 3/25/201 7 18
  • 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) 3/25/201 7 19
  • 20. Text = abcdabc abc = 97+98*101+99*(101)^2 = 1019894 != 999696 Text = abcdabc bcd = old hash – Value (old char) = 1019894 – 97 = 1019797 / 101 = 10097 + 100*(101)^2 =1030197 != 999696 3/25/201 7 20
  • 21. Text = abcdabc cda = 1030197 – 98 = 1030099 / 101 = 10199 + 97*(101)^2 = 999696 == 999696 (Pattern match) Text = abcdabc dab = 999696 – 99 = 999597/101 = 9897 + 98*(101)^2 = 1009595 != 999696 Text = abcdabc abc = 1009595 – 100 = 1009495 / 101 = 9995 + 99*(101)^2 = 1019894 != 999696 3/25/201 7 21
  • 22. So Pattern found in that text. Text = ABCDABC Pattern = 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. 3/25/201 7 22
  • 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” 3/25/201 7 23
  • 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; } } 3/25/201 7 24
  • 25. Knuth-Morris-Pratt Algorithm Knuth-Morris-Pratt Algorithm has 2 stage: 1. Prefix Function. 2. String Matching. 3/25/201 7 25
  • 26. Text = abxabcabcaby Pattern = abcaby Now Find Pattern Index: j i a b c a b y Here j!=i , So index will be 0. 3/25/201 7 26 0 0
  • 27. Now i is increase… i++; j i a b c a b y Here j!=i , So index will be 0. 3/25/201 7 27 0 0 0
  • 28. Now i is increase…. i++; j i a b c a b y Now j==i then index = j+1 = 0+1 = 1 3/25/201 7 28 0 0 0 1
  • 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 3/25/201 7 29 0 0 0 1 2
  • 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. 3/25/201 7 30 0 0 0 1 2
  • 31. j i a b c a b y Now start checking from ‘a’. 3/25/201 7 31 0 0 0 1 2
  • 32. j i a b c a b y Now j!=i , So index will be 0. 3/25/201 7 32 0 0 0 1 2 0
  • 33. String Matching Text = abxabcabcaby Pattern = abcaby a b x a b c a b c a b y a b c a b y 3/25/201 7 33 0 0 0 1 2 0
  • 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 3/25/201 7 34
  • 35. a b x a b c a b c a b y a b c a b y Pattern index:0 1 2 3 4 5 Here 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. 3/25/201 7 35
  • 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) 3/25/201 7 36