Knuth-Morris-Pratt PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Knuth-Morris-Pratt


1
Knuth-Morris-Pratt
Reference Chapter 19, Algorithms in C by R.
Sedgewick.Addison Wesley, 1990.
2
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
a
a
b
a
a
a
a
a
a
a
a
b
3
4
5
6
0
1
2
accept state
3
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
a
a
b
a
a
a
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
4
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
5
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
6
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
7
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
8
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
9
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
10
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
11
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
12
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
13
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
a
b
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
14
Knuth-Morris-Pratt
  • KMP algorithm.
  • Use knowledge of how search pattern repeats
    itself.
  • Build FSA from pattern.
  • Run FSA on text.

Search Pattern
Search Text
a
a
b
a
a
a
a
a
b
a
a
a
b
a
a
a
b
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
15
FSA Representation
  • FSA used in KMP has special property.
  • Upon character match, go forward one state.
  • Only need to keep track of where to go upon
    character mismatch.
  • go to state nextj if character mismatches in
    state j

0
1
2
3
4
5
a
1
2
2
4
5
6
Search Pattern
b
0
0
3
0
0
3
a
a
b
a
a
a
next
0
0
2
0
0
3
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
accept state
b
b
16
KMP Algorithm
  • Two key differences from brute force.
  • Text pointer i never backs up.
  • Need to precompute next table.

17
FSA Construction for KMP
  • FSA construction for KMP.
  • FSA builds itself!
  • Example. Building FSA for aabaaabb.
  • State 6. p0..5 aabaaa
  • assume you know state for p1..5 abaaa X 2
  • if next char is b (match) go forward 6 1 7
  • if next char is a (mismatch) go to state for
    abaaaa X 'a' 2
  • update X to state for p1..6 abaaab X 'b'
    3

b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
b
b
18
FSA Construction for KMP
  • FSA construction for KMP.
  • FSA builds itself!
  • Example. Building FSA for aabaaabb.

b
a
b
a
a
a
a
a
b
b
3
4
5
6
0
1
2
7
b
b
b
a
19
FSA Construction for KMP
  • FSA construction for KMP.
  • FSA builds itself!
  • Example. Building FSA for aabaaabb.
  • State 7. p0..6 aabaaab
  • assume you know state for p1..6 abaaab X 3
  • if next char is b (match) go forward 7 1 8
  • next char is a (mismatch) go to state for
    abaaaba X 'a' 4
  • update X to state for p1..7 abaaabb X 'b'
    0

b
a
b
a
a
a
a
a
b
b
3
4
5
6
0
1
2
7
b
b
b
a
20
FSA Construction for KMP
  • FSA construction for KMP.
  • FSA builds itself!
  • Example. Building FSA for aabaaabb.

b
a
a
b
a
a
a
a
a
b
b
b
3
4
5
6
0
1
2
7
8
b
b
b
a
21
FSA Construction for KMP
  • FSA construction for KMP.
  • FSA builds itself!
  • Crucial insight.
  • To compute transitions for state n of FSA,
    suffices to have
  • FSA for states 0 to n-1
  • state X that FSA ends up in with input p1..n-1
  • To compute state X' that FSA ends up in with
    input p1..n, it suffices to have
  • FSA for states 0 to n-1
  • state X that FSA ends up in with input p1..n-1

22
FSA Construction for KMP
X
Search Pattern
pattern1..j
j
a
a
b
a
a
a
b
b
a
b
b
a
0
1
23
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
0
a
1
b
0
b
a
0
1
24
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
0
1
a
1
2
b
0
0
b
a
a
0
1
2
b
25
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
a
b
0
2
2
0
1
2
a
1
2
2
b
0
0
3
a
b
a
a
b
3
0
1
2
b
26
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
a
b
0
2
2
0
1
2
3
a
b
a
1
3
0
a
1
2
2
4
b
0
0
3
0
a
b
a
a
a
b
3
4
0
1
2
b
b
27
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
a
b
0
2
2
0
1
2
3
4
a
b
a
1
3
0
a
1
2
2
4
5
a
b
a
a
2
4
0
b
0
0
3
0
0
b
a
b
a
a
a
a
b
3
4
5
0
1
2
b
b
28
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
a
b
0
2
2
0
1
2
3
4
5
a
b
a
1
3
0
a
1
2
2
4
5
6
a
b
a
a
2
4
0
b
0
0
3
0
0
3
a
b
a
a
a
2
5
3
b
a
b
a
a
a
a
a
b
3
4
5
6
0
1
2
b
b
b
29
FSA Construction for KMP
Search Pattern
X
pattern1..j
j
next
a
a
b
a
a
a
b
b
0
0
0
a
1
1
0
a
b
0
2
2
0
1
2
3
4
5
6
a
b
a
1
3
0
a
1
2
2
4
5
6
2
a
b
a
a
2
4
0
b
0
0
3
0
0
3
7
a
b
a
a
a
2
5
3
a
b
a
a
a
b
3
6
2
b
a
b
a
a
a
a
a
b
b
3
4
5
6
0
1
2
7
b
b
b
a
30
FSA Construction for KMP
X
Search Pattern
pattern1..j
j
next
0
0
a
a
b
a
a
a
b
b
0
a
1
1
0
a
b
0
2
2
0
1
2
3
4
5
6
7
a
b
a
1
3
0
a
1
2
2
4
5
6
2
4
a
b
a
a
2
4
0
b
0
0
3
0
0
3
7
8
a
b
a
a
a
2
5
3
a
b
a
a
a
b
3
6
2
a
b
a
a
a
b
b
0
7
4
31
FSA Construction for KMP
  • Code for FSA construction in KMP algorithm.
Write a Comment
User Comments (0)
About PowerShow.com