SlideShare a Scribd company logo
kmp_algorithm (string matching)
boring
boring
AABA AABA
AABA
kmp_algorithm (string matching)
length of the longest
proper suffix which is also the proper prefix for a substring from
zero to ‘i’.
• Proper Prefixes of “aababaab” : “a” , “aa” , “aab” , “aaba” , “aabab”
, “aababa” and “aababaa”.
• Proper Suffixes of “aababaab” : “b” , “ab” , “aab” , “baab” , “abaab”,
“babaab” and “ababaab”.
• There is only one common suffix and prefix : “aab”. Length is 3.
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
Index 0 1 2 3 4 5 6 7 8
Pattern a a b a a b a a a
LPS Array 0 1 0 1 2 3 4 5 2
kmp_algorithm (string matching)
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0
lps[0] initialized to zero
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0
len=0, i=1
pat[i] != pat[len]
Since len=0, lps[i] = 0
i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1
len=0, i=2
pat[i] == pat[len]
lps[i] = len + 1
i++, len++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2
len=1, i=3
pat[i] == pat[len]
lps[i] = len + 1
i++, len++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3
len=2, i=4
pat[i] == pat[len]
lps[i] = len + 1
i++, len++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3
len=3, i=5
pat[i] != pat[len]
len=lps[len-1]
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3
len=1, i=5
pat[i] != pat[len]
len=lps[len-1]
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0
len=0, i=5
pat[i] != pat[len]
Since len=0, lps[i]=0
i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1
len=0, i=6
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2
len=1, i=7
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3
len=2, i=8
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4
len=3, i=9
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5
len=4, i=10
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6
len=5, i=11
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7
len=6, i=12
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8
len=7, i=13
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9
len=8, i=14
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10
len=9, i=15
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11
len=10, i=16
pat[i] == pat[len]
lps[i]=len+1
len++, i++
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11
len=11, i=17
pat[i] != pat[len]
len=lps[len-1]
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11
len=5, i=17
pat[i] != pat[len]
len=lps[len-1]
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Pattern a c a c a b a c a c a b a c a c a c
LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 4
len=3, i=17
pat[i] == pat[len]
lps[i]=len+1
len++, i++
kmp_algorithm (string matching)
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
Now, since Pattern[j] doesn’t match with String[i], j=LPS[j-1], start=i-LPS[j-1].
i
j
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
Pattern[j] is still not equal to String[i].And since j=0, we increment ‘i’ and ‘start’ this time.
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
Again String[i]!=Pattern[j].Therefore j=LPS[j-1]. start=i-LPS[j-1].
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
start
Index 0 1 2 3 4 5 6 7 8 9 10 11
String a b x a b c a b c a b y
Index 0 1 2 3 4 5
Pattern a b c a b y
LPS Array 0 0 0 1 2 0
i
j
start
We reached the end of the pattern. Hence the pattern is found at index ‘start’.
kmp_algorithm (string matching)

More Related Content

PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PPTX
Knights tour on chessboard using backtracking
Abhishek Singh
 
PPTX
RABIN KARP ALGORITHM STRING MATCHING
Abhishek Singh
 
PPTX
Naive string matching
Abhishek Singh
 
PPTX
15 puzzle problem using branch and bound
Abhishek Singh
 
PPTX
sum of subset problem using Backtracking
Abhishek Singh
 
PPTX
Sudoku
Abhishek Singh
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
0 1 knapsack using branch and bound
Abhishek Singh
 
Knights tour on chessboard using backtracking
Abhishek Singh
 
RABIN KARP ALGORITHM STRING MATCHING
Abhishek Singh
 
Naive string matching
Abhishek Singh
 
15 puzzle problem using branch and bound
Abhishek Singh
 
sum of subset problem using Backtracking
Abhishek Singh
 

Recently uploaded (20)

PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Ad
Ad

kmp_algorithm (string matching)

  • 4. length of the longest proper suffix which is also the proper prefix for a substring from zero to ‘i’.
  • 5. • Proper Prefixes of “aababaab” : “a” , “aa” , “aab” , “aaba” , “aabab” , “aababa” and “aababaa”. • Proper Suffixes of “aababaab” : “b” , “ab” , “aab” , “baab” , “abaab”, “babaab” and “ababaab”. • There is only one common suffix and prefix : “aab”. Length is 3.
  • 6. Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 Index 0 1 2 3 4 5 6 7 8 Pattern a a b a a b a a a LPS Array 0 1 0 1 2 3 4 5 2
  • 8. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 lps[0] initialized to zero
  • 9. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 len=0, i=1 pat[i] != pat[len] Since len=0, lps[i] = 0 i++
  • 10. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 len=0, i=2 pat[i] == pat[len] lps[i] = len + 1 i++, len++
  • 11. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 len=1, i=3 pat[i] == pat[len] lps[i] = len + 1 i++, len++
  • 12. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 len=2, i=4 pat[i] == pat[len] lps[i] = len + 1 i++, len++
  • 13. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 len=3, i=5 pat[i] != pat[len] len=lps[len-1]
  • 14. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 len=1, i=5 pat[i] != pat[len] len=lps[len-1]
  • 15. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 len=0, i=5 pat[i] != pat[len] Since len=0, lps[i]=0 i++
  • 16. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 len=0, i=6 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 17. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 len=1, i=7 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 18. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 len=2, i=8 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 19. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 len=3, i=9 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 20. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 len=4, i=10 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 21. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 len=5, i=11 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 22. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 len=6, i=12 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 23. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 len=7, i=13 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 24. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 len=8, i=14 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 25. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 len=9, i=15 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 26. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 len=10, i=16 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 27. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 len=11, i=17 pat[i] != pat[len] len=lps[len-1]
  • 28. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 len=5, i=17 pat[i] != pat[len] len=lps[len-1]
  • 29. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Pattern a c a c a b a c a c a b a c a c a c LPS 0 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 4 len=3, i=17 pat[i] == pat[len] lps[i]=len+1 len++, i++
  • 31. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j start
  • 32. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j start
  • 33. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 Now, since Pattern[j] doesn’t match with String[i], j=LPS[j-1], start=i-LPS[j-1]. i j start
  • 34. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j Pattern[j] is still not equal to String[i].And since j=0, we increment ‘i’ and ‘start’ this time. start
  • 35. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j start
  • 36. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j Again String[i]!=Pattern[j].Therefore j=LPS[j-1]. start=i-LPS[j-1]. start
  • 37. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j start
  • 38. Index 0 1 2 3 4 5 6 7 8 9 10 11 String a b x a b c a b c a b y Index 0 1 2 3 4 5 Pattern a b c a b y LPS Array 0 0 0 1 2 0 i j start We reached the end of the pattern. Hence the pattern is found at index ‘start’.