Chapter 7: Priority Queues

A priority queue is an ADT for storing a collection of prioritized elements that supports arbitrary element insertion but supports removal of elements in order of priority; that is, the element with first priority can be removed at any time.
Examples:
The priority is typically a numerical value - the smallest numerical value should have first (highest) priority.

7.1.1 Keys, Priorities, and Total Order Relations
We define a key to be an object that is assigned to an element as a specific attribute for that element and that can be used to identify, rank, or weight that element.

Comparing Keys with Total Orders
Example 7.1: Suppose a certain flight is fully booked an hour prior to departure. Because of possibility of cancellations, the airline maintains a priority queue of standby passengers hoping to get a seat. The priority of each passenger is determined by the fare paid, the frequent-flyer status, and the time when the passenger is inserted into the priority queue. When a passenger requested to fly standby, the associated passenger object is inserted into the priority queue with an insertItem operation. Shortly before the flight departure, if seats become available (for example, due to last-minute cancellations), the airline repeatedly removes, from the priority queue, a standby passenger with first priority, using a combination of minElement() and removeMin() operations, and lets this person board. 

7.1.2 Sorting with a Priority Queue
  1. Insert the elements one by one with a series of insertItem(e,e) operations
  2. Remove the elements in sorted order with a series of removeMin() operations
  • The running time of this sorting method depends on the priority queue implementation

  • Algorithm PriorityQueueSort(S, P):
    Input: A sequence S storing n elements, on which a total order relation is
        defined, and a priority queue P, that compares keys using the same total
        order relation
    Output: The sequence S sorted by the total order relation
    while !S.isEmpty() do
       e <- S.removeFirst()   {remove an element e from
    S}
       
    P.insertItem(e,e)         {the key is the element itself}
    while !
    P.isEmpty() do
       
    e <- P.minElement()    {get a smallest element from P}
       
    P.removeMin()               {remove this element from P}
       
    S.insertLast(e)             {add the element at the end of S}
    Operation
    Output
    Priority Queue
    insertItem(5,A)
    -
    {(5,A)}
    insertItem(9,C) -
    {(5,A),(9,C)}
    insertItem(3,B) -
    {(3,B),(5,A),(9,C)}
    insertItem(7,D) -
    {(3,B),(5,A),(7,D),(9,C)}
    minElement()
    B
    {(3,B),(5,A),(7,D),(9,C)}
    minKey()
    3
    {(3,B),(5,A),(7,D),(9,C)}
    removeMin()
    -
    {(5,A),(7,D),(9,C)}
    size()
    3
    {(5,A),(7,D),(9,C)}
    minElement()
    A
    {(5,A),(7,D),(9,C)}
    removeMin()
    -
    {(7,D),(9,C)}
    removeMin() -
    {(9,C)}
    removeMin() -
    {}
    removeMin() ERROR
    {}
    isEmpty()
    true
    {}

    There are still two important issues that we have left undetermined to this point:
    7.1.4 Compositions and Comparators

    The Composition Pattern
    html-7.2 (Item)

    The Comparator Pattern
    bool operator<(const Point& p1, const Point& p2)
    { if (p1.getX() == p2.getX()) return p1.getY() < p2.getY();
    else return p1.getX() < p1.getX();
    }
    class LexCompare {
    public:
    int operator()(Point a, Point b) {
    if (a.x < b.x) return –1
    else if (a.x > b.x) return +1

    else if (a.y < b.y) return –1
    else if (a.y > b.y) return +1
    else return 0;
    }
    };
    html-7.3 (Compare)

    Using Comparator Objects

    Point p(2.3, 4.5);
    Point q(1.7, 7.3);
    LexCompare lexCompare;
    if (lexCompare(p, q) < 0) cout << “p less than q”;
    else if (lexCompare(p, q) == 0) cout << “p equals q”;
    else if (lexCompare(p, q) > 0) cout << “p greater than q”;
    html-7.4 (Generic)


    7.2 Implementing a Priority Queue with a Sequence ADT

    7.2.1 Implementation with an Unsorted Sequence 7.2.2 Implementation with a Sorted Sequence html-7.5 (SSPQ1)
    html-7.6 (SSPQ2)

    [priority.cpp]

    Comparing the Two Implementations

    • Implementation with an unsorted sequence
      • Store the items of the priority queue in a list-based sequence, in arbitrary order
    • Implementation with a sorted sequence
      • Store the items of the priority queue in a sequence, sorted by key
    • Performance:
      • insertItem takes O(1) time since we can insert the item at the beginning or end of the sequence
      • removeMin, minKey and minElement take O(n) time since we have to traverse the entire sequence to find the smallest key
    • Performance:
      • insertItem takes O(n) time since we have to find the place where to insert the item
      • removeMin, minKey and minElement take O(1) time since the smallest key is at the beginning of the sequence

    7.2.3 Selection-Sort and Insertion-Sort

    Selection-Sort
    1. Inserting the elements into the priority queue with n insertItem operations takes O(n) time
    2. Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to 1 + 2 + …+ n
  • Selection-sort runs in O(n2) time

  • Sequence S
    Priority Queue P
    (unsorted sequence)
    Input
    (7,4,8,2,5,3,9)
    ()
    Phase 1
    O(n)
    (4,8,2,5,3,9)
    (8,2,5,3,9)
    (2,5,3,9)
    (5,3,9)
    (3,9)
    (9)
    ()
    (7)
    (7,4)
    (7,4,8)
    (7,4,8,2)
    (7,4,8,2,5)
    (7,4,8,2,5,3)
    (7,4,8,2,5,3,9)
    Phase 2
    O(n2)
    (2)
    (2,3)
    (2,3,4)
    (2,3,4,5)
    (2,3,4,5,7)
    (2,3,4,5,7,8)
    (2,3,4,5,7,8,9)
    (7,4,8,5,3,9)
    (7,4,8,5,9)
    (7,8,5,9)
    (7,8,9)
    (8,9)
    (9)
    ()

    Insertion-Sort

    1. Inserting the elements into the priority queue with n insertItem operations takes time proportional to
      1 + 2 + …+ n
    2. Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time
  • Insertion-sort runs in O(n2) time


  • Sequence S
    Priority Queue P
    (sorted sequence)
    Input
    (7,4,8,2,5,3,9)
    ()
    Phase 1
    O(n2)
    (4,8,2,5,3,9)
    (8,2,5,3,9)
    (2,5,3,9)
    (5,3,9)
    (3,9)
    (9)
    ()
    (7)
    (4,7)
    (4,7,8)
    (2,4,7,8)
    (2,4,5,7,8)
    (2,3,4,5,7,8)
    (2,3,4,5,7,8,9)
    Phase 2
    O(n)
    (2)
    (2,3)
    (2,3,4)
    (2,3,4,5)
    (2,3,4,5,7)
    (2,3,4,5,7,8)
    (2,3,4,5,7,8,9)
    (3,4,5,7,8,9)
    (4,5,7,8,9)
    (5,7,8,9)
    (7,8,9)
    (8,9)
    (9)
    ()


    7.3 Heaps

    7.3.1 The Heap Data Structure

    Proposition 7.5: A heap T storing n keys has height h = [log(n + 1)].
    Proof: (we apply the complete binary tree property)


    7.3.2 Implementing a Priority Queue with a Heap

    The Vector Representation of a Heap

    Insertion





    Up-Heap Bubbling after an Insertion



    Removal


    Down-Heap Bubbling after a Removal



    Analysis

    Function
    Time
    size(), isEmpty()
    O(1)
    minElement(), minKey()
    O(1)
    insertItem(k,e)
    O(log n)
    removeMin()
    O(log n)

    7.3.3 C++ Implementation

    html-7.7 (HeapTree)
    html-7.8 (HPQ1)
    html-7.9 (HPQ2)

    7.3.4 Heap-Sort