What is the best and worst case of this algorithm? Explain with an example for each case, without going into mathematical details

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

The code shows an implementation of the Rabin-Karp algorithm in Python. What is the best and worst case of this algorithm? Explain with an example for each case, without going into mathematical details

# Look for the occurrence of the pattern (leftmost) [0
# [0
# and thus quickly discard chains that do not correspond.
def rabin_karp(text, pattern):
... m -1] in the text
i +m -1]
n -1] using a hash function to test on each substring [i
...
...
Len(text)
Len(pattern)
n =
m =
P_hash =
t_hash
hashFunction(pattern, m)
hashFunction(text, m)
%3D
i = 0
while i <= n-m:
if p_hash ==t_hash:
found = True
# No matching no detected yet
# check individual characters
j = 0
while j < m and found:
if text[i+j] != pattern[j]: # # If no matching occurs, the inner loop ends
found = False
j += 1
if found:
# the loop ends with a no matching, so the substring starts at text[i]
return i
if i+m <= n-1:
# A matching is attempted with text[i+1 ... i+m]
t_hash = updateHashFunction (t_hash, text[i], text[i+m])
i += 1
return -1
# No matching found
# Returns the sum of the ASCII values of the characters in [0
def hashFunction(string, m):
i = 0
m-1]
...
sum = 0
while i < m:
sum += ord(string[i])
i += 1
return sum
# Update the hash function based on the previous
# value of the character that is no longer being taken into account
def updateHashFunction(previous_hash, old, new):
return previous_hash
ord(old) + ord(new)
('r',
's', 'i',
'c', 'o', 'n'
('a', 'm',"
text =
'o'
in
pattern
r')
index = rabin_karp(text, pattern)
print(index)
Transcribed Image Text:# Look for the occurrence of the pattern (leftmost) [0 # [0 # and thus quickly discard chains that do not correspond. def rabin_karp(text, pattern): ... m -1] in the text i +m -1] n -1] using a hash function to test on each substring [i ... ... Len(text) Len(pattern) n = m = P_hash = t_hash hashFunction(pattern, m) hashFunction(text, m) %3D i = 0 while i <= n-m: if p_hash ==t_hash: found = True # No matching no detected yet # check individual characters j = 0 while j < m and found: if text[i+j] != pattern[j]: # # If no matching occurs, the inner loop ends found = False j += 1 if found: # the loop ends with a no matching, so the substring starts at text[i] return i if i+m <= n-1: # A matching is attempted with text[i+1 ... i+m] t_hash = updateHashFunction (t_hash, text[i], text[i+m]) i += 1 return -1 # No matching found # Returns the sum of the ASCII values of the characters in [0 def hashFunction(string, m): i = 0 m-1] ... sum = 0 while i < m: sum += ord(string[i]) i += 1 return sum # Update the hash function based on the previous # value of the character that is no longer being taken into account def updateHashFunction(previous_hash, old, new): return previous_hash ord(old) + ord(new) ('r', 's', 'i', 'c', 'o', 'n' ('a', 'm'," text = 'o' in pattern r') index = rabin_karp(text, pattern) print(index)
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY