finsolgg

# Answer the worst case is the case with the maximum

This preview shows pages 4–7. Sign up to view the full content.

Answer: The worst-case is the case with the maximum number of collisions. With double-hashing this happens if all keys hash to the same value with both hash functions. For our two hash functions, this will happen if all keys are congruent mod 7 · 3 = 21. So, for example, we can take 63 , 21 , 42 , 84 For each of these inputs, f hashes to 0 and g to 1. There are 0 + 1 + 2 + 3 = 6 collisions with this sequence of insertions. Afterwards, the table contains ------------------------------------ | | | | | | | | | 63 | 21 | 42 | 84 | | | | 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
| | | | | | | | ------------------------------------ index 0 1 2 3 4 5 6 Grading guidelines: 5pts for just understanding open addressing. 12pts for also understanding double hashing but getting an incorrect answer (for example an answer with fewer than 6 collisions). 7. (25pts) Consider the method foo shown to the right. (a) Analyze this code and write a recurrence relation for the running time T ( n ) of foo where n = c.length Explain your analysis. (b) Solve the recurrence relation and thus give a Big-Oh char- acterization of the running time of foo . static void foo(char[] c) { int n= c.length; if (n<2) return; char[] A = new char[n/2]; for (int i = 0; i < n/2; i++) A[i] = c[i]; foo(A); char[] B = new char[n/2]; for (int j = 0; j < n/2; j++) B[j] = c[n-1-j]; foo(B); for (int k = 0; k < n/2; k++) { c[k] = A[k]; c[n-1-k] = B[k]; } } Answer: (a) Let T ( n ) be the running time of foo(c) where n is c.length . For n = 1 the running time is constant, T (1) = c 1 . Otherwise, the array creation statements take constant time and the bodies of the three loops also take constant time. Each of the loops iterates n/ 2 times therefore each of the loops takes time O ( n ). In addition we have two recursive calls to foo each on an array of size n/ 2. This leads to the recurrence relation T ( n ) = 2 T ( n/ 2) + c 2 n + c 3 where c 2 n + c 3 accounts for the parts that are O ( n ). Here c 1 ,c 2 ,c 3 are some constants (as usual their exact value is ignored). Moreover, as we have justiﬁed in class, solutions that have c 3 = 0 are OK also. Answer: (b) One of several ways to solve the recurrence relation: deﬁne S ( m ) = T (2 m ), therefore S (0) = T (1) = c 1 and take also c 3 = 0 as we have explained and rewrite the recurrence relation as S ( m ) = 2 S ( m - 1) + c 2 2 m 5
Now write S ( m ) = 2 S ( m - 1) + c 2 2 m S ( m - 1) = 2 S ( m - 2) + c 2 2 m - 1 ··· ··· ··· S (1) = 2 S (0) + c 2 2 1 Now multiply the second equation with 2, the third with 2 2 , ..., the m ’th (and last one with 2 m - 1 ) add all left and right sides of the equations, cancel the same terms and you get; S ( m ) = 2 m c 1 + c 2 m 2 m Since m = log n we get T ( n ) = S (log n ) = nc 1 + c 2 n log n . Therefore

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page4 / 11

Answer The worst case is the case with the maximum number...

This preview shows document pages 4 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online