K’th Least Element in a Min-Heap
Given a min-heap of size n, find the kth least element in the min-heap.
Examples:
Input : {10, 50, 40, 75, 60, 65, 45} k = 4
Output : 50Input : {10, 50, 40, 75, 60, 65, 45} k = 2
Output : 40
Naive approach: We can extract the minimum element from the min-heap k times and the last element extracted will be the kth least element. Each deletion operation takes O(log n) time, so the total time complexity of this approach comes out to be O(k * log n).
Implementation:
C++
// C++ program to find k-th smallest // element in Min Heap. #include <bits/stdc++.h> using namespace std; // Structure for the heap struct Heap { vector< int > v; int n; // Size of the heap Heap( int i = 0) : n(i) { v = vector< int >(n); } }; // Generic function to // swap two integers void swap( int & a, int & b) { int temp = a; a = b; b = temp; } // Returns the index of // the parent node inline int parent( int i) { return (i - 1) / 2; } // Returns the index of // the left child node inline int left( int i) { return 2 * i + 1; } // Returns the index of // the right child node inline int right( int i) { return 2 * i + 2; } // Maintains the heap property void heapify(Heap& h, int i) { int l = left(i), r = right(i), m = i; if (l < h.n && h.v[i] > h.v[l]) m = l; if (r < h.n && h.v[m] > h.v[r]) m = r; if (m != i) { swap(h.v[m], h.v[i]); heapify(h, m); } } // Extracts the minimum element int extractMin(Heap& h) { if (!h.n) return -1; int m = h.v[0]; h.v[0] = h.v[h.n-- - 1]; heapify(h, 0); return m; } int findKthSmalles(Heap &h, int k) { for ( int i = 1; i < k; ++i) extractMin(h); return extractMin(h); } int main() { Heap h(7); h.v = vector< int >{ 10, 50, 40, 75, 60, 65, 45 }; int k = 2; cout << findKthSmalles(h, k); return 0; } |
Java
import java.util.*; // Class for the heap class Heap { List<Integer> v; int n; // Size of the heap Heap( int i) { n = i; v = new ArrayList<Integer>(Collections.nCopies(n, 0 )); } } // Main class public class Main { // Generic function to swap two integers public static void swap( int [] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } // Returns the index of the parent node public static int parent( int i) { return (i - 1 ) / 2 ; } // Returns the index of the left child node public static int left( int i) { return 2 * i + 1 ; } // Returns the index of the right child node public static int right( int i) { return 2 * i + 2 ; } // Maintains the heap property public static void heapify(Heap h, int i) { int l = left(i), r = right(i), m = i; if (l < h.n && h.v.get(i) > h.v.get(l)) m = l; if (r < h.n && h.v.get(m) > h.v.get(r)) m = r; if (m != i) { Collections.swap(h.v, m, i); heapify(h, m); } } // Extracts the minimum element public static int extractMin(Heap h) { if (h.n == 0 ) return - 1 ; int m = h.v.get( 0 ); h.v.set( 0 , h.v.get(h.n - 1 )); h.n--; heapify(h, 0 ); return m; } public static int findKthSmallest(Heap h, int k) { for ( int i = 1 ; i < k; ++i) extractMin(h); return extractMin(h); } public static void main(String[] args) { Heap h = new Heap( 7 ); h.v = Arrays.asList( 10 , 50 , 40 , 75 , 60 , 65 , 45 ); int k = 2 ; System.out.println(findKthSmallest(h, k)); } } |
Python3
import heapq # Structure for the heap class Heap: def __init__( self , i = 0 ): self .v = [ 0 ] * i self .n = i # Returns the index of the parent node def parent(i): return (i - 1 ) / / 2 # Returns the index of the left child node def left(i): return 2 * i + 1 # Returns the index of the right child node def right(i): return 2 * i + 2 # Maintains the heap property def heapify(h, i): l, r, m = left(i), right(i), i if l < h.n and h.v[i] > h.v[l]: m = l if r < h.n and h.v[m] > h.v[r]: m = r if m ! = i: h.v[i], h.v[m] = h.v[m], h.v[i] heapify(h, m) # Extracts the minimum element def extractMin(h): if not h.n: return - 1 m = h.v[ 0 ] h.v[ 0 ] = h.v[h.n - 1 ] h.n - = 1 heapify(h, 0 ) return m def findKthSmallest(h, k): for i in range ( 1 , k): extractMin(h) return extractMin(h) h = Heap( 7 ) h.v = [ 10 , 50 , 40 , 75 , 60 , 65 , 45 ] k = 2 print (findKthSmallest(h, k)) |
Javascript
// Structure for the heap class Heap { constructor(i = 0) { this .v = new Array(i); this .n = i; // Size of the heap } } // Returns the index of // the parent node function parent(i) { return Math.floor((i - 1) / 2); } // Returns the index of // the left child node function left(i) { return 2 * i + 1; } // Returns the index of // the right child node function right(i) { return 2 * i + 2; } // Maintains the heap property function heapify(h, i) { let l = left(i), r = right(i), m = i; if (l < h.n && h.v[i] > h.v[l]) m = l; if (r < h.n && h.v[m] > h.v[r]) m = r; if (m != i) { let temp = h.v[m]; h.v[m] = h.v[i]; h.v[i] = temp; heapify(h, m); } } // Extracts the minimum element function extractMin(h) { if (!h.n) return -1; let m = h.v[0]; h.v[0] = h.v[h.n-- - 1]; heapify(h, 0); return m; } function findKthSmallest(h, k) { for (let i = 1; i < k; ++i) extractMin(h); return extractMin(h); } const h = new Heap(7); h.v = [10, 50, 40, 75, 60, 65, 45]; const k = 2; console.log(findKthSmallest(h, k)); |
C#
using System; using System.Collections.Generic; using System.Linq; public class Heap { public List< int > v; public int n { get ; private set ; } // Size of the heap public Heap( int i) { n = i; v = Enumerable.Repeat(0, n).ToList(); } // Maintains the heap property private void heapify( int i) { int l = left(i), r = right(i), m = i; if (l < n && v[i] > v[l]) m = l; if (r < n && v[m] > v[r]) m = r; if (m != i) { swap(v, m, i); heapify(m); } } // Extracts the minimum element public int extractMin() { if (n == 0) return -1; int m = v[0]; v[0] = v[n - 1]; n--; heapify(0); return m; } public int findKthSmallest( int k) { for ( int i = 1; i < k; ++i) extractMin(); return extractMin(); } // Returns the index of the parent node private static int parent( int i) { return (i - 1) / 2; } // Returns the index of the left child node private static int left( int i) { return 2 * i + 1; } // Returns the index of the right child node private static int right( int i) { return 2 * i + 2; } // Generic function to swap two integers private static void swap(List< int > a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } public class MainClass { public static void Main( string [] args) { Heap h = new Heap(7); h.v = new List< int > { 10, 50, 40, 75, 60, 65, 45 }; int k = 2; Console.WriteLine(h.findKthSmallest(k)); } } |
40
Time Complexity: O(k * log n)
Efficient approach:
We can note an interesting observation about min-heap. An element x at ith level has i – 1 ancestor. By the property of min-heaps, these i – 1 ancestors are guaranteed to be less than x. This implies that x cannot be among the least i – 1 element of the heap. Using this property, we can conclude that the kth least element can have a level of at most k. We can reduce the size of the min-heap such that it has only k levels. We can then obtain the kth least element by our previous strategy of extracting the minimum element k times.
Note that the size of the heap is reduced to a maximum of 2k – 1, therefore each heapify operation will take O(log 2k) = O(k) time. The total time complexity will be O(k2). If n >> k, then this approach performs better than the previous one.
Implementation:
C++
// C++ program to find k-th smallest // element in Min Heap using k levels #include <bits/stdc++.h> using namespace std; // Structure for the heap struct Heap { vector< int > v; int n; // Size of the heap Heap( int i = 0) : n(i) { v = vector< int >(n); } }; // Generic function to // swap two integers void swap( int & a, int & b) { int temp = a; a = b; b = temp; } // Returns the index of // the parent node inline int parent( int i) { return (i - 1) / 2; } // Returns the index of // the left child node inline int left( int i) { return 2 * i + 1; } // Returns the index of // the right child node inline int right( int i) { return 2 * i + 2; } // Maintains the heap property void heapify(Heap& h, int i) { int l = left(i), r = right(i), m = i; if (l < h.n && h.v[i] > h.v[l]) m = l; if (r < h.n && h.v[m] > h.v[r]) m = r; if (m != i) { swap(h.v[m], h.v[i]); heapify(h, m); } } // Extracts the minimum element int extractMin(Heap& h) { if (!h.n) return -1; int m = h.v[0]; h.v[0] = h.v[h.n-- - 1]; heapify(h, 0); return m; } int findKthSmalles(Heap& h, int k) { h.n = min(h.n, int ( pow (2, k) - 1)); for ( int i = 1; i < k; ++i) extractMin(h); return extractMin(h); } int main() { Heap h(7); h.v = vector< int >{ 10, 50, 40, 75, 60, 65, 45 }; int k = 2; cout << findKthSmalles(h, k); return 0; } |
Java
// Java program to find k-th smallest // element in Min Heap using k levels import java.util.*; // Structure for the heap class Heap { Vector<Integer> v; int n; // Size of the heap Heap( int i) { n = i; v = new Vector<Integer>(n); } } public class Main { // Generic function to // swap two integers static void swap(Vector<Integer> v, int a, int b) { int temp = v.get(a); v.set(a, v.get(b)); v.set(b, temp); } // Returns the index of // the parent node static int parent( int i) { return (i - 1 ) / 2 ; } // Returns the index of // the left child node static int left( int i) { return 2 * i + 1 ; } // Returns the index of // the right child node static int right( int i) { return 2 * i + 2 ; } // Maintains the heap property static void heapify(Heap h, int i) { int l = left(i), r = right(i), m = i; if (l < h.n && h.v.get(i) > h.v.get(l)) m = l; if (r < h.n && h.v.get(m) > h.v.get(r)) m = r; if (m != i) { swap(h.v, m, i); heapify(h, m); } } // Extracts the minimum element static int extractMin(Heap h) { if (h.n == 0 ) return - 1 ; int m = h.v.get( 0 ); h.v.set( 0 , h.v.get(h.n-- - 1 )); heapify(h, 0 ); return m; } static int findKthSmalles(Heap h, int k) { h.n = Math.min(h.n, ( int )Math.pow( 2 , k) - 1 ); for ( int i = 1 ; i < k; ++i) extractMin(h); return extractMin(h); } public static void main(String[] args) { Heap h = new Heap( 7 ); h.v = new Vector<Integer>( Arrays.asList( 10 , 50 , 40 , 75 , 60 , 65 , 45 )); int k = 2 ; System.out.println(findKthSmalles(h, k)); } } |
Python3
# Python program to find k-th smallest # element in Min Heap using k levels import math # Structure for the heap class Heap: def __init__( self , i = 0 ): self .v = [ 0 ] * i self .n = i # Size of the heap # Generic function to # swap two integers def swap(a, b): temp = a a = b b = temp return a, b # Returns the index of # the parent node def parent(i): return (i - 1 ) / / 2 # Returns the index of # the left child node def left(i): return 2 * i + 1 # Returns the index of # the right child node def right(i): return 2 * i + 2 # Maintains the heap property def heapify(h, i): l, r, m = left(i), right(i), i if l < h.n and h.v[i] > h.v[l]: m = l if r < h.n and h.v[m] > h.v[r]: m = r if m ! = i: h.v[m], h.v[i] = swap(h.v[m], h.v[i]) heapify(h, m) # Extracts the minimum element def extractMin(h): if not h.n: return - 1 m = h.v[ 0 ] h.v[ 0 ] = h.v[h.n - 1 ] h.n - = 1 heapify(h, 0 ) return m def findKthSmallest(h, k): h.n = min (h.n, int (math. pow ( 2 , k) - 1 )) for i in range ( 1 , k): extractMin(h) return extractMin(h) if __name__ = = '__main__' : h = Heap( 7 ) h.v = [ 10 , 50 , 40 , 75 , 60 , 65 , 45 ] k = 2 print (findKthSmallest(h, k)) |
C#
using System; using System.Collections.Generic; using System.Linq; public class Heap { public List< int > v; public int n; // Size of the heap public Heap( int i) { n = i; v = new List< int >(n); } } public class MainClass { // Generic function to // swap two integers static void swap(List< int > v, int a, int b) { int temp = v[a]; v[a] = v[b]; v[b] = temp; } // Returns the index of // the parent node static int parent( int i) { return (i - 1) / 2; } // Returns the index of // the left child node static int left( int i) { return 2 * i + 1; } // Returns the index of // the right child node static int right( int i) { return 2 * i + 2; } // Maintains the heap property static void heapify(Heap h, int i) { int l = left(i), r = right(i), m = i; if (l < h.n && h.v[i] > h.v[l]) m = l; if (r < h.n && h.v[m] > h.v[r]) m = r; if (m != i) { swap(h.v, m, i); heapify(h, m); } } // Extracts the minimum element static int extractMin(Heap h) { if (h.n == 0) return -1; int m = h.v[0]; h.v[0] = h.v[h.n-- - 1]; heapify(h, 0); return m; } static int findKthSmalles(Heap h, int k) { h.n = Math.Min(h.n, ( int )Math.Pow(2, k) - 1); for ( int i = 1; i < k; ++i) extractMin(h); return extractMin(h); } public static void Main( string [] args) { Heap h = new Heap(7); h.v = new List< int >( new int [] { 10, 50, 40, 75, 60, 65, 45 }); int k = 2; Console.WriteLine(findKthSmalles(h, k)); } } |
Javascript
// JavaScript program to find k-th smallest // element in Min Heap using k levels // Structure for the heap class Heap { constructor(i = 0) { this .v = new Array(i).fill(0); this .n = i; // Size of the heap } } // Generic function to // swap two integers function swap(a, b) { const temp = a; a = b; b = temp; return [a, b]; } // Returns the index of // the parent node function parent(i) { return Math.floor((i - 1) / 2); } // Returns the index of // the left child node function left(i) { return 2 * i + 1; } // Returns the index of // the right child node function right(i) { return 2 * i + 2; } // Maintains the heap property function heapify(h, i) { let l = left(i); let r = right(i); let m = i; if (l < h.n && h.v[i] > h.v[l]) { m = l; } if (r < h.n && h.v[m] > h.v[r]) { m = r; } if (m != i) { [h.v[m], h.v[i]] = swap(h.v[m], h.v[i]); heapify(h, m); } } // Extracts the minimum element function extractMin(h) { if (!h.n) { return -1; } let m = h.v[0]; h.v[0] = h.v[h.n - 1]; h.n--; heapify(h, 0); return m; } function findKthSmallest(h, k) { h.n = Math.min(h.n, Math.pow(2, k) - 1); for (let i = 1; i < k; i++) { extractMin(h); } return extractMin(h); } const h = new Heap(7); h.v = [10, 50, 40, 75, 60, 65, 45]; const k = 2; console.log(findKthSmallest(h, k)); |
40
Time Complexity: O(k2) More efficient approach:
We can further improve the time complexity of this problem by the following algorithm:
- Create a priority queue P (or Min Heap) and insert the root node of the min-heap into P. The comparator function of the priority queue should be such that the least element is popped.
- Repeat these steps k – 1 times:
- Pop the least element from P.
- Insert left and right child elements of the popped element. (if they exist).
- The least element in P is the kth least element of the min-heap.
The initial size of the priority queue is one, and it increases by at most one at each of the k – 1 steps. Therefore, there are maximum k elements in the priority queue and the time complexity of the pop and insert operations is O(log k). Thus the total time complexity is O(k * log k).
Implementation:
C++
// C++ program to find k-th smallest // element in Min Heap using another // Min Heap (Or Priority Queue) #include <bits/stdc++.h> using namespace std; // Structure for the heap struct Heap { vector< int > v; int n; // Size of the heap Heap( int i = 0) : n(i) { v = vector< int >(n); } }; // Returns the index of // the left child node inline int left( int i) { return 2 * i + 1; } // Returns the index of // the right child node inline int right( int i) { return 2 * i + 2; } int findKthSmalles(Heap &h, int k) { // Create a Priority Queue priority_queue<pair< int , int >, vector<pair< int , int > >, greater<pair< int , int > > > p; // Insert root into the priority queue p.push(make_pair(h.v[0], 0)); for ( int i = 0; i < k - 1; ++i) { int j = p.top().second; p.pop(); int l = left(j), r = right(j); if (l < h.n) p.push(make_pair(h.v[l], l)); if (r < h.n) p.push(make_pair(h.v[r], r)); } return p.top().first; } int main() { Heap h(7); h.v = vector< int >{ 10, 50, 40, 75, 60, 65, 45 }; int k = 4; cout << findKthSmalles(h, k); return 0; } |
Python3
# Python program to find k-th smallest # element in Min Heap using another # Min Heap (Or Priority Queue) import heapq # Structure for the heap class Heap: def __init__( self , n): self .v = [ 0 ] * n self .n = n # Returns the index of # the left child node def left(i): return 2 * i + 1 # Returns the index of # the right child node def right(i): return 2 * i + 2 def findKthSmalles(h, k): # Create a Priority Queue p = [] # Insert root into the priority queue heapq.heappush(p, (h.v[ 0 ], 0 )) for i in range (k - 1 ): j = heapq.heappop(p)[ 1 ] l, r = left(j), right(j) if l < h.n: heapq.heappush(p, (h.v[l], l)) if r < h.n: heapq.heappush(p, (h.v[r], r)) return p[ 0 ][ 0 ] # Main function def main(): h = Heap( 7 ) h.v = [ 10 , 50 , 40 , 75 , 60 , 65 , 45 ] k = 4 print (findKthSmalles(h, k)) if __name__ = = '__main__' : main() |
50
Time Complexity: O(k * log k)
Please Login to comment...