Week 5 - Priority Queues
Week 5 - Priority Queues
WARNING
Sell orders
Matching
Engine
Buy orders
while True:
bid ← buy_orders.remove_max()
ask ← sell_orders.remove_min()
if bid.price ≥ ask.price then
carry out trade (bid, ask)
else
buy_orders.insert(bid)
sell_orders.insert(ask)
4 5 2 3 1 1 2 3 4 5
– insert in O(1) time since we can – insert in O(n) time since we have to find
insert the item at the beginning the place where to insert the item
or end of the sequence – remove_min and min in O(1) time since
– remove_min and min in O(n) time the smallest key is at the beginning
since we have to traverse the
entire list to find the smallest key
Complexity analysis:
def priority_queue_sorting(A):
– n insert operations
pq ← new priority queue
– n remove_min operations n ← size(A)
for i in [0:n] do
pq.insert(A[i])
Either sequence-based for i in [0:n] do
implementation take O(n2) A[i] = pq.remove_min()
i A s def selection_sort(A):
0 7, 4, 8, 2, 5, 3, 9 3 n ← size(A)
for i in [0:n] do
1 2, 4, 8, 7, 5, 3, 9 5 # find s ⩾ i minimizing A[s]
2 2, 3, 8, 7, 5, 4, 9 5 s ← i
3 2, 3, 4, 7, 5, 8, 9 4 for j in [i:n] do
if A[j] < A[s] then
4 2, 3, 4, 5, 7, 8, 9 4 s ← j
5 2, 3, 4, 5, 7, 8, 9 5 # swap A[i] and A[s]
A[i], A[s] ← A[s], A[i]
6 2, 3, 4, 5, 7, 8, 9 6
i A j def insertion_sort(A):
1 7, 4, 8, 2, 5, 3, 9 0 n ← size(A)
for i in [1:n] do
2 4, 7, 8, 2, 5, 3, 9 2 x ← A[i]
3 4, 7, 8, 2, 5, 3, 9 0 # move forward entries > x
4 2, 4, 7, 8, 5, 3, 9 2 j ← i
while j > 0 and x < A[j-1] do
5 2, 4, 5, 7, 8, 3, 9 1 A[j] ← A[j-1]
6 2, 3, 4, 5, 7, 8, 9 6 j ← j - 1
# if j>0 ⇒ x ≥ A[j-1]
# if j<i ⇒ x < A[j+1]
A[j] ← x
2
1. Heap-Order: for every node m ≠ root, 5 6
key(m) ³ key(parent(m))
9 7
2. Complete Binary Tree: let h be the height The last node is the
– every level i < h is full (i.e., there are 2i nodes) rightmost node of
maximum depth
– remaining nodes take leftmost positions of level h
(4,C)
(5,A) (6,Z)
depth keys
0 1
1 2
h-1 2h-1
h ≥1
insert(1)
2 2
5 6 5 6
9 7 9 7 1
9 7 6
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(2,T)
(5,A) (6,Z) (5,A) (5,A) (2,T)
(15,K) (9,F) (7,Q) (2,T) (15,K) (9,F) (7,Q) (15,K) (9,F) (7,Q) (6,Z)
(6,Z)
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B)
(2,T)
(2,T)
(4,C)
(5,A) (5,A) (4,C)
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B)
– Replace the root key with the key of the last node w
– Delete w
– Restore the heap-order property
remove_min()
2 7
5 9 5 9
w
6 7 6
6 9
Complexity: O(log n) time because
the height of the heap is log n 7 z
The University of Sydney Page 23
Example removal
(4,C) (13,W)
(13,W)
(13,W)
(5,A) (6,Z)
(5,A) (6,Z)
(5,A) (6,Z)
(15,K) (9,F) (7,Q) (20,B)
(15,K) (9,F) (7,Q) (20,B)
(15,K) (9,F) (7,Q) (20,B)
(16,X) (25,J) (14,E) (12,H) (11,S)
(16,X) (25,J) (14,E) (12,H) (11,S)
(16,X) (25,J) (14,E) (12,H) (11,S)
(5,A) (5,A)
(5,A)
(16,X) (25,J) (14,E) (12,H) (11,S) (16,X) (25,J) (14,E) (12,H) (11,S)
(16,X) (25,J) (14,E) (12,H) (11,S)
(5,A) (5,A)
Special nodes: 2
– root is at 0
5 6
– last node is at n-1
9 7
For the node at index i:
– the left child is at index 2i+1
– the right child is at index 2i+2
– Parent is at index ⌊(i-1)/2⌋
2 5 6 9 7
0 1 2 3 4
Heap-sort can be arranged to work in place using part of the array for
the output and part for the priority queue
Online trading system where orders are stored in two priority queues
(one for sell orders and one for buy orders) as (p, t, s) entries:
– The key is (p, t), the price of the order p and the time t
such that we first sort by p and break ties with t
– The value is s, the number of shares the order is for