0% found this document useful (0 votes)
107 views29 pages

Binomial Heap

A binomial heap is a data structure that implements a priority queue using a collection of binomial trees that satisfy the min-heap property. It allows faster merge operations compared to binary heaps. A binomial tree Bk is recursively defined, where B0 is a single node and Bk consists of two Bk-1 trees linked together. The number of trees in a binomial heap with n nodes equals the number of set bits in the binary representation of n. Common operations on binomial heaps like insertion, extraction, and merging take O(log n) time due to efficient merging of trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views29 pages

Binomial Heap

A binomial heap is a data structure that implements a priority queue using a collection of binomial trees that satisfy the min-heap property. It allows faster merge operations compared to binary heaps. A binomial tree Bk is recursively defined, where B0 is a single node and Bk consists of two Bk-1 trees linked together. The number of trees in a binomial heap with n nodes equals the number of set bits in the binary representation of n. Common operations on binomial heaps like insertion, extraction, and merging take O(log n) time due to efficient merging of trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Binomial Heap

What is a Binomial tree?


A Binomial tree Bk is an ordered tree defined recursively, where k is defined as the order of the
binomial tree.
o If the binomial tree is represented as B0, then the tree consists of a single node.
o In general terms, Bk consists of two binomial trees, i.e., B k-1 and Bk-1 that are linked
together in which one tree becomes the left subtree of another binomial tree.
We can understand it with the example given below.
If B0, where k is 0, there would exist only one node in the tree.

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.

Now, let's start the main topic 'Binomial Heap'.

What is a Binomial Heap?


A binomial heap can be defined as the collection of binomial trees that satisfies the heap
properties, i.e., min-heap. The min-heap is a heap in which each node has a value lesser than the
value of its child nodes. Mainly, Binomial heap is used to implement a priority queue. It is an
extension of binary heap that gives faster merge or union operations along with other operations
provided by binary heap.
Properties of Binomial heap
There are following properties for a binomial heap with n nodes -
o Every binomial tree in the heap must follow the min-heap property, i.e., the key of a
node is greater than or equal to the key of its parent.
o For any non-negative integer k, there should be atleast one binomial tree in a heap where
root has degree k.
The first property of the heap ensures that the min-heap property is hold throughout the heap.
Whereas the second property listed above ensures that a binary tree with n nodes should have at
most 1 + log2 n binomial trees, here log2 is the binary logarithm.
We can understand the properties listed above with the help of an example -

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.

Binomial Heap and the binary representation of a number


A binomial heap with n nodes consists the binomial trees equal to the number of set bits in the
binary representation of n.
Suppose we want to create the binomial heap of 'n' nodes that can be simply defined by the
binary number of 'n'. For example: if we want to create the binomial heap of 13 nodes; the binary
form of 13 is 1101, so if we start the numbering from the rightmost digit, then we can observe
that 1 is available at the 0, 2, and 3 positions; therefore, the binomial heap with 13 nodes will
have B0, B2, and B3 binomial trees.
We can use another example to understand it more clearly, suppose we have to create the
binomial heap with 9 nodes. The binary representation of 9 is 1001. So, in the binary
representation of 9, digit 1 is occurring at 0 and 3 positions, therefore, the binomial heap will
contain the binomial trees of 0 and 3 degrees.
Now, let's move towards the operations performed on Binomial heap.

Operations on Binomial Heap


The operations that can be performed on binomial heap are listed as follows -
o Creating a binomial heap
o Finding the minimum key
o Union or merging of two binomial heaps
o Inserting a node
o Extracting minimum key
o Decreasing a key
o Deleting a node
Now, let's discuss the above-listed operations in detail.
Creating a new binomial heap
When we create a new binomial heap, it simply takes O(1) time because creating a heap will
create the head of the heap in which no elements are attached.

Finding the minimum key


As stated above, binomial heap is the collection of binomial trees, and every binomial tree
satisfies the min-heap property. It means that the root node contains a minimum value.
Therefore, we only have to compare the root node of all the binomial trees to find the minimum
key. The time complexity of finding the minimum key in binomial heap is O(logn).

Union or Merging of two binomial heap


It is the most important operation performed on the binomial heap. Merging in a heap can be
done by comparing the keys at the roots of two trees, and the root node with the larger key will
become the child of the root with a smaller key than the other. The time complexity for finding a
union is O(logn). The function to merge the two trees is given as follows -
1. function merge(a,b)
2. if a.root.key ? b.root.key
3. return a.add(b)
4. else
5. return b.add(a)
To perform the union of two binomial heaps, we have to consider the below cases -
Case 1: If degree[x] is not equal to degree[next x], then move pointer ahead.
Case 2: if degree[x] = degree[next x] = degree[sibling(next x)] then,
Move the pointer ahead.
Case 3: If degree[x] = degree[next x] but not equal to degree[sibling[next x]]
and key[x] < key[next x] then remove [next x] from root and attached to x.
Case 4: If degree[x] = degree[next x] but not equal to degree[sibling[next x]]
and key[x] > key[next x] then remove x from root and attached to [next x].
Now, let's understand the merging or union of two binomial heaps with the help of an example.
Consider two binomial heaps –

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 -

x = 12, next[x] = 7, sibling[next[x]] = 3, and degree[x] = B 1, dgree[next[x]] = B1,


degree[sibling[next[x]]] = B1
Now we will reapply the cases in the above binomial heap. First, we will apply case 1. Since x is
pointing to node 12 and next[x] is pointing to node 7, the degree of x is equal to the degree of
next x; therefore, case 1 is not valid.
Here, case 2 is valid as the degree of x, next[x], and sibling[next[x]] is equal. So, according to
the case, we have to move the pointer ahead.

Therefore, x = 7, next[x] = 3, sibling[next[x]] = 15, and degree[x] = B1, dgree[next[x]] = B1,


degree[sibling[next[x]]] = B2
Now, let's try to apply case 3, here, first condition of case3 is satisfied as degree[x] =
degree[next[x]] ≠ degree[sibling[next[x]]], but second condition (key[x] < key[next x]) of case 3
is not satisfied.
Now, let's try to apply case 4. So, first condition of case4 is satisfied and second condition
(key[x] > key[next x]) is also satisfied. Therefore, remove x from the root and attach it to
[next[x]].

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.

Insert an element in the heap


Inserting an element in the heap can be done by simply creating a new heap only with the
element to be inserted, and then merging it with the original heap. Due to the merging, the single
insertion in a heap takes O(logn) time. Now, let's understand the process of inserting a new node
in a heap using an example.
In the above heap, there are three binomial trees of degrees 0, 1, and 2 are given where B0 is
attached to the head of the heap.
Suppose we have to insert node 15 in the above heap.

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.

Extracting the minimum key


It means that we have to remove an element with the minimum key value. As we know, in min-
heap, the root element contains the minimum key value. So, we have to compare the key value of
the root node of all the binomial trees. Let's see an example of extracting the minimum key from
the heap.
Suppose the heap is -
Now, compare the key values of the root node of the binomial trees in the above heap. So, 12, 7,
and 15 are the key values of the root node in the above heap in which 7 is minimum; therefore,
remove node 7 from the tree as shown in the below image -

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.

Deleting a node from the heap


To delete a node from the heap, first, we have to decrease its key to negative infinity (or -∞) and
then delete the minimum in the heap. Now we will see how to delete a node with the help of an
example. Consider the below heap, and suppose we have to delete the node 41 from the heap -

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.

Complexity of binomial heap


Now, let's see the time complexity of the operations performed on binomial heap.
Time Complexity

Operations Time complexity

Finding the minimum key O(log n)

Inserting a node O(log n)

Extracting minimum key O(log n)


Decreasing a key O(log n)

Union or merging O(log n)

Deleting a node O(log n)

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

Important properties of a Fibonacci heap are:

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.

3. It consists of a set of marked nodes. (Decrease key operation)

4. The trees within a Fibonacci heap are unordered but rooted.

Memory Representation of the Nodes in a Fibonacci Heap

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.

1. Deleting a node from the tree takes O(1) time.


2. The concatenation of two such lists takes O(1) time.

Insertion
Inserting a node into an already existing heap follows the steps below.

1. Create a new node for the element.

2. Check if the heap is empty.

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

The minimum element is always given by the min pointer.


Union

Union of two fibonacci heaps consists of following steps.

1. Concatenate the roots of both the heaps.

2. Update min by selecting a minimum key from the new root lists.

Union of two heaps

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.

The following steps are followed:

1. Delete the min node.

2. Set the min-pointer to the next root in the root list.

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.

6. Map the degree of next root to the degree in 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).

An implementation of the above steps can be understood in the example below.

1. We will perform an extract-min operation on the heap below.

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.

5. Unite those having the same degrees

6. Again, 7 and 17 have the same degrees, so unite them as well.

7. Unite those having the same degrees


8. Again 7 and 24 have the same degree, so unite them.

9. Unite those having the same degrees


10. Map the next nodes.

11. Map the remaining nodes

12. Again, 52 and 21 have the same degree, so unite them


13. Unite those having the same degrees

14. Similarly, unite 21 and 18.

15. Unite those having the same degrees

16. Map the remaining root.

17. Map the remaining nodes


18. The final heap is.

19. Final fibonacci heap

Decreasing a Key and Deleting a Node

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

In decreasing a key operation, the value of a key is decreased to a lower value.

Following functions are used for decreasing the 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

1. If the parent of y is not null then follow the following steps.


2. If y is unmarked, then mark y.
3. Else, call Cut(y) and Cascading-Cut(parent of y).

Decrease Key Example

The above operations can be understood in the examples below.

Example: Decreasing 46 to 15.

1. Decrease the value 46 to 15.

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

part: mark 24.


3. Add 15 to root list and mark 24
Example: Decreasing 35 to 5

1. Decrease the value 35 to 5.

D
ecrease 35 to 5
2. Cut part: Since 26 ≠ nill and 5<its parent, cut it and add it to the root list.

3. Cut 5 and add it to root list


4. Cascading-Cut part: Since 26 is marked, the flow goes to Cut and Cascading-Cut.
Cut(26): Cut 26 and add it to the root list and mark it as false.

5. Cut 26 and add it to root list


Cascading-Cut(24):
Since the 24 is also marked, again call Cut(24) and Cascading-Cut(7). These operations result in
the tree below.

6. Cut 24 and add it to root list


7. Since 5 < 7, mark 5 as min.

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.

You might also like