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 The Priority Queue Abstract Data Type

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
PriorityQueues.pdf 4
PriorityQueues.pdf 3
Example 7.1: (read)

7.1.2 Sorting with a Priority Queue

PriorityQueues.pdf 7
Algorithm PriorityQueue(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}

[Picture]
The running time of the algorithm is determined by the running times of operations
insertItem(e,e), minElement(), and removeMin(), which do depend how P is implemented.

7.1.3 Functions of a Priority Queue

PriorityQueues.pdf 3

Simplicity of the Priority Queue ADT

Example 7.2:
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
The composition pattern defines a single object that is a composition of other objects. A pair (k,e) is the simplest composition, for it composes two objects, k and e, into a single pair object.
html-7.2 (Item)


The Comparator Pattern
Lexicographical less than operator:
bool operator<(const Point& p1, const Point& p2)
{ if (p1.getX() == p2.getX()) return p1.getY() < p2.getY();
else return p1.getX() < p1.getX();
}
Assume that comp is a compatison class object. comp(a,b) returns integer i, such that
The approach that we will use is to overload "()" operator (to create so called function object).
PriorityQueues.pdf 5, 6

html-7.3 (Compare)

Using Comparator Objects
PriorityQueues.pdf 6
html-7.4 (Generic)


7.2 Implementing a Priority Queue with a Sequence

7.2.1 Implementation with an Unsorted Sequence

7.2.2 Implementation with a Sorted Sequence

html-7.5 (SSPQ1)
html-7.6 (SSPQ2)

Comparing the Two Implementations
PriorityQueues.pdf 8

7.2.3 Selection-Sort and Insertion-Sort

Selection-Sort
PriorityQueues.pdf 9

Insertion-Sort
PriorityQueues.pdf 10

7.3 Heaps

7.3.1 The Heap Data Structure

heap.pdf 10

Proposition 7.5: A heap T storing n keys has height h = [log(n + 1)].
heap.pdf 11

7.3.2 Implementing a Priority Queue with a Heap

heap.pdf 12

The Vector Representation of a Heap
heap.pdf 19

Insertion
heap.pdf 13

Up-Heap Bubbling after an Insertion
heap.pdf 14

Removal
heap.pdf 15

Down-Heap Bubbling after a Removal
heap.pdf 16

Analysis

7.3.3 C++ Implementation

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


7.3.4 Heap-Sort

heap.pdf 18

Implementing Heap-Sort In-Place