Title: Priority Queues
1 Priority Queues Pauline Wilcox and Brian
Palmer Department of Computer Science
2Aims
- Summary of rest of term 2
- Introduce priority queue structure
- The priority queue as an ADT
- Example applications
- Sorting
- Implementation Issues
3The Priority Queue
P5
P2
P1
P9
P25
- We call it a priority queue - but its not FIFO
- Items in queue have PRIORITY
- Elements are removed from priority queue in
either increasing or decreasing priority - Min Priority Queue
- Max Priority Queue
4The Priority Queue
P5
P2
P1
P9
P25
- Consider situation where we have a computer whose
services we are selling - Users need different amounts of time
- Maximise earnings by min priority queue of users
- i.e. when machine becomes free, the user who
needs least time gets the machine the average
delay is minimised
5The Priority Queue
P5
P2
P1
P9
P25
- Consider situation where users are willing to pay
more to secure access - they are in effect
bidding against each other - Maximise earnings by max priority queue of users
- i.e. when machine becomes free, the user who is
willing to pay most gets the machine
6The Priority Queue
- Common data structure in computer science
- Responsible for scheduling jobs
- Unix (linux) can allocate processes a priority
- Time allocated to process is based on priority of
job - Priority of jobs in print scheduler
7Max Priority Queue ADT
- AbstractDataType MaxPriorityQueue
- instances
- finite collection of zero or more elements each
has a priority - operations
- isEmpty() Return true if the queue is empty
else false - size() Return number of elements in the queue
- getMax() Return the element with the maximum
priority - put(x) Add element x into the priority queue
- removeMax() Remove the element with the
maximum priority from the priority and return it
8Max Priority Queue Interface
- public interface MaxPriorityQueue
- public boolean isEmpty()
- public int size()
- public Comparable getMax()
- public void put(Comparable theObject)
- public Comparable remove()
-
It may be desirable to include a method to allow
priority of elements to be changed (increased or
decreased) If we dont do this some items may
never be accessed!
9Application
- Sorting
- Use element key as priority
- Put elements to be sorted into a priority queue
- Extract elements in priority order
- if a min priority queue is used, elements are
extracted in ascending order of priority (or key) - if a max priority queue is used, elements are
extracted in descending order of priority (or
key)
10Sorting Example
- Sort five elements whose keys are 6, 8, 2, 4, 1
using a max priority queue - Step 1
- Put the five elements into a max priority queue
- Step 2
- Do five remove max operations placing removed
elements into the sorted array from right to left
11After Putting Into Max Priority Queue
- Max Priority Queue
- Sorted Array
6
8
4
1
2
12After First Remove Max Operation
- Max Priority Queue
- Sorted Array
4
6
1
2
13After Second Remove Max Operation
- Max Priority Queue
- Sorted Array
4
1
2
6
14After Third Remove Max Operation
- Max Priority Queue
- Sorted Array
1
2
6
4
15After Fourth Remove Max Operation
- Max Priority Queue
- Sorted Array
1
6
4
2
16After Fifth Remove Max Operation
- Max Priority Queue
- Sorted Array
6
4
2
1
17Complexity Of Sorting
- Sort n elements
- n put operations gt O(n log n) time
- n remove max operations gt O(n log n) time
- total time is O(n log n)
- Compare
- with O(n2) for sort methods like the bubblesort
- Heap Sort
- Uses a max priority queue that is implemented as
a heap - Initial put operations are replaced by a heap
initialization step that takes O(n) time
18Implementation Options
- Priority queue can be regarded as a linear list
- Unordered linear list
- Time complexities
- Ordered linear list
- Time complexities
put operation is O(1) removeMax is O(n)
put operation is O(n) removeMax is O(1)
19Implementation Options
- Priority queue can be regarded as a heap
- isEmpty, size, and get gt O(1) time
- put and remove gt O(log n) time
- where n is the size of the priority queue
- Were going to look at the heap
- A complete binary tree with values at its nodes
arranged in a particular way (the priority!)
i.e. this is better than linear list option on
average
20Min Tree Definition
- Each tree node has a value
- Value in any node is the minimum value in the
subtree for which that node is the root - Equivalently, no descendant has a smaller value
2
4
9
3
4
8
7
9
9
21Max Tree Example
9
4
9
8
4
2
7
3
1
22Heap Definitions
- Complete binary tree
- Min tree
- Complete binary tree
- Max tree
Min Heap
Max Heap
23Min Heap With 9 Nodes
- Complete binary tree with 9 nodes
24Min Heap With 9 Nodes
- Complete binary tree with 9 nodes that is also a
min tree
25Max Heap With 9 Nodes
- Complete binary tree with 9 nodes that is also a
max tree.
Since a heap is a complete binary tree, the
height of an n node heap is log2 (n1)
26A Heap Is Efficiently Represented As An Array
27Putting An Element Into A Max Heap
- Complete binary tree with 10 nodes
28Putting An Element Into A Max Heap
29Putting An Element Into A Max Heap
9
8
7
6
2
6
7
7
5
1
7
30Putting An Element Into A Max Heap
31Putting An Element Into A Max Heap
9
7
8
6
2
6
7
5
1
7
7
32Putting An Element Into A Max Heap
20
9
7
8
6
2
6
7
5
1
7
7
33Putting An Element Into A Max Heap
- Complete binary tree with 11 nodes
20
9
7
8
6
2
6
7
5
1
7
7
34Putting An Element Into A Max Heap
20
9
7
8
6
2
6
7
5
1
7
7
35Putting An Element Into A Max Heap
20
9
7
6
2
6
8
8
7
5
1
7
7
36Putting An Element Into A Max Heap
20
7
15
6
2
6
9
8
8
7
5
1
7
7
Complexity is O(log n), where n is heap size
37Removing The Max Element
- Max element is in the root
38Removing The Max Element
- After max element is removed - root is left empty
7
15
6
2
6
9
8
8
7
5
1
7
7
39Removing The Max Element
- Heap will have 10 nodes, reinsert last leaf node
(8) into the heap
7
15
6
2
6
9
8
8
7
5
1
7
7
40Removing The Max Element
7
15
6
2
6
9
7
5
1
7
7
41Removing The Max Element
15
7
6
2
6
9
7
5
1
7
7
42Removing The Max Element
15
7
9
6
2
6
8
7
5
1
7
7
43Removing The Max Element
15
7
9
6
2
6
8
7
5
1
7
7
44Removing The Max Element
- After max element is removed
7
9
6
2
6
8
7
5
1
7
7
45Removing The Max Element
- Heap with 9 nodes - reinsert last leaf node (7)
into the heap
7
9
6
2
6
8
7
5
1
7
7
46Removing The Max Element
7
9
6
2
6
8
5
1
47Removing The Max Element
9
7
6
2
6
8
5
1
48Removing The Max Element
9
8
7
6
2
6
7
5
1
Complexity is again O(log n)