SlideShare a Scribd company logo
7
7
Ex-3 : Let T = acaabc & P = aab
Find the value of s.
Answer : The value of s = 2
Ex-4 : Let T = 000010001010001
P = 0001
Find the values of ‘s’.
Answer : The value of s = 1 & 5 & 11
Ex-5 : Let T = an and P = am
Answer : The values of s = 0 to n – m
i.e., s contains n – m + 1 values
Most read
12
12
RABIN-KARP-MATCHER (T,P,d,q)
1 n = T.length
2 m = P.length
3 h = dm-1 (mod q)
4 p = 0
5 t0 = 0
6 for i = 1 to m // preprocessing
7 p = (dp + P[i]) mod q
8 t0 = (d t0 + T[i]) mod q
9 for s = 0 to n-m //matching
10 if (p = = ts )
11 if (P[1..m] = T[s+1..s+m])
12 print “Pattern occurs with shift” s
13 if (s < n – m)
14 ts+1 = (d (ts – T[s+1] h ) + T[s+m+1]) mod q.
Most read
16
16
4. The Knuth-Morris-Pratt Algorithm :
This algorithm is meant for ‘Pattern Matching’.
Here, the prefix function  for a pattern
encapsulates knowledge about how the pattern
matches against shifts of itself.
Ex-8 : Let the Text String T & Pattern P is :
T : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
b a c b a b a b a c a c a c a
P : 1 2 3 4 5 6 7
a b a b a c a
Most read
String-Matching Algorithms (UNIT-5)
1
2
1. String Matching :
Let there is an array of text, T[1..n] of length ‘n’.
Let there is a pattern of text, P[1..m] of length ‘m’.
Let T and P are drawn from a finite alphabet .
Here P and T are called ‘Strings of Characters’.
Here, the pattern P occurs with shift s in text T,
if, 0 ≤ s ≤ n – m
and T[s+1..s+m] = P[1..m]
i.e., for 1 ≤ j ≤ m, T[s+j] = P[j]
If P occurs with shift s in T, it is a VALID SHIFT.
Other wise, we call INVALID SHIFT.
3
The String-matching Problem is the problem of finding
all valid shifts with which a given pattern P occurs in a
given text T.
Ex-1 : Let text T : a b c a b a a b c a b a c
Let pattern P : a b a a
Find the number of valid shifts and ‘s’ values.
Answer : Only one Valid Shift. s = 3
The symbol * (read as ‘sigma-star’) is the set of all
finite-length strings formed using characters from
the alphabet .
4
The zero-length string is called ‘Empty String’.
denoted by ‘ɛ’, also belongs to *.
The length of the string ‘x’ is denoted |x|.
The concatenation of two strings x and y, denoted xy
has length |x| + |y|.
A string ω is a prefix of a string x, denoted as ω ⊏ x,
if x = ω y for some string y ∊ *.
Here, note that if ω ⊏ x, then |w| ≤ |x|.
Similarly, a string ω is a suffix of a string x, denoted
as ω ⊐ x, if x = y ω for some string y ∊ *.
Here, note that if ω ⊐ x, then |w| ≤ |x|.
5
Ex-2 : Let abcca is a string.
Here, ab ⊏ abcca and cca ⊐ abcca
Note-1: The empty string ɛ is both a suffix and
prefix of every string.
Note-2 : Both prefix and suffix are transitive
relations.
Lemma : Suppose that x, y, and z are strings
such that x ⊐ z and y ⊐ z.
Here, if |x| ≤ |y| then x ⊐ y.
if |x| ≥ |y| then y ⊐ x.
if |x| = |y| then x = y.
6
2. The Naïve String-matching Algorithm :
This 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.
NAÏVE-STRING-MATCHER(T,P)
1 n = T.length
2 m = P.length
3. for s = 0 to n – m
4. if P[1..m] = = T[s+1..s+m]
5 Print “Pattern occurs with shift s.”
7
Ex-3 : Let T = acaabc & P = aab
Find the value of s.
Answer : The value of s = 2
Ex-4 : Let T = 000010001010001
P = 0001
Find the values of ‘s’.
Answer : The value of s = 1 & 5 & 11
Ex-5 : Let T = an and P = am
Answer : The values of s = 0 to n – m
i.e., s contains n – m + 1 values
8
3. The Rabin-Karp Algorithm :
Let  = {0, 1, 2, … , 9}
Here each character is a decimal digit.
d = |  | = 10.
The string 31415 represents 31,415 in radix-d notation.
Let there is a text T[1..n].
Let there is a pattern P[1..m].
Let p denote the corresponding decimal value.
Let ts is the decimal value of the length –m substring
T[s+1..s+m], for s = 0,1,2,..n-m.
 ts = p iff T[s+1..s+m] = P[1..m]
 s is a valid shift iff ts = p
9
Now, the value of p can be computed using
Horner’s rule as follows:
p = P[1..m] = P[1] P[2] P[3]…P[m]
So, p = P[m] + 10 (P[m-1] + 10 (P[m-2] + … +
10 (P[2] + 10 P[1])…)).
Similarly, one can compute t0 as follows :
t0 = T[m] + 10 (T[m-1] + 10 (T[m-2] + … +
10 (T[2] + 10 T[1])…)).
Here we can compute ts+1 from ts as follows :
ts+1 = 10 (ts – 10m-1 T[s+1 ]) + T[s+m+1].
10
Let q is defined so that dq fits in one computer word
and the above recurrence equation can be written as :
ts+1 = (d (ts – T[s+1] h ) + T[s+m+1]) mod q.
Here, h  dm-1 (mod q)
i.e., h is the first digit in the m-digit text window.
Ex-6 : Let m = 5, ts = 31415
Let T[s+m+1] = 2
So, RHS = 10 (ts – 10m-1 T[s+1 ]) + T[s+m+1]
= 10 (31415 – 104 . 3) + 2 = 14150 + 2 = 14152
11
The test ts  p (mod q) is a fast heuristic
test to rule out invalid shifts s.
For any value of ‘s’,
if ts  p (mod q) is TRUE
and P[1..m] = T[s+1..s+m] is FALSE
then ‘s’ is called SPURIOUS HIT.
Note : a) If ts  p (mod q) is TRUE
then ts = p may be TRUE
b) If ts  p (mod q) is FALSE
then ts ≠ p is definitely TRUE
12
RABIN-KARP-MATCHER (T,P,d,q)
1 n = T.length
2 m = P.length
3 h = dm-1 (mod q)
4 p = 0
5 t0 = 0
6 for i = 1 to m // preprocessing
7 p = (dp + P[i]) mod q
8 t0 = (d t0 + T[i]) mod q
9 for s = 0 to n-m //matching
10 if (p = = ts )
11 if (P[1..m] = T[s+1..s+m])
12 print “Pattern occurs with shift” s
13 if (s < n – m)
14 ts+1 = (d (ts – T[s+1] h ) + T[s+m+1]) mod q.
13
Ex-7 : Let T = 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
Let P = 3 1 4 1 5
Here n = 19 m = 5 d = 10
q = 13 h = 3
p = 0 t0 = 0
First for statement :
i = 1 : p = 3 t0 = 2
i = 2 : p = 5 t0 = 10
i = 3 : p = 2 t0 = 1
i = 4 : p = 8 t0 = 6
i = 5 : p = 7 t0 = 8
14
Second for statement :
s p ts T p = = ts s < n – m ts+1
0 7 8 23590 FALSE TRUE 9
1 7 9 35902 FALSE TRUE 3
2 7 3 59023 FALSE TRUE 11
3 7 11 90231 FALSE TRUE 0
4 7 0 02314 FALSE TRUE 1
5 7 1 23141 FALSE TRUE 7
6 7 7 31415 TRUE S = 6 TRUE VM 8
7 7 8 14152 FALSE TRUE 4
8 7 4 41526 FALSE TRUE 5
15
s p ts T p = = ts s < n – m ts+1
9 7 5 15267 FALSE TRUE 10
10 7 10 52673 FALSE TRUE 11
11 7 11 26739 FALSE TRUE 7
12 7 7 67399 TRUE S = 12 TRUE SH 9
13 7 9 73992 FALSE TRUE 11
14 7 11 39921 FALSE FALSE ---
Hence, there is only ONE VALID MATCH at s = 6
there is only ONE SPURIOUS HIT at s = 12
16
4. The Knuth-Morris-Pratt Algorithm :
This algorithm is meant for ‘Pattern Matching’.
Here, the prefix function  for a pattern
encapsulates knowledge about how the pattern
matches against shifts of itself.
Ex-8 : Let the Text String T & Pattern P is :
T : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
b a c b a b a b a c a c a c a
P : 1 2 3 4 5 6 7
a b a b a c a
17
COMPUTE-PREFIX-FUNCTION (P) :
1. m = P.length
2. Let [1..m] be a new array
3. [1] = 0
4. k = 0
5. for q = 2 to m
6. while k > 0 and P[k+1]  P[q]
7. k = [k]
8. if P[k+1] = = P[q]
9. k = k + 1
10. [q] = k
11. return 
18
Ex-8 (contd…)
P : 1 2 3 4 5 6 7
a b a b a c a
INIT : m = 7 [1] = 0 k = 0
Step : q = 2 :
Here, k = 0 & P[k+1] = a & P[q] = b
So, while : FALSE & if : FALSE
Hence, [2] = 0
Step : q = 3 :
Here, k = 0 & P[k+1] = a & P[q] = a
So, while : FALSE & if : TRUE k = 1
Hence, [3] = 1
19
Step : q = 4 :
Here, k = 1 & P[k+1] = b & P[q] = b
So, while : FALSE & if : TRUE k = 2
Hence, [4] = 2
Step : q = 5 :
Here, k = 2 & P[k+1] = a & P[q] = a
So, while : FALSE & if : TRUE k = 3
Hence, [5] = 3
Step : q = 6 :
Here, k = 3 & P[k+1] = b & P[q] = c
So, while : TRUE  k = 1 ( = [3] )
& k = 1 & P[k+1] = b & P[q] = c
while : TRUE  k = 0 ( = [1] )
if : FALSE ([P[1] = = P[6])
Hence, [6] = 0
20
Step : q = 7 :
Here, k = 0 & P[k+1] = a & P[q] = a
So, while : FALSE &
if : TRUE (P[1] = = P[7] )
k = 1
Hence, [7] = 1
Hence the  array is as follows :
q : 1 2 3 4 5 6 7
 : 0 0 1 2 3 0 1
Hence, this returns the value : 1
21
KMP-MATCHER (T,P) :
1. n = T.length
2. m = P.length
3.  = COMPUTE-PREFIX-FUNCTION(P)
4. q = 0
5. for i = 1 to n
6. while q > 0 and P[q+1]  T[i]
7. q =  [q]
8. if P[q+1] = = T[i]
9. q = q + 1
10. if q = = m
11. print ”Pattern occurs with shift” i - m
12. q =  [q]
22
Ex-8 contd..
KMP-Matcher (T,P) :
INIT : n = 15 m = 7  =1 q = 0
----------------------------------------------------------------------------------------
i q C1 C2 wh q=  [q] if q++ if print q=  [q]
-------------------------------------------------------------------
1 0 F T F --- F ---- F ---- ----
2 0 F F F --- T q = 1 F ---- ----
3 1 T T T q = 0 F ---- F ---- ----
4 0 F T F --- F ---- F ---- ----
5 0 F F F --- T q = 1 F ---- ----
23
-----------------------------------------------------------------------------------------------
i q C1 C2 wh q=  [q] if q++ if print q=  [q]
-----------------------------------------------------------------------------------------------
6 1 T F F --- T q=2 F ---- ----
7 2 T F F --- T q=3 F ---- ----
8 3 T F F --- T q=4 F ---- ----
9 4 T F F --- T q=5 F ---- ----
10 5 T F F --- T q=6 F ---- ----
11 6 T F F --- T q=7 F shift 4 q=1
12 1 T T T q=0 F ---- F ---- ----
13 0 F F F ---- T q=1 F ---- ----
14 1 T T T q=0 F ---- F ---- ----
15 0 F F F ---- T q=1 F ---- ----
-----------------------------------------------------------------------------------------------

More Related Content

Similar to String-Matching Algorithms Advance algorithm (20)

Knuth morris pratt string matching algo
Knuth morris pratt string matching algoKnuth morris pratt string matching algo
Knuth morris pratt string matching algo
sabiya sabiya
 
String matching algorithms(knuth morris-pratt)
String matching algorithms(knuth morris-pratt)String matching algorithms(knuth morris-pratt)
String matching algorithms(knuth morris-pratt)
Neel Shah
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
Kamal Nayan
 
W9Presentation.ppt
W9Presentation.pptW9Presentation.ppt
W9Presentation.ppt
AlinaMishra7
 
String searching
String searching String searching
String searching
thinkphp
 
lec17.ppt
lec17.pptlec17.ppt
lec17.ppt
shivkr15
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithm
SadiaSharmin40
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
Ashikapokiya12345
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
Mahdi Esmailoghli
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)
Aditya pratap Singh
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
Dr Shashikant Athawale
 
25 String Matching
25 String Matching25 String Matching
25 String Matching
Andres Mendez-Vazquez
 
Lec17
Lec17Lec17
Lec17
Nikhil Chilwant
 
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION  ALGORITHM  IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION  ALGORITHM
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
NETAJI SUBHASH ENGINEERING COLLEGE , KOLKATA
 
Gp 27[string matching].pptx
Gp 27[string matching].pptxGp 27[string matching].pptx
Gp 27[string matching].pptx
SumitYadav641839
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
akruthi k
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
sabiya sabiya
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
Kiran K
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
Shiwani Gupta
 
4 report format
4 report format4 report format
4 report format
Ashikapokiya12345
 
Knuth morris pratt string matching algo
Knuth morris pratt string matching algoKnuth morris pratt string matching algo
Knuth morris pratt string matching algo
sabiya sabiya
 
String matching algorithms(knuth morris-pratt)
String matching algorithms(knuth morris-pratt)String matching algorithms(knuth morris-pratt)
String matching algorithms(knuth morris-pratt)
Neel Shah
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
Kamal Nayan
 
W9Presentation.ppt
W9Presentation.pptW9Presentation.ppt
W9Presentation.ppt
AlinaMishra7
 
String searching
String searching String searching
String searching
thinkphp
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithm
SadiaSharmin40
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)
Aditya pratap Singh
 
Gp 27[string matching].pptx
Gp 27[string matching].pptxGp 27[string matching].pptx
Gp 27[string matching].pptx
SumitYadav641839
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
akruthi k
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
sabiya sabiya
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
Kiran K
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
Shiwani Gupta
 

More from ssuseraf60311 (7)

Graph coloring with back tracking aoa.ppt
Graph coloring with back tracking aoa.pptGraph coloring with back tracking aoa.ppt
Graph coloring with back tracking aoa.ppt
ssuseraf60311
 
3526192.ppt
3526192.ppt3526192.ppt
3526192.ppt
ssuseraf60311
 
8259731.ppt
8259731.ppt8259731.ppt
8259731.ppt
ssuseraf60311
 
fit100-16-dom.ppt
fit100-16-dom.pptfit100-16-dom.ppt
fit100-16-dom.ppt
ssuseraf60311
 
6065165.ppt
6065165.ppt6065165.ppt
6065165.ppt
ssuseraf60311
 
application of http.pptx
application of http.pptxapplication of http.pptx
application of http.pptx
ssuseraf60311
 
Working of web browser.pptx
Working of web browser.pptxWorking of web browser.pptx
Working of web browser.pptx
ssuseraf60311
 
Graph coloring with back tracking aoa.ppt
Graph coloring with back tracking aoa.pptGraph coloring with back tracking aoa.ppt
Graph coloring with back tracking aoa.ppt
ssuseraf60311
 
application of http.pptx
application of http.pptxapplication of http.pptx
application of http.pptx
ssuseraf60311
 
Working of web browser.pptx
Working of web browser.pptxWorking of web browser.pptx
Working of web browser.pptx
ssuseraf60311
 
Ad

Recently uploaded (20)

Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdfIntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdf
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Research_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptxResearch_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptx
niranjancse
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Research_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptxResearch_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptx
niranjancse
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Ad

String-Matching Algorithms Advance algorithm

  • 2. 2 1. String Matching : Let there is an array of text, T[1..n] of length ‘n’. Let there is a pattern of text, P[1..m] of length ‘m’. Let T and P are drawn from a finite alphabet . Here P and T are called ‘Strings of Characters’. Here, the pattern P occurs with shift s in text T, if, 0 ≤ s ≤ n – m and T[s+1..s+m] = P[1..m] i.e., for 1 ≤ j ≤ m, T[s+j] = P[j] If P occurs with shift s in T, it is a VALID SHIFT. Other wise, we call INVALID SHIFT.
  • 3. 3 The String-matching Problem is the problem of finding all valid shifts with which a given pattern P occurs in a given text T. Ex-1 : Let text T : a b c a b a a b c a b a c Let pattern P : a b a a Find the number of valid shifts and ‘s’ values. Answer : Only one Valid Shift. s = 3 The symbol * (read as ‘sigma-star’) is the set of all finite-length strings formed using characters from the alphabet .
  • 4. 4 The zero-length string is called ‘Empty String’. denoted by ‘ɛ’, also belongs to *. The length of the string ‘x’ is denoted |x|. The concatenation of two strings x and y, denoted xy has length |x| + |y|. A string ω is a prefix of a string x, denoted as ω ⊏ x, if x = ω y for some string y ∊ *. Here, note that if ω ⊏ x, then |w| ≤ |x|. Similarly, a string ω is a suffix of a string x, denoted as ω ⊐ x, if x = y ω for some string y ∊ *. Here, note that if ω ⊐ x, then |w| ≤ |x|.
  • 5. 5 Ex-2 : Let abcca is a string. Here, ab ⊏ abcca and cca ⊐ abcca Note-1: The empty string ɛ is both a suffix and prefix of every string. Note-2 : Both prefix and suffix are transitive relations. Lemma : Suppose that x, y, and z are strings such that x ⊐ z and y ⊐ z. Here, if |x| ≤ |y| then x ⊐ y. if |x| ≥ |y| then y ⊐ x. if |x| = |y| then x = y.
  • 6. 6 2. The Naïve String-matching Algorithm : This 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. NAÏVE-STRING-MATCHER(T,P) 1 n = T.length 2 m = P.length 3. for s = 0 to n – m 4. if P[1..m] = = T[s+1..s+m] 5 Print “Pattern occurs with shift s.”
  • 7. 7 Ex-3 : Let T = acaabc & P = aab Find the value of s. Answer : The value of s = 2 Ex-4 : Let T = 000010001010001 P = 0001 Find the values of ‘s’. Answer : The value of s = 1 & 5 & 11 Ex-5 : Let T = an and P = am Answer : The values of s = 0 to n – m i.e., s contains n – m + 1 values
  • 8. 8 3. The Rabin-Karp Algorithm : Let  = {0, 1, 2, … , 9} Here each character is a decimal digit. d = |  | = 10. The string 31415 represents 31,415 in radix-d notation. Let there is a text T[1..n]. Let there is a pattern P[1..m]. Let p denote the corresponding decimal value. Let ts is the decimal value of the length –m substring T[s+1..s+m], for s = 0,1,2,..n-m.  ts = p iff T[s+1..s+m] = P[1..m]  s is a valid shift iff ts = p
  • 9. 9 Now, the value of p can be computed using Horner’s rule as follows: p = P[1..m] = P[1] P[2] P[3]…P[m] So, p = P[m] + 10 (P[m-1] + 10 (P[m-2] + … + 10 (P[2] + 10 P[1])…)). Similarly, one can compute t0 as follows : t0 = T[m] + 10 (T[m-1] + 10 (T[m-2] + … + 10 (T[2] + 10 T[1])…)). Here we can compute ts+1 from ts as follows : ts+1 = 10 (ts – 10m-1 T[s+1 ]) + T[s+m+1].
  • 10. 10 Let q is defined so that dq fits in one computer word and the above recurrence equation can be written as : ts+1 = (d (ts – T[s+1] h ) + T[s+m+1]) mod q. Here, h  dm-1 (mod q) i.e., h is the first digit in the m-digit text window. Ex-6 : Let m = 5, ts = 31415 Let T[s+m+1] = 2 So, RHS = 10 (ts – 10m-1 T[s+1 ]) + T[s+m+1] = 10 (31415 – 104 . 3) + 2 = 14150 + 2 = 14152
  • 11. 11 The test ts  p (mod q) is a fast heuristic test to rule out invalid shifts s. For any value of ‘s’, if ts  p (mod q) is TRUE and P[1..m] = T[s+1..s+m] is FALSE then ‘s’ is called SPURIOUS HIT. Note : a) If ts  p (mod q) is TRUE then ts = p may be TRUE b) If ts  p (mod q) is FALSE then ts ≠ p is definitely TRUE
  • 12. 12 RABIN-KARP-MATCHER (T,P,d,q) 1 n = T.length 2 m = P.length 3 h = dm-1 (mod q) 4 p = 0 5 t0 = 0 6 for i = 1 to m // preprocessing 7 p = (dp + P[i]) mod q 8 t0 = (d t0 + T[i]) mod q 9 for s = 0 to n-m //matching 10 if (p = = ts ) 11 if (P[1..m] = T[s+1..s+m]) 12 print “Pattern occurs with shift” s 13 if (s < n – m) 14 ts+1 = (d (ts – T[s+1] h ) + T[s+m+1]) mod q.
  • 13. 13 Ex-7 : Let T = 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1 Let P = 3 1 4 1 5 Here n = 19 m = 5 d = 10 q = 13 h = 3 p = 0 t0 = 0 First for statement : i = 1 : p = 3 t0 = 2 i = 2 : p = 5 t0 = 10 i = 3 : p = 2 t0 = 1 i = 4 : p = 8 t0 = 6 i = 5 : p = 7 t0 = 8
  • 14. 14 Second for statement : s p ts T p = = ts s < n – m ts+1 0 7 8 23590 FALSE TRUE 9 1 7 9 35902 FALSE TRUE 3 2 7 3 59023 FALSE TRUE 11 3 7 11 90231 FALSE TRUE 0 4 7 0 02314 FALSE TRUE 1 5 7 1 23141 FALSE TRUE 7 6 7 7 31415 TRUE S = 6 TRUE VM 8 7 7 8 14152 FALSE TRUE 4 8 7 4 41526 FALSE TRUE 5
  • 15. 15 s p ts T p = = ts s < n – m ts+1 9 7 5 15267 FALSE TRUE 10 10 7 10 52673 FALSE TRUE 11 11 7 11 26739 FALSE TRUE 7 12 7 7 67399 TRUE S = 12 TRUE SH 9 13 7 9 73992 FALSE TRUE 11 14 7 11 39921 FALSE FALSE --- Hence, there is only ONE VALID MATCH at s = 6 there is only ONE SPURIOUS HIT at s = 12
  • 16. 16 4. The Knuth-Morris-Pratt Algorithm : This algorithm is meant for ‘Pattern Matching’. Here, the prefix function  for a pattern encapsulates knowledge about how the pattern matches against shifts of itself. Ex-8 : Let the Text String T & Pattern P is : T : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 b a c b a b a b a c a c a c a P : 1 2 3 4 5 6 7 a b a b a c a
  • 17. 17 COMPUTE-PREFIX-FUNCTION (P) : 1. m = P.length 2. Let [1..m] be a new array 3. [1] = 0 4. k = 0 5. for q = 2 to m 6. while k > 0 and P[k+1]  P[q] 7. k = [k] 8. if P[k+1] = = P[q] 9. k = k + 1 10. [q] = k 11. return 
  • 18. 18 Ex-8 (contd…) P : 1 2 3 4 5 6 7 a b a b a c a INIT : m = 7 [1] = 0 k = 0 Step : q = 2 : Here, k = 0 & P[k+1] = a & P[q] = b So, while : FALSE & if : FALSE Hence, [2] = 0 Step : q = 3 : Here, k = 0 & P[k+1] = a & P[q] = a So, while : FALSE & if : TRUE k = 1 Hence, [3] = 1
  • 19. 19 Step : q = 4 : Here, k = 1 & P[k+1] = b & P[q] = b So, while : FALSE & if : TRUE k = 2 Hence, [4] = 2 Step : q = 5 : Here, k = 2 & P[k+1] = a & P[q] = a So, while : FALSE & if : TRUE k = 3 Hence, [5] = 3 Step : q = 6 : Here, k = 3 & P[k+1] = b & P[q] = c So, while : TRUE  k = 1 ( = [3] ) & k = 1 & P[k+1] = b & P[q] = c while : TRUE  k = 0 ( = [1] ) if : FALSE ([P[1] = = P[6]) Hence, [6] = 0
  • 20. 20 Step : q = 7 : Here, k = 0 & P[k+1] = a & P[q] = a So, while : FALSE & if : TRUE (P[1] = = P[7] ) k = 1 Hence, [7] = 1 Hence the  array is as follows : q : 1 2 3 4 5 6 7  : 0 0 1 2 3 0 1 Hence, this returns the value : 1
  • 21. 21 KMP-MATCHER (T,P) : 1. n = T.length 2. m = P.length 3.  = COMPUTE-PREFIX-FUNCTION(P) 4. q = 0 5. for i = 1 to n 6. while q > 0 and P[q+1]  T[i] 7. q =  [q] 8. if P[q+1] = = T[i] 9. q = q + 1 10. if q = = m 11. print ”Pattern occurs with shift” i - m 12. q =  [q]
  • 22. 22 Ex-8 contd.. KMP-Matcher (T,P) : INIT : n = 15 m = 7  =1 q = 0 ---------------------------------------------------------------------------------------- i q C1 C2 wh q=  [q] if q++ if print q=  [q] ------------------------------------------------------------------- 1 0 F T F --- F ---- F ---- ---- 2 0 F F F --- T q = 1 F ---- ---- 3 1 T T T q = 0 F ---- F ---- ---- 4 0 F T F --- F ---- F ---- ---- 5 0 F F F --- T q = 1 F ---- ----
  • 23. 23 ----------------------------------------------------------------------------------------------- i q C1 C2 wh q=  [q] if q++ if print q=  [q] ----------------------------------------------------------------------------------------------- 6 1 T F F --- T q=2 F ---- ---- 7 2 T F F --- T q=3 F ---- ---- 8 3 T F F --- T q=4 F ---- ---- 9 4 T F F --- T q=5 F ---- ---- 10 5 T F F --- T q=6 F ---- ---- 11 6 T F F --- T q=7 F shift 4 q=1 12 1 T T T q=0 F ---- F ---- ---- 13 0 F F F ---- T q=1 F ---- ---- 14 1 T T T q=0 F ---- F ---- ---- 15 0 F F F ---- T q=1 F ---- ---- -----------------------------------------------------------------------------------------------