Like Binomial heaps, Fibonacci heaps are collection of trees. They are loosely based on binomial heaps. Unlike trees within binomial heaps, trees within Fibonacci heaps are rooted but unordered.
Each node x in Fibonacci heaps contains a pointer p[x] to its parent, and a pointer child[x] to any one of its children. The children of x are linked together in a circular doubly linked list known as the child list of x.
Each child y in a child list has pointers left[y] and right[y] to point to the left and right siblings of y respectively. If node y is the only child, then left[y] = right[y] = y. The order in which siblings appear in a child list is arbitrary.
Structure of Fibonacci Heap
Each node x in Fibonacci heap contains the following fields:
- key[x]: key of node x
- degree[x]: number of children of x
- p[x]: parent of x
- child[x]: any one of the children of x
- left[x]: left sibling of x in the child list of x
- right[x]: right sibling of x in the child list of x
- mark[x]: boolean value to indicate whether x has lost a child since the last time x was made the child of another node
Each Fibonacci heap H is a set 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.
The root list of H is a circular doubly linked list of elements of H containing exactly one root from each tree in the root list. The min pointer in H points to the root of the tree containing the minimum key.

This Fibonacci Heap H consists of five Fibonacci Heaps and 16 nodes. The line with arrow head indicates the root list. Minimum node in the list is denoted by min[H] which is holding 4.
The asymptotically fast algorithms for problems such as computing minimum spanning trees, finding single source of shortest paths etc. make essential use of Fibonacci heaps.
Operations on Fibonacci Heap
Following are the operations that can be performed on Fibonacci heap:
- Make-Fib-Heap().
- Fib-Heap-Insert(H, x).
- Fib-Heap-Extract-Min(H).
- Fib-Heap-Decrease-Key(H, x, k).
- Fib-Heap-Delete(H, x).
Insert Operation on Fibonacci Heap
Adds a new node x to the heap H. Its super fast, just inserts without much rearranging.
Algorithm for Insert Operation
Let’s assume that we have a Fibonacci heap H and a node x. The algorithm to insert node x into Fibonacci heap H is as follows:
1: Create a new Fibonacci Heap H' containing only x. 2: Set x.left = x and x.right = x (circular doubly linked list). 3: If H is empty, set H.min = x. 4: Otherwise, Insert x into the root list of H and update H.min if x.key < H.min.key. 5: Increase the total node count of H.
Code for Insert Operation
Let’s write a simple code to insert a node into Fibonacci heap.
#include <stdio.h>#include <stdlib.h>#include <math.h>structNode{int key, degree;structNode*parent,*child,*left,*right;int mark;};structFibonacciHeap{structNode* min;int nodeCount;};structNode*createNode(int key){structNode* newNode =(structNode*)malloc(sizeof(structNode));
newNode->key = key;
newNode->degree =0;
newNode->parent = newNode->child =NULL;
newNode->mark =0;
newNode->left = newNode->right = newNode;return newNode;}structFibonacciHeap*createHeap(){structFibonacciHeap* heap =(structFibonacciHeap*)malloc(sizeof(structFibonacciHeap));
heap->min =NULL;
heap->nodeCount =0;return heap;}voidinsert(structFibonacciHeap* H,int key){structNode* x =createNode(key);if(!H->min){
H->min = x;}else{
x->right = H->min->right;
x->left = H->min;
H->min->right->left = x;
H->min->right = x;if(x->key < H->min->key){
H->min = x;}}
H->nodeCount++;}voidprintHeap(structFibonacciHeap* H){if(!H->min){printf("Heap is empty!\n");return;}structNode* temp = H->min;printf("Fibonacci Heap Root List: ");do{printf("%d ", temp->key);
temp = temp->right;}while(temp != H->min);printf("\n");}intmain(){structFibonacciHeap* fibHeap =createHeap();insert(fibHeap,10);insert(fibHeap,3);insert(fibHeap,15);insert(fibHeap,7);printHeap(fibHeap);printf("Minimum value in heap: %d\n", fibHeap->min->key);return0;}
Output
Following is the output of the above code:
Fibonacci Heap Root List: 3 7 10 15 Minimum value in heap: 3
Extract Minimum Operation on Fibonacci Heap
Removes the node containing the minimum key from the heap H. Its a bit complex operation as it involves rearranging the tree structure.
Algorithm
Let’s assume that we have a Fibonacci heap H. The algorithm to extract minimum node from Fibonacci heap H is as follows:
1: z = H.min 2: If z != NULL 1 - For each child x of z, add x to the root list of H. 2 - Remove z from the root list of H. 3 - If z = z.right, then H.min = NULL. 4 - Else, H.min = z.right and Consolidate(H). 5 - Decrease the total node count of H. 3: Return z.
Code for Extract Minimum Operation
Let’s write a simple code to extract minimum node from Fibonacci heap.
#include <stdio.h>#include <stdlib.h>structNode{int key, degree;structNode*parent,*child,*left,*right;int mark;};structFibonacciHeap{structNode* min;int nodeCount;};structNode*createNode(int key){structNode* newNode =(structNode*)malloc(sizeof(structNode));
newNode->key = key;
newNode->degree =0;
newNode->parent = newNode->child =NULL;
newNode->mark =0;
newNode->left = newNode->right = newNode;return newNode;}structFibonacciHeap*createHeap(){structFibonacciHeap* heap =(structFibonacciHeap*)malloc(sizeof(structFibonacciHeap));
heap->min =NULL;
heap->nodeCount =0;return heap;}voidinsert(structFibonacciHeap* H,int key){structNode* x =createNode(key);if(!H->min){
H->min = x;}else{
x->right = H->min->right;
x->left = H->min;
H->min->right->left = x;
H->min->right = x;if(x->key < H->min->key){
H->min = x;}}
H->nodeCount++;}voidprintHeap(structFibonacciHeap* H){if(!H->min){printf("Heap is empty!\n");return;}structNode* temp = H->min;printf("Fibonacci Heap Root List: ");do{printf("%d ", temp->key);
temp = temp->right;}while(temp != H->min);printf("\n");}structNode*extractMin(structFibonacciHeap* H){structNode* z = H->min;if(z){structNode* x = z->child;if(x){structNode* temp = x;do{structNode* next = temp->right;
temp->left->right = temp->right;
temp->right->left = temp->left;
temp->left = H->min;
temp->right = H->min->right;
H->min->right->left = temp;
H->min->right = temp;
temp->parent =NULL;
temp = next;}while(temp != x);}
z->left->right = z->right;
z->right->left = z->left;if(z == z->right){
H->min =NULL;}else{
H->min = z->right;// consolidate(H); }
H->nodeCount--;}return z;}intmain(){structFibonacciHeap* fibHeap =createHeap();insert(fibHeap,10);insert(fibHeap,3);insert(fibHeap,15);insert(fibHeap,7);printHeap(fibHeap);structNode* minNode =extractMin(fibHeap);if(minNode){printf("Minimum value extracted: %d\n", minNode->key);free(minNode);}else{printf("Heap is empty!\n");}printHeap(fibHeap);return0;}
Output
Following is the output of the above code:
Fibonacci Heap Root List: 3 7 10 15 Minimum value extracted: 3 Fibonacci Heap Root List: 7 10 15
Decrease Key Operation on Fibonacci Heap
Decreases the key of node x to the new key k. Its a bit complex operation as it involves rearranging the tree structure.
Algorithm for Decrease Key Operation
Let’s assume that we have a Fibonacci heap H, a node x and a new key k. The algorithm to decrease key of node x to k in Fibonacci heap H is as follows:
1: x.key = k 2: y = x.parent 3: If y != NULL and x.key < y.key 1 - Cut x from its parent y. 2 - Cascading-Cut(H, y). 4: If x.key < H.min.key, set H.min = x.
Code for Decrease Key Operation
Let’s write a simple code to decrease key of a node in Fibonacci heap.
#include <stdio.h>#include <stdlib.h>#include <limits.h>structNode{int key, degree;structNode*parent,*child,*left,*right;int mark;};structFibonacciHeap{structNode* min;int nodeCount;};structNode*createNode(int key){structNode* newNode =(structNode*)malloc(sizeof(structNode));
newNode->key = key;
newNode->degree =0;
newNode->parent = newNode->child =NULL;
newNode->mark =0;
newNode->left = newNode->right = newNode;return newNode;}structFibonacciHeap*createHeap(){structFibonacciHeap* heap =(structFibonacciHeap*)malloc(sizeof(structFibonacciHeap));
heap->min =NULL;
heap->nodeCount =0;return heap;}voidinsert(structFibonacciHeap* H,structNode* x){if(!H->min){
H->min = x;}else{
x->right = H->min->right;
x->left = H->min;
H->min->right->left = x;
H->min->right = x;if(x->key < H->min->key){
H->min = x;}}
H->nodeCount++;}voidcut(structFibonacciHeap* H,structNode* x,structNode* y){if(x->right == x){
y->child =NULL;}else{
x->right->left = x->left;
x->left->right = x->right;if(y->child == x){
y->child = x->right;}}
y->degree--;
x->left = H->min;
x->right = H->min->right;
H->min->right->left = x;
H->min->right = x;
x->parent =NULL;
x->mark =0;}voidcascadingCut(structFibonacciHeap* H,structNode* y){structNode* z = y->parent;if(z){if(!y->mark){
y->mark =1;}else{cut(H, y, z);cascadingCut(H, z);}}}voiddecreaseKey(structFibonacciHeap* H,structNode* x,int newKey){if(newKey > x->key){printf("New key is greater than current key!\n");return;}
x->key = newKey;structNode* y = x->parent;if(y && x->key < y->key){cut(H, x, y);cascadingCut(H, y);}if(x->key < H->min->key){
H->min = x;}}voidprintHeap(structFibonacciHeap* H){if(!H->min){printf("Heap is empty!\n");return;}structNode* temp = H->min;printf("Fibonacci Heap Root List: ");do{printf("%d ", temp->key);
temp = temp->right;}while(temp != H->min);printf("\n");}intmain(){structFibonacciHeap* fibHeap =createHeap();structNode* n1 =createNode(10);structNode* n2 =createNode(3);structNode* n3 =createNode(15);structNode* n4 =createNode(7);insert(fibHeap, n1);insert(fibHeap, n2);insert(fibHeap, n3);insert(fibHeap, n4);printHeap(fibHeap);decreaseKey(fibHeap, n3,1);printf("After Decrease-Key:\n");printHeap(fibHeap);return0;}
Output
Following is the output of the above code:
Fibonacci Heap Root List: 3 7 10 15 Fibonacci Heap Root List: 1 10 7 3
Delete Operation on Fibonacci Heap
Deletes the node x from the heap H. Its a bit complex operation as it involves rearranging the tree structure.
Algorithm for Delete Operation
Let’s assume that we have a Fibonacci heap H and a node x. The algorithm to delete node x from Fibonacci heap H is as follows:
1: Decrease key of node x to -. 2: Extract minimum node from heap H.
Code for Delete Operation
Let’s write a simple code to delete a node from Fibonacci heap.
#include <stdio.h>#include <stdlib.h>#include <limits.h>typedefstructNode{int key, degree;structNode*parent,*child,*left,*right;int mark;} Node;typedefstructFibonacciHeap{
Node *min;int nodeCount;} FibonacciHeap;
Node*createNode(int key){
Node* node =(Node*)malloc(sizeof(Node));
node->key = key;
node->degree =0;
node->parent = node->child =NULL;
node->mark =0;
node->left = node->right = node;return node;}
FibonacciHeap*createHeap(){
FibonacciHeap* heap =(FibonacciHeap*)malloc(sizeof(FibonacciHeap));
heap->min =NULL;
heap->nodeCount =0;return heap;}voidinsert(FibonacciHeap* heap, Node* x){if(heap->min ==NULL){
heap->min = x;}else{
x->right = heap->min->right;
x->left = heap->min;
heap->min->right->left = x;
heap->min->right = x;if(x->key < heap->min->key){
heap->min = x;}}
heap->nodeCount++;}voiddecreaseKey(FibonacciHeap* heap, Node* x,int newKey){if(newKey > x->key){printf("New key is greater than current key!\n");return;}
x->key = newKey;
Node* y = x->parent;if(y !=NULL&& x->key < y->key){// Implement cut and cascadingCut}if(x->key < heap->min->key){
heap->min = x;}}
Node*extractMin(FibonacciHeap* heap){
Node* z = heap->min;if(z !=NULL){if(z->child !=NULL){// Merge child nodes into root list}
z->left->right = z->right;
z->right->left = z->left;if(z == z->right){
heap->min =NULL;}else{
heap->min = z->right;// Implement consolidate function}
heap->nodeCount--;}return z;}voiddeleteNode(FibonacciHeap* heap, Node* x){decreaseKey(heap, x, INT_MIN);extractMin(heap);}voidprintHeap(FibonacciHeap* heap){if(heap->min ==NULL){printf("Heap is empty!\n");return;}
Node* temp = heap->min;printf("Fibonacci Heap Root List: ");do{printf("%d ", temp->key);
temp = temp->right;}while(temp != heap->min);printf("\n");}intmain(){
FibonacciHeap* fibHeap =createHeap();
Node* n1 =createNode(10);
Node* n2 =createNode(3);
Node* n3 =createNode(15);
Node* n4 =createNode(7);insert(fibHeap, n1);insert(fibHeap, n2);insert(fibHeap, n3);insert(fibHeap, n4);printHeap(fibHeap);deleteNode(fibHeap, n3);printf("After Delete:\n");printHeap(fibHeap);return0;}
Output
Following is the output of the above code:
Fibonacci Heap Root List: 3 7 10 15 Minimum value extracted: 3 Fibonacci Heap Root List: 7 10 15
Time Complexity of Fibonacci Heap
Let’s analyze the time complexity of various operations in Fibonacci heap:
- Insert Operation: O(1)
- Extract Minimum Operation: O(log n)
- Decrease Key Operation: O(1)
- Delete Operation: O(log n)
Thus, the Fibonacci heap is a data structure that supports insert, extract minimum, decrease key, and delete operations in amortized O(log n) time complexity.
Applications of Fibonacci Heap
Following are some of the applications of Fibonacci heap:
- It is used in Dijkstra’s algorithm to find the shortest path in a graph.
- Also, used in Prim’s algorithm to find the minimum spanning tree of a graph.
- Used in network routing algorithms.
Conclusion
In this tutorial, we learned about Fibonacci heap, its operations, and its time complexity. We also implemented the Fibonacci heap in C, C++, Java, and Python programming languages. We also discussed the applications of Fibonacci heap in various algorithms.








