GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
108 views

asked in Algorithms by Active (1.3k points)   | 108 views

2 Answers

+4 votes
Best answer

a) Priority queue is implemented efficiently .For minheap we can say priority of a node is inversely proportional to the weight of a node and similarly for maxheap priority is directly proportional to the weight of a node..

And we know deletion and insertion operation in binary heap(min heap or max heap) takes  O(logn) time..Hence it is an efficient data structure for priority queue implementation assuming almost equal number of insertion and deletion occurs.

If say no of insertion operation is much higher than no of deletion , then unsorted array would be the best data structure for priority queue implementation since insertion in unsorted array does not require finding the appropriate place for insertion hence it will take O(1) time for insertion.

 

b) Maxheap  can  be used efficiently to find kth minimum..Let us see how..Let we have n elements.

 Steps :

1) Build a Max-Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k)

2) For each element, after the k’th element (arr[k] to arr[n-1]), compare it with root of MH.
……a) If the element is less than the root then make it root and call heapify for MH
……b) Else ignore it.
// The step 2 is O((n-k)*logk)

3) Finally, root of the MH is the kth smallest element.

Time complexity of this solution is O(k + (n-k)*Logk)

In this way we can use the maxheap data structure easily to find kth minimum

Reference : http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/

Hence option C should be the correct option.

answered by Veteran (65.9k points)  
selected by
+1 vote
I think both i and ii

to find kth largest and smallest element from an array we can do:

sort the array which takes O(nlogn) time and find the largest/smallest: TC= O(nlogn)

Build the max heap and extract largest element : O(n) + O(1) = O(n) time

Build the min heap and extract the smallest element : O(n) + O(1) = O(n) time
answered by Boss (8.2k points)  


Top Users Apr 2017
  1. akash.dinkar12

    3660 Points

  2. Divya Bharti

    2580 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Debashish Deka

    1614 Points

  7. Shubham Sharma 2

    1610 Points

  8. Prashant.

    1492 Points

  9. Arjun

    1472 Points

  10. Arunav Khare

    1464 Points

Monthly Topper: Rs. 500 gift card

22,088 questions
28,063 answers
63,298 comments
24,173 users