COMP 2119: Solution For Assign-Ment 2
COMP 2119: Solution For Assign-Ment 2
ment 2
1
(a) [9%] Consider inserting keys 15, 24, 30, 59, 46, 75, 44, 17 into a hash ta-
ble of size m = 13 using open addressing with the primary hash function
h0 (k) = k mod m. Show the result of inserting these keys using (i) lin-
ear probing; (ii) quadratic probing: h(k, i) = (h0 (k) + 1/2(i + i2 )) mod
m; and (iii) double hashing: h(k, i) = (h0 (k) + ih2 (k)) mod m, where
h2 (k) = (k mod 11) + (k mod 3).
(b) [16%] For each of the following hash function, prove or disprove if the
resulting sequence of probes is always a permutation of 0, 1, , m − 1, where
h0 (k) = k mod m and m = 2p for some positive integer p.
(i) h(k, i) = (h0 (k) + 3i) mod m.
(ii) h(k, i) = (h0 (k) + 1/2(i + i2 )) mod m.
1
17
15 15 15
30 30 30
44 44 44
17 75
59 59 59
46 46
75 75 46
24 24 24
17
• 2m can divide (i − i0 );
2
the resulting sequence of probes is always a permutation of 0, 1, . . . , m − 1.
2
(a) [5%] Insert the following numbers into an initially empty AVL tree one
by one. Show the resulting tree after each insertion (including the balanced
factor using the notation in the lecture slide). Then, delete 118, 140. Show
also the resulting tree (with the balance factor) after each deletion.
Solution
a) See Figure Q2 a).
b)
(i) False. See figure Q2 b)
(ii) False. See figure Q2 b) (ii)
(iii) False. See figure Q2 b)
(iv) True. We design the algorithm as follows. For a preorder traversal
array a1 , . . . , an , firstly, we set the root is a1 . We find the first element ai ,
where ai > a1 . Then a2 , . . . , ai−1 should in the left subtree and ai , . . . , an
3
Figure 2: Q2 a)
4
Figure 3: Q2 b)
Figure 4: Q2 b) (ii)
5
should in the right subtree. Using this idea, we can design a recursive
algorithm. The time complexity is O(n2 ). If you use binary search to find
the element that ai > al , the time complexity can be O(n log n)
Algorithm 1 Q2 b) (iv)
1: procedure solve(array A, int l, int r)
2: if l > r then
3: return
4: end if
5: Node x
6: x.value = al
7: for i from l + 1 to r do
8: if ai > al then
9: break;
10: end if
11: end for
12: x.lef t = solve(l + 1, i − 1)
13: x.right = solve(i, r)
14: return x
15: end procedure
16: procedure main(array A)
17: Node root = solve(1, n)
18: end procedure
3
Consider the average case analysis for binary search. Let A[1..n] be the
given array of sorted distinct numbers and let n = 2k for some integer k.
Consider a successful search (i.e., the number you want to search exists in
the array A and there are only n possible cases for input).
(a) [4%] How many case(s) requiring only one comparison to locate the
given number? How many cases requiring exactly two comparisons to locate
the given number?
6
(b) [6%] How many case(s) requiring exactly i comparisons to locate the
given number (1 ≤ i ≤ k).
(c) [10%] Hence, or otherwise, compute the average case complexity for
a successful search in binary search assuming that all n cases are equally
likely
Solution
a. Only one case requires only one comparison. Two cases require exactly
two comparisons.
b. 2i−1 cases require exactly i comparisons.
c. The average case complexity for a successful search:
1×1+2×2+3×4+···+i×2i−1
n ,
where n = 1 + 2 + 4 + · · · + 2i−1 = 2i − 1 , i = log(n + 1).
1×1+2×2+3×4+···+i×2i−1
n
(1+2+4+···+2i−1 )+(2+4+···+2i−1 )+(4+···+2i−1 )+···+(2i−1 )
= 2i −1
(2i −1)+(2i −2)+2i −4+···+(2i −2i−1 )
= 2i −1
i i +1
= i·22−2
i −1 = i − 1 + 2ii−i
= O(log n)
4
(a) [10%] Insert the following numbers into an initially empty min-heap one
by one. Show the resulting heap after each insertion. Show the resulting
heaps after executing the Extract-Min operation twice.
198, 167, 132, 158, 202, 146, 177, 41, 148, 212, 136
(b) [10%] Given n distinct numbers (not sorted), design an O(n + k log n)
algorithm to retrieve the k smallest numbers.
Solution: (a)the procedure is shown in 5 and 6
(b)Given n distinct numbers, we can do the following steps to retrieve
the k smallest values.
7
198 167 132 132 132
158 167
167 158 167
198 198
198
198 202
132 132 41
198
41 41 41
132 146
132 146 132 146
177
148 202 167 177 148 202 167 177 148 136 167
8
132 136
177 177
148 202 167 158 202 167