SlideShare a Scribd company logo
1 of 28
Design and analysis of
algorithms
Prepared by: Aoun-Haider
FA21-BSE-133@cuilahore.edu.pk
Rabin Karp String Matching Algorithm:
• This algorithm has two basic versions.
• Take a hash function and generate a hash code and compare the code with
target string
• Complexity depends upon hash function we take
• Version#01: Take sum of weights assigned to each character and compare
sum with each possible pair of target string. If size of input string is ‘n’, pair
size will also be ‘n’ in the target string.
• Weights assigned can be user defined or based on ascii value of character
• Version#02: take a radix(base) and convert number into that base. For
example, if 26 alphabets are in the comparison then 26 will be base. If ascii
based comparison is taken, then 256 will be base.
Example: (Version#01)
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
a a a a a b
a a b
Input_str:
Hash_Function(a,a,b) = 1 + 1 +2 => 3
Character weights:
n = size of original string
m = size of input string
s = size of total characters in a table
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
a a a a a b
a a b
Input_str:
Hash_Function(a,a,b) = 1 + 1 +2 => 4
Hash_Function(a,a,a) = 1 + 1 +1 => 3
3 <> 4 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
a a a a a b
a a b
Input_str:
Hash_Function(a,a,b) = 1 + 1 +2 => 4
Hash_Function(a,a,a) = 1 + 1 +1 => 3
3 <> 4 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
a a a a a b
a a b
Input_str:
Hash_Function(a,a,b) = 1 + 1 +2 => 4
Hash_Function(a,a,a) = 1 + 1 +1 => 3
3 <> 4 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
a a a a a b
a a b
Input_str:
Hash_Function(a,a,b) = 1 + 1 +2 => 4
Hash_Function(a,a,a) = 1 + 1 +2 => 4
4 == 4 -> compare both strings
So, String exist.
O( n – m +1)
Worst Case:
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 3 + 3 +1 => 7
7 == 7 -> compare both strings
Not Equal -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 3 + 1 +3 => 7
7 == 7 -> compare both strings
Not Equal -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 1 + 3 +3 => 7
7 == 7 -> compare both strings
Not Equal -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 3 + 3 +1 => 7
7 == 7 -> compare both strings
Not Equal -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 3 + 1 +1 => 5
5 <> 7 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 1 + 1 +5 => 7
7 == 7 -> compare both strings
Not Equal -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 1 + 5 +4 => 10
10 <> 7 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 5 + 4 +2 => 11
11 <> 7 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
Input_str: b d a
Hash_Function(b,d,a) = 2 + 4 +1 => 7
Hash_Function(a,a,a) = 4 + 2 +1 => 7
7 == 7 -> compare both strings
Not Equal -> String not found!!
≈ O(nm)
Version#02:
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(c1,c2,…,cn) = P[0] x sm-1 + P[1] x sm-2 + P[2] x sm-3
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
m = 3
s = 10
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(c,c,a) = 3 x 102 + 3 x 101 +1 x 100=> 331
331 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(c,a,a) = 3 x 102 + 1 x 101 +3 x 100=> 313
OR [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 3 x 100
[[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 3
313 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(a,c,c) = 1 x 102 + 3 x 101 +3 x 100=> 133
OR [[3 x 102 + 1 x 101 +3 x 100] – 3 x 102 ] x 3 x 100
[[3 x 102 + 1 x 101 +3 x 100] – 3 x 102 ] x 3
133 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(c,c,a) = 3 x 102 + 3 x 101 +1 x 100=> 331
OR [[1 x 102 + 3 x 101 +3 x 100] – 3 x 102 ] x 1 x 100
[[1 x 102 + 3 x 101 +3 x 100] – 3 x 102 ] x 1
331 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(c,a,a) = 3 x 102 + 1 x 101 +1 x 100=> 331
OR [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 1 x 100
[[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 1
331 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(a,a,e) = 1 x 102 + 1 x 101 +5 x 100=> 115
OR [[3 x 102 + 1 x 101 +1 x 100] – 3 x 102 ] x 5 x 100
[[3 x 102 + 1 x 101 +1 x 100] – 3 x 102 ] x 5
115 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(a,a,e) = 1 x 102 + 5 x 101 +4 x 100=> 154
OR [[1 x 102 + 1 x 101 +5 x 100] – 3 x 102 ] x 4 x 100
[[1 x 102 + 1 x 101 +5 x 100] – 3 x 102 ] x 4
154 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(a,a,e) = 5 x 102 + 4 x 101 +2 x 100=> 542
OR [[1 x 102 + 5 x 101 +4 x 100] – 1 x 102 ] x 2 x 100
[[1 x 102 + 5 x 101 +4 x 100] – 1 x 102 ] x 2
542 <> 421 -> move next
Cont.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
c c a c c a a e d b a
b d a
Input_str:
Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421
Hash_Function(a,a,e) = 4 x 102 + 2 x 101 +1 x 100=> 421
OR [[5 x 102 + 4 x 101 +2 x 100] – 1 x 102 ] x 1 x 100
[[5 x 102 + 4 x 101 +2 x 100] – 1 x 102 ] x 1
421 == 421 -> Compare string -> String not found!!
O(n-m+1)
Comments:
• Complexity of rabin karp algorithm depends upon hash function we
choose.
• First version has problem of getting same sum multiple time and
requires a lot of comparisons which takes O(nm)
• The 2nd version still arise a problem. If a 32-bit integer is whose
system limit and number generate is large enough or exceed from 32
bits can cause spurious hits.
• Solution: We can also take mod of the hash function to get little
number with 231 because range is 0-31.

More Related Content

Similar to 10b- Rabin Karp String Matching Problem.pptx

An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
Pembahasan Soal Matematika Kelas 10 Semester 1
Pembahasan Soal Matematika Kelas 10 Semester 1Pembahasan Soal Matematika Kelas 10 Semester 1
Pembahasan Soal Matematika Kelas 10 Semester 1Pillar Adhikusumah
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problemsMake Mannan
 
All I Needed for Functional Programming I Learned in High School Algebra
All I Needed for Functional Programming I Learned in High School AlgebraAll I Needed for Functional Programming I Learned in High School Algebra
All I Needed for Functional Programming I Learned in High School AlgebraEric Normand
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monadskenbot
 
2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-donejendailey
 
matrices and determinantes
matrices and determinantes matrices and determinantes
matrices and determinantes gandhinagar
 
determinants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdfdeterminants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdfTGBSmile
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxjacksnathalie
 
Stacks image 1721_36
Stacks image 1721_36Stacks image 1721_36
Stacks image 1721_36Dreams4school
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationJaneAlamAdnan
 

Similar to 10b- Rabin Karp String Matching Problem.pptx (20)

An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Pembahasan Soal Matematika Kelas 10 Semester 1
Pembahasan Soal Matematika Kelas 10 Semester 1Pembahasan Soal Matematika Kelas 10 Semester 1
Pembahasan Soal Matematika Kelas 10 Semester 1
 
Lec ty
Lec tyLec ty
Lec ty
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problems
 
All I Needed for Functional Programming I Learned in High School Algebra
All I Needed for Functional Programming I Learned in High School AlgebraAll I Needed for Functional Programming I Learned in High School Algebra
All I Needed for Functional Programming I Learned in High School Algebra
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done
 
matrices and determinantes
matrices and determinantes matrices and determinantes
matrices and determinantes
 
Ruby 1.9
Ruby 1.9Ruby 1.9
Ruby 1.9
 
determinants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdfdeterminants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdf
 
determinants-160504230830.pdf
determinants-160504230830.pdfdeterminants-160504230830.pdf
determinants-160504230830.pdf
 
Modeling quadratic fxns
Modeling quadratic fxnsModeling quadratic fxns
Modeling quadratic fxns
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
FUNCTIONS L.1.pdf
FUNCTIONS L.1.pdfFUNCTIONS L.1.pdf
FUNCTIONS L.1.pdf
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
 
Puanumrahdalimon
PuanumrahdalimonPuanumrahdalimon
Puanumrahdalimon
 
Stacks image 1721_36
Stacks image 1721_36Stacks image 1721_36
Stacks image 1721_36
 
New stack
New stackNew stack
New stack
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain Multiplication
 

More from AOUNHAIDER7

10a String Matching(KMP).pptx
10a String Matching(KMP).pptx10a String Matching(KMP).pptx
10a String Matching(KMP).pptxAOUNHAIDER7
 
Travelling salesman problem(DAA).pdf
Travelling salesman problem(DAA).pdfTravelling salesman problem(DAA).pdf
Travelling salesman problem(DAA).pdfAOUNHAIDER7
 
10c -Articulation_point.pptx
10c -Articulation_point.pptx10c -Articulation_point.pptx
10c -Articulation_point.pptxAOUNHAIDER7
 
Computer Graphics Interview Questions.pdf
Computer Graphics Interview Questions.pdfComputer Graphics Interview Questions.pdf
Computer Graphics Interview Questions.pdfAOUNHAIDER7
 
Computer Graphics Notes 2.pdf
Computer Graphics Notes 2.pdfComputer Graphics Notes 2.pdf
Computer Graphics Notes 2.pdfAOUNHAIDER7
 
Computer Graphics Notes.pdf
Computer Graphics Notes.pdfComputer Graphics Notes.pdf
Computer Graphics Notes.pdfAOUNHAIDER7
 
Computer Graphics Full Tutorial.pptx
Computer Graphics Full Tutorial.pptxComputer Graphics Full Tutorial.pptx
Computer Graphics Full Tutorial.pptxAOUNHAIDER7
 

More from AOUNHAIDER7 (8)

10a String Matching(KMP).pptx
10a String Matching(KMP).pptx10a String Matching(KMP).pptx
10a String Matching(KMP).pptx
 
Travelling salesman problem(DAA).pdf
Travelling salesman problem(DAA).pdfTravelling salesman problem(DAA).pdf
Travelling salesman problem(DAA).pdf
 
10c -Articulation_point.pptx
10c -Articulation_point.pptx10c -Articulation_point.pptx
10c -Articulation_point.pptx
 
Recurrences.pdf
Recurrences.pdfRecurrences.pdf
Recurrences.pdf
 
Computer Graphics Interview Questions.pdf
Computer Graphics Interview Questions.pdfComputer Graphics Interview Questions.pdf
Computer Graphics Interview Questions.pdf
 
Computer Graphics Notes 2.pdf
Computer Graphics Notes 2.pdfComputer Graphics Notes 2.pdf
Computer Graphics Notes 2.pdf
 
Computer Graphics Notes.pdf
Computer Graphics Notes.pdfComputer Graphics Notes.pdf
Computer Graphics Notes.pdf
 
Computer Graphics Full Tutorial.pptx
Computer Graphics Full Tutorial.pptxComputer Graphics Full Tutorial.pptx
Computer Graphics Full Tutorial.pptx
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

10b- Rabin Karp String Matching Problem.pptx

  • 1. Design and analysis of algorithms Prepared by: Aoun-Haider FA21-BSE-133@cuilahore.edu.pk
  • 2. Rabin Karp String Matching Algorithm: • This algorithm has two basic versions. • Take a hash function and generate a hash code and compare the code with target string • Complexity depends upon hash function we take • Version#01: Take sum of weights assigned to each character and compare sum with each possible pair of target string. If size of input string is ‘n’, pair size will also be ‘n’ in the target string. • Weights assigned can be user defined or based on ascii value of character • Version#02: take a radix(base) and convert number into that base. For example, if 26 alphabets are in the comparison then 26 will be base. If ascii based comparison is taken, then 256 will be base.
  • 3. Example: (Version#01) a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 a a a a a b a a b Input_str: Hash_Function(a,a,b) = 1 + 1 +2 => 3 Character weights: n = size of original string m = size of input string s = size of total characters in a table
  • 4. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 a a a a a b a a b Input_str: Hash_Function(a,a,b) = 1 + 1 +2 => 4 Hash_Function(a,a,a) = 1 + 1 +1 => 3 3 <> 4 -> move next
  • 5. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 a a a a a b a a b Input_str: Hash_Function(a,a,b) = 1 + 1 +2 => 4 Hash_Function(a,a,a) = 1 + 1 +1 => 3 3 <> 4 -> move next
  • 6. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 a a a a a b a a b Input_str: Hash_Function(a,a,b) = 1 + 1 +2 => 4 Hash_Function(a,a,a) = 1 + 1 +1 => 3 3 <> 4 -> move next
  • 7. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 a a a a a b a a b Input_str: Hash_Function(a,a,b) = 1 + 1 +2 => 4 Hash_Function(a,a,a) = 1 + 1 +2 => 4 4 == 4 -> compare both strings So, String exist. O( n – m +1)
  • 8. Worst Case: a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7
  • 9. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 3 + 3 +1 => 7 7 == 7 -> compare both strings Not Equal -> move next
  • 10. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 3 + 1 +3 => 7 7 == 7 -> compare both strings Not Equal -> move next
  • 11. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 1 + 3 +3 => 7 7 == 7 -> compare both strings Not Equal -> move next
  • 12. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 3 + 3 +1 => 7 7 == 7 -> compare both strings Not Equal -> move next
  • 13. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 3 + 1 +1 => 5 5 <> 7 -> move next
  • 14. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 1 + 1 +5 => 7 7 == 7 -> compare both strings Not Equal -> move next
  • 15. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 1 + 5 +4 => 10 10 <> 7 -> move next
  • 16. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 5 + 4 +2 => 11 11 <> 7 -> move next
  • 17. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a Input_str: b d a Hash_Function(b,d,a) = 2 + 4 +1 => 7 Hash_Function(a,a,a) = 4 + 2 +1 => 7 7 == 7 -> compare both strings Not Equal -> String not found!! ≈ O(nm)
  • 18. Version#02: a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(c1,c2,…,cn) = P[0] x sm-1 + P[1] x sm-2 + P[2] x sm-3 Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 m = 3 s = 10
  • 19. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(c,c,a) = 3 x 102 + 3 x 101 +1 x 100=> 331 331 <> 421 -> move next
  • 20. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(c,a,a) = 3 x 102 + 1 x 101 +3 x 100=> 313 OR [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 3 x 100 [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 3 313 <> 421 -> move next
  • 21. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(a,c,c) = 1 x 102 + 3 x 101 +3 x 100=> 133 OR [[3 x 102 + 1 x 101 +3 x 100] – 3 x 102 ] x 3 x 100 [[3 x 102 + 1 x 101 +3 x 100] – 3 x 102 ] x 3 133 <> 421 -> move next
  • 22. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(c,c,a) = 3 x 102 + 3 x 101 +1 x 100=> 331 OR [[1 x 102 + 3 x 101 +3 x 100] – 3 x 102 ] x 1 x 100 [[1 x 102 + 3 x 101 +3 x 100] – 3 x 102 ] x 1 331 <> 421 -> move next
  • 23. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(c,a,a) = 3 x 102 + 1 x 101 +1 x 100=> 331 OR [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 1 x 100 [[3 x 102 + 3 x 101 +1 x 100] – 3 x 102 ] x 1 331 <> 421 -> move next
  • 24. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(a,a,e) = 1 x 102 + 1 x 101 +5 x 100=> 115 OR [[3 x 102 + 1 x 101 +1 x 100] – 3 x 102 ] x 5 x 100 [[3 x 102 + 1 x 101 +1 x 100] – 3 x 102 ] x 5 115 <> 421 -> move next
  • 25. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(a,a,e) = 1 x 102 + 5 x 101 +4 x 100=> 154 OR [[1 x 102 + 1 x 101 +5 x 100] – 3 x 102 ] x 4 x 100 [[1 x 102 + 1 x 101 +5 x 100] – 3 x 102 ] x 4 154 <> 421 -> move next
  • 26. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(a,a,e) = 5 x 102 + 4 x 101 +2 x 100=> 542 OR [[1 x 102 + 5 x 101 +4 x 100] – 1 x 102 ] x 2 x 100 [[1 x 102 + 5 x 101 +4 x 100] – 1 x 102 ] x 2 542 <> 421 -> move next
  • 27. Cont. a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 c c a c c a a e d b a b d a Input_str: Hash_Function(b,d,a) = 2x 102 + 4 x 101 +1 x 100=> 421 Hash_Function(a,a,e) = 4 x 102 + 2 x 101 +1 x 100=> 421 OR [[5 x 102 + 4 x 101 +2 x 100] – 1 x 102 ] x 1 x 100 [[5 x 102 + 4 x 101 +2 x 100] – 1 x 102 ] x 1 421 == 421 -> Compare string -> String not found!! O(n-m+1)
  • 28. Comments: • Complexity of rabin karp algorithm depends upon hash function we choose. • First version has problem of getting same sum multiple time and requires a lot of comparisons which takes O(nm) • The 2nd version still arise a problem. If a 32-bit integer is whose system limit and number generate is large enough or exceed from 32 bits can cause spurious hits. • Solution: We can also take mod of the hash function to get little number with 231 because range is 0-31.