Priority Queue: A data structure whose basic operations are:
inserting new elements, and
removing the largest or smallest element.
Example Uses
Simulations (events are ordered by the time at which they
should be executed)
Job scheduling in computer systems
(higher priority jobs should be executed first)
Constraint systems (higher priority
constraints should be satisfied
before lower priority constraints)
Operations we will Support
Construct a priority queue from a set of items
Insert a new item
Remove the smallest item
Delete an item
Change the priority of an item
Elementary Implementations
Unordered list: Use an array of unordered items
Construct: add items to the list one at a time -- O(n)
Insert: add an item to the end of the list -- O(1)
Remove smallest: search the list, remove the smallest item
and return it -- O(n)
Ordered List: Use an array of ordered items
Construct: requires a sort -- O(n log n)
Insert: find the proper place in the list, move all smaller
elements one position to the right -- O(n)
Remove smallest: remove the Nth element -- O(1)
Heap Data Structure
A heap is a binary tree in which:
the key in each node is greater than or equal to the
keys in its children, and
each interior node has its full complement of children,
except possibly for the rightmost interior node on
the bottom level
Example: See pg 213 in Weiss.
A heap can be represented as an array where:
the children of an interior node in position j are in positions
2j and 2j+1
the parent of a child in position j is at postion j/2
(rounded down to the nearest integer if j is odd)
Example: See pg 213 in Weiss
In a heap of N nodes, each path has about lg N nodes (roughly N/2
nodes on the bottom, roughly N/4 nodes with children on the bottom
roughly N/8 nodes with grandchildren on the bottom, etc. Since
each level has roughly half the nodes of the previous level, there
can be at most roughly lg N levels before one reaches a level with
only one node, the root).
Algorithms on Heaps
Insert: Make the newly inserted item the rightmost leaf, then push
it up the tree until both its children are less than or equal to
its value
Algorithm: pg 216 in Weiss
Example: pp 216-217 in Weiss
DeleteMin: Save the key at the root, then replace the key
at the root with the key at the rightmost leaf, and push it down
as far as possible.
Algorithm: pg 218 of Weiss
Example: pp 217-218 of Weiss
Decrease Key(p, amount): Lower the value of the item at
position p by a positive amount. Use percolate up to move the
item up in the heap. This operation is useful to system
administrators who want to increase the priority of a program.
Increase Key(p, amount): Increase the value of the item
at position p by a positive amount. Use percolate down to move
the item down in the heap. This operation is useful to system
administrators who want to decrease the priority of a program.
Delete(p): Do a decrease key by infinity and then do
a deletemin.
Running Times
insert, and deletemin: These operations
require less than 2 lg N comparisons when performed
on a heap of N elements. Their running time is O(lg N).
increase key, decrease key, and delete: These operations require less
than 2 ln N comparisons to re-establish the heap property. However,
they also may require a linear search to locate the item that
should be changed or deleted. This search requires an additional
N comparisons. Hence the total number of comparisons is less
than N + 2 lg N comparisons and both operations require O(N) time.
Although constructing a queue by inserting items one at a time
takes O(N lg N) in the worst case, on the average
it is O(N).
Indirect Priority Queues
If insert and deletemin are the only operations a priority queue
needs to support, than the array implementation given above is adequate.
However, if the priority queue also needs to support change priority and delete
operations and wants to support them efficiently (i.e., in O(lg N) time),
then indirect priority queues must be employed.
The Problem
In order to implement the change and delete operations in O(lg N) time,
we need to eliminate the linear search that is required to find the item's
position in the heap. We need to replace the linear search with a mechanism
that allows us to find the item's heap position in O(lg N) time or less.
The Solution
The solution is to store an item's position with each item. When the
item needs to be deleted or its priority needs to be changed, we look
up its position and use that position to index into the array. In order
to implement this change, we need to update position numbers as the
items are moved around in the heap.