1 / 34

Data Structures & Algorithms Binomial Queues

Binomial queues are a data structure that supports fast join and remove_max operations. This article explains the concepts behind binomial queues and how they can be efficiently implemented.

anjanette
Download Presentation

Data Structures & Algorithms Binomial Queues

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. Data Structures & Algorithms Binomial Queues

  2. Priority Queue None of the Priority Queue implementations supports insert, remove_max, andjoin that are all fast in the worst case. ??? lg N lg N lg N lg N lg N lg N

  3. Binomial Queues • None of the Priority Queue implementations supports insert, remove_max, andjoin that are all fast in the worst case. Fast = log time or better • Fast join eliminates arrays • Fast remove_max requires some order Observation: • Need to relax heap restrictions to get fast join • Yet keep enough order to make others operations fast

  4. Binomial Queues Developed by Vuillemin (1978). Defn. 9.4: A binary tree comprising nodes with keys is left heap ordered if the key in the node is >= all the keys in that node's left subtree. Big ?1 Med ?2 <Big <?1 <Med Left heap ordered complete binary tree

  5. Binomial Queues Defn. 9.5: A power-of-2 heap is a left-heap-ordered tree consisting of a root node with an empty right subtree and a complete left subtree. The tree corresponding to a power-of-2 heap by the left-child right-sibling correspondence is a binomial tree Biggest Biggest X X Left- heap- ordered complete Z Y Y binomial tree < X binomial tree < Y binomial tree < Z Z

  6. Power-of-2 Heap empty T 23-1 node left heap T I R O O L G H I G R L H A A Power-of-2 heap Corresponding Binomial Tree 23 nodes

  7. Binomial Queues • Defn. 9.6: A binomial queue is a set of power-of-2 heaps, no two of the same size. • The structure of a binomial queue is determined by that queue's size, by correspondence with the binary representation of integers.

  8. Binomial Queues Recall that a positive integer N has a binary representation, so N is an an-1 … a2 a1 a0 in binary, and N = an2n + an-12n-1 … + a222 + a12 + a0 where ai is either 0 or 1. A binomial queue with N elements has a power-of-2 heap of size 2i whenever ai = 1.

  9. Binomial Queue 9 nodes: T M 8-node Power-of-2 heap 1-node Power-of-2 heap R O L H G I A 9 = 1001 = 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20

  10. Binomial Queue 10 nodes: T M S 8-node Power-of-2 heap R 2-node Power-of-2 heap O L H G I A 10 = 1010 = 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20

  11. Binomial Queues Now consider joining two binomial queues, one of size N and one of size M, where N = anan-1…a1a0 and M = bnbn-1…b1b0. If at most one of ai and bi is 1 for all i,then just combine the collections. Otherwise, must combine two power-of-2 heaps to make a power-of-2 heap twice as large – like having to “carry a 1” So how do we join two power-of-2 heaps?

  12. Binomial Queues Joining two power-of-2 heaps... larger root 23-1 node left-heap 23-1 node left-heap T U O P R O L O I H N D A G C M

  13. Binomial Queues Joining two power-of-2 heaps... 24-1 node left-heap U T O P R O L O I H N D A G C M

  14. Binomial Queues Do we have to sift down the root of the left sub-tree? What would that entail? How much would it cost? Can we afford to do it?

  15. Binomial Queues Consider “fixing” left subtree as heap.... Broken 24-1 node heap P H G O F N C K D E L M A B I J May have to siftDown smaller of two roots

  16. Binomial Queues Consider “fixing” left subtree as heap.... P H G O F N C K D E L M A B I J May have to siftDown smaller of two roots

  17. Binomial Queues Consider “fixing” left subtree as heap.... P O G H F N C K D E L M A B I J May have to siftDown smaller of two roots

  18. Binomial Queues Consider “fixing” left subtree as heap.... P O G H F N C K D E L M A B I J May have to siftDown smaller of two roots

  19. Binomial Queues Consider “fixing” left subtree as heap.... P O G N F H C K D E L M A B I J May have to siftDown smaller of two roots

  20. Binomial Queues Consider “fixing” left subtree as heap.... P O G N F H C K D E L M A B I J May have to siftDown smaller of two roots

  21. Binomial Queues Consider “fixing” left subtree as heap.... P O G N F M C K D E L H A B I J May have to siftDown smaller of two roots

  22. Binomial Queues Consider “fixing” left subtree as heap.... 24-1 node heap P O G N F M C K D E L H A B I J Great! We now have a heap on the left.

  23. Binomial Queues But do we have to sift down the root of the left sub-tree? What would that entail? Sifting down the smaller root each time How much would it cost? k for a tree of size 2k May do for k = 1, 2, 3, 4, …, n-1 Where final tree is of size 2n Can we afford to do it? Uh … no – this is O(n2), or O(lg2 N) That's better than N, but not “fast”

  24. Binomial Queues Great News! Left sub-tree only has to be left-heap-ordered, not heap-ordered! No need to sift down (make the power-of-2 heap with the smaller tree the left subtree) But is insert still fast? Can't be worse! Fewer constraints! Still need to give details.... How about remove-max?

  25. Binomial Queues First Insert: Make new node a unit power-of-2 heap. If there is no “units” heap already, done! But if there is already a “units” heap, merge with it and create a “carry-one” heap of size 2. If there is already a size 2 power-of-2 heap, merge with it and make “carry-one” heap of size 4, etc. until a 0 is found (a power of 2 for which there is no heap).

  26. Binomial Queues Insert M That was easy! H G O F C K D E A B I J 13-node binomial queue 12-node binomial queue

  27. Binomial Queues Insert O H G F N C D E L M A B 11-node binomial queue

  28. Binomial Queues Insert H G F N O C D E L M A B 12-node binomial queue

  29. Binomial Queues Remove-max: Max has to be root of one of the lg N power-of-2 heaps – check them all, and remove the largest root. Now break up that left-heap ordered tree of size 2k-1 into k power-of-2 heaps. Then just join these with the remaining original power-of-2 heaps.

  30. Binomial Queues Remove_max P H G O F N C K D E L M A B I J 16-node power-of-2 heap

  31. Binomial Queues Remove_max P H G O F N C K D E L M A B I J

  32. Binomial Queues Remove_max 2-node po2 heap H G O F N C K D E L M A B I J unit po2 heap 4-node po2 heap 8-node power-of-2 heap 15-node left-heap-ordered complete binary tree

  33. Summary Use of Priority Queues – dynamic Simple implementations – fast insert and fast remove_max Heaps – fast for everything except join HeapSort – guaranteed O(N lg N) time Binomial Queues and power-of-2 heaps – fast O(lg N) for insert, find_max, remove_max, and join; relationship to binary numbers

  34. Next Exam 1 (Wed) – Ch 1-9 (NOT 10) Radix Sort (Ch 10) Binary Search Trees (Ch 12).

More Related