0% found this document useful (0 votes)
5 views

Advances in Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Advances in Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Heap Data Structure

What is Heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have
utmost two children. Before knowing more about the heap data structure, we should know
about the complete binary tree.
What is a complete binary tree?
A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf
node should be completely filled, and all the nodes should be left-justified.

In the above figure, we can observe that all the internal nodes are completely filled
except the leaf node; therefore, we can say that the above tree is a complete binary tree.

The above figure shows that all the internal nodes are completely filled except the leaf
node, but the leaf nodes are added at the right part; therefore, the above tree is not a complete
binary tree.

There are two types of the heap:


o Min Heap
o Max heap
Min Heap: The value of the parent node should be less than or equal to either of its children.
Or
In other words, the min-heap can be defined as, for every node i, the value of node i is greater
than or equal to its parent value except the root node. Mathematically, it can be defined as:
A[Parent(i)] <= A[i]
Let's understand the min-heap through an example.
In the above figure, 11 is the root node, and the value of the root node is less than the value of
all the other nodes (left child or a right child).

Max Heap: The value of the parent node is greater than or equal to its children.
Or
In other words, the max heap can be defined as for every node i; the value of node i is less
than or equal to its parent value except the root node. Mathematically, it can be defined as:
A[Parent(i)] >= A[i]

The above tree is a max heap tree as it satisfies the property of the max heap. Now, let's see
the array representation of the max heap.

Time complexity in Max Heap


The total number of comparisons required in the max heap is according to the height
of the tree. The height of the complete binary tree is always logn; therefore, the time
complexity would also be O(logn).

i. Max Heap Tree


44, 33, 77, 11, 55, 88, 66
Suppose we want to create the max heap tree. To create the max heap tree, we need to
consider the following two cases:
o First, we have to insert the element in such a way that the property of the complete
binary tree must be maintained.
o Secondly, the value of the parent node should be greater than the either of its child.

Step 1: First we add the 44 element in the tree as shown below:

Step 2: The next element is 33. As we know that insertion in the binary tree always starts
from the left side so 44 will be added at the left of 33 as shown below:

Step 3: The next element is 77 and it will be added to the right of the 44 as shown below:

As we can observe in the above tree that it does not satisfy the max heap property, i.e., parent
node 44 is less than the child 77. So, we will swap these two values as shown below:

Step 4: The next element is 11. The node 11 is added to the left of 33 as shown below:
Step 5: The next element is 55. To make it a complete binary tree, we will add the node 55 to
the right of 33 as shown below:

As we can observe in the above figure that it does not satisfy the property of the max heap
because 33<55, so we will swap these two values as shown below:

Step 6: The next element is 88. The left subtree is completed so we will add 88 to the left of
44 as shown below:

As we can observe in the above figure that it does not satisfy the property of the max heap
because 44<88, so we will swap these two values as shown below:
Again, it is violating the max heap property because 88>77 so we will swap these two values
as shown below:
Step 7: The next element is 66. To make a complete binary tree, we will add the 66 element
to the right side of 77 as shown below:

In the above figure, we can observe that the tree satisfies the property of max heap; therefore,
it is a heap tree.
Max Heap Operations-

We will discuss the construction of a max heap and how following operations are performed
on a max heap-
 Finding Maximum Operation
 Insertion Operation
 Deletion Operation

Max Heap Construction-


Given an array of elements, the steps involved in constructing a max heap are-
Step-01:
Convert the given array of elements into an almost complete binary tree.
Step-02:
Ensure that the tree is a max heap.
 Check that every non-leaf node contains a greater or equal value element than its child
nodes.
 If there exists any node that does not satisfies the ordering property of max heap,
swap the elements.
 Start checking from a non-leaf node with the highest index (bottom to top and right to
left).

Finding Maximum Operation-

 In max heap, the root node always contains the maximum value element.
 So, we directly display the root node value as maximum value in max heap.

Insertion Operation-

Insertion Operation is performed to insert an element in the heap tree.

The steps involved in inserting an element are-


Step-01:
Insert the new element as a next leaf node from left to right.
Step-02:
Ensure that the tree remains a max heap.
 Check that every non-leaf node contains a greater or equal value element than its child
nodes.
 If there exists any node that does not satisfies the ordering property of max heap,
swap the elements.
 Start checking from a non-leaf node with the highest index (bottom to top and right to
left).

Deletion Operation-

Deletion Operation is performed to delete a particular element from the heap tree.

When it comes to deleting a node from the heap tree, following two cases are possible-

Case-01: Deletion Of Last Node-

 This case is pretty simple.


 Just remove / disconnect the last leaf node from the heap tree.

Case-02: Deletion Of Some Other Node-

 This case is little bit difficult.


 Deleting a node other than the last node disturbs the heap properties.

The steps involved in deleting such a node are-


Step-01:
 Delete the desired element from the heap tree.
 Pluck the last node and put in place of the deleted node.
Step-02:
Ensure that the tree remains a max heap.
 Check that every non-leaf node contains a greater or equal value element than its child
nodes.
 If there exists any node that does not satisfies the ordering property of max heap,
swap the elements.
 Start checking from a non-leaf node with the highest index (bottom to top and right to
left).

Merge of Two Heaps:

Insert a Key
Let’s add a node with key 11 to our heap
Merge of Two Heaps
Inserting a node

we need to add node 15 to the heap mentioned above.

Extracting Minimum Key


PRACTICE PROBLEMS BASED ON MAX HEAP OPERATIONS-

Problem-01:
Construct a max heap for the given array of elements- 1, 5, 6, 8, 12, 14, 16
Solution-

Step-01:

We convert the given array of elements into an almost complete binary tree-

Step-02:
We ensure that the tree is a max heap.
 Node 6 contains greater element in its right child node.
 So, we swap node 6 and node 16.

The resulting tree is-

Step-03:
 Node 5 contains greater element in its right child node.
 So, we swap node 5 and node 12.

The resulting tree is-


Step-04:

 Node 1 contains greater element in its right child node.


 So, we swap node 1 and node 16.

The resulting tree is-

Step-05:

 Node 1 contains greater element in its left child node.


 So, we swap node 1 and node 14.

The resulting tree is-

This is the required max heap for the given array of elements.

Problem-02:
Consider the following max heap- 50, 30, 20, 15, 10, 8, 16.
Insert a new node with value 60.
Solution-

Step-01:

We convert the given array of elements into a heap tree-

Step-02:
We insert the new element 60 as a next leaf node from left to right.
The resulting tree is-

Step-03:

 We ensure that the tree is a max heap.


 Node 15 contains greater element in its left child node.
 So, we swap node 15 and node 60.

The resulting tree is-

Step-04:

 Node 30 contains greater element in its left child node.


 So, we swap node 30 and node 60.
The resulting tree is-

Step-05:

 Node 50 contains greater element in its left child node.


 So, we swap node 50 and node 60.

The resulting tree is-

This is the required max heap after inserting the node with value 60.

Problem-03:

Consider the following max heap- 50, 30, 20, 15, 10, 8, 16. Delete a node with value 50.
Solution-

Step-01:
We convert the given array of elements into a heap tree-
Step-02:

 We delete the element 50 which is present at root node.


 We pluck the last node 16 and put in place of the deleted node.

The resulting tree is-

Step-03:

 We ensure that the tree is a max heap.


 Node 16 contains greater element in its left child node.
 So, we swap node 16 and node 30.

The resulting tree is-


This is the required max heap after deleting the node with value 50.

Binomial Heap
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have
utmost two children. There are two types of heap that are defined as follows -
o Min Heap: The value of the parent node should be less than or equal to either of its
children.
Mathematically, the min-heap can be defined as -
A[Parent(i)] <= A[i]
o Max Heap: The value of the parent node is greater than or equal to its children.
Mathematically, the max-heap can be defined as -
A[Parent(i)] >= A[i]
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., Bk-1 and Bk-1 that are linked
together in which one tree becomes the left subtree of another binomial tree.
o We can understand it with the example given below.
o If B0, where k is 0, there would exist only one node in the tree.

o
o 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.

o
o 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.

o
o 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.

o
o 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.

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).
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.



 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 B 2. 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 B2, 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 B 3, 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 B1. The above heap is the
final heap after extracting the minimum key.
 AD

 Decreasing a key
o 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.
o Let's understand the process of decreasing a key in a binomial heap
using an example. Consider a heap given below -

o Decrease the key 45 by 7 of the above heap. After decreasing 45


by 7, the heap will be -
o 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 -

o 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 -

o Now, the min-heap property of the above heap is satisfied. So, the
above heap is the final heap after decreasing a key.
 AD

 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


o 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).
Leftist Tree / Leftist Heap

A leftist tree, also known as a leftist heap, is a type of binary heap data structure
used for implementing priority queues. Like other heap data structures, it is a
complete binary tree, meaning that all levels are fully filled except possibly the
last level, which is filled from left to right.
1. In a leftist tree, the priority of the node is determined by its key value,
and the node with the smallest key value is designated as the root node.
The left subtree of a node in a leftist tree is always larger than the right
subtree, based on the number of nodes in each subtree. This is known as
the “leftist property.”
2. One of the key features of a leftist tree is the calculation and maintenance
of the “null path length” of each node, which is defined as the distance
from the node to the nearest null (empty) child. The root node of a leftist
tree has the shortest null path length of any node in the tree.
3. The main operations performed on a leftist tree include insert, extract-min
and merge. The insert operation simply adds a new node to the tree, while
the extract-min operation removes the root node and updates the tree
structure to maintain the leftist property. The merge operation combines
two leftist trees into a single leftist tree by linking the root nodes and
maintaining the leftist property.

A leftist tree is a binary tree with properties:


1. Normal Min Heap Property : key(i) >= key(parent(i))
2. Heavier on left side : dist(right(i)) <= dist(left(i)). Here, dist(i) is the number of
edges on the shortest path from node i to a leaf node in extended binary tree
representation (In this representation, a null child is considered as external or leaf
node). The shortest path to a descendant external node is through the right child.
Every subtree is also a leftist tree and dist( i ) = 1 + dist( right( i ) ).

Example: The below leftist tree is presented with its distance calculated for each node with
the procedure mentioned above. The rightmost node has a rank of 0 as the right subtree of
this node is null and its parent has a distance of 1 by dist( i ) = 1 + dist( right( i )). The same is
followed for each node and their s-value( or rank) is calculated.

From above second property, we can draw two conclusions :


1. The path from root to rightmost leaf is the shortest path from root to a leaf.
2. If the path to rightmost leaf has x nodes, then leftist heap has atleast 2x – 1 nodes. This
means the length of path to rightmost leaf is O(log n) for a leftist heap with n nodes.
Operations :
1. The main operation is merge().
2. deleteMin() (or extractMin() can be done by removing root and calling merge() for
left and right subtrees.
3. insert() can be done be create a leftist tree with single key (key to be inserted) and
calling merge() for given tree and tree with single node.
Idea behind Merging : Since right subtree is smaller, the idea is to merge right subtree of a
tree with other tree. Below are abstract steps.
1. Put the root with smaller value as the new root.
2. Hang its left subtree on the left.
3. Recursively merge its right subtree and the other tree.
4. Before returning from recursion: – Update dist() of merged root. – Swap left and right
subtrees just below root, if needed, to keep leftist property of merged result
Detailed Steps for Merge:
1. Compare the roots of two heaps.
2. Push the smaller key into an empty stack, and move to the right child of smaller key.
3. Recursively compare two keys and go on pushing the smaller key onto the stack and
move to its right child.
4. Repeat until a null node is reached.
5. Take the last node processed and make it the right child of the node at top of the stack,
and convert it to leftist heap if the properties of leftist heap are violated.
6. Recursively go on popping the elements from the stack and making them the right
child of new stack top.
Example: Consider two leftist heaps given below:

Merge them into a single leftist heap


The subtree at node 7 violates the property of leftist heap so we swap it with the left child
and retain the property of leftist heap.

Convert to leftist heap. Repeat the process


The following picture shows a good example of merge steps. Note that the NplNpl of the
node in picture is 1 larger than our's definition. The blue curve represents the final swap step.

3. Perform the merge operation of the two leftist heaps.

4. Merging
1. Since the right subtree of a leftist heap is smaller than the left subtree, we merge the
right subtree with the other tree.
2. The following are the steps for merge operation:
3. Make the root with a smaller value than the new root.
4. Push this root into an empty stack and move to its right child.
5. Recursively compare the two keys, keep pushing the smaller key into the stack, and
move to its right child.
6. Perform step 3 until a null node is reached.
7. Pick the last node processed and make it the right child of the node present at the top
of the stack.
8. Recursively pop the elements from the stack and keep making them the right child of
the new node present at the stack top.

Inserting into a leftist heap


The insertion operation is done using the merge operation. We create a new leftist
heap with a single node having a value of the element to be inserted. We then merge this
newly created heap with the existing heap.
1. newHeap = createLeftistHeap(val)
2. merge(leftistHeap, newHeap)

Deleting Min element from a leftist heap


The min-element is the root of a leftist heap. So, to delete the min-element, we replace
the root element with the merge of its left and right subtrees.
1. Val = root.val
2. root = merge(root.right, root.left)
3. Return val

Fibonacci Heap
https://discourse.opengenus.org/t/fibonacci-heap/3589
https://www.programiz.com/dsa/fibonacci-heap
A Fibonacci heap is a heap data structure similar to the binomial heap. It uses Fibonacci
numbers and also used to implement the priority queue element in Dijkstra’s shortest path
algorithm which reduces the time complexity from O(m log n) to O(m + n log n), giving the
algorithm a huge boost in terms of running time. A fibonacci heap is a data structure that
consists of a collection of trees which follow min heap or max heap property. 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.

Structure
A Fibonacci heap is a collection of rooted trees that are min-heap ordered. That is, each tree
obeys the min-heap property: the key of a node is greater than or equal to the key of its
parent. Fibonacci heaps are similar to binomial heaps but Fibonacci heaps have a less rigid
structure. Each tree has an order just like the binomial heap that is based on the number of
children. Nodes within a Fibonacci heap can be removed from their tree without restructuring
them, so the order does not necessarily indicate the maximum height of the tree or number of
nodes it contains. An example of Fibonacci Heap consisting of five min-heap-ordered trees
and 14 nodes. The minimum node of the heap is the node containing the key 3.
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.

Marked Nodes
An important part of the Fibonacci Heap is how it marks nodes within the trees. The decrease
key operation marks a node when its child is cut from a tree, this allows it to track some
history about each node. Essentially the marking of nodes allows us to track whether:
 The node has had no children cut (unmarked)
 The node has had a single child cut (marked)
 The node is about to have a second child cut (removing a child of a marked node)
When a second child is cut from its parent, the parent is moved to the root list. This ensures
that the structure of the Fibonacci heap does not stray too far from that of the binomial heap,
which is one of the properties that enables the data structure to achieve its amortised time
bounds.

Notation
 n = number of nodes in heap.
 rank(x) = number of children of node x.
 rank(H) = max rank of any node in heap H.
 trees(H) = number of trees in heap H.
 marks(H) = number of marked nodes in heap H.
Then Potential Function of Fibonacci Heaps:

For the figure above, n = 14,


rank = 3, trees(H) = 5, marks(H) = 3 Then, potential of the heap = 5 + 2.3 = 11
Operations
The different operations supported by Fibonacci heap are:
 Find minimum 【O(1)】
 Insertion 【O(1)】
 Union 【O(1)】
 Extract minimum 【O(log N)】
 Decrease key 【O(1)】
 Deletion 【O(log N)】

Find Minimum
Finding minimum is one of the most important operations regarding Fibonacci heaps. A
pointer to minimum node of the root list is always kept up to date.
Insertion
Insertion to a Fibonacci heap is similar to the insert operation of a binomial heap. A heap of
one element is created and the two heaps are merged with the merge function. The minimum
element pointer is updated if necessary. The total number of nodes in the tree increases by
one. To insert a node in a Fibonacci heap H, the following algorithm is followed:
1. Create a new node 'x'.
2. Check whether heap H is empty or not.
3. If H is empty then:
o Make x as the only node in the root list.
o Set H(min) pointer to x.
4. Else:
o Insert x into root list and update H(min).
Example:
Union
Union concatenates the root lists of two Fibonacci heaps and sets the minimum node to which
ever tree’s minimum node is smaller. Union of two Fibonacci heaps Tree1 and Tree2 can be
accomplished by following algorithm:
1. Join root lists of Fibonacci heaps Tree1 and Tree2 and make a single Fibonacci heap
H.
2. If Tree1(min) < Tree2(min) then:
3. Else:
Example:
It 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. It is one of the most important
operations regarding Fibonacci heaps. Much of a Fibonacci heap’s speed advantage comes
from the fact that it delays consolidating heaps after operations until extract-min is called.
Binomial heaps, on the other hand, consolidate immediately. Consolidation occurs when heap
properties are violated, for example, if two heaps have the same order, the heaps must be
adjusted to prevent this. Following algorithm is used for Extract min in a Fibonacci Heap -
1. Delete the min node.
2. Set head to the next min node and add all the tree of the deleted node in root list.
3. Create an array of degree pointers of the size of the deleted node.
4. Set degree pointer to current node. Move to the next node.
o If degrees are different then set degree pointer to next node.
o If degrees are same then join the Fibonacci trees by union operation.
5. Repeat steps 4 and 5 until the heap is completed.
Example:
Decrease Key
Decrease key lowers the key of a node. The node is then cut from the tree, joining the root list
as its own tree. The parent of the node is then cut if it is marked, this continues for each
anscestor until a parent that is not marked is encountered, which is then marked. The pointer
to the minimum node is then updated if the node’s new value is less than the current
minimum.
Algorithm
1. Decrease the value of the node ‘x’ to the new chosen value.
2. CASE 1 - If min heap order not violated,
o Update min pointer if necessary.
3. CASE 2 - If min heap order violated and parent of ‘x’ is unmarked,
o Cut off the link between ‘x’ and its parent.
o Mark the parent of ‘x’.
o Add tree rooted at ‘x’ to the root list and update min pointer if necessary.
4. CASE 3 - If min heap order is violated and parent of ‘x’ is marked,
o Cut off the link between ‘x’ and its parent p[x].
o Add ‘x’ to the root list, updating min pointer if necessary.
o Cut off link between p[x] and p[p[x]].
o Add p[x] to the root list, updating min pointer if necessary.
o If p[p[x]] is unmarked, mark it.
o Else, cut off p[p[x]] and repeat steps 4.2 to 4.5, taking p[p[x]] as ‘x’.
Example:

Deletion
Delete is performed by calling decrease key to reduce the node to negative infinity which
pulls the node to the top of the tree. Extract minimum is then called on the node to remove it
from the heap.
Algorithm
1. Decrease the value of the node to be deleted ‘x’ to minimum by Decrease Key
function.
2. By using min heap property, heapify the heap containing ‘x’, bringing ‘x’ to the root
list.
3. Apply Extract Min algorithm to the Fibonacci heap.

Complexity + Comparison
Comparision of time complexities for various operations:
To determine the amortized cost of FIB-HEAP-INSERT, let H be the input Fibonacci heap
and H' be the resulting Fibonacci heap. Then, t(H') = t(H) + 1 and m(H') = m(H) and the
increase in potential is (t(H) + 1) + 2m(H)) - (t(H) + 2m(H)) = 1.Since the actual cost is O(1),
the amortized cost is O(1) + 1 = O(1).
The minimum node of a Fibonacci heap H is given by the pointer H.min, so we can find the
minimum node in O(1) actual time. Because the potential of H does not change, the
amortized cost of this operation is equal to its O(1) actual cost.

You might also like