ds(ii) ch09 priorityqueues
TRANSCRIPT
-
8/10/2019 DS(II) Ch09 PriorityQueues
1/65
1
C-C Tsai P.1
Chapter 9 Priority Queues
Single- and Double-EndedPriority Queues
Leftist Trees
Binomial Heaps
Fibonacci Heaps
Pairing Heaps Min-Max Heaps
Interval Heaps
C-C Tsai P.2
Single- and Double-EndedPriority Queues
A priority queue is a collection of elements suchthat each element has an associated priority.
For single-ended priority queues (SEPQ), theoperations supported by a min priority queue
are: SP1: Return an element with minimum priority.
minElement(); minKey();
SP2: Insert an element with an arbitrary priority.
insert();
SP3: Delete an element with minimum priority.
deleteMin();
Other ADTs: size(); isEmpty();
-
8/10/2019 DS(II) Ch09 PriorityQueues
2/65
2
C-C Tsai P.3
Illustration of Priority Queue
{}-deleteMin()
{(7,D),(9,C)}-deleteMin(){(9,C)}-deleteMin()
{(5,A),(7,D),(9,C)}AminElement()
{}trueisEmpty()
{}errordeleteMin()
{(5,A),(7,D),(9,C)}3size()
{(5,A),(7,D),(9,C)}-removeMin()
{(3,B),(5,A),(7,D),(9,C)}3minKey()
{(3,B),(5,A),(7,D),(9,C)}BminElement()
{(3,B),(5,A),(7,D),(9,C)}-insert(7,D)
{(3,B),(5,A),(9,C)}-insert(3,B)
{(5,A),(9,C)}-insert(9,C)
{(5,A)}-insert(5,A)
Priority QueueoutputOperation
C-C Tsai P.4
Heaps Applied for Priority Queue Max heap tree: a tree in which the key value in
each node is no smaller than the key values in itschildren. A max heapis a complete binary treethat is also a max tree.
Min heap tree: a tree in which the key value in
each node is no larger than the key values in itschildren. A min heapis a complete binary treethat is also a min tree.
[4]
14
12 7
810 6
[1]
[2] [3]
[5] [6]
2
7 4
810 6
[1]
[2] [3]
[5] [6][4]
max heap tree min heap tree
-
8/10/2019 DS(II) Ch09 PriorityQueues
3/65
3
C-C Tsai P.5
Double-Ended Priority Queues
For double-ended priority queues (DEPQ), theoperations supported by a min priority queue are: SP1: Return an element with minimum priority.
SP2: Return an element with maximum priority.
SP3: Insert an element with an arbitrary priority.
SP4: Delete an element with minimum priority.
SP5: Delete an element with maximum priority.
Primary operations
Insert
Delete Max Delete Min
Note that a single-ended priority queue supports justone of the above remove operations.
C-C Tsai P.6
Leftist Trees
Leftist trees provide an efficientimplementation of meldable priority queues.
Linked binary tree.
Can do everything a heap can do and in the
same asymptotic complexity. Can meld two leftist tree priority queues in
O(log n) time.
-
8/10/2019 DS(II) Ch09 PriorityQueues
4/65
4
C-C Tsai P.7
Two extended binary trees
Extended Binary Trees
A G
B HC I
ED F J
Internal node
External node
C-C Tsai P.8
Height-Based Leftist Tree (HBLT)
For any node xin an extended binary tree. Let leftChild(x) andrightChild(x) denote the left and right children of the internal x,respectively. Define shortest(x) be the length of a shortest path from xto an external node in the subtree rooted at x.
If x is an external node, shortest(x) = 0;
otherwiseshortest(x) = 1+min {shortest(leftChild(x)), shortest(rightChild(x))}
2 2
2 11 1
11 1 1
-
8/10/2019 DS(II) Ch09 PriorityQueues
5/65
5
C-C Tsai P.9
The Definition of Leftist Trees
A leftist tree is a binary tree such that if it is notempty, then for every internal node x:
shortest(leftChild(x) >= shortest(rightChild(x))
2 2
2 11 1
11 1 1
Is not a leftist tree Is a leftist tree
C-C Tsai P.10
Lemma of Leftist Trees Let x be the root of a leftist tree that has ninternal nodes.
a) n >= 2shortest(x) 1
b) The rightmost root to external node path is the shortestroot to external node path. Its length is
shortest(x)
-
8/10/2019 DS(II) Ch09 PriorityQueues
6/65
6
C-C Tsai P.11
Min-Leftist Trees
Definition:A min-leftist tree (max-leftist tree) isa leftist tree in which the key value in eachnode is no larger (smaller) than the key valuesin its children (if any).
Two min-leftist trees:
2
7 50
11 80
13
5
9 8
12 10
20 1518
C-C Tsai P.12
Combination of leftist trees
The combination steps (min-leftist trees)
1. Choose minimum root of the two trees,Aand B.
2. Leave the left subtree of smaller root (supposeA)unchanged and combine the right subtree ofAwith
B. Back to step 1, until no remaining vertices.3. Compare shortest(x) and swap to make it satisfy
the definition of leftist trees.
-
8/10/2019 DS(II) Ch09 PriorityQueues
7/65
7
C-C Tsai P.13
Example1: Meld Two Leftist Trees
2
7
5011
8013
5
9 8
12 10
20 1518
2
7 50
11 80
13
5
9 8
12 10
20 1518
2
1
2
1
21
1
1
1
1
11
1
1
combine
C-C Tsai P.14
Example1: Meld Two Leftist Trees
2
7
11
13
5
9
12
20 18
8
10
15
2
7
11
13
5
9
12
20 18
50
80
8
10
15
50
80
-
8/10/2019 DS(II) Ch09 PriorityQueues
8/65
8
C-C Tsai P.15
Example1: Meld Two Leftist Trees
2
7
50
11
80
13
5
9 8
12 10
20 1518
8
10
15
2
7
11
13
5
9
12
20 18
50
80
C-C Tsai P.16
Example1: Meld Two Leftist Trees2
7
50
11
80
13
5
9 8
12 10
20 1518
2
1
1
1
2
2
1
1111
2
1
1
2
7
50
11
80
13
5
98
1210
2015 18
2
1
1
1
2
2
1
11 11
2
1
1
2
7
50
11
80
13
5
9 8
12 10
20 1518
2
1
1
1
2
2
1
1111
2
1
1
exchange1 exchange2
-
8/10/2019 DS(II) Ch09 PriorityQueues
9/65
9
C-C Tsai P.17
Combination of leftist trees
Step 1. Choose smallerroot and set as a.
Step 2. If a has noright_chlid, set b as asright_child. Else,recursively combine asright_child and b.
Step 3. Make thecombined tree satisfiedthe leftist tree property.
O(log n)
C-C Tsai P.18
Combination of leftist trees
Both insert and delete min operations can beimplemented by using the combine operation.
Insert: Treat the inserting node as a single nodebinary tree. Combine with the original one.
Delete: Remove the node can get two separatesubtrees. Combine the two trees.
-
8/10/2019 DS(II) Ch09 PriorityQueues
10/65
10
C-C Tsai P.19
Weight-Based Leftist Tree (WBLT) Define the weigth w(x) of node x to be the number of internal
nodes in the subtree with root x. The weight is 0 for an externalnode and is 1+ sum of the children weights of an internalnode.
A binary tree is a WBLT iff at every internal node the w value of theleft child is greater than or equal to the w value of the right child.
For any node xin an extended binary tree. The length, rightmost(x),of the rightmost path from xto an external node satisfies
rightmost(x)
-
8/10/2019 DS(II) Ch09 PriorityQueues
11/65
11
C-C Tsai P.21
Definition of Binomial Heaps
A min (max) binomial heap (B-heap) is acollection of min (max) trees.
The min trees should be Binomial trees.
Example: a B-heap consists of three min trees
10
8 5
3
6
4 15
12
20
30
7
1
16
9
C-C Tsai P.22
Representation of Binomial Heaps The representation of B-heap:
Degree: number of children a node has. Child: point to any one of its children.
Left_link, Right_link: maintain doubly linked circularlist of siblings.
The position of pointer a is the min element.
8
10
3
5 4
6
1
7 1612
15 30
20
9
Siblings
parent
child
a
pointera
-
8/10/2019 DS(II) Ch09 PriorityQueues
12/65
12
C-C Tsai P.23
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
40
Combine a min tree of 40
C-C Tsai P.24
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
40
Combine a min tree of 40
-
8/10/2019 DS(II) Ch09 PriorityQueues
13/65
13
C-C Tsai P.25
Pairwise1 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
C-C Tsai P.26
Pairwise1 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
-
8/10/2019 DS(II) Ch09 PriorityQueues
14/65
14
C-C Tsai P.27
Pairwise2 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
C-C Tsai P.28
Pairwise2 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
-
8/10/2019 DS(II) Ch09 PriorityQueues
15/65
15
C-C Tsai P.29
Pairwise2 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
C-C Tsai P.30
Pairwise3 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
-
8/10/2019 DS(II) Ch09 PriorityQueues
16/65
16
C-C Tsai P.31
Pairwise3 combine
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
C-C Tsai P.32
Combination of Binomial Heaps
8
10
3
5 4
6
1
7 1612
15 30
20
9
a
20
40
Pairwise3 combine
-
8/10/2019 DS(II) Ch09 PriorityQueues
17/65
17
C-C Tsai P.33
Combination of Binomial Heaps
The insertion is to combine a single vertex B-heap to original B-heap.
After we delete the min element, we get severalB-heaps that originally subtrees of the removedvertex. Then combine them together.
C-C Tsai P.34
Time Complexity of Binomial Heaps
Binomial heapsLeftist trees
Actual Amortized
Insert O(log n) O(1) O(1)
Delete min (or max) O(log n) O(n) O(log n)
Meld O(log n) O(1) O(1)
-
8/10/2019 DS(II) Ch09 PriorityQueues
18/65
18
C-C Tsai P.35
Fibonacci Heaps
A min (max) Fibonacci heap (F-heap) is acollection of min (max) trees.
The min trees need not be Binomial trees.
B-heaps are a special case of F-heaps.
So that what B-heaps can do can be done in F-heaps.
More than that, F-heap may delete an arbitrary nodeand decrease key.
C-C Tsai P.36
Deletion From an F-heap
The deletion of 12
8
10
3
5 4
6
1
7 1612
15 30
20
9
-
8/10/2019 DS(II) Ch09 PriorityQueues
19/65
19
C-C Tsai P.37
Deletion From an F-heap
After the deletion of 12
8
10
3
5 4
6
1
7 16
15 30
20
9
C-C Tsai P.38
Deletion From an F-heap
After the deletion of 12
8
10
3
5 4
6
1
7 16
15 30
20
9
-
8/10/2019 DS(II) Ch09 PriorityQueues
20/65
20
C-C Tsai P.39
Decrease Key From an F-heap
Decrease key of 15 to be 11
8
10
3
5 4
6
1
7 1612
15 30
20
911
C-C Tsai P.40
Decrease Key From an F-heap
After decrease key of 15=>11
8
10
3
5 4
6
1
7 1612
11 30
20
9
-
8/10/2019 DS(II) Ch09 PriorityQueues
21/65
21
C-C Tsai P.41
Decrease Key From an F-heap
After decrease key of 15=>11
8
10
3
5 4
6
1
7 1612
11 30
20
9
C-C Tsai P.42
Cascading Cut
When theNode is cut out of its sibling list in aremove or decrease key operation, follow pathfrom parent of theNode to the root.
Encountered nodes (other than root) with
ChildCut = true are cut from their sibling lists andinserted into top-level list.
Stop at first node with ChildCut = false.
For this node, set ChildCut = true.
-
8/10/2019 DS(II) Ch09 PriorityQueues
22/65
22
C-C Tsai P.43
Cascading Cut Example
8
7 3
1
6
5 9
2
8
6 7
4
9
9 8
theNode
T
T
F
Decrease key by 2.
C-C Tsai P.44
Cascading Cut Example
8
7 3
1
6
5 9
2
6 7
4
6
9
9 8
T
T
F
-
8/10/2019 DS(II) Ch09 PriorityQueues
23/65
23
C-C Tsai P.45
Cascading Cut Example
8
7 3
1
6
5 9
2
6
7
4
6
9
9 8
T
F
C-C Tsai P.46
Cascading Cut Example
8
7 3
1
6
5 9
2 6
7 46
9
9 8F
-
8/10/2019 DS(II) Ch09 PriorityQueues
24/65
24
C-C Tsai P.47
Cascading Cut Example
8
7 3
1
6
5 9
2 6
7 46
9
9 8T
Actual complexity of cascading cut is O(h) = O(n).
C-C Tsai P.48
Time Complexity of Fibonacci Heaps
Actual Amortized
Insert O(1) O(1)
Delete min (or max) O(n) O(log n)
Meld O(1) O(1)
Delete O(n) O(log n)
Decrease key (or
increase)
O(n) O(1)
-
8/10/2019 DS(II) Ch09 PriorityQueues
25/65
25
C-C Tsai P.49
Pairing Heaps
Fibonacci Pairing
Insert O(1) O(1)
Delete min (or max) O(n) O(n)
Meld O(1) O(1)
Delete O(n) O(n)
Decrease key (or
increase)
O(n) O(1)
C-C Tsai P.50
Pairing Heaps
Fibonacci Pairing
Insert O(1) O(log n)
Delete min (or max) O(log n) O(log n)
Meld O(1) O(log n)
Delete O(log n) O(log n)
Decrease key (or
increase)
O(1) O(log n)
-
8/10/2019 DS(II) Ch09 PriorityQueues
26/65
26
C-C Tsai P.51
Pairing Heaps
Experimental results suggest that pairing heapsare actually faster than Fibonacci heaps.
Simpler to implement.
Smaller runtime overheads.
Less space per node.
C-C Tsai P.52
Definition
A min (max) pairing heap is a min (max) treein which operations are done in a specifiedmanner.
3
5 3
8
1
2 1
4
1
4 5
6
2
3
-
8/10/2019 DS(II) Ch09 PriorityQueues
27/65
27
C-C Tsai P.53
Node Structure Child
Pointer to first node of children list.
Left and Right Sibling Used for doubly linked linked list (not circular) of
siblings.
Left pointer of first node is to parent.
x is first node in list iffx.left.child = x.
Data
Note: No Parent, Degree, or ChildCut fields.
C-C Tsai P.54
MeldMax Pairing Heap Compare-Link Operation
Compare roots.
Tree with smaller root becomes leftmost subtree.
6
7
3
6 7
9
+ =
3
6 7
9
6
7
Actual cost = O(1).
-
8/10/2019 DS(II) Ch09 PriorityQueues
28/65
28
C-C Tsai P.55
Insert
Create 1-element max tree with new itemand meld with existing max pairing heap.
3
6 7
9
6
7
+insert(2) =
3
6 7
9
6
72
C-C Tsai P.56
Insert
Create 1-element max tree with new itemand meld with existing max pairing heap.
3
6 7
9
6
7
+insert(14)=
3
6 7
9
6
7
14
Actual cost = O(1).
-
8/10/2019 DS(II) Ch09 PriorityQueues
29/65
29
C-C Tsai P.57
Worst-Case Degree
Insert 9, 8, 7, , 1, in this order.
9
871
Worst-case degree = n 1.
C-C Tsai P.58
Worst-Case Height Insert 1, 2, 3, , n, in this order.
1
2
Worst-case height = n.3
4
5
-
8/10/2019 DS(II) Ch09 PriorityQueues
30/65
30
C-C Tsai P.59
IncreaseKey(theNode, theAmount)
Since nodes do not have parent fields, wecannot easily check whether the key in theNodebecomes larger than that in its parent.
So, detach theNode from sibling doubly-linkedlist and meld.
C-C Tsai P.60
IncreaseKey(theNode, theAmount)
If theNode is not the root, remove subtree
rooted at theNode from its sibling list.
1
2 6
9
4
5 1
6
1
3 2
4 2
4
3
3
theNode
4=>18
-
8/10/2019 DS(II) Ch09 PriorityQueues
31/65
31
C-C Tsai P.61
IncreaseKey(theNode, theAmount)
Meld subtree with remaining tree.
1
2 6
9
4
5 1
6
1
3 2
4
2
18
3
3
C-C Tsai P.62
IncreaseKey(theNode, theAmount)
1
2 6
9
4
5 1
6
1
3 2
4
2
18
3
3
Actual cost = O(1).
-
8/10/2019 DS(II) Ch09 PriorityQueues
32/65
32
C-C Tsai P.63
Delete Max
If empty => fail.
Otherwise, remove tree root and meldsubtrees into a single max tree.
How to meld subtrees?
Good way => O(log n) amortized complexity forremove max.
Bad way => O(n) amortized complexity.
C-C Tsai P.64
Bad Way To Meld Subtrees
currentTree = first subtree.
for (each of the remaining trees)
currentTree = compareLink (currentTree,
nextTree);
-
8/10/2019 DS(II) Ch09 PriorityQueues
33/65
33
C-C Tsai P.65
Example
Delete max.
75268 4 1 3
Meld into one tree.
9
75268 4 1 3
8
6157 3 2 4
C-C Tsai P.66
Example
Actual cost of insert is 1.
Actual cost of delete max is degree of root.
n/2 inserts (9, 7, 5, 3, 1, 2, 4, 6, 8) followed by
n/2 delete maxs. Cost of inserts is n/2.
Cost of delete maxs is 1 + 2 + + n/21 = (n2).
If amortized cost of an insert is O(1), amortized costof a delete max must be (n).
-
8/10/2019 DS(II) Ch09 PriorityQueues
34/65
34
C-C Tsai P.67
Good Ways To Meld Subtrees
Two-pass scheme and Multipass scheme. Both havesame asymptotic complexity.
Two-pass scheme gives better observed performance.
Pass 1.
Examine subtrees from left to right.
Meld pairs of subtrees, reducing the number of subtrees tohalf the original number.
If # subtrees was odd, meld remaining original subtree withlast newly generated subtree.
Pass 2. Start with rightmost subtree of Pass 1. Call this the working
tree.
Meld remaining subtrees, one at a time, from right to left,into the working tree.
C-C Tsai P.68
Two-Pass SchemeExample
T2 T3 T4T1 T6 T7 T8T5
S1 S2 S3 S4
R1
R2
R3
-
8/10/2019 DS(II) Ch09 PriorityQueues
35/65
35
C-C Tsai P.69
Multipass Scheme
Place the subtrees into a FIFO queue.
Repeat until 1 tree remains.
Remove 2 subtrees from the queue.
Meld them.
Put the resulting tree onto the queue.
C-C Tsai P.70
Multipass SchemeExample
T2T1 T3 T4 T6 T7 T8T5
S2S1T6 T7 T8T5
S1T3 T4 T6 T7 T8T5
S3S2S1T7 T8
-
8/10/2019 DS(II) Ch09 PriorityQueues
36/65
36
C-C Tsai P.71
Multipass Scheme--Example
S3S2S1T7 T8
S4S3S2S1
S4S3 R1
R1 R2
C-C Tsai P.72
Multipass Scheme--Example
R1 R2
Q1
Actual cost = O(n).
-
8/10/2019 DS(II) Ch09 PriorityQueues
37/65
37
C-C Tsai P.73
Delete Nonroot Element
Remove theNode from its sibling list.
Meld children of theNode using either 2-pass ormultipass scheme.
Meld resulting tree with whats left of originaltree.
C-C Tsai P.74
Delete(theNode)
Remove theNode from its doubly-linked
sibling list.
1
2 6
9
4
5 1
6
1
3 2
4 2
4
3
3
theNode
-
8/10/2019 DS(II) Ch09 PriorityQueues
38/65
38
C-C Tsai P.75
Delete(theNode)
Meld children of theNode.
2
4
3
3
1
2 6
9
4
5 1
6
1
3 2
4
C-C Tsai P.76
Delete(theNode)
Meld with whats left of original tree.
1
2 6
9
4
5 1
6
1
3 2
4
2 3
3
-
8/10/2019 DS(II) Ch09 PriorityQueues
39/65
39
C-C Tsai P.77
Delete(theNode)
1
2 6
9
4
5 1
6
1
3 2
42 3
3
Actual cost = O(n).
C-C Tsai P.78
MIN-MAX Heaps
Complete binary tree.
Set root on a min level.
A node xin min level would have smaller keyvalue than all its descendents. (xis a minnode.)
7
70 40
30 109 15
5045 2030 12
min
max
min
max
-
8/10/2019 DS(II) Ch09 PriorityQueues
40/65
40
C-C Tsai P.79
Insertion into a min-max heap
7
70 40
30 109 15
5045 2030 12
min
max
min
max5
Insert 5
C-C Tsai P.80
Insertion into a min-max heap
7
70 40
30 59 15
5045 2030 12
min
max
min
max10
Insert 5
-
8/10/2019 DS(II) Ch09 PriorityQueues
41/65
41
C-C Tsai P.81
Insertion into a min-max heap
5
70 40
30 79 15
5045 2030 12
min
max
min
max10
Insert 5
C-C Tsai P.82
Insertion into a min-max heap
Check if it satisfies min heap.(Compare with its parent.)
If no, move the key of currentparent to current position.If yes, skip.
-
8/10/2019 DS(II) Ch09 PriorityQueues
42/65
42
C-C Tsai P.83
Insertion into a min-max heap Initial Heap={7,70,40,30,9,10,15,45,50,30,20,12}
parent=7;
We want to insertion 80 into the position 13
*n=13, item=80;
80>10 verify_max(heap,13,80)
7
70 40
30 109 15
5045 2030 12
min
max
min
max80
C-C Tsai P.84
Insertion into a min-max heap
Input:verify_max(Heap,13,80)
grandparent=3
80>40
heap[13]=heap[3]
i=3
grandparent=0
7
70 40
30 109 15
5045 2030 12
min
max
min
max80
-
8/10/2019 DS(II) Ch09 PriorityQueues
43/65
43
C-C Tsai P.85
Insertion into a min-max heap
Input
verify_max(Heap,3,80)
grandparent=null
break;
heap[3]=80;
7
70 80
30 109 15
5045 2030 12
min
max
min
max40
C-C Tsai P.86
Insertion into a min-max heap
The time complexity of insertion into a min-max heap with n elements is O(log n).
A min-max heap with nelements has O(log n) levels.
-
8/10/2019 DS(II) Ch09 PriorityQueues
44/65
44
C-C Tsai P.87
Deletion of min element
The smallest element is in the root.
We do the deletion as follows:
Remove the root node and the node xwhich is theend of the heap.
Reinsert the key of x into the heap.
C-C Tsai P.88
Deletion of min element
7
70 40
30 109 15
5045 2030 12
min
max
min
max
Delete 7
-
8/10/2019 DS(II) Ch09 PriorityQueues
45/65
45
C-C Tsai P.89
Deletion of min element
70 40
30 109 15
5045 2030
min
max
min
max
12
Delete 7
C-C Tsai P.90
Deletion of min element
i i
It means the item is the only one element, soit should be at root in heap.
The reinsertion may have 2 cases:
Case1: No child. (Only one node in the heap)
-
8/10/2019 DS(II) Ch09 PriorityQueues
46/65
46
C-C Tsai P.91
Deletion of min element
i i
Case2: The root has at least one child
Find the min value. (Let this be node k.)
a. A item.key heap[k].key
It means the item is the min element, and themin element should be at root in heap.
C-C Tsai P.92
Deletion of min element
b. item.key> heap[k].key and k is child of root.
i
Since kis in max level, it has no descendants.
k
k
i
-
8/10/2019 DS(II) Ch09 PriorityQueues
47/65
47
C-C Tsai P.93
Deletion of min element
c. item.key> heap[k].key and k is grandchild ofroot.
i
Example (p>i,): We should make sure the node inmax level contains the largest key. Then redoinsertion to the subtree with the root in red line.
p
k
k
p
i
C-C Tsai P.94
Deletion of min element
Get the min value of the heap.
Move kto the root of this heap.
Swap iandpin case 2c.
-
8/10/2019 DS(II) Ch09 PriorityQueues
48/65
48
C-C Tsai P.95
Deletion of min elementmin
max
min
max
70 40
30 109 15
5045 2030
12
Delete 7
min
max
min
max
70 40
30 1012 15
5045 2030
9
C-C Tsai P.96
Deletion of min element
Deletion of min element of a min-max heapwith n elements need O(log n) time.
In each iteration, imoves down two levels. Since amin-max heap is a complete binary tree, heap has
O(log n) levels.
-
8/10/2019 DS(II) Ch09 PriorityQueues
49/65
49
C-C Tsai P.97
Symmetric Min-Max Heaps
Symmetric Min-Max Heap (SMMH) is acomplete binary tree.
SMMH is either empty or satisfies theproperties:
The root contains no element.
The left subtree is a min-heap.
The right subtree is a max-heap.
If the right subtree is not empty. Let i be any nodein left subtree, and j be the corresponding node inthe right subtree. If no, choose the parent one.i_keyj_key.
C-C Tsai P.98
Example of a SMMH
5 45
10 258 40
1915 309 20
Min-heap Max-heap
-
8/10/2019 DS(II) Ch09 PriorityQueues
50/65
50
C-C Tsai P.99
SMMH
Let i be any node in left subtree and j be thecorresponding node in the right subtree. The relationbetween iandj.
;2/
)(if
;2 1log2
=
>
+=
j
nj
ij i
1
2 3
4 65 7
98 1110 12
Ex1:
i = 4;j = 4+2(2-1) = 6;
Ex2:
i = 9;j = 9+2(3-1) = 13;
j =13 > 12 j = 6;
C-C Tsai P.100
Insertion into a SMMH
The insertion steps1. max_heap(n): Check iffnis a position in the max-
heap of the SMMH.
2. min_partner(n) or max_partner(n) : Compute themin-heap/ max-heap node that corresponding to n.
( / )
1. Compare key of iandjto satisfy SMMH.
2. min_insert or max_insert.
1log22
n
n 2/)2(1log2 +
nn
-
8/10/2019 DS(II) Ch09 PriorityQueues
51/65
51
C-C Tsai P.101
Insertion into a SMMH
5 45
10 258 40
1915 309 20 4
ji
Insert 4: i = 13-2(3-1) = 13-4 = 9 for min_heap
C-C Tsai P.102
Insertion into a SMMH
5 45
10 258 40
415 309 20 19
-
8/10/2019 DS(II) Ch09 PriorityQueues
52/65
52
C-C Tsai P.103
Insertion into a SMMH
4 45
5 258 40
1015 309 20 19
C-C Tsai P.104
Insertion into a SMMH
Step 1. max_heap.
Step 2. min_partner.
Step 3. Compare key of i and j.
Step 4. min_insert or max_insert.
The time complexity is O(log n) as the height of the SMMH is O(log n).
-
8/10/2019 DS(II) Ch09 PriorityQueues
53/65
53
C-C Tsai P.105
Deletion of min element
The deletion steps
1. Save the last element as temp and remove thisnode from SMMH.
2. Find the node with smaller key from the children ofremoved minimum element and loop down untilreaching leaves.
3. Insert temp into the left subtree of SMMH.
C-C Tsai P.106
Deletion of min element
5 45
10 258 40
1915 309 20temp
Delete 5:
-
8/10/2019 DS(II) Ch09 PriorityQueues
54/65
54
C-C Tsai P.107
Deletion of min element
45
10 258 40
1915 309
j
temp:20
i
C-C Tsai P.108
Deletion of min element
8 45
10 25 40
1915 309
j
i
temp:20
-
8/10/2019 DS(II) Ch09 PriorityQueues
55/65
55
C-C Tsai P.109
Deletion of min element
8 45
10 259 40
1915 30
i
temp:20
C-C Tsai P.110
Deletion of min element
8 45
10 259 40
1915 3020
-
8/10/2019 DS(II) Ch09 PriorityQueues
56/65
56
C-C Tsai P.111
Deletion of min element
Step 1. Save the min element.
Step 2. Find the node withsmaller key.
Step 3. (Exercise 2).
The time complexity is O(log n) as the height of the SMMH is O(log n).
C-C Tsai P.112
Interval Heaps
Complete binary tree.
Each node (except possibly last one) has 2 elements. Lastnode has 1 or 2 elements.
Let a and b be the elements in a node P, a
-
8/10/2019 DS(II) Ch09 PriorityQueues
57/65
57
Example of an Interval Heap
28,55 35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Left end points define a min heap.
Right end points define a max heap.
28,55 35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Min and max elements are in the root.
Store as an array
Height is ~log2 n.
Example of an Interval Heap
-
8/10/2019 DS(II) Ch09 PriorityQueues
58/65
58
Insert An Element
28,55 35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Insert 27.3527,35
New element becomes a left end point.
Insert new element into min heap.
Another Insert
28,55 35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Insert 18.18,35
New element becomes a left end point.
Insert new element into min heap.
-
8/10/2019 DS(II) Ch09 PriorityQueues
59/65
59
28,55 25,35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Insert 18.
18,60
New element becomes a left end point.
Insert new element into min heap.
Another Insert
28,55 25,35
20,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Insert 18.
,70
New element becomes a left end point.
Insert new element into min heap.
18,70
Another Insert
-
8/10/2019 DS(II) Ch09 PriorityQueues
60/65
60
Yet Another Insert
28,55 35
25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,70
15,80 30,60
10,90
Insert 82.35,82New element becomes a right end point.
Insert new element into max heap.
After 82 Inserted
28,55 35,60
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,80
15,82 30,60
10,90
-
8/10/2019 DS(II) Ch09 PriorityQueues
61/65
61
28,55
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,80
15,82 30,60
10,90
One More Insert Example
Insert 8.
New element becomes both a left and a
right end point.
Insert new element into min heap.
8
2528,55
20,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2015,80
10,82 30,60
8,90
After 8 Is Inserted
-
8/10/2019 DS(II) Ch09 PriorityQueues
62/65
62
C-C Tsai P.123
Remove Min Element
Remove Min element
If n = 0 => fail.
If n = 1 => heap becomes empty.
If n = 2 => only one node, take out left end point.
If n > 2 => not as simple.
Remove Min Element Example
28,55 35,60
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,80
15,82 30,60
10,90
Remove left end point from root.
Remove left end point from last node.
Reinsert into min heap, begin at root.
10,90
35,60
Delete last node if now empty.
35
-
8/10/2019 DS(II) Ch09 PriorityQueues
63/65
63
28,55 60
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,80
15,82 30,60
15,90
Swap with right end point if necessary.
,82
35
Remove Min Element Example
28,55 60
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6015,2020,80
15,82 30,60
15,90
Swap with right end point if necessary.
,20
35
Remove Min Element Example
-
8/10/2019 DS(II) Ch09 PriorityQueues
64/65
64
28,55 60
25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43
35,5045,6016,3520,80
15,82 30,60
15,90
Swap with right end point if necessary.
,19
20
Remove Min Element Example
28,55 60
25,70 30,50 19,20 17,17 50,55 47,58 40,45 40,43
35,5045,6016,3520,80
15,82 30,60
15,90
Remove Min Element Example
-
8/10/2019 DS(II) Ch09 PriorityQueues
65/65
C-C Tsai P.129
Application Of Interval Heaps Complementary range search problem.
Collection of 1D points (numbers).
Insert a point. O(log n)
Remove a point given its location in the structure. O(log n)
Report all points not in the range [a,b], a