Binomial Heap
Binomial Heap
If B1, where k is 1. Therefore, there would be two binomial trees of B 0 in which one B0 becomes
the left subtree of another B0.
If B2, where k is 2. Therefore, there would be two binomial trees of B 1 in which one B1 becomes
the left subtree of another B1.
If B3, where k is 3. Therefore, there would be two binomial trees of B 2 in which one B2 becomes
the left subtree of another B2.
The above figure has three binomial trees, i.e., B 0, B2, and B3. The above all three binomial trees
satisfy the min heap's property as all the nodes have a smaller value than the child nodes.
The above figure also satisfies the second property of the binomial heap. For example, if we
consider the value of k as 3, we can observe in the above figure that the binomial tree of degree 3
exists in a heap.
We can see that there are two binomial heaps, so, first, we have to combine both heaps. To
combine the heaps, first, we need to arrange their binomial trees in increasing order.
In the above heap first, the pointer x points to the node 12 with degree B 0, and the pointer next[x]
points the node 18 with degree B 0. Node 7 with degree B1 is the sibling of 18, therefore, it is
represented as sibling[next[x]].
Now, first apply Case1 that says 'if degree[x] ≠ degree[next x] then move pointer ahead' but
in the above example, the degree[x] = degree[next[x]], so this case is not valid.
Now, apply Case2 that says 'if degree[x] = degree[next x] = degree[sibling(next x)] then
Move pointer ahead'. So, this case is also not applied in the above heap.
Now, apply Case3 that says ' If degree[x] = degree[next x] ≠ degree[sibling[next x]] and
key[x] < key[next x] then remove [next x] from root and attached to x'. We will apply this
case because the above heap follows the conditions of case 3 -
degree[x] = degree[next x] ≠ degree[sibling[next x]] {as, B 0 = B0¬ ≠ B1} and key[x] < key[next
x] {as 12 < 18}.
So, remove the node 18 and attach it to 12 as shown below -
Now, the pointer x points to node 3, next[x] points to node 15, and sibling[next[x]] points to the
node 6. Since, the degree of x is equal to the degree of next[x] but not equal to the
degree[sibling[next[x]]], and the key value of x is less than the key value of next[x], so we have
to remove next[x] and attach it to x as shown below -
Now, x represents to the node 3, and next[x] points to node 6. Since, the degree of x and next[x]
is not equal, so case1 is valid. Therefore, move the pointer ahead. Now, the pointer x points the
node 6.
The B4 is the last binomial tree in a heap, so it leads to the termination of the loop. The above
tree is the final tree after the union of two binomial heaps.
First, we have to combine both of the heaps. As both node 12 and node 15 are of degree 0, so
node 15 is attached to node 12 as shown below -
Now, assign x to B0 with value 12, next(x) to B0 with value 15, and assign sibling(next(x)) to
B1 with value 7. As the degree of x and next(x) is equal. The key value of x is smaller than the
key value of next(x), so next(x) is removed and attached to the x. It is shown in the below image
-
Now, x points to node 12 with degree B 1, next(x) to node 7 with degree B 1, and sibling(next(x))
points to node 15 with degree B2. The degree of x is equal to the degree of next(x) but not equal
to the degree of sibling(next(x)). The key value of x is greater than the key value of next(x);
therefore, x is removed and attached to the next(x) as shown in the below image -
Now, x points to node 7, and next(x) points to node 15. The degree of both x and next(x) is B 2,
and the key value of x is less than the key value of next(x), so next(x) will be removed and
attached to x as shown in the below image -
Now, the degree of the above heap is B3, and it is the final binomial heap after inserting node 15.
Now, the degree of node 12 and node 25 is B 0, and the degree of node 15 is B2. Pointer x points
to the node 12, next(x) points to the node 25, and sibling(next(x)) points to the node 15. Since
the degree of x is equal to the degree of next(x) but not equal to the degree of sibling(next(x)).
Value of pointer x is less than the pointer next(x), so node 25 will be removed and attached to
node 12 as shown in the below image -
Now, the degree of node 12 is changed to B 1. The above heap is the final heap after extracting
the minimum key.
Decreasing a key
Now, let's move forward to another operation to be performed on binomial heap. Once the value
of the key is decreased, it might be smaller than its parent's key that results in the violation of
min-heap property. If such case occurs after decreasing the key, then exchange the element with
its parent, grandparent, and so on until the min-heap property is satisfied.
Let's understand the process of decreasing a key in a binomial heap using an example. Consider a
heap given below -
Decrease the key 45 by 7 of the above heap. After decreasing 45 by 7, the heap will be -
After decreasing the key, the min-heap property of the above heap is violated. Now, compare 7
wits its parent 30, as it is lesser than the parent, swap 7 with 30, and after swapping, the heap will
be -
Again compare the element 7 with its parent 8, again it is lesser than the parent, so swap the
element 7 with its parent 8, after swapping the heap will be -
Now, the min-heap property of the above heap is satisfied. So, the above heap is the final heap
after decreasing a key.
First, replace the node with negative infinity (or -∞) as shown below -
Now, swap the negative infinity with its root node in order to maintain the min-heap property.
Now, again swap the negative infinity with its root node.
The next step is to extract the minimum key from the heap. Since the minimum key in the above
heap is -infinity so we will extract this key, and the heap would be:
The above is the final heap after deleting the node 41.
The time complexity of finding the minimum element from the heap can be reduced to O(1). It
can be done by using an additional pointer to the minimum element.
Space Complexity
The space complexity of a binomial heap with 'n' elements is O(n).
Fibonacci heap
A fibonacci heap is a data structure that consists of a collection of trees which follow min heap
or max heap property. We have already discussed min heap and max heap property in
the Heap Data Structure article. These two properties are the characteristics of the trees present
on a fibonacci heap.
In a fibonacci heap, a node can have more than two children or no children at all. Also, it has
more efficient heap operations than that supported by the binomial and binary heaps.
The fibonacci heap is called a fibonacci heap because the trees are constructed in a way such
that a tree of order n has at least Fn+2 nodes in it, where Fn+2 is the (n + 2)th Fibonacci number.
Properties of a Fibonacci Heap
1. It is a set of min heap-ordered trees. (i.e. The parent is always smaller than the children.)
2. A pointer is maintained at the minimum element node.
The roots of all the trees are linked together for faster access. The child nodes of a parent node
are connected to each other through a circular doubly linked list as shown below.
There are two main advantages of using a circular doubly linked list.
Insertion
Inserting a node into an already existing heap follows the steps below.
3. If the heap is empty, set the new node as a root node and mark it min.
4. Else, insert the node into the root list and update min.
Inser
tion Example
Find Min
2. Update min by selecting a minimum key from the new root lists.
Extract Min
It is the most important operation on a fibonacci heap. In this operation, the node with minimum
value is removed from the heap and the tree is re-adjusted.
3. Create an array of size equal to the maximum degree of the trees in the heap before deletion.
4. Do the following (steps 5-7) until there are no multiple roots with the same degree.
5. Map the degree of current root (min-pointer) to the degree in the array.
7. If there are more than two mappings for the same degree, then apply union operation to those
roots such that the min-heap property is maintained (i.e. the minimum is at the root).
Fibo
nacci Heap
2. Delete the min node, add all its child nodes to the root list and set the min-pointer to the next root
in the root list.
Delet
e the min node
3. The maximum degree in the tree is 3. Create an array of size 4 and map degree of the next roots
with the array.
Creat
e an array
4. Here, 23 and 7 have the same degrees, so unite them.
These are the most important operations which are discussed in Decrease Key and Delete Node
Operations.
Now, we will discuss two of its important operations.
1. Decrease a key: decreases the value of a the key to any lower value
2. Delete a node: deletes the given node
Decreasing a Key
Decrease-Key
1. Select the node to be decreased, x, and change its value to the new value k.
2. If the parent of x, y, is not null and the key of parent is greater than that of the k then
call Cut(x) and Cascading-Cut(y) subsequently.
3. If the key of x is smaller than the key of min, then mark x as min.
Cut
1. Remove x from the current position and add it to the root list.
2. If x is marked, then mark it as false.
Cascading-Cut
Decrease 46 to 15
2. Cut part: Since 24 ≠ nill and 15 < its parent, cut it and add it to the root list. Cascading-Cut
D
ecrease 35 to 5
2. Cut part: Since 26 ≠ nill and 5<its parent, cut it and add it to the root list.
Ma
rk 5 as min
Deleting a Node
This process makes use of decrease-key and extract-min operations. The following steps are
followed for deleting a node.
1. Let k be the node to be deleted.
2. Apply decrease-key operation to decrease the value of k to the lowest possible value (i.e. -∞).
3. Apply extract-min operation to remove this node.