## Fibonacci HeapIn this article, we will learn about the Fibonacci heap, its properties, advantages and, later on, its implementation: Before discussing the Fibonacci heap, let us first take a quick overview of ideas used in building a Fibonacci heap:
Heaps are the abstract data type which is used to show the relationship between parents and children: Heap is categorized into Min-Heap and Max-Heap: - A
**min-heap**is a tree in which, for all the nodes, the key value of the parent must be smaller than the key value of the children. - A
**max-heap**is a tree in which, for all the nodes, the key value of the parent must be greater than the key value of the children.
A doubly-linked list is the advanced version of the singly-linked list. Each node in a double-linked list contains two pointers left and right pointers. The left-pointer points to its left node, and the right-pointer points to the right node. The right pointer of the last node points to the first node, and the left pointer of the first node points to the last node. The figure shows how a circular doubly-linked list is observed: Let us now discuss the Fibonacci heap:
Fibonacci Heap - A Fibonacci heap is defined as the collection of rooted-tree in which all the trees must hold the property of Min-heap. That is, for all the nodes, the key value of the parent node should be greater than the key value of the parent node:
The above Fibonacci Heap consists of five rooted min-heap-ordered trees with 14 nodes. The min-heap-ordered tree means the tree which holds the property of a min-heap. The dashed line shows the root list. The minimum node in the Fibonacci heap is the node containing the key = 3 pointed by the pointer FH-min. Here, 18, 39, 26, and 35 are marked nodes, meaning they have lost one child. The potential of the Fibonacci series = No. of rooted tree + Twice the number of marked nodes = 5 + 2 * 4 = 13. Let us now observe the above example with the complete representation of the Fibonacci Heap: In the above figure, we can observe that each node contains four pointers, the parent points to the parent (Upward), the child points to the child (downward), and the left and right pointers for the siblings (sideways).
- It can have multiple trees of equal degrees, and each tree doesn't need to have 2^k nodes.
- All the trees in the Fibonacci Heap are rooted but not ordered.
- All the roots and siblings are stored in a separated circular-doubly-linked list.
- The degree of a node is the number of its children. Node X -> degree = Number of X's children.
- Each node has a mark-attribute in which it is marked TRUE or FALSE. The FALSE indicates the node has not any of its children. The TRUE represents that the node has lost one child. The newly created node is marked FALSE.
- The potential function of the Fibonacci heap is F(FH) = t[FH] + 2 * m[FH]
- The Fibonacci Heap (FH) has some important technicalities listed below:
- min[FH] - Pointer points to the minimum node in the Fibonacci Heap
- n[FH] - Determines the number of nodes
- t[FH] - Determines the number of rooted trees
- m[FH] - Determines the number of marked nodes
- F(FH) - Potential Function.
The time complexities of different operations performed on a Fibonacci heap are listed below in the table:
The number of rooted trees, t(FH) = zero, The minimum node, FH -> min = NIL, Number of nodes, FH -> n = 0, Number of marked nodes, FH -> m = 0, and The potential of the empty Fibonacci heap ?(FH) = 0. It is created by the MAKE-FIB-HEAP, which allocates and returns the Fibonacci heap object, FH in amortized cost O(1) equal to its actual cost:
The below pseudo code of INSERT-FIB-HEAP inserts a node x in the Fibonacci heap, FH. Assume that the node x has already been allocated and x -> key is already filled:
- Initializing the degree of the node x = 0
- Initializing the parent of the node x = NIL
- Initializing the child of the node x = NIL
- Marking FALSE in the mark attribute of the x
- If-condition checks whether the Fibonacci heap (FH) is empty or not
- If yes, the creates a new root list and adds x to it
- Making the newly added x the minimum node
- If the if-condition fails
- Inserts the x in the root list
- Checks whether the x -> key is smaller than the FH -> min
- If yes, then it makes the x the minimum node of the FH by updating the FH -> min = x
- In the last line, it increases the number of nodes of the FH by 1
The below pseudo-code simply concatenates the root lists of two Fibonacci heaps, FH1 and FH2 and destroys both of them during the process, determines the min node and returns the new Fibonacci heap, FH.
- Creates a new Root List FH
- Set the min -> FH = min -> FH1; the minimum node of the FH is now the minimum node of FH1.
- Joins (Concatenates) the root list of FH2 with the root list of FH
- Checks whether the min -> FH1 is null, min -> FH2 is null, and whether the min -> FH2 is smaller than the min -> FH1 or not
- If yes, it sets the min -> FH = min -> FH2
- Number of nodes in FH = sum of the number of nodes in FH1 and FH2
- Returning the FH
The extraction of minimum Node is one of the most complicated operations performed on the Fibonacci Heap. It is also where the delayed work of consolidating trees in the root list finally occurs. The below pseudo-code extracts the minimum node of the Fibonacci tree. For ease of use, the code assumes that when a node is removed from a linked list, pointers in the list are updated but not pointers in the extracted node. Additionally, it invokes the auxiliary procedure CONSOLIDATE, which we'll see in a moment.
- Saves the min pointer in Y
- Checks if the Fibonacci heap is empty or not
- If not, then it Runs a loop for all the children of Y
- Adds the children of Y in the root list
- Set the parent attributes = NIL for all the children
- Deletes the Y node from the root list
- If the right pointer of the Y is equal to the Y, it means that it was the only node in the Fibonacci heap.
- If Y was the only node setting, the FH -> min = NIL. Fibonacci heap is empty now.
- Else part run if there is another node in the root list of FH
- Sets the min -> FH = Y -> Right; now, min points to a different node present on the right of the node (
**Note: Root list is a circular doubly linked list**) - Calls the CONSOLIDATE (FH) function, which we have discussed below:
- Decreases the number of nodes of FH by 1
- Returning Y
1. 2. 3. - In the second iteration of the for-loop, it selects another node from the root list and stores it in X and its degree in d. The while condition becomes true if there is a node in the root list of the same degree as of 'X'. Suppose the condition of the while-loop is true. Now, it stores the node of the same degree as of 'X' in Y and checks which has the larger key. If Y has a key larger than the key of X., it swaps both of them and calls
The whole idea of the code is to have exactly one node of each degree in the root list and make other nodes a child of them without violating the condition of the Fibonacci heap. 4.
1. It removes the Y from the root list 2. Makes the Y nodes a child of X and increases the degree of X by 1 3. Marks FALSE in the Y -> mark.
Decreasing a key is performed on the Fibonacci heap to decrease the key of any node and can be performed in the O(1) amortized time. In the below pseudo code, we have assumed that removing a from the linked list does not change any of the structural attributes in the removed node. - Is the actual Fibonacci heap and
- represents the heap after decreasing the node storing key = 45 to 15.
1. 2. 3. 4. 5. 6. 7. 8.
In the above pseudo-code, it first finds the parent of Y and stores it in Z. Then, it checks whether the Y is in the root list or not. If not means Z is not equal to NIL, Again, it checks whether it is marked or not, If it is marked FALSE then it changes it to TRUE and the control returns to the FIB-DECREASE-KEY. If it is marked TRUE, then it removes Y from Z and places Y in the root list and repeats the same process and stops when one of the parent nodes is found unmarked.
The deleting a node from an n-node Fibonacci heap can be performed in O(D(n)) amortized cost time. In the below pseudo-code, we have assumed that as of now there is no key value of -infinity in the heap. The |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]