1 / 33

Binomial Heaps

Binomial Heaps. 1. 4. 6. 5. 5. 2. 3. 7. 9. 9. 9. 4. 5. 8. 7. 6. 6. 8. Min Binomial Heap. Collection of min trees. Node Structure. Degree Number of children. Child Pointer to one of the node’s children. Null iff node has no child. Sibling

nichelle
Download Presentation

Binomial Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binomial Heaps

  2. 1 4 6 5 5 2 3 7 9 9 9 4 5 8 7 6 6 8 Min Binomial Heap • Collection of min trees.

  3. Node Structure • Degree • Number of children. • Child • Pointer to one of the node’s children. • Null iff node has no child. • Sibling • Used for circular linked list of siblings. • Data

  4. A 1 4 6 5 5 2 3 7 9 9 9 4 5 8 7 6 6 8 Binomial Heap Representation • Circular linked list of min trees. Degree fields not shown.

  5. A 1 4 6 5 5 2 3 7 10 9 9 9 4 5 8 7 6 6 8 Insert 10 • Update min-element pointer if necessary. • Add a new single-node min tree to the collection.

  6. 4 1 7 5 B 3 7 9 A 8 Meld • Combine the 2 top-level circular lists. • Set min-element pointer.

  7. Meld 4 1 7 5 3 C 7 9 8

  8. Remove Min • Empty binomial heap=> fail.

  9. A 1 4 6 5 5 2 3 7 10 9 9 9 4 5 8 7 6 6 8 Nonempty Binomial Heap • Remove a min tree. • Reinsert subtrees of removed min tree. • Update binomial heap pointer.

  10. a b c d e d A Remove Min Tree • Same as remove a node from a circular list. • No next node => empty after remove. • Otherwise, copy next-node data and remove next node.

  11. 4 1 7 5 3 7 9 B A subtrees 8 Reinsert Subtrees • Combine the 2 top-level circular lists. • Same as in meld operation.

  12. Update Binomial Heap Pointer • Must examine roots of all min trees to determine the min value.

  13. Complexity Of Remove Min • Remove a min tree. • O(1). • Reinsert subtrees. • O(1). • Update binomial heap pointer. • O(s), where s is the number of min trees in final top-level circular list. • s = O(n). • Overall complexity of remove min is O(n).

  14. Enhanced Remove Min • During reinsert of subtrees, pairwise combine min trees whose roots have equal degree.

  15. 1 4 6 5 5 10 2 3 7 9 9 9 4 5 8 7 6 6 8 Pairwise Combine Examine the s = 7 trees in some order. Determined by the 2 top-level circular lists.

  16. 1 4 6 5 5 10 2 3 7 9 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Use a table to keep track of trees by degree.

  17. 1 4 6 5 5 10 2 3 7 9 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine

  18. 1 4 6 5 5 10 2 3 7 9 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Combine 2 min trees of degree 0. Make the one with larger root a subtree of other.

  19. 10 1 4 6 5 5 2 3 7 9 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Update tree table.

  20. 10 1 4 6 5 5 2 3 7 9 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Combine 2 min trees of degree 1. Make the one with larger root a subtree of other.

  21. 10 1 5 4 6 5 2 3 9 7 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Update tree table.

  22. 10 1 5 4 6 5 2 3 9 7 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine

  23. 10 1 5 4 6 5 2 3 9 7 9 9 4 5 8 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Combine 2 min trees of degree 2. Make the one with larger root a subtree of other.

  24. 1 4 6 5 2 3 7 10 5 9 9 4 5 8 9 4 3 tree table 2 7 6 6 1 0 8 Pairwise Combine Combine 2 min trees of degree 3. Make the one with larger root a subtree of other.

  25. 4 2 1 6 5 10 9 5 3 4 5 7 9 9 7 6 6 8 4 3 tree table 2 8 1 0 Pairwise Combine Update tree table.

  26. 4 2 1 6 5 10 9 5 3 4 5 7 9 9 7 6 6 8 4 3 tree table 2 8 1 0 Pairwise Combine

  27. 4 2 1 6 5 10 9 5 3 4 5 7 9 9 7 6 6 8 4 3 tree table 2 8 1 0 Pairwise Combine Create circular list of remaining trees.

  28. Complexity Of Remove Min • Create and initialize tree table. • O(MaxDegree). • Done once only. • Examine s min trees and pairwise combine. • O(s). • Collect remaining trees from tree table, reset table entries to null, and set binomial heap pointer. • O(MaxDegree). • Overall complexity of remove min. • O(MaxDegree + s).

  29. B0 … Bk-1 B2 B1 B0 Binomial Trees • Bkis degree k binomial tree. • Bk, k > 0, is:

  30. B3 B2 B0 B1 Examples

  31. Bk-1 B2 B1 B0 B0 Number Of Nodes In Bk N0 = 1 • Nk = number of nodes in Bk. • Bk, k > 0, is: • Nk = N0 + N1 + N2 + …+ Nk-1 + 1 • = 2k.

  32. B3 B2 B1 Equivalent Definition • Bk, k > 0, is two Bk-1s. • One of these is a subtree of the other.

  33. Nk And MaxDegree • N0 = 1 • Nk = 2Nk-1 = 2k. • If we start with zero elements and perform operations as described, then all trees in all binomial heaps are binomial trees. • So, MaxDegree = O(log n).

More Related