Data Structures & Algorithms Multiple Choice Questions on “Rabin-Karp Algorithm”.
1. What is a Rabin and Karp Algorithm? Answer: a 2. What is the pre-processing time of Rabin and Karp Algorithm? 3. Rabin Karp Algorithm makes use of elementary number theoretic notions. Answer: a 4. What is the basic formula applied in Rabin Karp Algorithm to get the computation time as Theta(m)? Answer: b 5. What is the worst case running time of Rabin Karp Algorithm? Answer: c 6. Rabin- Karp algorithm can be used for discovering plagiarism in a sentence. Answer: a 7. If n is the length of text(T) and m is the length of the pattern(P) identify the correct pre-processing algorithm. (where q is a suitable modulus to reduce the complexity) b) c) d) Answer: d 8. If n is the length of text(T) and m is the length of the pattern(P) identify the correct matching algorithm. b) c) d) Answer: b 9. What happens when the modulo value(q) is taken large? 10. Given a pattern of length-5 window, find the suitable modulo value. a) 13 11. Given a pattern of length- 5 window, find the valid match in the given text. a) 11-16 Answer: c 12. Given a pattern of length- 5 window, find the spurious hit in the given text string. a) 6-10 Answer: d 13. If the expected number of valid shifts is small and modulus is larger than the length of pattern what is the matching time of Rabin Karp Algorithm? Answer: b 14. What is the basic principle in Rabin Karp algorithm? Answer: a 15. Who created the Rabin Karp Algorithm? Answer: c
a) String Matching Algorithm
b) Shortest Path Algorithm
c) Minimum spanning tree Algorithm
d) Approximation Algorithm
Clarification: The string matching algorithm which was proposed by Rabin and Karp, generalizes to other algorithms and for two-dimensional pattern matching problems.
a) Theta(m2)
b) Theta(mlogn)
c) Theta(m)
d) Big-Oh(n)
Answer: c
Clarification: The for loop in the pre-processing algorithm runs for m(length of the pattern) times. Hence the pre-processing time is Theta(m).
a) True
b) False
Clarification: Rabin Karp Algorithm makes use of elementary theoretic number notions such as the equivalence of two numbers modulo a third number.
a) Halving rule
b) Horner’s rule
c) Summation lemma
d) Cancellation lemma
Clarification: The pattern can be evaluated in time Theta(m) using Horner’s rule:
p = P[m] + 10(P[m-1] + 10(P[m-2] +…+ 10(P[2]+10P[1])…)).
a) Theta(n)
b) Theta(n-m)
c) Theta((n-m+1)m)
d) Theta(nlogm)
Clarification: The worst case running time of Rabin Karp Algorithm is Theta(n-m+1)m). We write Theta(n-m+1) instead of Theta(n-m) because there are n-m+1 different values that the given text takes on.
a) True
b) False
Clarification: Since Rabin-Karp algorithm is a pattern detecting algorithm in a text or string, it can be used for detecting plagiarism in a sentence.
p=0; t0=0;
a)for i=1 to n
do t0=(dt0 + P[i])mod q
p=(dp+T[i])mod q
for i=1 to n
do p=(dp + P[i])mod q
t0=(dt0+T[i])mod q
for i=1 to m
do t0=(dp + P[i])mod q
p=(dt0+T[i])mod q
for i=1 to m
do p=(dp + P[i])mod q
t0=(dt0+T[i])mod q
Clarification: The pre-processing algorithm runs m (the length of pattern) times. This algorithm is used to compute p as the value of P[1….m] mod q and t0 as the value of T[1….m]mod q.
a)for s=0 to n
do if p=t0
then if P[1..m]=T[s+1..s+m]
then print “Pattern occurs with shift” s
for s=0 to n-m
do if p=ts
then if P[1..m]=T[s+1..s+m]
then print “Pattern occurs with shift” s
for s=0 to m
do if p=ts
then if P[1..m]=T[s+1..s+m]
then print “Pattern occurs with shift” s
for s=0 to n-m
do if p!=ts
then if P[1..m]=T[s+1..s+m]
then print “Pattern occurs with shift” s
Clarification: The matching algorithm runs for n-m times. Rabin Karp algorithm explicitly verifies every valid shift. If the required pattern matches with the given text then the algorithm prints pattern found as result.
a) Complexity increases
b) Spurious hits occur frequently
c) Cost of extra checking is low
d) Matching time increases
Answer: c
Clarification: If the modulo value(q) is large enough then the spurious hits occur infrequently enough that the cost of extra checking is low.
b) 14
c) 12
d) 11
Answer: a
Clarification: The modulus q is typically chosen as a prime number that is large enough to reduce the complexity when p is very large.Pattern: 2 1 9 3 6
Modulus: 21
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Text: 9 2 7 2 1 8 3 0 5 7 1 2 1 2 1 9 3 6 2 3 9 7
b) 3-8
c) 13-18
d) 15-20
View Answer
Clarification: The pattern 2 1 9 3 6 occurs in the text starting from position 13 to 18. In the given pattern value is computed as 12 by having the modulus as 21. The same text string values are computed for each possible position of a 5 length window. Pattern: 3 1 4 1 5
Modulus: 13
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Text: 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1 3 9
b) 12-16
c) 3-7
d) 13-17
View Answer
Clarification: The sub string in the range 13-17, 6 7 3 9 9 produces the same value 7 as the given pattern. But the pattern numbers don’t match with sub string identified, hence it is a spurious hit.
a) Theta(m)
b) Big-Oh(n+m)
c) Theta(n-m)
d) Big-Oh(n)
Clarification: When the number of valid shifts(v) is Big-Oh(1) and q>=m then the matching time is given by O(n)+O(m(v+n/q)) is simplified as O(n+m).
a) Hashing
b) Sorting
c) Augmenting
d) Dynamic Programming
Clarification: The basic principle employed in Rabin Karp algorithm is hashing. In the given text every substring is converted to a hash value and compared with the hash value of the pattern.
a) Joseph Rabin and Michael Karp
b) Michael Rabin and Joseph Karp
c) Richard Karp and Michael Rabin
d) Michael Karp and Richard Rabin
Clarification: Rabin Karp algorithm was invented by Richard Karp and Michael Rabin in the year 1987 for searching a pattern in the given string.