CS331 Advanced Data Structures – Spring 2016 Due: April 27

advertisement
CS331 Advanced Data Structures – Spring 2016
Homework 9 – 45 points
Due: April 27
1. (15 points) Assume we perform the following union operations on a disjoint set: union(A,B),
union(C,A), union(D,E), union(F,G), union(C,E), union(B,H), union(I,J), union(G,A),
union(I,A).
(a) Draw the resulting up-tree if unions are performed by having the second tree become
the child of the first tree.
(b) Draw the resulting up tree if union-by size is used, with ties being broken by using the
rule in a).
(c) Show the result of perfoming a find(A) operation on the first tree, using path compression.
2. (15 points) Suppose we are using the Rabin-Karp algorithm to search for the first four letters
of your last name in a text consisting of lowercase letters.
(a) Determine the hash value for the first four letters of your last name using the alphabet
{a,b,c,...,z} where a = 0, b = 1, etc., using a modulus of 23. Recall that for my
name the calculation performed would be (263 (’b’) + 262 (’o’) + 26(’n’) + ’o’) mod 23.
(b) Find an 8 character string starting with abc which leads to the maximum number of
false hits when searching for the first four letters of your last name.
(c) Find an 8 character string that has 4 false hits before successfully finding the pattern
consisting of the first four letters of your last name.
3. (10 points)
(a) Determine the prefix values for the string B = abcaabcaabc.
(b) Determine the total number of comparisons used in the KMP algorithm when looking
for the first appearance B in the string abcabcaabcabcaabcaababcaabcaabc. Compare
this to the total number of comparisons used in the brute force algorithm.
4. (5 points) In our proof that the KMP if O(n) we argued that the maximum number of times
that either the index i of the larger string of length n is incremented or the smaller pattern
of length m is shifted is 2n − m (if we count initializing i = 0 as the first increment). Give
a general example of a string of length n and a pattern of length m where this maximum is
achieved and prove your answer.
Download