SlideShare a Scribd company logo
STRING
MATCHING WITH
FINITE AUTOMATA
SUBMITTED TO : MAM MAIMOONA
SUBMITTED BY : IQRA MUNIR
ANMOL HAMID
ANALYSIS OF ALGORITHM
INTRODUCTION
…
STRING MATCHING WITH
FINITE AUTOMATA:
• String matching algorithm builds a finite
automaton
• A simple machine for processing information
that scans the text string T for all occurrences of
the pattern P
FINITE AUTOMATA…
FINITE
AUTOMATA
STRING MATCHING
WITH
FINITE AUTOMATA…
STRING
MATCHING
AUTOMATA
(SUFFIX
FUNCTION):• In order to specify the string-matching automaton
corresponding to a given pattern P[1…m]
• An auxiliary function σ, called the suffix function
corresponding to P
• The function σ maps * to {0,1,…..,m} such thatƩ
σ(x) is the length of the longest prefix of P that is
also a suffix of x:
• σ(x) = max{k: Pk ⊐x}
ALGORITHMS…
COMPUTE TRANSITION
FUNCTION:
COMPUTE-TRANSITION-FUNCTION(P, )Ʃ
1.m-P.length
2.for q=0 to m
3. for each character a є Ʃ
4. k= min(m+1,q+2)
5. repeat
6. K=k-1
7. until P k P q a⊐
8. δ(q ,a )=k
9.return δ
DRY RUNNING…
COMPUTE TRANSITION
FUNCTION:
• TEXT=abababacaba
• PATTERN=ababaca
• ={a,b,c}Ʃ
Compute transition
function(P, )Ʃ
1 m← length[P]
m← 7
2 for q←0 to m
for q←0 to 7
3 do for each character aєƩ
// ={a,b,c}Ʃ
1st
ITERATION:
q=0, a=a
3. for each character aєa
4. k←min(m+1,q+2)
= min(7+1,0+2)=2
K←2
7. Pk Pqa= P2 P0a⊐ ⊐
(ab a) FALSE⊐
K←1
7.Pk Pqa= P1 P0a⊐ ⊐
(a a) TRUE⊐
8. δ(q,a)←k
δ(0,a)←1
state a b c P
0 1 a
1 b
2 a
3 b
4 a
5 c
6 a
7
CONT…
q=0
3. for each character aєb
4. K=2
7. Pk Pqa=P2 P0a⊐ ⊐
(ab b) FALSE⊐
K=1
7.Pk Pqa=P1 P0a⊐ ⊐
(a b) FALSE⊐
K=0
Pk Pqa=P0 P0b⊐ ⊐
(є b) TRUE⊐
8.δ(q,a)←k
δ(0,b)←0
state a b c P
0 1 0 a
1 b
2 a
3 b
4 a
5 c
6 a
7
CONT…
q=0
3. for each character aєc
4. K=2
7. Pk Pqa=P2 P0a⊐ ⊐
(ab c) FALSE⊐
K=1
7.Pk Pqa=P1 P0a⊐ ⊐
(a c) FALSE⊐
K=0
Pk Pqa=P0 P0s⊐ ⊐
(є c) TRUE⊐
8.δ(q,a)←k
δ(0,c)←0
state a b c P
0 1 0 0 a
1 b
2 a
3 b
4 a
5 c
6 a
7
2nd
ITERATION:
2.for q←1 to 7
3.for each character aє(a,b,c)
q=1,a=a
4.k←min(m+1,q+2)=min(7+1,1
+2)=3
K←3
Pk Pqa=P3 P1a=(aba aa)⊐ ⊐ ⊐
FALSE
k←2
7. Pk Pqa=P2 P1a⊐ ⊐
(ab aa) FALSE⊐
K=1
7.Pk Pqa=P1 P1a⊐ ⊐
(a aa) TRUE⊐
8.δ(q,a)←k
δ(1,a)←1
State a b c P
0 1 0 0 a
1 1 b
2 a
3 b
4 a
5 c
6 a
7
CONT…
q=1
for each character aєb
4.K←3
Pk Pqa=P3 P1a=(aba ab)⊐ ⊐ ⊐
FALSE
k←2
7. Pk Pqa=P2 P1a⊐ ⊐
(ab ab) TRUE⊐
8.δ(1,b) 2←
State a b c P
0 1 0 0 a
1 1 2 b
2 a
3 b
4 a
5 c
6 a
7
CONT…
q=1
for each character aєc
4.K←3
Pk Pqa=P3 P1a=(aba ac)⊐ ⊐ ⊐
FALSE
k←2
7. Pk Pqa=P2 P1a⊐ ⊐
(ab ac) FALSE⊐
k 1←
Pk Pqa=P1 P1a⊐ ⊐
(a ac) FALSE⊐
k 0←
Pk Pqa=P0 P1a⊐ ⊐
(є ac) TRUE⊐
8.δ(1,c)←0
State a b c P
0 1 0 0 a
1 1 2 0 b
2 a
3 b
4 a
5 c
6 a
7
3rd
ITERATION:
2.for q←2 to 7
3.for each character aє(a,b,c)
q=2,a=a
4.k←min(m+1,q+2)=min(8,4)=4
K←4
Pk Pqa=P4 P2a=(abab aba)⊐ ⊐ ⊐
FALSE
Pk Pqa=P3 P2a=(aba aba)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(2,a)←3
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 a
3 b
4 a
5 c
6 a
7
CONT…
q=2
for each character aєb
K 4←
Pk Pqa=P4 P2a=(abab abb)⊐ ⊐ ⊐
FALSE
K=3
Pk Pqa=P3 P2a=(aba abb)⊐ ⊐ ⊐
FALSE
k←2
7. Pk Pqa=P2 P2a =(ab abb)⊐ ⊐ ⊐
FALSE
K=1
7.Pk Pqa=P1 P2a⊐ ⊐
(a abb) FALSE⊐
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 a
3 b
4 a
5 c
6 a
7
K=0
7.Pk Pqa=P0 P2a⊐ ⊐
(є abb)TRUE⊐
8.δ(q,a)←k
δ(2,b)←0
CONT…
q=2
for each character aєc
K 4←
Pk Pqa=P4 P2a=(abab abc)⊐ ⊐ ⊐
FALSE
K=3
Pk Pqa=P3 P2a=(aba abc)⊐ ⊐ ⊐
FALSE
k←2
7. Pk Pqa=P2 P2a =(ab abc)⊐ ⊐ ⊐
FALSE
K=1
7.Pk Pqa=P1 P2a⊐ ⊐
(a abc) FALSE⊐
K=0
7.Pk Pqa=P0 P2a⊐ ⊐
(є abc)TRUE⊐
8.δ(q,a)←k
δ(2,c)←0
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 b
4 a
5 c
6 a
7
4TH
ITERATION: K 1←
Pk Pqa=P1 P3a=(a abaa)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(3,a)←1
2.for q←3 to 7
3.for each character aє(a,b,c)
q=3,a=a
4.k←min(m+1,q+2)=min(8,4)=4
K←5
Pk Pqa=P5 P3a=(ababa abaa)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P3a=(abab abaa)⊐ ⊐ ⊐
FALSE
K←3
Pk Pqa=P3 P3a=(aba abaa)⊐ ⊐ ⊐
FALSE
k←2
Pk Pqa=P2 P3a=(ab abaa)⊐ ⊐ ⊐
FALSE
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 b
4 a
5 c
6 a
7
CONT…
2.for q←3
3.for each character aєb
K←5
Pk
Pqa=P5 P3a=(ababa abab)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P3a=(abab abab)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(3,b)←4
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 b
4 a
5 c
6 a
7
CONT…
2.q=3
3.for each character aєc
K←5
Pk Pqa=P5 P3a=(ababa abac)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P3a=(abab abac)⊐ ⊐ ⊐
FALSE
K←3
Pk Pqa=P3 P3a=(aba abac)⊐ ⊐ ⊐
FALSE
k←2
Pk Pqa=P2 P3a=(ab abac)⊐ ⊐ ⊐
FALSE
K←1
Pk Pqa=P1 P3a=(a abac)⊐ ⊐ ⊐
FALSE
K←0
Pk Pqa=P0 P3a=(⊐ ⊐ є abac)⊐
TRUE
8.δ(q,a)←k
δ(3,c)←0
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 a
5 c
6 a
7
5th
ITERATION:
2.for q←4to 7
3.for each character aє(a,b,c)
q=4,a=a
4.k←min(m+1,q+2)=min(8,6)=6
K←6
Pk Pqa=P6 P4a=(ababac ababa)⊐ ⊐ ⊐
FALSE
K←5
Pk Pqa=P5 P4a=(ababa abaBA)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(4,a)←5
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 a
5 c
6 a
7
CONT… k←1
Pk Pqa=P2 P4a=(a ababb)⊐ ⊐ ⊐
FALSE
k←0
Pk Pqa=P2 P4a=(є ababb)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(4,b)←0
2.q=4
3.for each character aєb
K←6
Pk Pqa=P6 P4a=(ababac ababb)⊐ ⊐ ⊐
FALSE
K←5
Pk Pqa=P5 P4a=(ababa ababb)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P4a=(abab ababb)⊐ ⊐ ⊐
FALSE
K←3
Pk Pqa=P3 P4a=(aba ababb)⊐ ⊐ ⊐
FALSE
k←2
Pk Pqa=P2 P4a=(ab ababb)⊐ ⊐ ⊐
FALSE
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 a
5 c
6 a
7
CONT…
2.q=4
3.for each character aєc
K←6
Pk Pqa=P6 P4a=(ababac ababc)⊐ ⊐ ⊐
FALSE
K←5
Pk Pqa=P5 P4a=(ababa ababc)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P4a=(abab ababc)⊐ ⊐ ⊐
FALSE
K←3
Pk Pqa=P3 P4a=(aba ababc)⊐ ⊐ ⊐
FALSE
k←2
Pk Pqa=P2 P4a=(ab ababc)⊐ ⊐ ⊐
FALSE
k←1
Pk Pqa=P2 P4a=(a ababbc⊐ ⊐ ⊐
FALSE
k←0
Pk Pqa=P2 P4a=(є ababc)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(4,c)←0
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 c
6 a
7
6th
ITERATION:
k←2
Pk Pqa=P2 P5a=(ab ababaa)⊐ ⊐ ⊐
FALSE
k←1
Pk Pqa=P2 P4a=(a ababbaa⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(5,a)←1
2.for q←5to 7
3.for each character aє(a,b,c)
q=5,a=a
4.k←min(m+1,q+2)=min(8,7)=7
K←7
Pk Pqa=P7 P5a=(ababaca ababaa)⊐ ⊐ ⊐
FALSE
K←6
Pk Pqa=P6 P5a=(ababac ababaa)⊐ ⊐ ⊐
FALSE
K←5
Pk Pqa=P5 P5a=(ababa ababaa) FALSE⊐ ⊐ ⊐
k←4
Pk Pqa=P4 P5a=(abab ababaa)⊐ ⊐ ⊐
FALSE
K←3
Pk Pqa=P3 P5a=(aba ababaa) FALSE⊐ ⊐ ⊐
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 c
6 a
7
CONT…
4.q←5
3.for each character aєb
K←7
Pk Pqa=P7 P5a=(ababaca ababab)⊐ ⊐ ⊐
FALSE
K←6
Pk Pqa=P6 P5a=(ababac ababab)⊐ ⊐ ⊐
FALSE
K←5
Pk Pqa=P5 P5a=(ababa ababab)⊐ ⊐ ⊐
FALSE
k←4
Pk Pqa=P4 P5a=(abab ababab)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(5,b)←4
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 c
6 a
7
CONT…
4.q←5
3.for each character aєc
K←7
Pk Pqa=P7 P5a=(ababaca ababac)⊐ ⊐ ⊐
FALSE
K←6
Pk Pqa=P6 P5a=(ababac ababac)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(5,c)←6
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 a
7
7TH
ITERATION:
2.for q←6to 7
3.for each character aє(a,b,c)
q=6,a=a
4.k←min(m+1,q+2)=min(8,8)=8
K←8
Pk Pqa=P8 P6a=(ababacaa ababaca)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P6a=(ababaca ababaca)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(6,a)←7
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 a
7
CONT… K←1
Pk Pqa=P1 P6a=(a ababacb) FALSE⊐ ⊐ ⊐
K←0
Pk Pqa=P0 P6a=(є ababacb) TRUE⊐ ⊐ ⊐
8.δ(q,a)←k
δ(6,b)←0
2.q←6
3.for each character aєb
K←8
Pk Pqa=P8 P6a=(ababacaa ababacb)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P6a=(ababaca ababacb)⊐ ⊐ ⊐
FALSE
K←6
Pk Pqa=P6 P6a=(ababac ababacb) FALSE⊐ ⊐ ⊐
K←5
Pk Pqa=P5 P6a=(ababa ababacb) FALSE⊐ ⊐ ⊐
K←4
Pk Pqa=P4 P6a=(abab ababacb) FALSE⊐ ⊐ ⊐
K←3
Pk Pqa=P3 P6a=(aba ababacb) FALSE⊐ ⊐ ⊐
K←2
Pk Pqa=P2 P6a=(ab ababacb) FALSE⊐ ⊐ ⊐
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 a
7
CONT…
2.q←6
3.for each character aєc
K←8
Pk Pqa=P8 P6a=(ababacaa ababac)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P6a=(ababaca ababac) FALSE⊐ ⊐ ⊐
K←6
Pk Pqa=P6 P6a=(ababac ababac) FALSE⊐ ⊐ ⊐
K←5
Pk Pqa=P5 P6a=(ababa ababac) FALSE⊐ ⊐ ⊐
K←4
Pk Pqa=P4 P6a=(abab ababac) FALSE⊐ ⊐ ⊐
K←3
Pk Pqa=P3 P6a=(aba ababac) FALSE⊐ ⊐ ⊐
K←2
Pk Pqa=P2 P6a=(ab ababac) FALSE⊐ ⊐ ⊐
K←1
Pk Pqa=P1 P6a=(a ababac) FALSE⊐ ⊐ ⊐
K←0
Pk Pqa=P0 P6a=(є ababac) TRUE⊐ ⊐ ⊐
8.δ(q,a)←k
δ(6,c)←0
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7
8TH
ITERATION:2.q←7 to 7
3.for each character aє(a,b,c)
q=7,a=a
4.k←min(m+1,q+2)=min(8,9)=8
K←8
Pk Pqa=P8 P7a=(ababacaa ababacaa)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P7a=(ababaca ababacaa) FALSE⊐ ⊐ ⊐
K←6
Pk Pqa=P6 P7a=(ababac ababacaa) FALSE⊐ ⊐ ⊐
K←5
Pk Pqa=P5 P7a=(ababa ababacaa) FALSE⊐ ⊐ ⊐
K←4
Pk Pqa=P4 P7a=(abab ababacaa) FALSE⊐ ⊐ ⊐
K←3
Pk Pqa=P3 P7a=(aba ababacaa) FALSE⊐ ⊐ ⊐
K←2
Pk Pqa=P2 P7a=(ab ababacaa) FALSE⊐ ⊐ ⊐
K←1
Pk Pqa=P1 P7a=(a ababacaa) TRUE⊐ ⊐ ⊐
8.δ(q,a)←k
δ(7,a)←1
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1
CONT… 8.δ(q,a)←k
δ(7,b)←2
2.q←7
3.for each character aєb
K←8
Pk Pqa=P8 P7a=(ababacaa ababacab)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P7a=(ababaca ababacab) FALSE⊐ ⊐ ⊐
K←6
Pk Pqa=P6 P7a=(ababac ababacab) FALSE⊐ ⊐ ⊐
K←5
Pk Pqa=P5 P7a=(ababa ababacab) FALSE⊐ ⊐ ⊐
K←4
Pk Pqa=P4 P7a=(abab ababacab) FALSE⊐ ⊐ ⊐
K←3
Pk Pqa=P3 P7a=(aba ababacab) FALSE⊐ ⊐ ⊐
K←2
Pk Pqa=P2 P7a=(ab ababacab) TRUE⊐ ⊐ ⊐
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2
CONT…
K←1
Pk Pqa=P1 P7a=(a ababacac)⊐ ⊐ ⊐
FALSE
K←0
Pk Pqa=P0 P7a=(є ababacac)⊐ ⊐ ⊐
TRUE
8.δ(q,a)←k
δ(7,c)←0
2.q←7
3.for each character aєc
K←8
Pk Pqa=P8 P7a=(ababacaa ababacac)⊐ ⊐ ⊐
FALSE
K←7
Pk Pqa=P7 P7a=(ababaca ababacac)⊐ ⊐ ⊐
FALSE
K←6
Pk Pqa=P6 P7a=(ababac ababacac) FALSE⊐ ⊐ ⊐
K←5
Pk Pqa=P5 P7a=(ababa ababacac) FALSE⊐ ⊐ ⊐
K←4
Pk Pqa=P4 P7a=(abab ababacac) FALSE⊐ ⊐ ⊐
K←3
Pk Pqa=P3 P7a=(aba ababacac) FALSE⊐ ⊐ ⊐
K←2
Pk Pqa=P2 P7a=(ab ababacac) FALSE⊐ ⊐ ⊐
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2 0
REQUIRED
RESULT:
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2 0
THE FINITE STATE
AUTOMATA IS:
0 1 2 3 4 5 6 7
a b a b a c a
a
a
b
b
a
a
ALGORITHM…
FINITE AUTOMATON
MATCHER
FINITE-AUTOMATON-MATCHER(T,δ,m)
1.n=T . length
2.q=0
3.for I =1 to n
4. q= δ( q, T[ i ] )
5. If q==m
6. Print “Pattern occurs with shift” i -m
DRY RUNNING…
FINITE-AUTOMATON
MATCHER
i= 1 2 3 4 5 6 7 8 9 10 11
T= a b a b a b a c a b a
1.n← length[T]
n←11
2. q←0
3. for i←1 to n
for i←1 to 11
1st
ITERATION:
3. for i←1 to 11
4. do q←δ(q,T[i])
q←δ(0,T[1])=δ(0,a)
q←(0,a)=1
5. if q=m
if 1=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
2ND
ITERATION:
i=2,q=2
4. do q←δ(1,T[2])
=δ(1,b)
q←2
5 if 2=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
3RD
ITERATION:
i=3,q=2
4. do q←δ(2,T[3])
=δ(2,a)
q←3
5 if 3=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
4th
ITERATION:
i=4,q=3
4. do q←δ(3,T[4]) =δ(3,b)
q←4
5 if 4=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
5th
ITERATION:
i=5,q=4
4. do q←δ(4,T[5])
=δ(4,a)=5
q←5
5 if 5=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
6TH
ITERATION:
i=6,q=5
4. do q←δ(5,T[6])
=δ(5,b)=4
q←5
5 if 4=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
7TH
ITERATION:
i=7,q=4
4. do q←δ(4,T[7])
=δ(4,a)=5
q←5
5 if 5=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
8TH
ITERATION:
i=8,q=5
4. do q←δ(5,T[8])
=δ(5,c)=6
q←6
5 if 6=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
9TH
ITERATION:
i=9,q=6
4. do q←δ(6,T[9])
=δ(6,a)=7
q←7
5 if 7=7 TRUE
Then print “pattern occurs with
shift”i-m
Print” pattern occurs with
shift”9-7=2
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
10th
ITEARTION:
i=10,q=7
4. do q←δ(7,T[10])
=δ(7,b)=2
q←2
5 if 2=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
11TH
ITEARTION:
i=11,q=2
4. do
q←δ(2,T[11])=δ(2,a)=3
q←3
5 if 3=7 FALSE
0 1 2 3 4 5 6 7
a b a b a c a
a
b
a
b
a
a
REQUIRED OUTPUT:
After removing useless edges:
0 1 2 3 4 5 6 7
a
a
a a ab b c
b
b
CONT..
State a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2 0
i= - 1 2 3 4 5 6 7 8 9 10
11
T[i] - a b a b a b a c a b a
state ΦT[i] 0 1 2 3 4 5 4 5 6 77 2 3
COMPLEXITY…
COMPLEXITY
ANALYSIS:
• The simple loop time of FINITE-AUTOMATON-MATCHER implies that
its matching time on the text string of length is Θ(n).This matching
time does not include the preprocessing time required to compute
the transition function δ)
• The running time of COMPUTE-TRANSITION-FUNCTION is
O(m^3 ∑ ),because the outer loops contribute a factor of│ │
m ∑ ,the inner repeat loop can run atmost m+1 times, and the test│ │
Pk Pqa on line 7 can require computing upto m characters.⊐
• Much faster procedures exist; the time required to compute δ from
P can be improved to O(m ∑ ) by utilizing some cleverly computed│ │
information about the pattern P. With this improved procedure for
computing δ from P to O(m| |), we can find all occurrences of aƩ
length-m pattern in a length-n text over an alphabet ∑ with
O(m ∑ ) preprocessing time and Θ(n) matching time.│ │
ADVANTAGES &
DISADVANTAGE…
ADVANTAGES &
DISADVANTAGE:
• String matching automaton are very efficient
• Examine each text character exactly once taking
constant time per text character.
• The time to build the automaton can be large if Ʃ
is large
THANK YOU

More Related Content

What's hot (20)

PPTX
N queen problem
Ridhima Chowdhury
 
PPTX
Topological Sorting
ShahDhruv21
 
PPTX
Peephole Optimization
United International University
 
PPTX
Data structure - Graph
Madhu Bala
 
PPTX
Bellman ford algorithm
MdSajjadulislamBappi
 
PDF
NFA to DFA
Animesh Chaturvedi
 
PPTX
Matrix chain multiplication
Respa Peter
 
PDF
Lecture: Context-Free Grammars
Marina Santini
 
PPTX
NP completeness
Amrinder Arora
 
PPT
Bottom - Up Parsing
kunj desai
 
PPTX
Shortest path algorithm
sana younas
 
PPTX
Data Structures : hashing (1)
Home
 
PPTX
linked list in data structure
shameen khan
 
PDF
Matrix chain multiplication
Kiran K
 
PDF
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
PDF
module6_stringmatchingalgorithm_2022.pdf
Shiwani Gupta
 
PDF
Kmp
akruthi k
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPTX
Bellman ford algorithm
AnuragChaudhary70
 
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
N queen problem
Ridhima Chowdhury
 
Topological Sorting
ShahDhruv21
 
Peephole Optimization
United International University
 
Data structure - Graph
Madhu Bala
 
Bellman ford algorithm
MdSajjadulislamBappi
 
NFA to DFA
Animesh Chaturvedi
 
Matrix chain multiplication
Respa Peter
 
Lecture: Context-Free Grammars
Marina Santini
 
NP completeness
Amrinder Arora
 
Bottom - Up Parsing
kunj desai
 
Shortest path algorithm
sana younas
 
Data Structures : hashing (1)
Home
 
linked list in data structure
shameen khan
 
Matrix chain multiplication
Kiran K
 
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
module6_stringmatchingalgorithm_2022.pdf
Shiwani Gupta
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
Bellman ford algorithm
AnuragChaudhary70
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 

Viewers also liked (12)

PPTX
String Matching Finite Automata & KMP Algorithm.
Malek Sumaiya
 
PPT
String Matching with Finite Automata,Aho corasick,
8neutron8
 
PPT
0227 regularlanguages
issbp
 
PDF
Context free langauges
sudhir sharma
 
PPT
Simplifies and normal forms - Theory of Computation
Nikhil Pandit
 
PPT
Context free languages
Jahurul Islam
 
PPT
Context free grammars
Ronak Thakkar
 
PPT
Deterministic Finite Automata
Mohammad jawad khan
 
PPTX
Theory of Automata and formal languages unit 2
Abhimanyu Mishra
 
PPT
Introduction to fa and dfa
deepinderbedi
 
PPT
Turing machines
lavishka_anuj
 
PDF
Lecture: Regular Expressions and Regular Languages
Marina Santini
 
String Matching Finite Automata & KMP Algorithm.
Malek Sumaiya
 
String Matching with Finite Automata,Aho corasick,
8neutron8
 
0227 regularlanguages
issbp
 
Context free langauges
sudhir sharma
 
Simplifies and normal forms - Theory of Computation
Nikhil Pandit
 
Context free languages
Jahurul Islam
 
Context free grammars
Ronak Thakkar
 
Deterministic Finite Automata
Mohammad jawad khan
 
Theory of Automata and formal languages unit 2
Abhimanyu Mishra
 
Introduction to fa and dfa
deepinderbedi
 
Turing machines
lavishka_anuj
 
Lecture: Regular Expressions and Regular Languages
Marina Santini
 
Ad

Similar to String matching with finite state automata (20)

PDF
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
PDF
ゲーム理論BASIC 演習37 -3人ゲームの混合戦略ナッシュ均衡を求める-
ssusere0a682
 
PDF
ゲーム理論BASIC 演習41 -2人ゼロ和ゲームにおけるマックスミニ値-
ssusere0a682
 
PPT
Learning Algorithms For Life Scientists
Brian Frezza
 
PDF
ゲーム理論BASIC 演習42 -2人ゼロ和ゲームにおけるマックスミニ値2-
ssusere0a682
 
PDF
3人ゲームの混合戦略ナッシュ均衡を求める ゲーム理論 BASIC 演習1の補足
ssusere0a682
 
PDF
Randomized smoothing
Simossyi Funabashi
 
PDF
「ガロア表現」を使って素数の分解法則を考える #mathmoring
Junpei Tsuji
 
PDF
ゲーム理論BASIC 演習52 -完全ベイジアン均衡-
ssusere0a682
 
PPT
Cs262 2006 lecture6
BioinformaticsInstitute
 
PPTX
quick and merge.pptx
LakshayYadav46
 
PDF
ゲーム理論NEXT 戦略形協力ゲーム第11回 -寡占市場ゲームにおける結託耐性ナッシュ均衡-
ssusere0a682
 
PDF
Discrete mathematic question answers
Samet öztoprak
 
PDF
【ゲーム理論応用】 - 寡占市場分析4 -
ssusere0a682
 
PDF
Control as Inference (強化学習とベイズ統計)
Shohei Taniguchi
 
PDF
26 Computational Geometry
Andres Mendez-Vazquez
 
PDF
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #2
ssusere0a682
 
PDF
Chart parsing with features
SRah Sanei
 
PDF
Laplace1 8merged
cohtran
 
DOCX
Discrete Structures
AQCreations
 
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
ゲーム理論BASIC 演習37 -3人ゲームの混合戦略ナッシュ均衡を求める-
ssusere0a682
 
ゲーム理論BASIC 演習41 -2人ゼロ和ゲームにおけるマックスミニ値-
ssusere0a682
 
Learning Algorithms For Life Scientists
Brian Frezza
 
ゲーム理論BASIC 演習42 -2人ゼロ和ゲームにおけるマックスミニ値2-
ssusere0a682
 
3人ゲームの混合戦略ナッシュ均衡を求める ゲーム理論 BASIC 演習1の補足
ssusere0a682
 
Randomized smoothing
Simossyi Funabashi
 
「ガロア表現」を使って素数の分解法則を考える #mathmoring
Junpei Tsuji
 
ゲーム理論BASIC 演習52 -完全ベイジアン均衡-
ssusere0a682
 
Cs262 2006 lecture6
BioinformaticsInstitute
 
quick and merge.pptx
LakshayYadav46
 
ゲーム理論NEXT 戦略形協力ゲーム第11回 -寡占市場ゲームにおける結託耐性ナッシュ均衡-
ssusere0a682
 
Discrete mathematic question answers
Samet öztoprak
 
【ゲーム理論応用】 - 寡占市場分析4 -
ssusere0a682
 
Control as Inference (強化学習とベイズ統計)
Shohei Taniguchi
 
26 Computational Geometry
Andres Mendez-Vazquez
 
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #2
ssusere0a682
 
Chart parsing with features
SRah Sanei
 
Laplace1 8merged
cohtran
 
Discrete Structures
AQCreations
 
Ad

Recently uploaded (20)

PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
epi editorial commitee meeting presentation
MIPLM
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Introduction presentation of the patentbutler tool
MIPLM
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Difference between write and update in odoo 18
Celine George
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 

String matching with finite state automata

  • 1. STRING MATCHING WITH FINITE AUTOMATA SUBMITTED TO : MAM MAIMOONA SUBMITTED BY : IQRA MUNIR ANMOL HAMID ANALYSIS OF ALGORITHM
  • 3. STRING MATCHING WITH FINITE AUTOMATA: • String matching algorithm builds a finite automaton • A simple machine for processing information that scans the text string T for all occurrences of the pattern P
  • 7. STRING MATCHING AUTOMATA (SUFFIX FUNCTION):• In order to specify the string-matching automaton corresponding to a given pattern P[1…m] • An auxiliary function σ, called the suffix function corresponding to P • The function σ maps * to {0,1,…..,m} such thatƩ σ(x) is the length of the longest prefix of P that is also a suffix of x: • σ(x) = max{k: Pk ⊐x}
  • 9. COMPUTE TRANSITION FUNCTION: COMPUTE-TRANSITION-FUNCTION(P, )Ʃ 1.m-P.length 2.for q=0 to m 3. for each character a є Ʃ 4. k= min(m+1,q+2) 5. repeat 6. K=k-1 7. until P k P q a⊐ 8. δ(q ,a )=k 9.return δ
  • 11. COMPUTE TRANSITION FUNCTION: • TEXT=abababacaba • PATTERN=ababaca • ={a,b,c}Ʃ Compute transition function(P, )Ʃ 1 m← length[P] m← 7 2 for q←0 to m for q←0 to 7 3 do for each character aєƩ // ={a,b,c}Ʃ
  • 12. 1st ITERATION: q=0, a=a 3. for each character aєa 4. k←min(m+1,q+2) = min(7+1,0+2)=2 K←2 7. Pk Pqa= P2 P0a⊐ ⊐ (ab a) FALSE⊐ K←1 7.Pk Pqa= P1 P0a⊐ ⊐ (a a) TRUE⊐ 8. δ(q,a)←k δ(0,a)←1 state a b c P 0 1 a 1 b 2 a 3 b 4 a 5 c 6 a 7
  • 13. CONT… q=0 3. for each character aєb 4. K=2 7. Pk Pqa=P2 P0a⊐ ⊐ (ab b) FALSE⊐ K=1 7.Pk Pqa=P1 P0a⊐ ⊐ (a b) FALSE⊐ K=0 Pk Pqa=P0 P0b⊐ ⊐ (є b) TRUE⊐ 8.δ(q,a)←k δ(0,b)←0 state a b c P 0 1 0 a 1 b 2 a 3 b 4 a 5 c 6 a 7
  • 14. CONT… q=0 3. for each character aєc 4. K=2 7. Pk Pqa=P2 P0a⊐ ⊐ (ab c) FALSE⊐ K=1 7.Pk Pqa=P1 P0a⊐ ⊐ (a c) FALSE⊐ K=0 Pk Pqa=P0 P0s⊐ ⊐ (є c) TRUE⊐ 8.δ(q,a)←k δ(0,c)←0 state a b c P 0 1 0 0 a 1 b 2 a 3 b 4 a 5 c 6 a 7
  • 15. 2nd ITERATION: 2.for q←1 to 7 3.for each character aє(a,b,c) q=1,a=a 4.k←min(m+1,q+2)=min(7+1,1 +2)=3 K←3 Pk Pqa=P3 P1a=(aba aa)⊐ ⊐ ⊐ FALSE k←2 7. Pk Pqa=P2 P1a⊐ ⊐ (ab aa) FALSE⊐ K=1 7.Pk Pqa=P1 P1a⊐ ⊐ (a aa) TRUE⊐ 8.δ(q,a)←k δ(1,a)←1 State a b c P 0 1 0 0 a 1 1 b 2 a 3 b 4 a 5 c 6 a 7
  • 16. CONT… q=1 for each character aєb 4.K←3 Pk Pqa=P3 P1a=(aba ab)⊐ ⊐ ⊐ FALSE k←2 7. Pk Pqa=P2 P1a⊐ ⊐ (ab ab) TRUE⊐ 8.δ(1,b) 2← State a b c P 0 1 0 0 a 1 1 2 b 2 a 3 b 4 a 5 c 6 a 7
  • 17. CONT… q=1 for each character aєc 4.K←3 Pk Pqa=P3 P1a=(aba ac)⊐ ⊐ ⊐ FALSE k←2 7. Pk Pqa=P2 P1a⊐ ⊐ (ab ac) FALSE⊐ k 1← Pk Pqa=P1 P1a⊐ ⊐ (a ac) FALSE⊐ k 0← Pk Pqa=P0 P1a⊐ ⊐ (є ac) TRUE⊐ 8.δ(1,c)←0 State a b c P 0 1 0 0 a 1 1 2 0 b 2 a 3 b 4 a 5 c 6 a 7
  • 18. 3rd ITERATION: 2.for q←2 to 7 3.for each character aє(a,b,c) q=2,a=a 4.k←min(m+1,q+2)=min(8,4)=4 K←4 Pk Pqa=P4 P2a=(abab aba)⊐ ⊐ ⊐ FALSE Pk Pqa=P3 P2a=(aba aba)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(2,a)←3 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 a 3 b 4 a 5 c 6 a 7
  • 19. CONT… q=2 for each character aєb K 4← Pk Pqa=P4 P2a=(abab abb)⊐ ⊐ ⊐ FALSE K=3 Pk Pqa=P3 P2a=(aba abb)⊐ ⊐ ⊐ FALSE k←2 7. Pk Pqa=P2 P2a =(ab abb)⊐ ⊐ ⊐ FALSE K=1 7.Pk Pqa=P1 P2a⊐ ⊐ (a abb) FALSE⊐ State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 a 3 b 4 a 5 c 6 a 7 K=0 7.Pk Pqa=P0 P2a⊐ ⊐ (є abb)TRUE⊐ 8.δ(q,a)←k δ(2,b)←0
  • 20. CONT… q=2 for each character aєc K 4← Pk Pqa=P4 P2a=(abab abc)⊐ ⊐ ⊐ FALSE K=3 Pk Pqa=P3 P2a=(aba abc)⊐ ⊐ ⊐ FALSE k←2 7. Pk Pqa=P2 P2a =(ab abc)⊐ ⊐ ⊐ FALSE K=1 7.Pk Pqa=P1 P2a⊐ ⊐ (a abc) FALSE⊐ K=0 7.Pk Pqa=P0 P2a⊐ ⊐ (є abc)TRUE⊐ 8.δ(q,a)←k δ(2,c)←0 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 b 4 a 5 c 6 a 7
  • 21. 4TH ITERATION: K 1← Pk Pqa=P1 P3a=(a abaa)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(3,a)←1 2.for q←3 to 7 3.for each character aє(a,b,c) q=3,a=a 4.k←min(m+1,q+2)=min(8,4)=4 K←5 Pk Pqa=P5 P3a=(ababa abaa)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P3a=(abab abaa)⊐ ⊐ ⊐ FALSE K←3 Pk Pqa=P3 P3a=(aba abaa)⊐ ⊐ ⊐ FALSE k←2 Pk Pqa=P2 P3a=(ab abaa)⊐ ⊐ ⊐ FALSE State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 b 4 a 5 c 6 a 7
  • 22. CONT… 2.for q←3 3.for each character aєb K←5 Pk Pqa=P5 P3a=(ababa abab)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P3a=(abab abab)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(3,b)←4 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 b 4 a 5 c 6 a 7
  • 23. CONT… 2.q=3 3.for each character aєc K←5 Pk Pqa=P5 P3a=(ababa abac)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P3a=(abab abac)⊐ ⊐ ⊐ FALSE K←3 Pk Pqa=P3 P3a=(aba abac)⊐ ⊐ ⊐ FALSE k←2 Pk Pqa=P2 P3a=(ab abac)⊐ ⊐ ⊐ FALSE K←1 Pk Pqa=P1 P3a=(a abac)⊐ ⊐ ⊐ FALSE K←0 Pk Pqa=P0 P3a=(⊐ ⊐ є abac)⊐ TRUE 8.δ(q,a)←k δ(3,c)←0 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 a 5 c 6 a 7
  • 24. 5th ITERATION: 2.for q←4to 7 3.for each character aє(a,b,c) q=4,a=a 4.k←min(m+1,q+2)=min(8,6)=6 K←6 Pk Pqa=P6 P4a=(ababac ababa)⊐ ⊐ ⊐ FALSE K←5 Pk Pqa=P5 P4a=(ababa abaBA)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(4,a)←5 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 a 5 c 6 a 7
  • 25. CONT… k←1 Pk Pqa=P2 P4a=(a ababb)⊐ ⊐ ⊐ FALSE k←0 Pk Pqa=P2 P4a=(є ababb)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(4,b)←0 2.q=4 3.for each character aєb K←6 Pk Pqa=P6 P4a=(ababac ababb)⊐ ⊐ ⊐ FALSE K←5 Pk Pqa=P5 P4a=(ababa ababb)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P4a=(abab ababb)⊐ ⊐ ⊐ FALSE K←3 Pk Pqa=P3 P4a=(aba ababb)⊐ ⊐ ⊐ FALSE k←2 Pk Pqa=P2 P4a=(ab ababb)⊐ ⊐ ⊐ FALSE State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 a 5 c 6 a 7
  • 26. CONT… 2.q=4 3.for each character aєc K←6 Pk Pqa=P6 P4a=(ababac ababc)⊐ ⊐ ⊐ FALSE K←5 Pk Pqa=P5 P4a=(ababa ababc)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P4a=(abab ababc)⊐ ⊐ ⊐ FALSE K←3 Pk Pqa=P3 P4a=(aba ababc)⊐ ⊐ ⊐ FALSE k←2 Pk Pqa=P2 P4a=(ab ababc)⊐ ⊐ ⊐ FALSE k←1 Pk Pqa=P2 P4a=(a ababbc⊐ ⊐ ⊐ FALSE k←0 Pk Pqa=P2 P4a=(є ababc)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(4,c)←0 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 c 6 a 7
  • 27. 6th ITERATION: k←2 Pk Pqa=P2 P5a=(ab ababaa)⊐ ⊐ ⊐ FALSE k←1 Pk Pqa=P2 P4a=(a ababbaa⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(5,a)←1 2.for q←5to 7 3.for each character aє(a,b,c) q=5,a=a 4.k←min(m+1,q+2)=min(8,7)=7 K←7 Pk Pqa=P7 P5a=(ababaca ababaa)⊐ ⊐ ⊐ FALSE K←6 Pk Pqa=P6 P5a=(ababac ababaa)⊐ ⊐ ⊐ FALSE K←5 Pk Pqa=P5 P5a=(ababa ababaa) FALSE⊐ ⊐ ⊐ k←4 Pk Pqa=P4 P5a=(abab ababaa)⊐ ⊐ ⊐ FALSE K←3 Pk Pqa=P3 P5a=(aba ababaa) FALSE⊐ ⊐ ⊐ State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 c 6 a 7
  • 28. CONT… 4.q←5 3.for each character aєb K←7 Pk Pqa=P7 P5a=(ababaca ababab)⊐ ⊐ ⊐ FALSE K←6 Pk Pqa=P6 P5a=(ababac ababab)⊐ ⊐ ⊐ FALSE K←5 Pk Pqa=P5 P5a=(ababa ababab)⊐ ⊐ ⊐ FALSE k←4 Pk Pqa=P4 P5a=(abab ababab)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(5,b)←4 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 c 6 a 7
  • 29. CONT… 4.q←5 3.for each character aєc K←7 Pk Pqa=P7 P5a=(ababaca ababac)⊐ ⊐ ⊐ FALSE K←6 Pk Pqa=P6 P5a=(ababac ababac)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(5,c)←6 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 a 7
  • 30. 7TH ITERATION: 2.for q←6to 7 3.for each character aє(a,b,c) q=6,a=a 4.k←min(m+1,q+2)=min(8,8)=8 K←8 Pk Pqa=P8 P6a=(ababacaa ababaca)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P6a=(ababaca ababaca)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(6,a)←7 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 a 7
  • 31. CONT… K←1 Pk Pqa=P1 P6a=(a ababacb) FALSE⊐ ⊐ ⊐ K←0 Pk Pqa=P0 P6a=(є ababacb) TRUE⊐ ⊐ ⊐ 8.δ(q,a)←k δ(6,b)←0 2.q←6 3.for each character aєb K←8 Pk Pqa=P8 P6a=(ababacaa ababacb)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P6a=(ababaca ababacb)⊐ ⊐ ⊐ FALSE K←6 Pk Pqa=P6 P6a=(ababac ababacb) FALSE⊐ ⊐ ⊐ K←5 Pk Pqa=P5 P6a=(ababa ababacb) FALSE⊐ ⊐ ⊐ K←4 Pk Pqa=P4 P6a=(abab ababacb) FALSE⊐ ⊐ ⊐ K←3 Pk Pqa=P3 P6a=(aba ababacb) FALSE⊐ ⊐ ⊐ K←2 Pk Pqa=P2 P6a=(ab ababacb) FALSE⊐ ⊐ ⊐ State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 a 7
  • 32. CONT… 2.q←6 3.for each character aєc K←8 Pk Pqa=P8 P6a=(ababacaa ababac)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P6a=(ababaca ababac) FALSE⊐ ⊐ ⊐ K←6 Pk Pqa=P6 P6a=(ababac ababac) FALSE⊐ ⊐ ⊐ K←5 Pk Pqa=P5 P6a=(ababa ababac) FALSE⊐ ⊐ ⊐ K←4 Pk Pqa=P4 P6a=(abab ababac) FALSE⊐ ⊐ ⊐ K←3 Pk Pqa=P3 P6a=(aba ababac) FALSE⊐ ⊐ ⊐ K←2 Pk Pqa=P2 P6a=(ab ababac) FALSE⊐ ⊐ ⊐ K←1 Pk Pqa=P1 P6a=(a ababac) FALSE⊐ ⊐ ⊐ K←0 Pk Pqa=P0 P6a=(є ababac) TRUE⊐ ⊐ ⊐ 8.δ(q,a)←k δ(6,c)←0 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7
  • 33. 8TH ITERATION:2.q←7 to 7 3.for each character aє(a,b,c) q=7,a=a 4.k←min(m+1,q+2)=min(8,9)=8 K←8 Pk Pqa=P8 P7a=(ababacaa ababacaa)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P7a=(ababaca ababacaa) FALSE⊐ ⊐ ⊐ K←6 Pk Pqa=P6 P7a=(ababac ababacaa) FALSE⊐ ⊐ ⊐ K←5 Pk Pqa=P5 P7a=(ababa ababacaa) FALSE⊐ ⊐ ⊐ K←4 Pk Pqa=P4 P7a=(abab ababacaa) FALSE⊐ ⊐ ⊐ K←3 Pk Pqa=P3 P7a=(aba ababacaa) FALSE⊐ ⊐ ⊐ K←2 Pk Pqa=P2 P7a=(ab ababacaa) FALSE⊐ ⊐ ⊐ K←1 Pk Pqa=P1 P7a=(a ababacaa) TRUE⊐ ⊐ ⊐ 8.δ(q,a)←k δ(7,a)←1 State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1
  • 34. CONT… 8.δ(q,a)←k δ(7,b)←2 2.q←7 3.for each character aєb K←8 Pk Pqa=P8 P7a=(ababacaa ababacab)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P7a=(ababaca ababacab) FALSE⊐ ⊐ ⊐ K←6 Pk Pqa=P6 P7a=(ababac ababacab) FALSE⊐ ⊐ ⊐ K←5 Pk Pqa=P5 P7a=(ababa ababacab) FALSE⊐ ⊐ ⊐ K←4 Pk Pqa=P4 P7a=(abab ababacab) FALSE⊐ ⊐ ⊐ K←3 Pk Pqa=P3 P7a=(aba ababacab) FALSE⊐ ⊐ ⊐ K←2 Pk Pqa=P2 P7a=(ab ababacab) TRUE⊐ ⊐ ⊐ State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1 2
  • 35. CONT… K←1 Pk Pqa=P1 P7a=(a ababacac)⊐ ⊐ ⊐ FALSE K←0 Pk Pqa=P0 P7a=(є ababacac)⊐ ⊐ ⊐ TRUE 8.δ(q,a)←k δ(7,c)←0 2.q←7 3.for each character aєc K←8 Pk Pqa=P8 P7a=(ababacaa ababacac)⊐ ⊐ ⊐ FALSE K←7 Pk Pqa=P7 P7a=(ababaca ababacac)⊐ ⊐ ⊐ FALSE K←6 Pk Pqa=P6 P7a=(ababac ababacac) FALSE⊐ ⊐ ⊐ K←5 Pk Pqa=P5 P7a=(ababa ababacac) FALSE⊐ ⊐ ⊐ K←4 Pk Pqa=P4 P7a=(abab ababacac) FALSE⊐ ⊐ ⊐ K←3 Pk Pqa=P3 P7a=(aba ababacac) FALSE⊐ ⊐ ⊐ K←2 Pk Pqa=P2 P7a=(ab ababacac) FALSE⊐ ⊐ ⊐ State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1 2 0
  • 36. REQUIRED RESULT: State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1 2 0 THE FINITE STATE AUTOMATA IS: 0 1 2 3 4 5 6 7 a b a b a c a a a b b a a
  • 38. FINITE AUTOMATON MATCHER FINITE-AUTOMATON-MATCHER(T,δ,m) 1.n=T . length 2.q=0 3.for I =1 to n 4. q= δ( q, T[ i ] ) 5. If q==m 6. Print “Pattern occurs with shift” i -m
  • 40. FINITE-AUTOMATON MATCHER i= 1 2 3 4 5 6 7 8 9 10 11 T= a b a b a b a c a b a 1.n← length[T] n←11 2. q←0 3. for i←1 to n for i←1 to 11
  • 41. 1st ITERATION: 3. for i←1 to 11 4. do q←δ(q,T[i]) q←δ(0,T[1])=δ(0,a) q←(0,a)=1 5. if q=m if 1=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 42. 2ND ITERATION: i=2,q=2 4. do q←δ(1,T[2]) =δ(1,b) q←2 5 if 2=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 43. 3RD ITERATION: i=3,q=2 4. do q←δ(2,T[3]) =δ(2,a) q←3 5 if 3=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 44. 4th ITERATION: i=4,q=3 4. do q←δ(3,T[4]) =δ(3,b) q←4 5 if 4=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 45. 5th ITERATION: i=5,q=4 4. do q←δ(4,T[5]) =δ(4,a)=5 q←5 5 if 5=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 46. 6TH ITERATION: i=6,q=5 4. do q←δ(5,T[6]) =δ(5,b)=4 q←5 5 if 4=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 47. 7TH ITERATION: i=7,q=4 4. do q←δ(4,T[7]) =δ(4,a)=5 q←5 5 if 5=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 48. 8TH ITERATION: i=8,q=5 4. do q←δ(5,T[8]) =δ(5,c)=6 q←6 5 if 6=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 49. 9TH ITERATION: i=9,q=6 4. do q←δ(6,T[9]) =δ(6,a)=7 q←7 5 if 7=7 TRUE Then print “pattern occurs with shift”i-m Print” pattern occurs with shift”9-7=2 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 50. 10th ITEARTION: i=10,q=7 4. do q←δ(7,T[10]) =δ(7,b)=2 q←2 5 if 2=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 51. 11TH ITEARTION: i=11,q=2 4. do q←δ(2,T[11])=δ(2,a)=3 q←3 5 if 3=7 FALSE 0 1 2 3 4 5 6 7 a b a b a c a a b a b a a
  • 52. REQUIRED OUTPUT: After removing useless edges: 0 1 2 3 4 5 6 7 a a a a ab b c b b
  • 53. CONT.. State a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1 2 0 i= - 1 2 3 4 5 6 7 8 9 10 11 T[i] - a b a b a b a c a b a state ΦT[i] 0 1 2 3 4 5 4 5 6 77 2 3
  • 55. COMPLEXITY ANALYSIS: • The simple loop time of FINITE-AUTOMATON-MATCHER implies that its matching time on the text string of length is Θ(n).This matching time does not include the preprocessing time required to compute the transition function δ) • The running time of COMPUTE-TRANSITION-FUNCTION is O(m^3 ∑ ),because the outer loops contribute a factor of│ │ m ∑ ,the inner repeat loop can run atmost m+1 times, and the test│ │ Pk Pqa on line 7 can require computing upto m characters.⊐ • Much faster procedures exist; the time required to compute δ from P can be improved to O(m ∑ ) by utilizing some cleverly computed│ │ information about the pattern P. With this improved procedure for computing δ from P to O(m| |), we can find all occurrences of aƩ length-m pattern in a length-n text over an alphabet ∑ with O(m ∑ ) preprocessing time and Θ(n) matching time.│ │
  • 57. ADVANTAGES & DISADVANTAGE: • String matching automaton are very efficient • Examine each text character exactly once taking constant time per text character. • The time to build the automaton can be large if Ʃ is large