1,395 views
3 votes
3 votes

Suppose we have a heap containing n = 2k elements in an array of size ‘n’, and suppose that we repeatedly extract the minimum element, ‘n’ times, never performing insertions. To make the heap space efficient, we move the heap over to an array of size 2j whenever an extraction decreases the number of elements to 2j for any integer j. Suppose that the cost of each such move is Θ(2j). What is the total movement cost caused by ‘n’ extract-mins starting from the heap of n elements? (Ignore the Θ(n log n) cost from the heapify operations themselves.

  1.   Θ(n)
  2.   Θ(2n)
  3.   Θ(logn)
  4.   Θ(nlogn)

2 Answers

Best answer
3 votes
3 votes
  • Heap is constructed using array.
  • Initial Size of the array was $n = 2^{k}$
  • because of extraction of minimum and never insertion, whenever size drops to $2^{k-1}$ we transfer the current heap elements to a new array of size $ n_1 = 2^{k-1}$ de-allocating that $n$ element array. Here $n_1 < n$
  • Similarly whenever size drops to $2^{k-2}$ we transfer current elements to a new array of size $ n_2 = 2^{k-2}$ de-allocating that $n_1$ element array. Here $n_2 < n_1$.

$$\begin{align*} & \text{1st movement cost } &=2^{k-1} \\ & \text{2nd movement cost } &=2^{k-2} \\ & \text{3rd movement cost } &=2^{k-3} \\ & ----------- \\ & \text{last movement cost } &=2^{k-k} \\ & \text{Net movement cost } \frac{1*\left ( 2^{k}-1 \right )}{2-1} = \Theta(n) \\ \end{align*}$$ 

selected by
3 votes
3 votes
Ok, Let first move occur when elements equivalent to 2^(k-1)

Second move  occur when left elements equivalent to 2^(k - 2)

Any jth move will occur when left  elements equivalent to 2^(k - j)

Hence total number of moves will be k , and

and cost of jth move at any time will be 2^j

total cost :  2^0 + 2^1 + 2^2 ..... 2 ^ (k -1 )  =  2^k - 1  

which is almost N  (  N = 2^k )    

Hence theta(N)  complexity  , correct answer will be A.

Related questions

6 votes
6 votes
2 answers
1
radha gogia asked Jul 15, 2015
9,049 views
Assume that a CPU can process $10^8$ operations per second. Suppose you have to sort an array with $10^6$ elements. Which of the following is true? 1. Insertion sort will...
1 votes
1 votes
2 answers
2