Question

Provide an O(k log k) algorithm that uses a heap data structure to find the kth largest element from the heap of n elements w

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To find the kth largest element from the heap of n elements, an additional heap data structure will be required.

Algorithm:

Step 1: Using the first k elements of the given heap, build a min-heap M. The time complexity of this step is O(k).

Step 2: After the above step, each of the next elements of the given heap is compared with the root of the min-heap M. If the element is greater than the root of the min-heap M, then make it root and call min-heapify function for M. Otherwise (if element is less than root of M), just ignore it. The time complexity of this step is O((n-k)*logk)

Step 3: After performing all the above steps, the root of the min-heap M is the kth largest element.

Time Complexity: O((n-k)*logk) = O(klogk)

For the given example:

1. The heap from the first seven elements is:

70 80 40 65 60

2. Min-heapify this heap.

40 60 100 65 70 80

3. Compare the root of this heap with remaining elements. 20, 40, 10 and 30 are smaller than the root 40, so nothing is done. But 45 is greater than 40, so 40 is replaced with 45. Thus, the 7th largest element is 45.

45 60 80 100 65 70

CODE:

The language used is C++.

In the code given below, an array data structure is given to represent the given heap. If the given heap is in tree form, store it into array using level order traversal and use the same code given below to find kth largest element. Note that level order traversal of a tree can be done using queue data structure.

In this code, priority_queue of Standard Template Library is used to represent min heap.

#include<bits/stdc++.h>
using namespace std;

int kthLargest(int arr[], int n, int k)
{
//Min heap
priority_queue<int, vector<int>, greater<int> > Q;

//Push first k elements into the priority_queue.
//The top or root of the priority_queue is the minimum element.
for(int i=0;i<k;i++)
Q.push(arr[i]);

//Compare remaining elements with top or root of the priority_queue.
//Top of the priority_queue Q can be obtained by Q.top().
for(int i=k;i<n;i++){
if(arr[i] > Q.top()){
Q.pop(); //Used to remove top of the priority_queue.
Q.push(arr[i]);
//When arr[i] is pushed, Q is automatically arranged so that minimum is at the root or top.
}
}
//Return root or top of the priority_queue.
return Q.top();
}

int main()
{
int arr[] = {100, 80, 70, 40, 50, 65, 60, 20, 40, 10, 30, 45};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<kthLargest(arr, n, 7);
}

Code Snippets:

1 2 3 # include<bits/stdc++ .h> using namespace std; int kthLargest (int arr, int n, int k) //Min heap priority queue<int, ve27 int main) 28 29 30 31 int arr(100, 80, 70, 40, 50, 65, 60, 20, 40, 10, 30,45) int n sizeof (arr) /sizeof (arr [0]); cout<<

Sample Output:

45 Process returned θ (0x0) execution time : 0.266 s Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
Provide an O(k log k) algorithm that uses a heap data structure to find the kth...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT