[prev] 30 [next]

Knuth-Morris-Pratt Algorithm (cont)

KMP preprocesses the pattern P[0..m-1] to find matches of its prefixes with itself
  • Failure function F(j) defined as
    • the size of the largest prefix of P[0..j] that is also a suffix of P[1..j]
      • for each position j=0..m-1
  • if mismatch occurs at Pj   ⇒ advance j to F(j-1)
Example: P = abaaba
j012345
Pjabaaba
F(j)001123

[Diagram:Pic/kmp-failure-function.png]