KMP Pattern Search Algorithm

DayWithAlgorithm 95 views 34 slides Jan 30, 2021
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Pattern Search Algorithm. Explains how both the Naive and KMP Pattern Search algorithm.


Slide Content

@ daywithalgorithm KMP Pattern Search Created By: Donald K nuth, James H. M orris, Vaughan P ratt.

@ daywithalgorithm 1. Why Should you Use this Algorithm for Pattern Search ? In order to know the reason, we shall revisit the process that is undertaken in the naive algorithm. By doing so, you will be understanding the reason behind it. The pattern (search text) is usually represented as needle and the paragraph or the text where we are searching is called as Haystack.

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH Text : 1 2 3 4 5 6 7 8 9 Index of Text : O N I O N S Pattern : 1 2 3 4 5 Index of Pattern :

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 0 ctr = 0

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 1 ctr = 1

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 2 ctr = 2

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 3 ctr = 3

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 4 ctr = 4

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 5 Not a Match

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 1 ctr = 0 Not a Match Here we’re re-inventing the wheel

@ daywithalgorithm O N I O N I O N S O NAIVE APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 3 ctr = 0 Time Complexity : Best Case Average Case Worst Case O(N) O(N-M) O(N*M)

@ daywithalgorithm Inorder to stop reinventing the wheel, we need to make a track of the values that we traversed. That could be acheived by Longest Prefix also Suffix Method.

@ daywithalgorithm Longest Prefix also Suffix Method Longest Prefix also Suffix Method (LPS) is a method to identify the longest prefix of a string, which is also a suffix. 1 2 3 4 5 O N I O N S LETS DECODE THE METHOD

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1. Initialize an LPS array with the same length of pattern array and set all index value to 0 1 2 3 4 5 O N I O N S LPS

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Inorder to find the prefix which is also a suffix, we are using 2 iterators itr and ctr. Where itr = 0, ctr = 1. Then we will be comparing pattern[itr] and pattern[ctr]. Based on the output we will be updating the values in the LPS array. Itr Ctr Patrn.

@ daywithalgorithm Longest Prefix also Suffix Method If Pattern[itr] == Pattern[ctr], then insert LPS[ctr] = ctr. Increment both ctr and itr. 2. If Pattern[itr] != Pattern[ctr], there are two cases. If ctr less than zero, then increment itr. If ctr greater than zero, then set value of ctr to LPS[ctr-1]. LOGIC

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Itr Ctr Patrn. Patrn[itr] != Patrn[ctr] and itr == 0. So Increment ctr by 1.

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Itr Ctr Patrn. Patrn[itr] != Patrn[ctr] and itr == 0. So Increment ctr by 1.

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Itr Ctr 1 Patrn. Patrn[itr] = = Patrn[ctr] and itr == 0. So Increment ctr by 1 and itr by 1 . Set LPS[ctr] = itr

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Itr Ctr 1 2 Patrn. Patrn[itr] == Patrn[ctr] and itr == 0. So Increment ctr by 1 and itr by 1. Set LPS[ctr] = itr

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 O N I O N S 1 2 3 4 5 LPS Itr Ctr 1 2 Patrn. Patrn[itr] == Patrn[ctr] and itr == 0. So Increment ctr by 1 and itr by 1. Set LPS[ctr] = itr

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 LPS 1 2 So now, we have successfully generated the LPS array.

@ daywithalgorithm Longest Prefix also Suffix Method 1 2 3 4 5 LPS 1 2 O N I O N I O N S O Text : 1 2 3 4 5 6 7 8 9 Index of Text : O N I O N S Pattern : 1 2 3 4 5 Index of Pattern :

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 0 ctr = 0 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 1 ctr = 1 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 2 ctr = 2 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 3 ctr = 3 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 4 ctr = 4 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 5 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 5 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 5 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 2 1 2 3 4 5 LPS 1 2

@ daywithalgorithm O N I O N I O N S O KMP APPROACH 1 2 3 4 5 6 7 8 9 O N I O N S 1 2 3 4 5 itr = 5 ctr = 2 1 2 3 4 5 LPS 1 2

KMP APPROACH Best Case Average Case Worst Case O(N + M) O(N + M) O(N) @ daywithalgorithm