CmSc 250 Intro to Algorithms


Applications of Priority Queues - the Selection Problem

  1. The problem

  2. Given a list of N elements (here "list" means a sequence, it does not mean ADT list)
    and an integer k, k £ N, find the kth largest element.

  3. Solution 1

  4. Sort the elements in an array and return the element in the kth position.

    Complexity:

    simple sorting algorithm O(N2)
    advanced sorting algorithm O(NlogN)

  5. Solution 2

  6. Read k elements in an array and sort them. Let Sk be the smallest element. For each next element E do the following:

    If E > Sk remove Sk and insert E in the appropriate position in the array
    Return Sk

    Complexity: O(N*k)

    k2 for the initial sorting of k elements
    (N-k)*k for inserting each next element

    O(k2)+ O((N-k)*k) = O(k2 + N*k - k2) = O(N*k)

    Worst case: k = N/2, complexity: O(N2)

  7. Solution 3

  8. Assume we change the heap-order property - the highest priority corresponds to the highest key value.

    Read N elements in an array
    Build a heap of these N elements - O(N)
    Perform k DeleteMax operations - O(klogN)

    Complexity: O(N) + O(klogN)
    For k = N/2 the complexity is O(NlogN)

  9. Solution 4

  10. We return back to the usual heap-order property – the smallest element is at the top.

    Build a heap of k elements. The k-th largest element among k elements is the smallest element in that heap and it will be at the top. Complexity O(k)

    Compare each next element with the top element O(1)
    If the new element is larger, DeleteMin the top element and insert the new element in the heap. - O(log(k))

    At the end of the input the smallest element in the heap is the k-th largest element in the list of N elements.

    Complexity: O(k) + O((N-k)*log(k)) = O(Nlog(k))


Back to Contents page
Created by Lydia Sinapova