KEMBAR78
String Matching Algorithms-The Naive Algorithm | PPTX
STRING
MATCHING
ALGORITHMS
A B C A B A A C A B
SHIFT=3
A B A A
BY: Adeel Rasheed
 For More Slides and Information Visit:
 https://chauhantricks.blogspot.com/
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
STRING MATCHING PROBLEM
TEXTA B C A B A A C A B
SHIFT=3
A B A A
Types of SMA
 The Naive string-matching algorithm
 The Rabin-Krap algorithm
 String matching with finite automata
 The Knuth-Morris-Pratt algorithm
THE NAIVE ALGORITHM
 The idea of the naive solution is just to make a
comparison character by character of the text for all
pattern.
 It returns all the valid shifts found.
 P=Pattern
 T=Text
 S=Shift
THE NAIVE ALGORITHM
T[n]=T[0,1…n-1]
P[m]=P[0,1…m-1]
NAIVE-STRING-MATCHER(T,P)
1) n = T.length
2) m = P.length
3) for s=0 to n-m
4) if P[0…m-1]==T[s+0….s+m-1]
5) print” Pattern occurs with shift ” s
EXAMPLE
SUPPOSE,
T=1011101110
P=111
FIND ALL VALIDSHIFT……
1 0 1 1 1 0 1 1 1 0
1 1 1P=Patter
n
T=Text
S=0
1 0 1 1 1 0 1 1 1 0
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…
1 0 1 1 1 0 1 1 1 0
S=3
1 1 1
1 0 1 1 1 0 1 1 1 0
S=4
1 1 1
1 0 1 1 1 0 1 1 1 0
1 1 1
S=5
1 0 1 1 1 0 1 1 1 0
S=6
1 1 1
So, S=6 is a valid shift…
1 0 1 1 1 0 1 1 1 0
1 1 1
S=7
String Matching Algorithms-The Naive Algorithm

String Matching Algorithms-The Naive Algorithm