![Page 1: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/1.jpg)
1
CSE 326: Data Structures Priority Queues (Heaps)
Lecture 9: Monday, Jan 27, 2003
![Page 2: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/2.jpg)
2
Not Quite Queues
• Consider applications– ordering CPU jobs– searching for the exit in a maze– emergency room admission processing
• Problems?– short jobs should go first– most promising nodes should be searched first– most urgent cases should go first
![Page 3: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/3.jpg)
3
Priority Queue ADT
• Priority Queue operations
• Priority Queue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y
F(7) E(5) D(100) A(4)
B(6)
insert deleteMin
G(9) C(3)
create : heapinsert : heap value heapfindMin : heap valuedeleteMin : heap heapis_empty : heap boolean
![Page 4: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/4.jpg)
4
Applications of the Priority Q
• Hold jobs for a printer in order of length• Store packets on network routers in order of
urgency• Simulate events• Anything greedy
![Page 5: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/5.jpg)
5
Discrete Event Simulation
• An event is a pair (x,t) where x describes the event and t is time it should occur
• A discrete event simulator (DES) maintains a set S of events which it intends to simulate in time order
repeat { Find and remove (x0,t0) from S such that t0 is minimum; Do whatever x0 says to do; in the process new events (x1,t1)…(xk,tk) may be generated; Insert the new events into S; }
![Page 6: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/6.jpg)
6
Emergency Room Simulation • Patient arrive at time t with injury of criticality C
– If no patients waiting and a free doctor, assign them to doctor and create a future departure event; else put patient in the Criticality priority queue
• Patient departs at time t– If someone in Criticality queue, pull out most critical and
assign to doctor; create a future departure event
timequeue
arrive(t,c)criticality(triage)queueassign
patient todoctor
patientgenerator depart(t)
depart(t)
arrive(t,c)
![Page 7: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/7.jpg)
7
Naïve Priority Queue Data Structures
• Unsorted list:– insert:– findMin:– deleteMin:
• Sorted list:– insert:– findMin:– deleteMin:
![Page 8: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/8.jpg)
8
BST Tree Priority Queue Data Structure
4
121062
115
8
14137 9
•Regular BST:–insert:–findMin:–deleteMin:
•AVL Tree:–insert:–findMin:–deleteMin:
Can we do better?
![Page 9: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/9.jpg)
9
Binary Heap Priority Q Data Structure
201412911
81067
54
2
• Heap-order property– parent’s key is less than
children’s keys– result: minimum is always
at the top
• Structure property– complete tree with fringe
nodes packed to the left– result: depth is always
O(log n); next open location always known
How do we find the minimum?
![Page 10: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/10.jpg)
10
201412911
81067
54
2
2 4 5 7 6 10 8 11 9 12 14 20121 2 3 4 5 6 7 8 9 10 11 12
1
2 3
4 5 6 7
8 9
10 11 12
Nifty Storage Trick• Calculations:
– child:
– parent:
– root:
– next free:
0
![Page 11: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/11.jpg)
11
201412911
81067
54
2
2 4 5 7 6 10 8 11 9 12 14 20121 2 3 4 5 6 7 8 9 10 11 12
1
2 3
4 5 6 7
8 9
10 11 12
Nifty Storage Trick• Calculations:
– child: left = 2*node right=2*node+1– parent: floor(node/2)
– root: 1
– next free: length+1
0
![Page 12: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/12.jpg)
12
findMin
201412911
81067
54
2
pqueue.findMin()
2
Time = O(1)
![Page 13: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/13.jpg)
13
DeleteMin
201412911
81067
54
202
201412911
81067
54
2
pqueue.deleteMin()
![Page 14: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/14.jpg)
14
Percolate Down
1412911
81067
54
20
1412911
81067
520
4
1412911
810207
56
4
1420911
810127
56
4
![Page 15: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/15.jpg)
15
DeleteMin CodeComparable deleteMin(){ x = A[1]; A[1]=A[size--]; percolateDown(1); return x;}
percolateDown(int hole) { tmp=A[hole]; while (2*hole <= size) { left = 2*hole; right = left + 1; if (right <= size && A[right] < A[left]) target = right; else target = left; if (A[target] < tmp) { A[hole] = A[target]; hole = target; } else break; } A[hole] = tmp;}
Trick to avoid repeatedly copying the value at A[1]
Move downTime = O(log n)(why ?)
![Page 16: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/16.jpg)
16
Insert
201412911
81067
54
2
201412911
81067
54
2
pqueue.insert(3)
3
![Page 17: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/17.jpg)
17
Percolate Up
201412911
81067
54
2
3 201412911
8367
54
2
10
201412911
8567
34
2
10
![Page 18: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/18.jpg)
18
Insert Codevoid insert(Comparable x) { // Efficiency hack: we won’t actually put x // into the heap until we’ve located the position // it goes in. This avoids having to copy it // repeatedly during the percolate up. int hole = ++size; // Percolate up for( ; hole>1 && x < A[hole/2] ; hole = hole/2) A[hole] = A[hole/2]; A[hole] = x;}
Time = O(log n)(why ?)
![Page 19: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/19.jpg)
19
Performance of Binary Heap
• In practice: binary heaps much simpler to code, lower constant factor overhead
Binary heapworst case
Binary heap avg case
AVL tree worst case
BST tree avg case
Insert O(log n) O(1)percolates 1.6 levels
O(log n) O(log n)
Delete Min
O(log n) O(log n) O(log n) O(log n)
![Page 20: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/20.jpg)
20
Changing Priorities
• In many applications the priority of an object in a priority queue may change over time– if a job has been sitting in the printer queue for a long
time increase its priority– unix “renice”
• Must have some (separate) way of find the position in the queue of the object to change (e.g. a hash table)
![Page 21: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/21.jpg)
21
Other Priority Queue Operations
• decreaseKey – Given the position of an object in the queue, increase
its priority (lower its key). Fix heap property by:• increaseKey
– given the position of an an object in the queue, decrease its priority (increase its key). Fix heap property by:
• remove– given the position of an an object in the queue, remove
it. Do increaseKey to infinity then …
![Page 22: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/22.jpg)
22
BuildHeap
• Task: Given a set of n keys, build a heap all at once
• Approach 1: Repeatedly perform Insert(key)
• Complexity:
![Page 23: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/23.jpg)
23
BuildHeapFloyd’s Method
5 11 3 10 6 9 4 8 1 7 212
pretend it’s a heap and fix the heap-order property!
27184
96103
115
12buildHeap(){
for (i=size/2; i>0; i--)
percolateDown(i);
}
![Page 24: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/24.jpg)
24
Build(this)Heap
67184
92103
115
12
671084
9213
115
12
1171084
9613
25
12
1171084
9653
21
12
![Page 25: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/25.jpg)
25
Finally…
11710812
9654
23
1
![Page 26: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/26.jpg)
26
Complexity of Build Heap• Note: size of a perfect binary tree doubles (+1)
with each additional layer• At most n/4 percolate down 1 level
at most n/8 percolate down 2 levelsat most n/16 percolate down 3 levels…
O(n)
22
4242...
163
82
41
log
1
log
12
nninninnn n
ii
n
ii
![Page 27: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/27.jpg)
27
Thinking about Heaps
• Observations– finding a child/parent index is a multiply/divide by two– operations jump widely through the heap– each operation looks at only two new nodes– inserts are at least as common as deleteMins
• Realities– division and multiplication by powers of two are fast– looking at one new piece of data terrible in a cache line– with huge data sets, disk accesses dominate
![Page 28: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/28.jpg)
28
4
9654
23
1
8 1012
7
11
Solution: d-Heaps• Each node has d children• Still representable by array• Good choices for d:
– optimize performance based on # of inserts/removes
– choose a power of two for efficiency
– fit one set of children in a cache line
– fit one set of children on a memory page/disk block
3 7 2 8 5 12 11 10 6 9112
![Page 29: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/29.jpg)
29
New Operation: Merge
Merge(H1,H2): Merge two heaps H1 and H2 of size O(N). – E.g. Combine queues from two different sources to run on one
CPU.
1. Can do O(N) Insert operations: O(N log N) time2. Better: Copy H2 at the end of H1 (assuming array
implementation) and use Floyd’s Method for BuildHeap.Running Time: O(N)
Can we do even better? (i.e. Merge in O(log N) time?)
![Page 30: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/30.jpg)
30
Binomial Queues
• All in O(log n) time• Recursive Definition of Binomial Tree Bk of height k:
– B0 = single root node– Bk = Attach Bk-1 to root of another Bk-1
• Idea: a binomial heap H is a forest of binomial trees:H = B0 B1 B2 ... Bk
where each Bi may be present, or may be empty
insert : heap value heapfindMin : heap valuedeleteMin : heap heapmerge : heap heap heap
![Page 31: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/31.jpg)
31
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 32: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/32.jpg)
32
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 33: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/33.jpg)
33
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 34: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/34.jpg)
34
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 35: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/35.jpg)
35
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 36: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/36.jpg)
36
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
![Page 37: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/37.jpg)
37
Building a Binomial Tree• To construct a binomial tree Bk of height k:
1. Take the binomial tree Bk-1 of height k-1
2. Place another copy of Bk-1 one level below the first
3. Attach the root nodes• Binomial tree of height k has exactly 2k nodes (by induction)
B0 B1 B2 B3
Recall: a binomial heap may have any subset of these trees
![Page 38: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/38.jpg)
38
Why Binomial?• Why are these trees called binomial?
– Hint: how many nodes at depth d?
B0 B1 B2 B3
![Page 39: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/39.jpg)
39
Why Binomial?• Why are these trees called binomial?
– Hint: how many nodes at depth d?Number of nodes at different depths d for Bk =
[1], [1 1], [1 2 1], [1 3 3 1], …Binomial coefficients of (a + b)k : k!/((k-d)!d!)
B0 B1 B2 B3
![Page 40: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/40.jpg)
40
Binomial Queue PropertiesSuppose you are given a binomial queue of N nodes1. There is a unique set of binomial trees for N nodes2. What is the maximum number of trees that can be in
an N-node queue?– 1 node 1 tree B0; 2 nodes 1 tree B1; 3 nodes 2 trees
B0 and B1; 7 nodes 3 trees B0, B1 and B2 …
– Trees B0, B1, …, Bk can store up to 20 + 21 + … + 2k = 2k+1 – 1 nodes = N.
– Maximum is when all trees are used. So, solve for (k+1).– Number of trees is log(N+1) = O(log N)
![Page 41: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/41.jpg)
41
Definition of Binomial Queues
3
Binomial Queue = “forest” of heap-ordered binomial trees
1
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
B0 B2 B0 B1 B3
Binomial queue H1
5 elements = 101 base 2 B2 B0
Binomial queue H2
11 elements = 1011 base 2 B3 B1 B0
![Page 42: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/42.jpg)
42
findMin()• In each Bi, the minimum key is at the root• So scan sequentially B1, B2, ..., Bk, compute the
smallest of their keys:• Time: O(log n) (why ?)
B0 B1 B2 B3
![Page 43: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/43.jpg)
43
Binomial Queues: Merge• Main Idea: Merge two binomial queues by merging
individual binomial trees– Since Bk+1 is just two Bk’s attached together, merging trees
is easy• Steps for creating new queue by merging:
1. Start with Bk for smallest k in either queue.
2. If only one Bk, add Bk to new queue and go to next k.
3. Merge two Bk’s to get new Bk+1 by making larger root the child of smaller root. Go to step 2 with k = k + 1.
![Page 44: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/44.jpg)
44
Example: Binomial Queue Merge
31
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
H1: H2:
![Page 45: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/45.jpg)
45
Example: Binomial Queue Merge
31
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
H1: H2:
![Page 46: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/46.jpg)
46
Example: Binomial Queue Merge
3
1
7
-1
2 1 3
8 11 5
6
5
9 6
721
H1: H2:
![Page 47: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/47.jpg)
47
Example: Binomial Queue Merge
3
1
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
H1: H2:
![Page 48: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/48.jpg)
48
Example: Binomial Queue Merge
3
1
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
H1: H2:
![Page 49: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/49.jpg)
49
Example: Binomial Queue Merge
3
1
7
-1
2 1 3
8 11 5
6
5
9 6
7
21
H1: H2:
![Page 50: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/50.jpg)
50
Binomial Queues: Merge and Insert
• What is the run time for Merge of two O(N) queues?
• How would you insert a new item into the queue?
![Page 51: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/51.jpg)
51
Binomial Queues: Merge and Insert
• What is the run time for Merge of two O(N) queues?– O(number of trees) = O(log N)
• How would you insert a new item into the queue?– Create a single node queue B0 with new item and
merge with existing queue– Again, O(log N) time
• Example: Insert 1, 2, 3, …,7 into an empty binomial queue
![Page 52: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/52.jpg)
52
Insert 1,2,…,7
1
![Page 53: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/53.jpg)
53
Insert 1,2,…,7
1
2
![Page 54: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/54.jpg)
54
Insert 1,2,…,7
1
2
3
![Page 55: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/55.jpg)
55
Insert 1,2,…,7
1
2
3
4
![Page 56: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/56.jpg)
56
Insert 1,2,…,7
1
2 3
4
![Page 57: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/57.jpg)
57
Insert 1,2,…,7
1
2 3
4
5
![Page 58: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/58.jpg)
58
Insert 1,2,…,7
1
2 3
4
5
6
![Page 59: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/59.jpg)
59
Insert 1,2,…,7
1
2 3
4
5
6
7
![Page 60: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/60.jpg)
60
Binomial Queues: DeleteMin• Steps:
1. Find tree Bk with the smallest root
2. Remove Bk from the queue
3. Delete root of Bk (return this value); You now have a new queue made up of the forest B0, B1, …, Bk-1
4. Merge this queue with remainder of the original (from step 2)
• Run time analysis: Step 1 is O(log N), step 2 and 3 are O(1), and step 4 is O(log N). Total time = O(log N)
• Example: Insert 1, 2, …, 7 into empty queue and DeleteMin
![Page 61: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/61.jpg)
61
Insert 1,2,…,7
1
2 3
4
5
6
7
![Page 62: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/62.jpg)
62
DeleteMin
2 3
4
5
6
7
![Page 63: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/63.jpg)
63
Merge
2 3
4
5
6
7
![Page 64: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/64.jpg)
64
Merge
2 3
4
5
6
7
![Page 65: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/65.jpg)
65
Merge
2
3
4
5
6
7
![Page 66: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/66.jpg)
66
Merge
2
3
4
5
6
7
DONE!
![Page 67: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/67.jpg)
67
Implementation of Binomial Queues• Need to be able to scan through all trees, and given
two binomial queues find trees that are same size– Use array of pointers to root nodes, sorted by size– Since is only of length log(N), don’t have to worry
about cost of copying this array– At each node, keep track of the size of the (sub) tree
rooted at that node• Want to merge by just setting pointers
– Need pointer-based implementation of heaps • DeleteMin requires fast access to all subtrees of root
– Use First-Child/Next-Sibling representation of trees
![Page 68: 1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1b797f8b9ab0599b8814/html5/thumbnails/68.jpg)
68
Efficient BuildHeap for Binomial Queues
• Insert one at a time - O(n log n)• Better algorithm:
– Start with each element as a singleton tree– Merge trees of size 1– Merge trees of size 2– Merge trees of size 4
• Complexity:
log log
1 1
log
1
(1 log1) (1 log 2) (1 log 4) ...2 4 8
(1 ) ( )2 2
because 22
n n
i ii i
n
ii
n n n
n i iO n O n
i