06. string matching
DESCRIPTION
String MatchingTRANSCRIPT
![Page 1: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/1.jpg)
String Matching Algorithms
Kamalesh Karmakar
Assistant Professor,
Dept. of CSE & IT,
Meghnad Saha Institute of Technology.
![Page 2: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/2.jpg)
String Matching Algorithms &
Complexity
We formalize the string-matching problem as follows. We assume
that the text is an array T [1 . . n] of length n and that the pattern is
an array P[1 . .m] of length m ≤ n. We further assume that the
elements of P and T are characters drawn from a finite alphabet ∑.
For example, we may have ∑ = {0,1} or ∑ = {a, b, . . . , z}. The
character arrays P and T are often called strings of characters.
![Page 3: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/3.jpg)
String Matching Algorithms
We say that pattern P occurs with shift s in text T (or, equivalently, that
pattern P occurs beginning at position s + 1 in text T) if 0 ≤ s ≤ n − m
and T [s + 1 . . s + m] = P[1 . .m] (that is, if T [s + j ] = P[ j ], for 1 ≤ j
≤ m).
If P occurs with shift s in T , then we call s a valid shift; otherwise, we
call s an invalid shift. The string-matching problem is the problem of
finding all valid shifts with which a given pattern P occurs in a given
text T . Figure 32.1 illustrates these definitions.
![Page 4: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/4.jpg)
Complexity comparison of Different
Algorithms
![Page 5: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/5.jpg)
The Naive string-matching
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 + 1
possible values of s.
Procedure NAIVE-STRING-MATCHER takes time O((n − m +
1)m), and this bound is tight in the worst case.
![Page 6: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/6.jpg)
String matching with finite
automata
![Page 7: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/7.jpg)
String matching with finite
automata
![Page 8: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/8.jpg)
String matching with finite
automata
![Page 9: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/9.jpg)
String matching with finite
automata
![Page 10: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/10.jpg)
The Knuth-Morris-Pratt
matching algorithm
![Page 11: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/11.jpg)
The Knuth-Morris-Pratt
matching algorithm
![Page 12: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/12.jpg)
![Page 13: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/13.jpg)
Example
![Page 14: 06. string matching](https://reader033.vdocument.in/reader033/viewer/2022052508/559b06191a28ab6e758b45dc/html5/thumbnails/14.jpg)
Q & A