MADFL 2025 Expt8
MADFL 2025 Expt8
Pattern Matching
Theory:
String operations
• Substring of an m-character string P refers to a string of the form P[i]P[i+1]P[i+2]…P[j],
for some 0<=i<=j<=m-1
• Let P [i…j] denote the substring of P from index i to index j inclusive i.e.
P [i…j] = P[i]P[i+1] …P[j]
• If i>j then P [i…j] = null string of length = 0
• Prefix of P: any substring of the form P [0…i] for 0<=i<=m-1
• Suffix of P: any substring of the form P [i…m-1] for 0<=i<=m-1
• Note: null string is prefix and suffix of any other string
Reference:
https://youtu.be/jXERe53h5zc
Last(c):
input – character c from the alphabet
output – how far to shift pattern P {if a character = c is found in the text that does not match
the pattern}
• If characters can be used as indices in arrays then the last function can be
implemented as a lookup table
Text = abacaabaccabacabaabb
Pattern = abacab
2) Boyer Moore Algorithm
Text = abacaabaccabacabaabb
Pattern = abacab
3) Knuth-Morris-Pratt Algorithm
Text = abacaabaccabacabaabb
Pattern = abacab
Text = 1100011010001010
Pattern = 0010
Programs and Output:
Display the number of character comparisons for all three programs for the following:
Text = abacaabaccabacabaabb
Pattern = abacab
Conclusion:
Compare all three algorithms.