String Algorithms
Print this page
 
Fig : 4
   
 
  • KMP ( Knuth-Morris-pratt) algorithm avoids some redundant comparisons. When a mismatch occurs, the most we can shift the pattern so as to avoid redundant comparisons is the largest prefix of P[1..j] that is also suffix of P[2..j], where j is the number of characters that are known to be matched successfully before the mismatch.
  • The amount of shift that is not necessarily invalid can be pre-computed by comparing the pattern against itself. For string matching process we need to find a "Failure Function" FF which will give the amount of shift that is not necessarily invalid. If j characters are already successfully matched and there is a mismatch position j+1, then the output of the FF is the largest prefix of P[1..j] that is also a suffix of P[2..j].
   
Prev