100% found this document useful (1 vote)
356 views

16 String Matching - Naive String Algorithm

Uploaded by

ctxxfx5ykz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
356 views

16 String Matching - Naive String Algorithm

Uploaded by

ctxxfx5ykz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Design and Analysis of Algorithms

String Matching Problem


(Naïve String Matching Algorithm)

Dr. D. P. Acharjya
Professor, SCOPE
SJT Annex – 201 E
Dr. D. P. Acharjya 2/23/2024 1
String Matching Problem
 Finding all occurrences of a pattern (String) in a text
leads to string matching problem.
 It arises frequently in text editing programs where text
is a document, and the string searched for is a
particular word.
 String-matching algorithms are also used to search for
particular patterns in DNA sequences.
 For Example:

Dr. D. P. Acharjya 2/23/2024 2


Mathematical Formulation
 Assume that the text is an array T[1  n] of length n.
The pattern is an array P[1  m] of length m. It is to be
noted that m ≤ n.

 It is to be noted that the elements of P and T are


characters drawn from a finite alphabet Σ. For example,
Σ = {0, 1} or Σ = {a, b, , z}.

 The character arrays P and T are often called strings of


characters.
Dr. D. P. Acharjya 2/23/2024 3
Continued…
 The pattern P occurs with shift s in text T (or 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]
 It means that T[s + j] = P[j], for 1 ≤ j ≤ m.
 If P occurs with shift s in T, then it is a valid shift
otherwise, s is 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.

Dr. D. P. Acharjya 2/23/2024 4


String Matching Algorithms
1. Naïve String-matching Algorithm

2. Rabin-Karp Algorithm

3. Knuth Morris Pratt (KMP) algorithm

4. String matching with finite automata (Suffix Trees)

Dr. D. P. Acharjya 2/23/2024 5


Naïve String Matching Algorithm
1. NAIVE-STRING-MATCHER (T, P)
2. n ← Length[T]
3. m ←Length[P]
4. for s ← 0 to (n – m)
5. do if P[1  m] = T[s + 1  s+m]
6. then print "Pattern occurs with shift" s

 The number of possible values of s is (n – m + 1)


 Each time we have to check P[j] = T[s + j]; 1 j  m
 Computing Time is O((n – m + 1)m)
Dr. D. P. Acharjya 2/23/2024 6
Numerical Illustration
 Consider a text T = acaabc,
find all the valid shifts for the
pattern P = aab.

Dr. D. P. Acharjya 2/23/2024 7


Numerical Illustration

 The process terminates with a valid shift s = 2.

Dr. D. P. Acharjya 2/23/2024 8


Practice Problem
 Show the comparisons the naive string matcher makes
for the pattern P = 0001 in the text T =
000010001010001.

Dr. D. P. Acharjya 2/23/2024 9

You might also like