The document describes the Knuth-Morris-Pratt (KMP) string matching algorithm. It uses two pointers i and j to iterate through the original and input strings respectively. If the characters at i and j match, both pointers advance. If they do not match, j returns to an index stored in a preprocessing table. This allows j to skip already matched substrings, resulting in an overall O(n+m) runtime where n and m are the lengths of the original and input strings. The document provides an example run of the algorithm on two strings.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
10a String Matching(KMP).pptx
1. Design and Analysis of
Algorithms
String matching problem
Presented by: Aoun-Haider
2. Knuth Moris Patt (KMP) Algorithm:
• Prepare a matrix of input string and write index where duplication is
available to avoid extra comparison.
• Computes in O(n+m)
• Take to iterators ‘i’ & ‘j’ and ‘i’ will move on original string and j will
move on input string
• If element at ith and jth index are same more both
• If not then move j at available index
3. Example:
a b a b c a b c a b a b a b d
i
a b a b d
j
Input_str:
4. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
- a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Input_str:
5. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
- a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move i and j)
Input_str:
6. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
- a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Input_str:
7. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
- a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move i and j)
Input_str:
8. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
- a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Input_str:
9. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move i and j)
10. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
11. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move i and j)
12. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
13. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equal!! (move j at input_str[1][4] = 2)
14. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
15. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equal!! (move j at input_str[0][0] = 0)
16. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
17. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equal!! (just move ‘i’ because ‘j’ is at
its minimum position)
18. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
19. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j’)
20. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
21. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equall!! (move ‘i’ and ‘j’)
22. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
23. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equall!! (move ‘j’ at index 0)
24. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
25. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equall!! (move ‘i’)
26. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
27. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
28. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
29. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
30. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
31. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
32. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
33. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
34. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
35. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Not Equall!! (move ‘j’ at index 2)
36. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
37. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
38. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
39. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
Equal!! (move ‘i’ and ‘j‘)
40. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and input_str[j+1]
41. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and
input_str[j+1]
Equal!! (No more move string exist)
42. Example:
a b a b c a b c a b a b a b d
i
0 1 2 3 4 5
Input_str: - a b a b d
- 0 0 1 2 0
j
Compare orignal_str[i] and
input_str[j+1]
Equal!! (No more move string exist)
O(n+m)