knuth morris pratt n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Knuth-Morris-Pratt PowerPoint Presentation
Download Presentation
Knuth-Morris-Pratt

Loading in 2 Seconds...

play fullscreen
1 / 31

Knuth-Morris-Pratt - PowerPoint PPT Presentation


  • 154 Views
  • Uploaded on

Knuth-Morris-Pratt. Reference: Chapter 19, Algorithms in C by R. Sedgewick. Addison Wesley, 1990. 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. b. a.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Knuth-Morris-Pratt' - nevin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
knuth morris pratt

Knuth-Morris-Pratt

Reference: Chapter 19, Algorithms in C by R. Sedgewick.Addison Wesley, 1990.

knuth morris pratt1
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

b

a

0

1

3

4

2

5

6

accept state

knuth morris pratt2
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt3
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt4
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt5
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt6
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt7
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt8
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt9
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt10
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt11
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt12
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

knuth morris pratt13
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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

fsa representation
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 next[j] 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

b

a

0

1

3

4

2

5

6

b

accept state

b

b

kmp algorithm

int kmpsearch(char p[], char t[], int next[]) {

int i, j = 0;

int M = strlen(p); // pattern length

int N = strlen(t); // text length

for (i = 0; i < N; i++) {

if (t[i] == p[j]) j++; // char match

else j = next[j]; // char mismatch

if (j == M) return i – M + 1; // found

}

return N; // not found

}

KMP String Search

KMP Algorithm
  • Two key differences from brute force.
    • Text pointer i never backs up.
    • Need to precompute next[] table.
fsa construction for kmp
FSA Construction for KMP
  • FSA construction for KMP.
    • FSA builds itself!
  • Example. Building FSA for aabaaabb.
    • State 6. p[0..5] = aabaaa
      • assume you know state for p[1..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 p[1..6] = abaaab X + 'b' = 3

b

a

b

a

a

a

a

b

a

0

1

3

4

2

5

6

b

b

b

fsa construction for kmp1
FSA Construction for KMP
  • FSA construction for KMP.
    • FSA builds itself!
  • Example. Building FSA for aabaaabb.

b

a

b

a

a

a

a

b

a

b

0

1

3

4

7

2

5

6

b

b

b

a

fsa construction for kmp2
FSA Construction for KMP
  • FSA construction for KMP.
    • FSA builds itself!
  • Example. Building FSA for aabaaabb.
    • State 7. p[0..6] = aabaaab
      • assume you know state for p[1..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 p[1..7] = abaaabb X + 'b' = 0

b

a

b

a

a

a

a

b

a

b

0

1

3

4

7

2

5

6

b

b

b

a

fsa construction for kmp3
FSA Construction for KMP
  • FSA construction for KMP.
    • FSA builds itself!
  • Example. Building FSA for aabaaabb.

b

a

a

b

a

a

a

a

b

b

a

b

0

1

3

4

7

2

5

6

8

b

b

b

a

fsa construction for kmp4
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 p[1..n-1]
    • To compute state X' that FSA ends up in with input p[1..n], it suffices to have:
      • FSA for states 0 to n-1
      • state X that FSA ends up in with input p[1..n-1]
fsa construction for kmp5
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

a

a

b

a

a

a

b

b

a

b

b

a

0

1

fsa construction for kmp6
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

0

a

1

b

0

b

a

0

1

fsa construction for kmp7
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

0

1

a

1

2

b

0

0

b

a

a

0

1

2

b

fsa construction for kmp8
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

a

1

2

2

b

0

0

3

a

b

a

a

b

0

1

3

2

b

fsa construction for kmp9
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

3

3

a

b

a

1

0

a

1

2

2

4

b

0

0

3

0

a

b

a

a

a

b

0

1

3

4

2

b

b

fsa construction for kmp10
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

3

4

3

a

b

a

1

0

a

1

2

2

4

5

4

a

b

a

a

2

0

b

0

0

3

0

0

b

a

b

a

a

a

a

b

0

1

3

4

2

5

b

b

fsa construction for kmp11
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

3

4

5

3

a

b

a

1

0

a

1

2

2

4

5

6

4

a

b

a

a

2

0

b

0

0

3

0

0

3

5

a

b

a

a

a

2

3

b

a

b

a

a

a

a

b

a

0

1

3

4

2

5

6

b

b

b

fsa construction for kmp12
FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

3

4

5

6

3

a

b

a

1

0

a

1

2

2

4

5

6

2

4

a

b

a

a

2

0

b

0

0

3

0

0

3

7

5

a

b

a

a

a

2

3

6

a

b

a

a

a

b

3

2

b

a

b

a

a

a

a

b

b

a

0

1

3

4

7

2

5

6

b

b

b

a

fsa construction for kmp13

b

a

a

b

a

a

a

a

b

b

a

b

0

1

3

4

7

2

5

6

8

b

b

b

a

FSA Construction for KMP

Search Pattern

j

pattern[1..j]

X

next

a

a

b

a

a

a

b

b

0

0

0

1

a

1

0

2

a

b

0

2

0

1

2

3

4

5

6

7

3

a

b

a

1

0

a

1

2

2

4

5

6

2

4

4

a

b

a

a

2

0

b

0

0

3

0

0

3

7

8

5

a

b

a

a

a

2

3

6

a

b

a

a

a

b

3

2

7

a

b

a

a

a

b

b

0

4

fsa construction for kmp14

void kmpinit(char p[], int next[]) {

int j, X = 0, M = strlen(p);

next[0] = 0;

for (j = 1; j < M; j++) {

if (p[X] == p[j]) { // char match

next[j] = next[X];

X = X + 1;

}

else { // char mismatch

next[j] = X + 1;

X = next[X];

}

}

}

FSA Construction for KMP

FSA Construction for KMP
  • Code for FSA construction in KMP algorithm.