Algorithms Course Notes Binomial Heaps
Total Page:16
File Type:pdf, Size:1020Kb
Algorithms Course Notes Binomial Heaps Ian Parberry∗ Spring 2003 Priority Queue Implementations • Bk consists of two copies of Bk−1. The root of one is the leftmost child of the other. • Binary heap (classic version) • Binomial heap (faster union) • Fibonacci heap (faster amortized) Bk Priority Queue Operations Bk−1 • makenull: make an empty priority queue Bk−1 • union: combine two of them • insert: insert a new element • min: return the smallest • deletemin: delete and return the smallest • decreasenode: decrease value in a node • deletenode: delete a node Running Times Examples The following figures are worst-case for binary and binomial heaps, amortized for Fibonacci heaps. Binary Binomial Fibonacci Operation Heap Heap Heap B0 B1 B2 makenull O(1) O(1) O(1) union O(n) O(log n) O(1) insert O(log n) O(log n) O(1) min O(1) O(log n) O(1) deletemin O(log n) O(log n) O(log n) decreasenode O(log n) O(log n) O(1) deletenode O(log n) O(log n) O(log n) B3 Binomial Trees A binomial tree Bk is defined recursively: • B0 is a single node ∗Copyright c Ian Parberry, 1992–2003. 1 Example B4 . B0 B1 B2 B k−2 B k−1 Proof by induction (pretty obvious). Corollary: the maximum degree of an n-node bino- mial tree is log n. Binomial Heaps Facts About Binomial Trees A binomial heap is a linked list of binomial trees The binomial tree Bk has: with the following properties: k 1. 2 nodes 1. The binomial trees are linked in increasing order 2. heightk of size. k i ≤ i ≤ k 2. There is at most one binomial tree of each size. 3. i nodes at depth for all 0 3. Each binomial tree has the heap structure: the valueineachnodeis≤ the values in its children. Proof: By induction on k. The base case is trivial (B0 is a single node). For the inductive step the key Binomial heap properties: facts are: • The root of each binomial tree contains the smallest element in that tree. 1. Bk consists of two copies of Bk−1,andsoby k−1 k−1 • There can be at most log n + 1 binomial trees the induction hypothesis, Bk has 2 +2 in a binomial heap with n nodes. That’s be- nodes. cause binomial trees only come in sizes that are 2. The maximum height of a node in Bk is one powers of 2, and n has log n +1bits. more than the maximum height of a node in the left-hand copy of Bk−1, which by the induction hypothesis is k − 1. Makenull 3. Let D(k, i) be the number of nodes at depth i in Bk. Then, An empty heap consists of an empty list. D k, i D k − ,i D k − ,i− Time O(1). ( )=( 1 )+( 1 1) k − 1 k − 1 = + Union i i − 1 k . = i • This is the hard one. • It’s used in all the other operations. Another Binomial Tree Property • In fact it makes all of the other operations triv- ial. • The root of Bk has degree k, which is the highest We’ll get back to it later. • O n degree in the tree, and Bk looks like this: The running time will be (log ). 2 Insert 2. Now we have something that looks like a binary heap except that we may have two trees of any given size. So we fix it by combining trees of 1. Make a single-node binomial heap containing duplicate size. the new value. 2. Union that with the other binomial heap. Step 1 can be done in O(log n)time.Buthowdowe Time O(1) for Step 1, O(log n) for Step 2. do step 2? First, how do we combine two binomial trees of the Min and Deletemin same size? It’s easy: make one root point to the other. Which one? The root with the larger value should point to the root with the smaller value. 1. Search the list of binomial trees for the one with smallest root m.Returnm.(Fortheminopera- Combining trees may make more duplicates. For ex- tion, stop here.) Remove the tree T containing ample, if we have trees of sizes 8,8,16 we can combine m from the binomial heap. Call the resulting the two trees of size 8 to get another tree of size 16. binomial heap H. 2. Delete the node containing m from T .Makea We want to avoid doing multiple passes through the binomial heap out of the children of the root by tree list. simply reversing the order of its children. Call this new binomial heap H. Perform a single pass along the tree list, combining 3. Union together H and H. any pair of trees of the same size into a new tree. The catch is that this may lead to three trees of the Time O(log n) for each step. same size. For example: Decreasenode Sizes 881616 Same as in regular heaps: repeatedly swap the value with its parent until it reaches a place where it is large than its parent. O n Time (log ). Combine two 8s to Deletenode get a 16 16 16 16 1. Replace the value in the node with −∞ and do a decreasenode. 2. Do a deletemin. Time O(1) for step 1, O(log n) for Step 2. Luckily there can be most three. Intuition: it’s just like binary addition: you only Now for Union need a single carry bit (in our case, “carry tree”). Since a binomial heap has O(log n) binomial trees in We put this off earlier. Two steps: its tree list, this operation takes O(log n)time. 1. Merge together the two lists of trees in order of The textbook CLRS makes a big deal out of this. size. Don’t get lost in the details. 3 Fibonacci Heaps Fibonacci heaps are just like binomial heaps except: • The trees don’t have to be binomial trees. • The tree list doesn’t have to be sorted in order of size. • There may be more than one tree of any partic- ular size. • There is an external pointer to the root with the smallest value. The main idea is that you don’t try to tidy up the data structure until deletemin or deletenode oper- ations are performed. This gives better amortized performance of the nondeleting operations. Unfortunately, the algorithm is not practical: it is mainly of theoretical interest. Easy Operations • makenull: make an empty list with a null min- pointer. • insert: make a new single-node tree and insert it into the tree list. If it is smaller than the min- imum element, adjust the min-pointer to point to it. • min: follow the min-pointer and return the value stored at that node. • union: concatenate the two lists. Make the min pointer point to the smallest of the two minima. Difficult Operations • deletemin • decreasenode • deletenode 4.