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

asked in Algorithms by Active (1.2k points)   | 87 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 (59.6k 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 (7.2k points)  
Top Users Jan 2017
  1. Debashish Deka

    8126 Points

  2. sudsho

    5042 Points

  3. Habibkhan

    4706 Points

  4. Vijay Thakur

    4458 Points

  5. Bikram

    4348 Points

  6. saurabh rai

    4212 Points

  7. Arjun

    4010 Points

  8. santhoshdevulapally

    3722 Points

  9. GateSet

    3292 Points

  10. Sushant Gokhale

    3286 Points

Monthly Topper: Rs. 500 gift card

19,122 questions
24,033 answers
52,725 comments
20,276 users