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
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 first 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
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).
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).