Lab 2. Binomial Heaps and Fibonacci Heaps
Lab 2. Binomial Heaps and Fibonacci Heaps
1 Binomial Heaps
In general, a heap is an array data structure in which each key is guaranteed to be larger than the keys at two other specific positions. In turn, each of those keys must be larger than two more keys, and so forth. This ordering is very easy to see if we draw the array in a two-dimensional tree structure with lines down from each key to the two keys known to be smaller.
Figure 1. Sample heap viewed as binary tree and as array An array is a heap if for all values the heap properties hold: A[i] A[2*i] A[i] A[2*i +1] From the tree point of view, holding the heap condition becomes: the key in each node should be larger than (or equal to) the keys in its sons (if it has any). Note that this implies in particular that the largest key is in the root. Binomial heaps are an extension or a generalization of the classical heap structure. The generalization regards the fact that a binomial heap is a collection of binomial trees and a binomial tree is a multiway heap.
a)
b)
c)
d)
Figure 2. Sample Binomial Trees The properties of the binomial tree of order k, Bk are, 1. The Bk tree has 2k nodes. 2. The height of the Bk tree is k. 3. In a Bk tree, at depth i there are exactly D(k, i) = D(k-1, i) + D(k-1,i-1) nodes. It is known that D(0,0) = 1, D(1,0) = D(1,1) =1, D(2,0) = D(2,2) = 1. Example: D(3, 2) = D(2,2) + D(2,1) = 1 + D(1,1) + D(1,0) = 1+1+1 = 3. 4. The root has degree k, which is greater than that of any other node; moreover if i the children of the root are numbered from left to right by k - 1, k - 2, ..., 0, child i is the root of a subtree Bi. The structure of a node from a binomial tree is: typedef struct BinomialNode { int inf,rank; BinomialNode *llink,*rlink,*down; }*pBinomialNode; The meaning of the fields from the structure of node is:
inf contains the key of the node. The integer values stored in the tree respect the heap property. rank represents the degree of the node of the binomial tree. For example, in a B3 tree the rank value of the root is 3. llink, rlink, down contain pointers (addresses) to other nodes. down pointer holds the address of the direct child of a node. llink and rlink pointers are used to manage a double linked list of nodes which are children of a certain parent node. There is just only one physical connection between the parent node and only one of the children and that is represented by the down pointer. The next figure presents the physical structure of binomial trees.
Next figure shows shows a binomial heap H with 13 nodes. The binary representation of 13 is 1101, and H consists of binomial trees B3, B2, and B0, having 8, 4, and 1 nodes respectively, for a total of 13 nodes.
Figure 4. Physical structure of binomial heap H13=<B3, B2, -, B0> As shown above figure, each binomial tree within a binomial heap is stored according with the representation used in previous section. Each field of the refVectHeap array holds the address of a binomial tree root if exists. Thus, the roots of the binomial trees within a binomial heap are stored in an array which is referred to as refVectHeap. The degrees of the roots strictly increase as we traverse the array. Thus, in position zero of the array we may find only the address of a binomial tree of degree 0. If such a tree does not exist, a NULL is placed in position zero of the array.
y x; x llink[x]; } return y; }
Figure 5. Merging binomial heaps-input data Step 1. B0 tree from H1 is linked with B0 tree from H2. The result of this linking is a B1 tree.
Step 2. B1 tree from H1 is linked with B1 tree from H2. The result of this linking is a B2 tree.
Step 3. B2 tree from H1 is linked with B2 tree from H2. The result of this linking is a B3 tree.
Result. The resulting heap obtained by union of the initial binomial heaps H1 and H2 is:
Step 2. Node x is removed from the binomial heap H. Step 3. All the children of the binomial tree node x are placed in the binomial heap H. Step 4. The current binomial heap H is merged with the binomial heap H. Thus, it is obtained the new binomial heap H which does not contain key 1.
The procedure decreases a key in the same manner as in general heap, by "bubbling up" the key. After ensuring that the new key is in fact no greater than the current key and then assigning the new key to x, the procedure goes up the tree, with y initially pointing to node x. In each iteration of the while loop key[y] is checked against the key of y's parent z. If y is the root or key[y] key[z], the binomial tree is now correctly ordered. Otherwise, node y violates min-heap ordering, and so its key is exchanged with the key of its parent z. Next figures present the decreasing of key. Step1. Step 2. Step 2. Here is presented in input Value 15 is compared with the Value 15 is compared with the value binomial heap containing one value of the key from the of the key from the parent. Since the binomial tree, B2. parent. Since the value from value from the parent, which is 10, The procedure wants to the parent, which is 30, is is smaller than 15 it means the decrease the value of key 40 to greater than 15 it means key bubbling process ends.. the value of 15. 15 needs to be exchanged with key 30.
2 Fibonacci Heaps
Fibonacci heaps are a different flavor of heap structures. They support the same operations as binomial heaps and have similar structure due to the similar rules of building the structure. Fibonacci heaps are especially used in problems where there is a relatively small number of delete and minimum key extraction operations. For example, there are classical algorithms on graphs, which need operations on edges (e.g. modify/decrease the weight) and in this situation an O(1) amortized time for such operations is a big improvement over the O(lg n) worst-case time of binary or binomial heaps. The main drawback of Fibonacci heaps structure regards the programming complexity which is much higher than ordinary heaps or binomial heaps. That is why, Fibonacci heaps are predominantly of theoretical interest due to the complexity of the data structure. Like a binomial heap, a Fibonacci heap is a collection of trees. Fibonacci heaps, in fact, are loosely based on binomial heaps. Like binomial heaps, Fibonacci heaps are not designed to give efficient support to the operation search; operations that refer to a given node therefore require a pointer to that node as part of
their input. When using Fibonacci heaps a good approach is to have access also to a pointer to the application object from each Fibonacci-heap element.
10
The procedure CreateNewFibonacciTree() creates a new Fibonacci tree node, sets its rank to zero, and all its pointers to NULL. For example, adding 6 to the presented sample Fibonacci heap creates the following structure.
11
n[H] n[H] 1; } return z; } The procedure works by first making a root out of each of the minimum node's children and removing the minimum node from the root list. It then consolidates the root list by linking roots of equal degree until at most one root remains of each degree. The procedure that consolidates the heap is: procedure Consolidate(FibonacciHeap H){ for i 0 to D(n[H]) do{ A[i] NIL; } for each node w in the root list of H do{ xw d degree[x] while (A[d] NIL) do{ y A[d]; //Another node with the same degree as x. if ( key[x] > key[y] ) exchange x y FibonacciHeapLink(H, y, x); A[d] NIL; d d + 1; }//end while A[d] x; }//end for each min[H] NIL; for i 0 to D(n[H]) do{ if ( A[i] NIL ){ #add A[i] to the root list of H; if ( min[H] = NIL or key[A[i]] < key[min[H]] ) min[H] A[i]; }// end if }// end for } The functionality of FibonacciHeapLink procedure is quite similar with the functionality of BinomialLink procedure used for binomial heaps. procedure FibonacciHeapLink (FibonacciHeap H, FibonacciTreeRoot y, FibonacciTreeRoot x){ # remove y from the root list of H # make y a child of x, incrementing degree[x] mark[y] FALSE; } Next figures present the steps of extracting the minimum key from a Fibonacci heap. Input: This figure presents the input Fibonacci heap with the minimum key of value 1 in grey filling.
12
Step1: At this step, all the children of root node 1 (trees with roots 19, 18 and 25) are inserted as trees of the Fibonacci heap. Since none of the newly added tree roots is not smaller than current minimum value, the minimum of the Fibonacci heap remains the same. The next steps will consolidate the heap by uniting trees of the same degree in trees of higher degree.
Step 2: Merge 8 and 10, merge 18 and 19. Tree with root 8, containing only one node, is merged with the tree with root 10, also containing only one node. The resulting tree has root 8 with degree 1. In the same way, the tree with root 18, containing two nodes, is merged with the tree with root 19, containing two nodes. The resulting tree has root 18 with degree 2. Step 3. Merge 2 and 8, merge 18 and 11. Tree with root 8, containing two nodes, is merged with the tree with root 2, also containing two nodes. The resulting tree has root 2 with degree 2. In the same way, the tree with root 18 is merged with the tree with root 11. The resulting tree has root 11 with
13
degree 3.
14
Decrease the value of key 10 to value 3 Initially, the Fibonacci heap has two trees with roots 25 and 2. The degrees of roots are 0 and 2, respectively. Decreasing key 10 to value 3 cuts the node with key 10, puts value 3 in it and places it as a root node with degree zero.
Decrease the value of key 8 to value 1 Initially, the Fibonacci heap has three trees with roots 3, 25 and 2. The degrees of roots are 0, 0 and 2, respectively. Decreasing key 8 to value 1 cuts the node with key 8, puts value 1 in it and places it as a root node with degree zero.
Here is the final shape of the Fibonacci heap. Observe that the min pointer now contains the address of root node with key 1.
3 Assignments
1) Compute the number of nodes at level 5 in a B8 tree (binomial tree of degree 8). 2) Suppose that x is a node in a binomial tree within a binomial heap, and assume that llink[x] NIL. Can x be a root node? What is the value of degree[llink[x]] compared to degree[x]? 3) If x is a nonroot node in a binomial tree within a binomial heap, how does degree[x] compare to degree[parent[x]]? 4) Present the final binomial heap after inserting keys 7, 2, 6, 10, 15, 32, 17. Present the obtained binomial heap after each insertion. 5) Implement a function that directly inserts a binomial node into a binomial heap without merging two heaps. 6) Implement graph algorithms such as computing minimum spanning trees and finding single source shortest paths using Fibonacci heaps. 7) Show the Fibonacci heap that results from calling FibonacciHeapExtractMin on the Fibonacci heap shown in below figure.
15
16