0% found this document useful (0 votes)
168 views9 pages

COMP 2119: Solution For Assign-Ment 2

The document provides solutions to several problems related to data structures and algorithms. It includes: 1) Solving problems involving inserting keys into a hash table using different probing functions and proving properties of hash functions. 2) Inserting and deleting keys from an AVL tree, including showing the tree after each operation. 3) Analyzing the average case time complexity of binary search and proving properties of binary search trees. 4) Inserting keys into a min-heap and extracting the minimum element, showing the resulting heap after each operation. 5) Designing an algorithm to retrieve the k smallest elements from a set of n distinct numbers in O(n + k log n) time using a

Uploaded by

Lavabo At Lavabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views9 pages

COMP 2119: Solution For Assign-Ment 2

The document provides solutions to several problems related to data structures and algorithms. It includes: 1) Solving problems involving inserting keys into a hash table using different probing functions and proving properties of hash functions. 2) Inserting and deleting keys from an AVL tree, including showing the tree after each operation. 3) Analyzing the average case time complexity of binary search and proving properties of binary search trees. 4) Inserting keys into a min-heap and extracting the minimum element, showing the resulting heap after each operation. 5) Designing an algorithm to retrieve the k smallest elements from a set of n distinct numbers in O(n + k log n) time using a

Uploaded by

Lavabo At Lavabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMP 2119: Solution for Assign-

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.

The solution to 1(a) is shown in Figure 1


1(b)(i): True.
(Proof of contradiction) Assumption: we assume that h(k, i) = h(k, i0 ) where
i and i0 are different values in {0, 1, . . . , m − 1}. Then we have:
h(k, i) = h(k, i0 )
k + 3i = k + 3i0 mod m (1)
0
3i = 3i mod m
Since m = 2p , we have gcd(3, m) = 1, then we get i = i0 mod m, which
is contradictory to our assumption. Thus, the assumption is invalid. In
other words, the resulting sequence of probes is always a permutation of
0, 1, . . . , m − 1.
1(b)(ii):True.
(Proof of contradiction) Assumption: we assume that h(k, i) = h(k, i0 ) where
i and i0 are different values in {0, 1, . . . , m − 1}. Then we have:
h(k, i) = h(k, i0 )
2
k + 1/2(i + i2 ) = k + 1/2(i0 + i0 ) mod m (2)
0 0
1/2(i − i )(i + i + 1) = 0 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

(i) (ii) (iii)

Figure 1: solution to 1(a).

Since m = 2p , we have (i − i0 )(i + i0 + 1) = 0 mod 2m. In addition,


(i − i0 ) + (i + i0 + 1) = 2i + 1, which is odd, so (i − i0 ) and (i + i0 + 1)
can not be even at the same time. In other words, only one of the followings
is valid:

• 2m can divide (i − i0 );

• 2m can divide (i + i0 + 1).

Since i, i0 ∈ {0, 1, . . . , m − 1}, so (i + i0 + 1) ∈ {0, 1, . . . , 2m − 1}, which


shows that 2m can not divide (i + i0 + 1). Thus, only the other case is valid.
However, 2m can divide (i − i0 ) when and only when i = i0 , which is contra-
dictory to our assumption. Thus, the assumption is invalid. In other words,

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.

58, 45, 32, 140, 77, 116, 118, 63

(b) [20%] Consider each of the following cases. If it is correct, give a


proof for (i) and (ii); provide an algorithm to reconstruct the tree for (iii)
and (iv). If it is incorrect, give a counter-example.
(i) In a binary search tree, if node u has two children, its predecessor
has no left child.
(ii) In a binary search tree, if node u has one child, its predecessor has
no right child.
(iii) Given the preorder and postorder traversal of a binary tree, we can
uniquely determine the topology of the binary tree (i.e, there does NOT exist
two different binary trees with the same preorder and postorder traversals).
(iv) Given a preorder traversal of a binary search tree, we can uniquely
determine the topology of the tree.
(c) [5%] If we modify the definition of AVL tree as follows. A modified-
AVL tree is a binary search tree such that for every node, the height of its
left subtree and the height of its right subtree differ by at most 2. Prove or
disprove that the height of a modified-AVL tree of n nodes is bounded by
O(log n).

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

(c) Proof: We assume the height of modified-AVL tree is h, now, consider


the total number of nodes in the modified-AVL tree. The minimum number
of nodes in the AVL tree is n ≥ F (h) = F (h − 1) + F (h − 3) > 2F (h − 3) >
22 F (h − 6) > 2h/3 F (0) = 2h/3 . So we can get log n > h/3 from n > 2h/3 .
So n = O(log n)

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.

1. First, build a min-heap bottom-up with complexity O(n),


2. then execute k times of Extract-Min which run in O(log n) time for
each Extract-Min including the Min-heapify operation.
To sum up, the total time to retrieve the k smallest numbers if O(n +
k log n) time.

7
198 167 132 132 132

158 167
167 158 167
198 198
198
198 202
132 132 41

158 146 158 146 132 146

158 202 167 177


198 202 167 198 202 167 177

198
41 41 41

132 146
132 146 132 146

177
148 202 167 177 148 202 167 177 148 136 167

198 158 198 158 212 198 158 212 202

Figure 5: solution to 4(a).

8
132 136

136 146 148 146

177 177
148 202 167 158 202 167

198 158 212 198 212

extract 41 extract 132

Figure 6: solution to 4(a).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy