Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007."— Presentation transcript:

1 CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007

2 2 Adminstrivia Due today: Homework #1 Released today: Homework #2

3 3 BuildHeap: Floyd’s Method 511310694817212 Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 27184 96103 115 12

4 4 Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime:

5 5 BuildHeap: Floyd’s Method 67184 92103 115 12 671084 9213 115 12 1171084 9613 25 12 1171084 9653 21 12

6 6 Finally… 11710812 9654 23 1 runtime:

7 7 Facts about Heaps Observations: finding a child/parent index is a multiply/divide by two operations jump widely through the heap each percolate step looks at only two new nodes inserts are at least as common as deleteMins Realities: division/multiplication by powers of two are equally fast looking at only two new pieces of data: bad for cache! with huge data sets, disk accesses dominate

8 8 CPU – 1 cycle Cache – ~10 cycles Memory – ~200 cycles Disk – ~200,000 cycles Cycles to access:

9 9 4 9654 23 1 81012 7 11 A Solution: d-Heaps Each node has d children Still representable by array Good choices for d: –(choose a power of two for efficiency) –fit one set of children in a cache line –fit one set of children on a memory page/disk block 3728512111069112

10 10 Operations on d-Heap Insert : runtime = deleteMin: runtime = Does this help insert or deleteMin more?

11 11 One More Operation Merge two heaps. Ideas?

12 12 New Operation: Merge Given two heaps, merge them into one –First attempt: aren’t two heaps “just like” one heap after deleteMin? NO. Violates the completeness property –Second attempt: Foreach element in smaller heap, insert into larger one Runtime? –Third attempt: Concatenate the arrays of the two heaps, then run buildHeap Runtime?

13 13 Merging heaps Binary Heap is a special purpose hot rod –FindMin, DeleteMin and Insert only –does not support fast merges of two heaps For some applications, the items arrive in prioritized clumps, rather than individually Is there somewhere in the heap design that we can give up a little performance so that we can gain faster merge capability?

14 14 Binomial Queues Binomial Queues are designed to be merged quickly with one another Using pointer-based design we can merge large numbers of nodes at once by simply pruning and grafting tree structures More overhead than Binary Heap, but the flexibility is needed for improved merging speed

15 15 Worst Case Run Times Insert FindMin DeleteMin Merge  (log N)  (1)  (N)  (log N) Binary Heap  (log N) O(log N)  (log N) Binomial Queue

16 16 Binomial Queues Binomial queues give up  (1) FindMin performance in order to provide O(log N) merge performance A binomial queue is a collection (or forest) of heap-ordered trees –Not just one tree, but a collection of trees –each tree has a defined structure and capacity –each tree has the familiar heap-order property

17 17 Binomial Queue with 5 Trees B0B0 B1B1 B2B2 B3B3 B4B4 depth number of elements 4 2 4 = 16 3 2 3 = 8 2 2 2 = 4 1 2 1 = 2 0 2 0 = 1

18 18 Structure Property Each tree contains two copies of the previous tree –the second copy is attached at the root of the first copy The number of nodes in a tree of depth d is exactly 2 d B0B0 B1B1 B2B2 depth number of elements 2 2 2 = 4 1 2 1 = 2 0 2 0 = 1

19 19 Powers of 2 Any number N can be represented in base 2 –A base 2 value identifies the powers of 2 that are to be included 2 0 = 1 10 2 1 = 2 10 2 2 = 4 10 2 3 = 8 10 Hex 16 Decimal 10 11 33 100 44 101 55

20 20 Numbers of nodes Any number of entries in the binomial queue can be stored in a forest of binomial trees Each tree holds the number of nodes appropriate to its depth, ie 2 d nodes So the structure of a forest of binomial trees can be characterized with a single binary number –100 2  1·2 2 + 0·2 1 + 0·2 0 = 4 nodes

21 Structure Examples 4 8 N=2 10 =10 2 2 1 = 2 94 8 2 0 = 1 94 85 7 2 2 = 42 1 = 22 0 = 1 4 85 7 2 2 = 4 N=3 10 =11 2 N=4 10 =100 2 N=5 10 =101 2 2 2 = 4 2 0 = 1 2 1 = 2

22 22 What is a merge? There is a direct correlation between –the number of nodes in the tree –the representation of that number in base 2 –and the actual structure of the tree When we merge two queues, the number of nodes in the new queue is the sum of N 1 +N 2 We can use that fact to help see how fast merges can be accomplished

23 4 8 N=2 10 =10 2 2 1 = 2 94 8 2 0 = 1 N=3 10 =11 2 2 2 = 4 2 0 = 1 N=1 10 =1 2 2 1 = 22 2 = 42 0 = 1 9 Example 1. Merge BQ.1 and BQ.2 Easy Case. There are no comparisons and there is no restructuring. BQ.1 + BQ.2 = BQ.3

24 4 6 N=2 10 =10 2 2 1 = 2 2 0 = 1 N=4 10 =100 2 2 2 = 4 2 0 = 1 N=2 10 =10 2 2 1 = 22 2 = 42 0 = 1 Example 2. Merge BQ.1 and BQ.2 This is an add with a carry out. It is accomplished with one comparison and one pointer change: O(1) BQ.1 + BQ.2 = BQ.3 1 3 1 34 6

25 4 6 N=3 10 =11 2 2 1 = 2 2 0 = 1 N=2 10 =10 2 2 2 = 4 2 0 = 1 N=3 10 =11 2 2 1 = 22 2 = 42 0 = 1 Example 3. Merge BQ.1 and BQ.2 Part 1 - Form the carry. BQ.1 + BQ.2 = carry 1 3 7 8 7 8

26 4 6 N=3 10 =11 2 2 1 = 2 2 0 = 1 N=6 10 =110 2 2 2 = 4 2 0 = 1 N=3 10 =11 2 2 1 = 22 2 = 42 0 = 1 Example 3. Part 2 - Add the existing values and the carry. + BQ.1 + BQ.2 = BQ.3 1 3 7 8 7 8 2 1 = 22 0 = 1 N=2 10 =10 2 2 2 = 4 carry 7 8 1 34 6

27 27 Merge Algorithm Just like binary addition algorithm Assume trees X 0,…,X n and Y 0,…,Y n are binomial queues –X i and Y i are of type B i or null C 0 := null; //initial carry is null// for i = 0 to n do combine X i,Y i, and C i to form Z i and new C i+1 Z n+1 := C n+1

28 28 Exercise 94 8 2 1 = 22 0 = 1 12 107 12 2 2 = 42 1 = 22 0 = 1 N=3 10 =11 2 N=5 10 =101 2 2 2 = 4 13 15

29 29 O(log N) time to Merge For N keys there are at most  log 2 N  trees in a binomial forest. Each merge operation only looks at the root of each tree. Total time to merge is O(log N).

30 30 Insert Create a single node queue B 0 with the new item and merge with existing queue O(log N) time

31 31 DeleteMin 1.Assume we have a binomial forest X 0,…,X m 2.Find tree X k with the smallest root 3.Remove X k from the queue 4.Remove root of X k (return this value) –This yields a binomial forest Y 0, Y 1, …,Y k-1. 5.Merge this new queue with remainder of the original (from step 3) Total time = O(log N)

32 32 Implementation Binomial forest as an array of multiway trees –FirstChild, Sibling pointers –Sibling pointers act like a linked list 0 1 2 3 4 5 6 7 5 2 9 1 107 12 4 813 15 5 2 9 1 4710 12138 15

33 33 DeleteMin Example 1 And return this 0 1 2 3 4 5 6 7 52 9 1 4710 12138 15 0 1 2 3 4 5 6 7 5 10 2 479 12138 15

34 34 DeleteMin step 1: remove min 1 Return this 0 1 2 3 4 5 6 7 52 9 1 52 9 4710 12138 15 4710 12138 15 Keep these

35 35 0 1 2 3 4 5 6 7 New forest Old forest 107 12 4 138 15 107 12 4 138 15 DeleteMin step 2: new forest 0 1 2 3 4 5 6 7 52 9 52 9

36 36 0 1 2 3 4 5 6 7 52 9 Merge 5 10 0 1 2 3 4 5 6 7 4710 12138 15 2 479 12138 15 DeleteMin step 3: merge forests

37 37 Merge in detail 0 1 2 3 4 5 6 7 52 9 5 10 0 1 2 3 4 5 6 7 4710 12138 15 2 479 12138 15

38 38 Merge step 1: 1-trees 5510 0 1 2 3 Carry out

39 39 97 Merge step 2: 2-trees 2 9 0 1 2 3 4 5 6 7 5 10 29125 10 0 1 2 3 Carry out Carry in Merging two trees is just a stack push! 7 12

40 40 Merge step 3: 4-trees 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3 4 138 15 79 12 2 4 138 15 2 79 12 Carry in 2 Carry out

41 41 Merge step 4: 8-trees 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3 2 479 12138 15 Carry in 2 479 12138 15

42 42 Why Binomial? B0B0 B1B1 B2B2 B3B3 B4B4 tree depth d nodes at depth k 4 1, 4, 6, 4, 1 3 1, 3, 3, 1 2 1, 2, 1 1 1, 1 0 1

43 43 Other Priority Queues Leftist Heaps –O(log N) time for insert, deletemin, merge Skew Heaps –O(log N) amortized time for insert, deletemin, merge Calendar Queues –O(1) average time for insert and deletemin –Assuming insertions are “random”

44 44 Exercise Solution 94 8 12 107 12 13 15 + 1 9 2 107 12 4 813 15


Download ppt "CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007."

Similar presentations


Ads by Google