|
|
|
|
|
|
|
|
|
|
String Algorithms |
|
|
|
|
|
|
|
|
|
|
|
|
Knuth-Morris-Pratt(KMP) string matching algorithm |
|
|
|
|
|
|
|
|
|
|
|
-
In brute force method, irrespective of the structure of the pattern P, if there is a mismatch, we restart the matching process with shift s = s+1.
-
But if we know the structure of the pattern, we can intelligently avoid some number of shifts.
|
|
|
|
|
|
|
|
|
|
|
|
|
 |
|
|
|
|
|
Fig : 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Suppose 6 characters are matched successfully and there is a mismatch at the 7 th position. The shift s = s+1 is obviously invalid as the first pattern character ‘a' would be aligned to the text character ‘b', which is known to match the second pattern character.
- Also if we observe, the shift s = s+2 is also invalid.
- But the shift s = s+3 may be valid as first three characters of the pattern will be nicely aligned to the last three characters of the portion of the text which are already matched.
|
|
|
Prev |
|
|