Title: Priority Queues
1Priority Queues
- A priority queue is an ADT where
- Each element has an associated priority
- Efficient extraction of the highest-priority
element is supported. - Typical operations of a priority queue are
- Enqueue (insert an element)
- Dequeue (remove the highest-priority element)
- Max (return the highest-priority element)
- Meld (join two priority queues into one)
2Priority Queues Applications
- Scheduling
- Examples
- Printer scheduling.
- Jobs sent to a printer by faculty have a higher
priority than jobs sent by students - Process scheduling.
- Several processes require CPU time. Priority may
be based on various factors - Shortest jobs have higher priority
- Jobs with a shorter remaining time have higher
priority - Jobs that have waited the longest have higher
priority - etc.
3Priority Queues Applications
- Discrete-event simulations
- Simulations of systems modeled as collections of
entities that interact with one another through
events that occur at discrete times. - The priority assigned to an event is its start
time. - Example
- PA1, bank simulation
4Binary Heaps
- Binary heap
- a binary tree that is
- complete
- every level except possibly the bottom one is
completely filled and the leaves in the bottom
level are as far left as possible - satisfies the heap property
- the key stored in every node is greater than or
equal to the keys stored in its children - this is called a max-heap. If the key at each
node is smaller than or equal to the keys of its
children, then we have a min-heap.
5Binary Heaps
- Since heaps are complete trees, they can easily
be stored in arrays
18
1
9
12
3
2
- given index i of a node,
- the index of its parent is ?i / 2?
- the indices of its children are 2i and 2i1
7
8
10
7
5
4
6
5
1
4
3
8
10
9
18
12
9
8
10
7
5
1
4
3
index
1
2
3
4
5
6
7
8
9
10
6Binary Heaps
- Is every array a heap?
- Not necessarily. But every sorted array is a
- max-heap, if sorted in descending order.
- min-heap, if sorted in ascending order.
- What is the height of a heap?
- O(lgn) where n is the number of elements in the
heap. - Where in a max-heap is the smallest element?
- It is always a leaf but we cannot know exactly
where it is located. - What are the maximum and minimum numbers of
elements in a heap of height h? - max elements 2h1-1
- min elements 2h
7Binary Heaps Heapify
- In the discussion that follows, "heap" refers to
a max-heap unless otherwise noted. - Every time we extract or insert an element, the
heap property may be violated.
Heapify Given node i of heap that potentially
violates the heap property, fix it. Input Array
A storing a heap, index i Preconditions The
children of node i are heaps. Algorithm
while (i lt heapsize) max_index max(Ai,
A2i, A2i1) if (max_index i)
return swap(A, i, max_index) // swap contents
at indices i, max_index i max_index // Since
we moved Ai to Amax_index, the heap
// property may now be violated there.
8Binary Heaps Heapify
- Running time of Heapify
- O(lgn) because we may have to travel all the way
from the root to a leaf, fixing the heap
property. - Can you come up with an array that causes Heapify
to be called recursively on every node on a path
from the root to a leaf? - Any array sorted in ascending order.
9Binary Heaps MakeHeap
- Given a random array, create a heap
- How?
- Algorithm sketch
- Elements A?n/2? 1 through An are leaves, so
they can be considered one-element heaps. - Call Heapify in a bottom-up manner starting at
index ?n/2?. - Running time
- easy analysis O(nlgn)
- more exact analysis (proof omitted) O(n)
10Binary Heaps MakeHeap
MakeHeap Given random array A, turn it into a
heap Input Array A Output Array A as a
heap Algorithm heapsize(A) length(A) for i
?length(A)/2? downto 1 Heapify(A, i) endfor
- Why does the loop run in decreasing order?
- If it ran in increasing order, would we still get
a heap?
11Binary Heaps Insert
- New elements are always inserted at the end (last
element of the array) - This may violate the heap property
- Travel up the tree looking for an appropriate
position for the new node (one that maintains the
heap property). As you go, copy (shift) down the
values of the nodes you pass, to make room
HeapInsert (A, key) i heapsize(A) while i gt1
and Aparent(i)ltkey Ai Aparent(i) i
parent(i) Ai key
Running time O(lgn) since we may end up
traveling from a leaf all the way to the root.
12Binary Heaps ExtractMax
- The maximum element is always at the root.
- Extract the maximum by replacing it with the last
element of the array and then Heapifying.
ExtractMax (A) max A1 A1
Aheapsize(A) heapsize(A) heapsize(A)-1 Heapify
(A,1) return max
Running time O(lgn) since we may end up
traveling from the root all the way to a leaf.
13Binary Heaps Sorting
- Idea
- Since the maximum element is at the root, we can
move it to the last position in the array, and
decrement the size of the heap. - The root will now contain the new maximum
- Repeat...
Heapsort(A) Input Array A Output Array A in
ascending order. Algorithm Build-heap(A)
for i length(A) downto 1
swap(Ai, A1) heapsize
heapsize 1 Heapify(A, i)
Running time O(nlgn) since the loop is
executed n times and Heapify takes O(lgn)
14Priority queues in the STL
includeltvectorgt includeltiostreamgt includeltqueue
gt int main() priority_queueltint,
vectorltintgt, greaterltintgt gt q int
nums10 10,6,8,1,5,3,7,2,9,4
for (int i 0 i lt 10 i)
q.push(numsi) stdcout ltlt
q.size() ltlt "\n" while (!q.empty() )
stdcout ltlt q.top() ltlt
"\n" q.pop()
return 0
based on a vector
higher values lower priority
15Heaps in the STL
includeltalgorithmgt includeltiostreamgt includeltve
ctorgt includeltfunctionalgt int main ()
vectorltintgt nums for (int i1 ilt11 i)
nums.push_back(i) make_heap(nums.begin(),
nums.end()) sort_heap(nums.begin(),
nums.end()) nums.push_back(32) make_heap(nums
.begin(), nums.end()) // remake
heap pop_heap(nums.begin(), nums.end()) //
extraxt max return 0