Lecture08_PriorityQueue
Lecture08_PriorityQueue
Algorithms
"
Priority Queues!
Outline"
• Priority Queues!
• Heaps!
• Adaptable Priority Queues!
• Performance:!
– insert takes O(1) time
• Performance:!
since we can insert the – insert takes O(n) time
item at the beginning or since we have to find the
end of the sequence! place where to insert the
– removeMin and min take item!
O(n) time since we have – removeMin and min take
to traverse the entire O(1) time, since the
sequence to find the
smallest key ! smallest key is at the
beginning!
Phạm Bảo Sơn - DSA
Selection-Sort"
• Selection-sort is the variation of PQ-sort where the
priority queue is implemented with an unsorted
sequence!
• Running time of Selection-sort:!
1. Inserting the elements into the priority queue with n insert
operations takes O(n) time!
2. Removing the elements in sorted order from the priority
queue with n removeMin operations takes time
proportional to
! ! !1 + 2 + …+ n!
• Selection-sort runs in O(n2) time !
Phạm Bảo Sơn - DSA
Selection-Sort Example"
!Sequence S ! !Priority Queue P!!
Input: ! !(7,4,8,2,5,3,9) ! !() !!
!
Phase 1!!
!(a) ! !(4,8,2,5,3,9) ! !(7) !!
!(b) ! !(8,2,5,3,9) ! !(7,4) !!
!.. ! !.. !.. !!
!. ! !. !. !!
!(g) ! !() ! ! !(7,4,8,2,5,3,9) !!
Phase 2!!
!(a) ! !(2) ! ! !(7,4,8,5,3,9) !!
!(b) ! !(2,3) ! ! !(7,4,8,5,9) !!
!(c) ! !(2,3,4) ! ! !(7,8,5,9)!!
!(d) ! !(2,3,4,5)! ! !(7,8,9) !!
!(e) ! !(2,3,4,5,7) ! !(8,9) !!
!(f) ! !(2,3,4,5,7,8) ! !(9) !!
!(g) ! !(2,3,4,5,7,8,9) ! !()!
Phạm Bảo Sơn - DSA
Insertion-Sort"
• Insertion-sort is the variation of PQ-sort where the
priority queue is implemented with a sorted
sequence!
• Running time of Insertion-sort:!
1. Inserting the elements into the priority queue with n insert
operations takes time proportional to
! ! !1 + 2 + …+ n!
2. Removing the elements in sorted order from the priority
queue with a series of n removeMin operations takes
O(n) time!
• Insertion-sort runs in O(n2) time !
5 6
9 7
Recall Priority Queue ADT"
• A priority queue stores a • Additional methods!
collection of entries! – min()
• Each entry is a pair returns, but does not
(key, value)! remove, an entry with
smallest key!
• Main methods of the Priority – size(), isEmpty()!
Queue ADT!
– insert(k, x)
inserts an entry with key k
and value x! • Applications:!
– removeMin() – Standby flyers!
removes and returns the – Auctions!
entry with smallest key! – Stock market!
depth keys
0 1
1 2
h-1 2h-1
h 1
2 1
5 1 5 2
9 7
z 6 9 7
z 6
7 5
5 6 7 6
w w
9 9
16 15 4 12 6 7 23 20
25 5 11 27
16 15 4 12 6 7 23 20
25 5 11 27
16 15 4 12 6 9 23 20
15 4 6 20
16 25 5 12 11 9 23 27
15 4 6 20
16 25 5 12 11 9 23 27
4 6
15 5 8 20
16 25 7 12 11 9 23 27
4 6
15 5 8 23
16 25 7 12 11 9 27 20
5 6
15 7 8 23
16 25 10 12 11 9 27 20
5 g 4 e
Recall the Entry and Priority
Queue ADTs"
• An entry stores a (key, • Priority Queue ADT:!
value) pair within a data – insert(k, x)
structure! inserts an entry with
key k and value x!
• Methods of the entry
ADT:! – removeMin()
removes and returns
– key(): returns the key the entry with
associated with this smallest key!
entry!
– min()
– value(): returns the value returns, but does not
paired with the key remove, an entry
associated with this with smallest key!
entry!
– size(), isEmpty()!
Phạm Bảo Sơn - DSA
Motivating Example"
• Suppose we have an online trading system where orders to
purchase and sell a given stock are stored in two priority queues
(one for sell orders and one for buy orders) as (p,s) entries:!
– The key, p, of an order is the price!
– The value, s, for an entry is the number of shares!
– A buy order (p,s) is executed when a sell order (p’,s’) with price
p’<p is added (the execution is complete if s’>s)!
– A sell order (p,s) is executed when a buy order (p’,s’) with price
p’>p is added (the execution is complete if s’>s)!
• What if someone wishes to cancel their order before it
executes?!
• What if someone wishes to update the price or number of
shares for their order?!
2 c 4 c 5 c 8 c
entries
Phạm Bảo Sơn - DSA
Heap Implementation"
• A location-aware 2 d
heap entry is an
object storing! 4 a 6 b
– key!
– value!
– position of the entry
in the underlying
heap!
• In turn, each heap
position stores an
entry!
• Back pointers are
updated during 8 g 5 e 9 c
entry swaps!
Phạm Bảo Sơn - DSA
Performance"
• Using location-aware entries we can achieve
the following running times (times better than
those achievable without location-aware
entries are highlighted in red):!
Method Unsorted List Sorted List Heap
size, isEmpty O(1) O(1) O(1)
insert O(1) O(n) O(log n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(log n)
remove O(1) O(1) O(log n)
replaceKey O(1) O(n) O(log n)
replaceValue O(1) O(1) O(1)"
Phạm Bảo Sơn - DSA