Knuth – Morris – Pratt algorithm
is a PATTERN matching algorithm
Knuth – Morris – Pratt algorithm
is used to search a pattern in a text
Knuth – Morris – Pratt algorithm
was introduced in 1974 by
Donald Knuth and Vaughan Pratt,
and independently by
James H. Morris
Knuth – Morris – Pratt algorithm
was jointly published by all three, in
1977
Knuth – Morris – Pratt algorithm
We use two terms,
Pattern and Text
Pattern
Word which we want to search
Text
Data in which we want to search for pattern
How does KMP work
Every character in Pattern is compared
with characters in Text
If all characters in Pattern are matching
with characters in text, then search success
If any mismatch found then, shift the
pattern according to Prefix Table and
continue with comparison
What is Prefix Table
A table derived for Pattern, which specifies
“How many positions the pattern has to shift”
when we found mismatch
0 1 2 3 4 5 6
A B C D A B D
0 0 0 0 1 2 1
Index
Pattern
Shifting Value
0 0 0 0 1 2 1
How to create Prefix Table
A prefix table is created using length of
substrings in pattern
0 1 2 3 4 5 6
A B C D A B D
0 0 0 0 1 2 1
Index
Pattern
Shifting Value
0 0 0 0 1 2 1
How to create Prefix Table
A prefix table is created using length of
substrings in pattern
0 1 2 3 4 5 6
A B C D A B D
0 0 0 0 1 2 1
Index
Pattern
Shifting Value

KMP.ppt

  • 2.
    Knuth – Morris– Pratt algorithm is a PATTERN matching algorithm
  • 3.
    Knuth – Morris– Pratt algorithm is used to search a pattern in a text
  • 4.
    Knuth – Morris– Pratt algorithm was introduced in 1974 by Donald Knuth and Vaughan Pratt, and independently by James H. Morris
  • 5.
    Knuth – Morris– Pratt algorithm was jointly published by all three, in 1977
  • 6.
    Knuth – Morris– Pratt algorithm We use two terms, Pattern and Text Pattern Word which we want to search Text Data in which we want to search for pattern
  • 7.
    How does KMPwork Every character in Pattern is compared with characters in Text If all characters in Pattern are matching with characters in text, then search success If any mismatch found then, shift the pattern according to Prefix Table and continue with comparison
  • 8.
    What is PrefixTable A table derived for Pattern, which specifies “How many positions the pattern has to shift” when we found mismatch 0 1 2 3 4 5 6 A B C D A B D 0 0 0 0 1 2 1 Index Pattern Shifting Value
  • 9.
    0 0 00 1 2 1 How to create Prefix Table A prefix table is created using length of substrings in pattern 0 1 2 3 4 5 6 A B C D A B D 0 0 0 0 1 2 1 Index Pattern Shifting Value
  • 10.
    0 0 00 1 2 1 How to create Prefix Table A prefix table is created using length of substrings in pattern 0 1 2 3 4 5 6 A B C D A B D 0 0 0 0 1 2 1 Index Pattern Shifting Value