Naive string matching - PowerPoint PPT Presentation

About This Presentation
Title:

Naive string matching

Description:

string matching alogorithm – PowerPoint PPT presentation

Number of Views:4634
Slides: 10
Provided by: dyners
Category:
Tags:

less

Transcript and Presenter's Notes

Title: Naive string matching


1
Naive-String-MatcherAlgorithm
Expert Arena
ea

2
The problem of String Matching
  • Given a string S, the problem of string
    matching deals with finding whether a pattern p
    occurs in S and if p does occur then
    returning position in S where p occurs.

3
Naive-String-Matcher(T P)
  • 1 n lenghtT
  • 2 m lenghtP // Pattern to be matched
  • 3 for s 0 to n - m do // s number of shifts
  • 4 if P1m Ts 1s m then
  • 5 print Pattern occurs with shift s

4
Initially n size of S 15 m
size of p 7 Step 1 comparing p1 with S1
b a c b a b a b a b a c a a b
S
a b a b a c a
p
s 1
P1 does not match with S1. p will be
shifted one position to the right.
Step 2 comparing p1 with S2
b a c b a b a b a b a c a a b
S
a b a b a c a
p
P1 matches S2. Since there is a match, p is
not shifted.
5
  • Step 3 Comparing p2 with S3

p2 does not match with S3
S
b a c b a b a b a b a c a a b
p
a b a b a c a
s 2
P2 does not match with S3. p will be
shifted one position to the right.
Step 4 comparing p1 with S3
p1 does not match with S3
b a c b a b a b a b a c a a b
S
p
a b a b a c a
s 3
Step 5 comparing p1 with S4
p1 matches with S4
b a c b a b a b a b a c a a b
S
p
a b a b a c a
P1 does not match with S4. p will be
shifted one position to the right.
6
Step 6 Comparing p1 with S5
p1 matches with S5
S
b a c b a b a b a b a c a a b
a b a b a c a
p
s 4
Step 7 Comparing p2 with S6
p2 matches with S6
b a c b a b a b a b a c a a b
S
a b a b a c a
p
Step 8 Comparing p3 with S7
p3 matches with S7
b a c b a b a b a b a c a a b
S
p
a b a b a c a
7
Step 9 Comparing p4 with S8 and than p5
with S9
p4 matches with S8 and p5 with S9
S
b a c b a b a b a b a c a a b
p
a b a b a c a
Step 10 Comparing p6 with S10
p6 doesnt match with S10
b a c b a b a b a b a c a a b
S
p
a b a b a c a
s 6
Shifting again and again we come to shift 6
Step 11 Comparing p5 with S11
p5 matches with S11
b a c b a b a b a b a c a a b
S
p
s 6
a b a b a c a
8
Step 12
Comparing p6 with S12
p6 matches with S12
b a c b a b a b a b a c a a b
S
p
a b a b a c a
Step 13
Comparing p7 with S13
p7 matches with S13
b a c b a b a b a b a c a a b
S
p
a b a b a c a
s6
Pattern p has been found to completely occur in
string S. The total number of shifts that took
place for the match to be found are 6 shifts.
9
  • Algorithm Analysis
  • It takes time ((n - m 1)m) in the worst case.
    For instance if we have string T as an and a
    pattern P of am.
  • For each of the (n - m 1) possible shifts s,
    line 4 will execute m times. Hence, the
    worst-case running time is
  • ((n - m 1)m), which is (m2) if m n/2.
  • Key points
  • The main inefficiency in this algorithm is that
    the valuable information gained about the text
    for one shift s is totally ignored when
    considering other shifts of s.
Write a Comment
User Comments (0)
About PowerShow.com