Priority Queues - Worksheet Collection of priority-job pairs; priorities are comparable.
• insert(p, j):insertjobjwith priorityp • max():read(-only) job of max priority
• extract-max():readandremovejobjof max priority • increase-priority(j, p0):increasepriority of jobjtop0 Applications: • • • • • Use Heaps! Aheapis one way to store apriority queue. Aheapis:
• abinary tree
• “nearly complete”: every levelihas2inodes, except the bottom level; the bottom fills inleft to right • at each node: itspriorityis≥bothchildren’s priorities
16 14 8 2 4 7 1 10 9 3
(only priorities shown) Heap Insert: Example
Q.How do we insert a key withpriority 15?
X The tree is still “nearly-complete”.
X Order of priorities bad. Fix? X Order of priorities good.
16 14 8 2 4 7 1 15 10 9 3 Final Tree:
Heap insert: Summary
1. Create new leaf atbottom level,leftmostopen location⇐maintains requirement that the tree is “nearly-complete” 2. Assignpriority(and job) in the new node
3. v = the new node 4. Percolatevup:
whilevhasparentwithsmallerpriority: swapthem
v=v .parent Complexity?
Worst case timeΘ(height ).
Later we will see whyheight = blg nc + 1 ∈ Θ(lg n).
Heap extract-max Example Extract themaxpriority key, then:
Replace by thebottom level,rightmostitem andheapify! X The tree is still “nearly-complete”.
X Order of prioritiesbad. Fix?
⇒ swapwith the larger child. Why not the smaller child? X Order of priorities good.
? 15 8 2 4 14 1 7 10 9 3 7 15 8 2 4 14 1 10 9 3
Heap extract-max: Summary 1. Replaceroot bybottom level,rightmost item⇐keeps the tree “nearly-complete”.
2. v :=root 3. Heapify:
whilevhaslargerchild: swapwith thelargestchild vassigned thatchildnode Worst caseΘ(height )time.
The steps in line 3 are an algorithm we will use again calledHeapify.
Binary Tree Size - Determining the Height Letnbe thenumber of nodes,hbe theheight.
Q.How manynodesdoes afullbinary tree ofheighthhave? A. h n 1 2 3 4 5 i So a binary tree ofheighthhasat most nodes.
Heap: Height We will boundnto findh. A tree of heighthcan have arangeof values forn. Q.What is thisrange? Draw the smallest heap possible and largest heap possible. A.
smallest heap of height h ≤ n ≤ largest heap of height h ≤ n ≤ ≤n≤ ≤n< ≤n< ≤n< h =
Implementing a Heap
H
EAP INARRAY
/VECTOR
16 1 14 2 8 4 2 8 9 4 7 5 1 10 10 3 9 6 7 3 16 14 10 8 7 9 3 2 4 1 0 1 2 3 4 5 6 7 8 9 10 11 Observations: Startindex is 1.
Left childof nodeiis at2i. Right childof nodeiis at2i + 1.
Anna Bretscher CSCB63 Winter 2018 January 27, 2018 11 / 20
HEAP IN
ARRAY/VECTOR
16 1 14 2 8 4 2 8 9 4 7 5 1 10 10 3 9 6 7 3 16 14 10 8 7 9 3 2 4 1 0 1 2 3 4 5 6 7 8 9 10 11 Observations: Startindex is 1.Left childof nodeiis at2i. Right childof nodeiis at2i + 1.
Anna Bretscher CSCB63 Winter 2018 January 27, 2018 11 / 20
Observations:
• Startindex is 1. May need to storeheap size. • Left childof nodeiis at2i.
• Right childof nodeiis at2i + 1. • Parentof nodeiis atbi
2c.
Heap in Array/Vector
H
EAP IN
A
RRAY
/V
ECTOR
16 1 14 2 8 4 2 8 9 4 7 5 1 10 10 3 9 6 7 3 16 14 10 8 7 9 3 2 4 1 0 1 2 3 4 5 6 7 8 9 10 11 Observations: Startindex is 1.
Left childof nodei is at2i. Right childof nodei is at2i + 1.
Anna Bretscher CSCB63 Winter 2018 January 27, 2018 11 / 20 Q.How do weinsertinto a heap with anarray?
A.
Q.How do weextract max? A.
Increase a Priority
Q.If we knowwherea node is in aheapstored in arrayA, how can weincreasethepriority valueinΘ(log n)time? A.
increase priority(A, index, p):
# array A, int index, priority p
if p > A[index]:
# priority is not better so do nothing
return
A[index] = p
while A[bindex/2c] < A[index]: What is this? percolate or heapify?
swap them
Building heaps
Given an arrayAofelementswithpriorities, whose onlyempty slotsare at the farright, then we can viewAas acomplete binary tree...
...but not necessarily aheap... Q.How can we turnAinto aheap?
1. Option 1. Complexity? 2. Option 2.
Complexity? 3. Option 3.
• Use the fact that each subtree of a heap is a heap.
• Start with thesmallest subtrees, turn into heaps usingheapify. • Workupthe tree.
Heapify
We already sawHeapifywhen we didextract-max. Remind yourself of how heapify works:
heapify 1 14 5th 8 2 4th 9 4 2nd 3 8 9 7 2 5 1st 10 10 16 3 3rd 1 6 7 4
Q. On whichnodesshould we callheapify? A.
Q.How do we calculate this node? A.
build heap(A, heapsize)
for i = bheapsize/2c down to 1:
heapify(A, i, heapsize)
heapify(A, i, size): # array A, start index i, int
size = heapsize
max = i
if (2i<=size and A[2i]>A[i]): max = 2i
if (2i+1 ≤ size and A[2i+1]>A[max]): max = 2i+1
if ( max != i ):
swap A[i] and A[max] heapify(A,max,size)
Building A Heap - Example 14 1 5th 8 2 4th 9 4 2nd 3 8 9 7 2 5 1st 10 10 16 3 3rd 1 6 7 4 14 8 16 9 2 1 4 3 7 10 0 1 2 3 4 5 6 7 8 9 10 11 5th 4th 3rd 2nd 1st
Build a Heap Complexity A node at heighthtakesh − 1steps to fix.
Q. At most how manynodesare there at heighth? Draw a tree/chart to determine the answer: A.
We need to sum the cost of each call toheapify.
? X
h=2
(num trees of height h) × (h − 1) =
≤ =
Heaps and Sorting Q.Given anarray, how can we use aheapto efficientlysortthe array? A.
• •
• Total time
-Repeatedly Extract - Max Example To Sort
0 1 2 3 4 5 6 7 8 9 10 11 16 10 14 9 8 1 4 3 7 2 16 1 10 2 9 4 3 8 9 7 8 5 2 10 14 3 1 6 7 4 while heap-size > 1: A[heapsize] = extract-max() heap-size = heap-size - 1 Max vs Min We have been storinglarger prioritiesnear the top to support
max, extract-max, increase-priority
⇒These aremax priority queuesandmax-heaps. But you could storesmaller prioritiesnear the top to support
min, extract-min, decrease-priority
⇒These aremin priority queuesandmin-heaps. Example ofmin priority queue: A todo-list with start times. Another application: coming soon!
Min-Heap Example 11 1 17 2 23 4 29 8 9 47 31 5 37 10 13 3 43 6 7 27 11 17 13 23 31 43 27 29 47 37 0 1 2 3 4 5 6 7 8 9 10 11