Assignment 1: TH TH
Assignment 1: TH TH
Q1.
Q2.
a. The algorithm is as follows:
Create a min heap of size k (number of lists) containing the first element from
each of the k lists. Extract the minimum (root node) from this min heap and
replace it with the next element of the array from which the root node was taken.
Repeat this m times until we get the smallest m elements(or in the special case,
all the elements in all the lists).
This algorithm works since it stores only k elements at a time in the heap (initially the first
element from all the lists). Then it will keep replacing the root node (minimum element)
with the next one from it's respective list until we have all m items (this is done using a
for loop from 1 to m). Hence the overall heap is in ascending order (because we will call
Insert which will ensure the heap is ordered correctly and later when extracting the
minimum, it will also reorder the heap with the minimum element at the root node) so
is called m
when ExtractMin times, it retrieves the smallest m elements from all the lists.
Q3.
a. For a binomial heap H with size n where (n) is the number of 1s in the binary
representation, (n) is the number of trees in the heap. For every tree Ti where
1 i (n), with size ni the number of edges is ni - 1.
(n)
So for the sum of edges = { ni } - (n) = n - (n)
i=1
b. Referring to the hint given in the question, we need to relate the number of key
comparisons required to the number of extra edges formed in H. We know the number of
comparisons will equal the number of extra edges formed because a comparison only
happens when there are two trees of the same size after an insertion and thus resulting
in one extra edge being formed to merge those tree.
The number of extra edges formed in H:
Initially H has n elements (given in the question). So from part (a), it has n - (n)
edges. After k insertions, the size of H is n + k, so the number of edges is (n+k) -
(n+k) (also from part (a)).
So the number of extra edges formed is:
(n+k) - (n+k) - [n - (n)] = k - (n+k) + (n)
<= k + (n) #upperbound by removing
subtraction
Now we know that there will be (at most) k + (n) pairwise comparisons. However, as
stated in part (a), (n) is the number of 1s in the binary representation. Therefore, (n)
<= floor(logn) + 1. Going back to the number of pairwise comparisons, k + (n) <= k +
floor(logn) + 1. However, as stated in the question we assume k > logn, so k + (n) <=
2k + 1 = O(k). Since k is a constant, the worst-case total cost of the k successive
insertions divided by k is bounded above by a constant.