Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms

Similar presentations


Presentation on theme: "Data Structures & Algorithms"— Presentation transcript:

1 Data Structures & Algorithms
Binomial Queues 1 1

2 Priority Queue None of the Priority Queue implementations supports insert, remove_max, and join that are all fast in the worst case. Implementation Insert Remove Max Find Change Priority Join Ordered array N 1 Ordered list Unordered array Unordered list Heap lg N Best in theory ??? lg N lg N lg N lg N lg N lg N 2 2

3 Binomial Queues None of the Priority Queue implementations supports insert, remove_max, and join 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 3 3

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 Med ?1 <Med <Big <?1 ?2 Left heap ordered complete binary tree 4 4

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 Y Z Y binomial tree < X binomial tree < Y binomial tree < Z Z 5 5

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

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. 7 7

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. 8 8

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

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

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? 11 11

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 L R O O A G I H C M N D 12 12

13 Binomial Queues Joining two power-of-2 heaps... 24-1 node left-heap U
C M N D 13 13

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? 14 14

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

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

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

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

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

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

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

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

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” 23 23

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? 24 24

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). 25 25

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

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

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

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. 29 29

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

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

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

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 33 33

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


Download ppt "Data Structures & Algorithms"

Similar presentations


Ads by Google