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

DAA Unit-2 notes

Aktu 3rd sem

Uploaded by

Amit Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DAA Unit-2 notes

Aktu 3rd sem

Uploaded by

Amit Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

The given figure is an example of the Fibonacci tree:

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).
Operations on a Fibonacci Heap:
1. 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.
2. 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.

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

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.

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.
4. Here, 23 and 7 have the same degrees, so unite them.

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

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


7. Map the next nodes.

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

9. Similarly, unite 21 and 18.


10. Map the remaining root.

4. Decreasing a Key and Deleting a Node


Decrease Key and Delete Node Operations on a Fibonacci Heap

A fibonacci heap is a tree based data structure which consists of a collection of trees
with min heap or max heap property. Its operations are more efficient in terms of time
complexity than those of its similar data structures like binomial heap and binary heap.

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

Example: Decreasing 46 to 15.

1. Decrease the value 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.

Example: Decreasing 35 to 5

1. Decrease the value 35 to 5.

2. Cut part: Since 26 ≠ nill and 5<its parent, cut it and add it to the root list.
3. 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.

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

5. Since 5 < 7, mark 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