
CSE 548: Analysis of Algorithms Lectures 11, 12 & 13 ( Binomial Heaps ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Fall 2012 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 child͟Ǝ1,͟Ǝ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 Generating function: ͦ & ͍& ͮ Ɣ ͧ&,ͤ ƍ ͧ&,ͥͮ ƍ ͧ&,ͦͮ ƍ … ƍ ͧ&,&ͮ & & & & $ $ $ $ ͍&ͤ ͮ Ɣ ȕ ͧ&,$ͮ Ɣ ȕ ͧ&ͯͥ,$ͮ ƍ ȕ ͧ&ͯͥ,$ͯͥͮ ƍ ʞ͟ Ɣ 0ʟ ȕ ʞ͝ Ɣ 0ʟͮ $Ͱͤ $Ͱͤ $Ͱͤ $Ͱͤ &ͯͥ &ͯͥ $ $ Ɣ ȕ ͧ&ͯͥ,$ͮ ƍ ͮ ȕ ͧ&ͯͥ,$ͮ ƍ ͟ Ɣ 0 $Ͱͤ $Ͱͤ Ɣ ͍&ͯͥ ͮ ƍ ͍ͮ&ͯͥ ͮ ƍ ͟ Ɣ 0 Ɣ 1 ƍ ͮ ͍&ͯͥ ͮ ƍ ʞ͟ Ɣ 0ʟ 1 ͚͝ ͟ Ɣ 0, ⇒ ͍& ͮ Ɣ Ƥ 1 ƍ ͮ ͍&ͯͥ ͮ ͙͙ͣͨͦͫͧ͜͝. & Ɣ 1 ƍ ͮ Equating the coefficient 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 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 . ͬ ͕ͦ͢͟ ͬ ͬ The figure on the right shows the rank ̼ͧ of each node in . 3 ̼ͧ Observe that . 2 1 0 ͕ͦ͢͟ ͦͣͣͨ ̼& Ɣ ͟ Rank of a binomial tree is the rank of 1 0 0 its root. Hence, 0 ͕ͦ͢͟ ̼& Ɣ ͕ͦ͢͟ ͦͣͣͨ ̼& Ɣ ͟ 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 ͂ ̼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heap Operations: UNION ( ) UNION works in exactly the same way as binary¤ ̊addition., ¤̋ ͂ͥ, ͂ͦ Let be the number of nodes in . ͢$ ͂$ ʚ͝ Ɣ 1,2ʛ Then the largest binomial tree in is a , where . ͂$ ̼&Ĝ ͟$ Ɣ log ͦ ͢$ Thus can be treated as a bit binary͂$ number , where bit͟$ ƍis 1 if contains a , ͬand$ 0 otherwise.͞ 1 ͂$ ̼% If , then can be͂ viewed Ɣ ͏ͣ͢͢͝ 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 most͞ ∈ three ʞ0, ͟ʟ ( 3 ) ’s: ̼% − at most 1 from ( input ), − at most 1 from ͂ͥ ( input ), and − if at most͂ 1ͦ from ( carry ) ͞ Ƙ 0, ͂ Binomial Heap Operations: UNION ( ) UNION works in exactly the same way as binary¤ ̊addition., ¤̋ ͂ͥ, ͂ͦ When the number of ’s at location is: ̼% ͞ ∈ ʞ0, ͟ʟ − 0: location of is set to − 1: location ͞ of ͂ points to͢͝͠ that − 2: the two ͞ ’s are͂ linked to produce̼% a which̼% is stored as a carry at̼ location%ͮͥ of , and location is͞ set ƍ 1 to ͂ − 3: two ’s ͞are linked͢͝͠ to produce a ̼%which is stored as a carry̼%ͮͥ at location of , and the 3 rd ͞is ƍ 1 stored͂ at location̼% ͞ 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., . ͂ͦ ͂ͥ ͦ͢ ƚ ͥ͢ 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.̼&u The cost is Ω , but Ο . ͥ ͦ Worst-case cost͟ is still Ο ͟ Ο . ͟ Ɣ log ͢ Binomial Heap Operations: INSERT ( ) ¤, Î Step 1: MAKE -HEAP ̼ͤ ¤′ 5 ͂′Takes ← Θ time. ͬ 1 min ͂′ Step 2: UNION ̼ͦ ̼ͥ ̼ͤ ¤ 8 12 ͂ ← ( in-place ͂, at ͂′ ) 11 17 Takes Ο time,͂ where 27 min ͂ is the number oflog nodes ͢ in .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages49 Page
-
File Size-