CS 221
Analysis of AlgorithmsData StructuresPriority Queues and Heaps
Portions of the following slides are from
Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002
and
Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book
Priority queues
Remember queues and stacks in both the removal of an element was
based on the order it was placed on the structure…
… but what if removal was based on other criteria
Priority queues
Priority queue removal based on priority value rather
than… the order it was placed on the structure,
or… even the value of the element (maybe)
Priority queues Key
each element to be place in a priority queue is a two part element the element value key – a value associated with the element
value Think of a take-a-number ticket for counter
service (like at DMV) (really a FIFO queue, right?)
You are the element value Your ticket number is your key
Priority queues
Key
Key ev
element
Key ev
Key ev
Key ev
Key ev
Key ev
Key ev
Priority queue ADT
A priority queue stores a collection of items
An item is a pair(key, element)
Main methods of the Priority Queue ADT insertItem(k, o)
inserts an item with key k and element o
removeMin()removes the item with smallest key and returns its element
Additional methods minKey()
returns, but does not remove, the smallest key of an item
minElement()returns, but does not remove, the element of an item with smallest key
size(), isEmpty() Applications:
Standby flyers Auctions Stock market
Priority queues
Need two more things Total Order Relation Comparator
to realize the Total Order Relation
Total Order Relation
Keys in a priority queue can be arbitrary objects on which an order is defined
Two distinct items in a priority queue can have the same key
Mathematical concept of total order relation Reflexive property:
x x Antisymmetric
property:x y y x x = y
Transitive property: x y y z x z
Comparator A comparator
encapsulates the action of comparing two objects according to a given total order relation
A generic priority queue uses an auxiliary comparator
The comparator is external to the keys being compared
When the priority queue needs to compare two keys, it uses its comparator
Methods of the Comparator ADT, all with Boolean return type isLessThan(x, y) isLessThanOrEqualTo(x,
y) isEqualTo(x,y) isGreaterThan(x, y) isGreaterThanOrEqualTo
(x,y) isComparable(x)
Priority Queue with an unordered sequence
How to implement? vector? linked list?
Priority Queue with an unordered sequence Selection-Sort
Input: unordered sequence Output: ordered sequence
Insert element at end of queue (first phase) O(?) remove elements in key order (second phase) Run-time? In other words-
How many steps does it take to execute removeMin(P) to remove the smallest element value?
How many times will you run removeMin(P)?
Priority Queue with an unordered sequence Selection-Sort First phase O(1) Second phase
O(n+(n-1)+(n-2)+…2+1) or O(Σi, i=1,n) or O(n(n+1)/2 so, O(n2)
Priority Queue with an ordered sequence Insertion-Sort
Input: unordered sequence Output: ordered sequence
Insert element in key order position O(?) remove elements in key order (second phase) Run-time? In other words-
How many steps does it take to execute removeMin(P) to remove the smallest element value?
How many times will you run removeMin(P)?
Priority Queue with an unordered sequence Selection-Sort First phase O(1)
O(n+(n-1)+(n-2)+…2+1) or O(Σi, i=1,n) or O(n(n+1)/2 so, O(n2)
Second phase O(1)
Heaps
Remember Trees especially binary tree we like trees, right? Why?
Heaps
Imagine a Priority queue but rather than linear vector or list implemented as a tree that is called a heap
Heaps We still need
Total Order Relation Comparators
We need a few more things Heap-Order Property
for every node v (except root) the key of v k(v) is greater than or equal to the key of its parent node
Complete Binary Tree – binary tree of height h where all levels (0…h-1) have the maximum number of nodes
Heaps Complete Binary Tree – what?
leaf (external nodes) hold no elements just placeholders all element values are in internal nodes
Now we need to keep track of the lastnode() The last node of a heap is the rightmost
internal node of depth h 1
Heaps
2
65
79
last node
Heaps So, let’s w=lastnode() z = insertion point in heap
first external node to the right of w this is where we will always add new elements (k,e)
2
65
79
w z
Heaps
Heap depth and number of keys
1
2
2h2
1
keys
0
1
h2
h1
depth
Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each internal
node We keep track of the position of the last node For simplicity, we show only the keys in the pictures
(2, Sue)
(6, Mark)(5, Pat)
(9, Jeff) (7, Anna)
Insertion into a Heap (§2.4.3)
Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap
The insertion algorithm consists of three steps Find the insertion node
z (the new last node) Store k at z and expand
z into an internal node Restore the heap-order
property (discussed next)
2
65
79
insertion node
2
65
79 1
z
z
Insertion - Upheap Bubbling
2
15
79 6z
1
25
79 6z
2
65
79 1z
removeMin() from heap
Removal from a Heap (§2.4.3) Method removeMin of
the priority queue ADT corresponds to the removal of the root key from the heap
The removal algorithm consists of three steps Replace the root key
with the key of the last node w
Compress w and its children into a leaf
Restore the heap-order property (discussed next)
2
65
79
last node
w
7
65
9w
7
65
9w
5
67
9w
Downheap Bubbling
2
65
79
7
65
9w