Binomial Heaps )
Total Page:16
File Type:pdf, Size:1020Kb
CSE 548: Analysis of Algorithms Lecture 9 ( Binomial Heaps ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Fall 2017 Mergeable Heap Operations MAKE -HEAP ( ): return a new heap containing only element INSERT ( ): insert element into heap , MINIMUM( ): return a pointer to an element in containing the smallest key EXTRACT -MIN ( ): delete an element with the smallest key from and return a pointer to that element UNION ( ): return a new heap containing all elements of , heaps and , and destroy the input heaps More mergeable heap operations: DECREASE -KEY ( ): change the key of element of heap to , , assuming the current key of DELETE ( ): delete element from heap , Mergeable Heap Operations Heap Binary Heap Binomial Heap Operation ( worst-case ) ( amortized ) MAKE -HEAP Θ Θ 1 1 INSERT Ο Θ log 1 MINIMUM Θ Θ 1 1 EXTRACT -MIN Ο Ο log log UNION Θ Θ 1 DECREASE -KEY Ο log DELETE Ο log Binomial Trees A binomial tree is an ordered tree defined recursively as follows. − consists of a single node − For , consists of two ’s that are linked together so that the 0 root of one is the left child of the root of the other Binomial Trees Some useful properties of are as follows. 1. it has exactly nodes 2 2. its height is 3. there are exactly nodes at depth 0,1,2, … , 4. the root has degree 5. if the children of the root are numbered from left to right by , then child1,2,…,0 is the root of a Binomial Trees Prove: has exactly nodes at depth . 0,1,2,…, Proof: Suppose has nodes at depth . , 0 # $ 0 %& , , !1 # 0, , , %'()&*). , , , , , , 1 , , , , , Binomial Trees 0 # $ 0 %& , , !1 # 0, , , %'()&*). , / 0 0 01 , , 0 Generating function: , - , ,- ,- … ,- ,2 - 3 ,- 3 ,- 3 ,- / 01 3 / 01- 4 4 4 4 3 ,- - 3 ,- 0 4 4 , - -, - 0 1 - , - / 01 1 # 0, ., - 5 1 - , - %'()&*). Equating the coefficient 1 - of from both sides: , - Binomial Heaps A binomial heap is a set of binomial trees that satisfies the following properties: Binomial Heaps A binomial heap is a set of binomial trees that satisfies the following properties: 1. each node has a key 2. each binomial tree in obeys the min-heap property 3. for any integer , there is at most one binomial tree in whose root node 0 0 has degree 6 1 10 8 14 29 12 25 11 17 38 18 min 27 Rank of Binomial Trees The rank of a binomial tree node , denoted , is the number of children of . &9 The figure on the right shows the rank of each node in . 3 Observe that . 2 1 0 &9 &%%' Rank of a binomial tree is the rank of 1 0 0 its root. Hence, 0 &9 &9 &%%' A Basic Operation: Linking Two Binomial Trees Given two binomial trees of the same rank , say, two ’s, we link them in constant time by making the root of one tree the left child ; of the root of the other, and thus 6 producing a . 8 14 29 ; If the trees are part of a binomial 11 17 38 min-heap, we always make the root 27 with the smaller key the parent, and the one with the larger key the child. Ties are broken arbitrarily. Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min < 6 1 18 14 29 25 38 min min < Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min < 6 1 18 14 29 25 38 min 12 18 min Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min 6 1 18 14 29 25 38 min < 12 18 min Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min 6 1 18 14 29 25 38 min < 1 12 12 25 18 18 min Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min < 6 1 18 14 29 25 38 min 1 12 25 18 min Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min < 6 1 18 14 29 25 38 min 6 1 8 14 29 12 25 11 17 38 18 27 min Binomial Heap Operations: UNION( ) , 8 12 11 17 27 min 6 1 18 14 29 25 38 min =>?@> , 6 1 8 14 29 12 25 11 17 38 18 27 min Binomial Heap Operations: UNION( ) UNION works in exactly the same way as binary addition., , Let be the number of nodes in . A 1,2B Then the largest binomial tree in is a , where . C log Thus can be treated as a bit binary number , where bit is 1 if contains a , and 0 otherwise.D 1 E If , then can be viewed F% as a , bit binary number log , where . Binomial Heap Operations: UNION( ) UNION works in exactly the same way as binary addition., , Initially, does not contain any binomial trees. Melding starts from ( LSB ) and continues up to ( MSB ). At each location , one encounters at mostD G three /0, 1 ( 3 ) ’s: E − at most 1 from ( input ), − at most 1 from ( input ), and − if at most 1 from ( carry ) D 0, Binomial Heap Operations: UNION( ) UNION works in exactly the same way as binary addition., , When the number of ’s at location is: E D G /0, 1 − 0: location of is set to − 1: location D of points to< that − 2: the two D ’s are linked to produceE a whichE is stored as a carry at locationE; of , and location isD set 1 to − 3: two ’s Dare linked< to produce a Ewhich is stored as a carryE; at location of , and the 3 rd Dis 1 stored at locationE D Binomial Heap Operations: UNION( ) UNION works in exactly the same way as binary addition., , Worst case cost of UNION is clearly Θ , where is the total number of nodes in, and . log Observe that this operation fills out locations of , where . 1 log It does only Θ work for each location. 1 Hence, total cost is Θ Θ . log Binomial Heap Operations: UNION( ) One can improve the performance of UNION as follows., , W.l.o.g., suppose is at least as large as , i.e., . 0 We also assume that has enough space to store at least up to , where, . log Then instead of melding and to a new heap , we can meld them in-place at . After melding till , we stop once the carry stops propagating.H The cost is Ω , but Ο . Worst-case cost is still Ο Ο . log Binomial Heap Operations: INSERT ( ) , Step 1: MAKE -HEAP I 5 ITakes J Θ time. 1 min I Step 2: UNION 8 12 J ( in-place , at I ) 11 17 Takes Ο time, where 27 min is the number oflog nodes in . Thus the worst-case cost of 8 5 11 17 12 INSERT is Ο , where 27 min is the , number oflog items already in the heap. Binomial Heap Operations: EXTRACT -MIN( ) 6 Step 1: Step 2: remove the binomial tree with remove the smallest root from the input heap minimum min 6 10 element 8 14 29 12 25 11 17 38 18 27 10 12 25 Step 3: remove the root of the binomial 18 min < tree with the minimum element, and form a new binomial heap from the children of the removed root Step 4: UNION and update the min pointer K , I 8 14 29 8 14 29 11 17 38 10 11 17 38 27 min I < 12 25 27 min 18 Binomial Heap Operations: EXTRACT -MIN( ) 6 Step 1: Step 2: remove the binomial tree with remove the smallest root from the input heap minimum min 6 10 element Θ 8 14 29 12 25 Θ 1 11 17 38 18 1 27 10 12 25 Step 3: remove the root of the binomial 18 min < Tree with the minimum element, and form a new binomial heap from the children of the removed root Step 4: UNION and update the min pointer Ο K , log Ο log I 8 14 29 8 14 29 11 17 38 10 11 17 38 27 min I < 12 25 27 min 18 Thus, the worst-case cost of EXTRACT -MIN is Ο log Binomial Heap Operations Heap Worst-case Operation MAKE -HEAP Θ 1 INSERT Ο log MINIMUM Θ 1 EXTRACT -MIN Ο log UNION Ο log Amortized Analysis ( Accounting Method ) We maintain a credit account for every tree in the heap, and always maintain the following invariant: L M&)N' E 1 OPGQ MAKE -HEAP ( ): actual cost, ( for creating the singleton heap ) extra charge,M 1 ( for storing in the credit account R 1 of the new tree ) amortized cost, Θ MS M R 2 1 Amortized Analysis ( Accounting Method ) We maintain a credit account for every tree in the heap, and always maintain the following invariant: L M&)N' E 1 OPGQ LINK ( ): AB AB T actual,T cost, ( for linking the two trees ) M 1 We use pay for this actual work. AB M&)N' Let be the newly created tree. We restore the credit invariant ; by transferring to . AB M&)N' M&)N' ; Hence, amortized cost, MS M R 1 1 0 Amortized Analysis ( Accounting Method ) We maintain a credit account for every tree in the heap, and always maintain the following invariant: L M&)N' E 1 OPGQ INSERT ( ): , Amortized cost of MAKE -HEAP is 2 Then UNION is simply a sequence of free LINK operations K with only a constant , amount of additional work that do not create any new trees. Thus the credit invariant is maintained, and the amortized cost of this step is .