0% found this document useful (0 votes)
53 views5 pages

Fib On 1

Fibonacci heaps are a type of priority queue that provide faster decrease key and delete minimum operations than other heap data structures like binary heaps. They consist of a collection of trees arranged in heap order and connected by root and sibling lists. The key operations are: 1. Insert adds a new node to the root list in O(1) time. 2. Decrease key cuts nodes from trees and performs cascading cuts in O(1) amortized time by reducing the number of marked nodes. 3. Extract minimum removes the minimum node by adding its children to the root list and consolidating trees in O(log n) amortized time. 4.

Uploaded by

shanthisec
Copyright
© Attribution Non-Commercial (BY-NC)
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)
53 views5 pages

Fib On 1

Fibonacci heaps are a type of priority queue that provide faster decrease key and delete minimum operations than other heap data structures like binary heaps. They consist of a collection of trees arranged in heap order and connected by root and sibling lists. The key operations are: 1. Insert adds a new node to the root list in O(1) time. 2. Decrease key cuts nodes from trees and performs cascading cuts in O(1) amortized time by reducing the number of marked nodes. 3. Extract minimum removes the minimum node by adding its children to the root list and consolidating trees in O(log n) amortized time. 4.

Uploaded by

shanthisec
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 5

Fibonacci Heap

Operation make-heap insert find-min

Priority Queues
Heaps Linked List 1 1 N N 1 1 N 1 Binary 1 log N 1 log N N log N log N 1 Binomial 1 log N log N log N log N log N log N 1 Fibonacci * 1 1 1 log N 1 1 log N 1 Relaxed 1 1 1 log N 1 1 log N 1

Thanks to Sartaj Sahni for the original version of the slides

delete-min union decrease-key delete is-empty Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key

O(|V|2)

O(|E| log |V|)

O(|E| + |V| log |V|)

Fibonacci heaps
Similar to binomial heaps, consists of a collection of trees, each arranged in a heap-order (each node is smaller than each of its children) Unlike binomial heaps, can have many trees of the same cardinality, and a tree does not have to have exactly 2i nodes. Main idea laziness is welcomed. Try to postpone doing the hard work, until no other solution works.

General Structure
Very similar to Bionomail heaps Main structure: A collection of trees, each in a heap-order. All root are stored in a doubly connected list, called the roots-list. Every node points to one of its childrens. All the children are stored in a doubly connected list, called the sibling-list. A pointer min(H) always points to the min element.
2 4 6 8 9 7 5 6 5 6 9 9 4 roots list 7 8 1

sibling list
3

Node Structure
Very similar to Bionomail heaps Each node v stores its
degree, a points to its parent, a points to a child, data, Pointers to left and right sibling used for circular doubly linked list of siblings, called the sibling list. More in next slide

Node Structure
Each node v stores its also stores a flag ChildCut a flag (not existing in binomial heaps) True if v has lost a child since v became a child of its current parent. We say that v is marked. Set to false by remove min, which is the only operation that makes one node a child of another. Undefined for a root node (not used)

Fibonacci Heap Representation


2 4 roots list 9 7 1

Potential Function

sibling list

6 5 5

Some nodes would be marked (to be explained later) We use the potential functions for the heap H (H) = t(H) + 2 m(H) Where t(H) is the number of trees in H And m(H) is the number of marked nodes in H.

8 9

Insert(x)
Create a new tree consisting of a single node v whose key is x, Add v to the roots list. Actual time wi needed for the operation is 1 Number of trees increased by 1. Changes in potential function (H)-(H)= (H) = t(H) + 2 m(H)- t(H) - 2 m(H)=1 So the amortized work ai = wi+ (H) = 2

DecreaseKey(theNode, theAmount)
1 6 5

theNode Decreased by 4
2 4 7 3 9

10

Example without ChildCut

If theNode is not a root and new key < parent key, remove subtree rooted at theNode from its sibling list.
8

Insert theNode into roots-list. Perform cascading_cut from parent(theNode )


(described later)

DecreaseKey(theNode, theAmount)
0 1 6 5 10 5 2 7 3 9

Cascading Cut
When theNode is cut out of its sibling list in a decrease key operation, follow path from parent of theNode upward toward the root. Encountered nodes (other than root) with ChildCut = true are cut from their sibling lists and inserted into roots-list. Stop at first node with ChildCut = false. For this node, set ChildCut = true. (since it just lost exactly one child) In other words, if a node lost two children since it became a child, it must move itself from the the parent to the roots-list.

9 4 5

Cascading Cut Example


1 3 2 F T 9 1 3 7 8 6 7 8 6

Cascading Cut Example


6 2 F T 9 9 8

5 6

5 6

T 7

T 7

theNode
9 8

Decrease key by 2.

Cascading Cut Example


1 3 2 F T 9 6 9 8 7 1 3 7 8 6 7 8

Cascading Cut Example


6 2 F 9 9 8 7 6 4

5 6

5 6

Note a node that moves to the root lists loose its mark (becomes unmarked).

Cascading Cut Example


1 3 2 T 9 6 9 8 7 6 4 7 8

Amortized time
Note that the number of marked nodes decreases by k or k+1, and the number of trees increased by k+1. Let H to be H denote the heap before and after the Decrease_min operation, then t(H) = t(H)+k+1 and m(H)=m(H)-k

5 6

The change in the potential function is (denoting H to be H after the Decrease_min ) is

Actual time complexity of the cascading_cut of a path of length k is (k+1) ( can be (h) in the worst case, where h is the height ) Assume we specify the time of an elementary operations, so that this time wi is k+1. Note that the number of trees increases by k+1, and the number of marked nodes decreases by either k or k+1

(H) - (H) = ( t(H) + 2m(H) ( t(H) + 2 m(H) ) = -k+2


And ai =wi+ (H) - (H) = k+(-k+2)=2

Deletion of a node v
Perform DecreaseKey(v) to value - Perform ExtractMin(H) seen next.

Extract_min
Remember - there is a pointer (min[H] ) pointing to the min. Set theNode= min[H] Moved all children of theNode to the the roots-list. This is done by merging their sibling list with the root list change their parent pointer to NULL If any of them was marked, set it to be unmarked. Remove theNode from its sibling list. Free theNode. Perform Consolidate( H ) - merging trees. /* This is a good time to reduce the number of trees */

Extract_min example (1.1)


1 6 5 4 7 3 9 4

Extract_min example (2)


2 4 7 3 6 5 9

10

10

Next comes the consolidation

Time analysis for this part of the Extract_min operation


Let deg(v) is the number of children of v. Lemma: (CLRS 20.3): The number of nodes in a tree the tree rooted at v is deg(v) 1.5 deg(v) Conclusion 1: deg(v) =O(log n), for every node v. The actual time wi needed for disconnecting v from its children O(log n)=deg(v) and adding them to the roots list The amortized time for this operation is at most t(H)-t(H) + 2 (m(H)-m(H)) = O(d(v)) = O(log n) (the number of has increased by deg(v) 1, the number of marked nodes had decreased by deg(v) +1) So the amortized time ai for this part is O(log n)

Union of two trees.


(Need for Consolidation)
(Similar (but not identical) operation was seen in the binomial heaps) Degree of a tree is defined as the degree of the root of the tree. Given two trees with the same degree of their roots, connect the root of one as a child of the other root. There is always a way to do so while maintaining the heap order:
0 4 10 10 5 8 9 19 6

The root with larger key becomes the child of the smaller root
Point of potential confusion: For Binomial heaps, trees have the same size iff they have the same degree. Not true here

Extract_min cont: Consolidation.


Each extract_min is followed by the consolidation operation: This operation repeatedly joins trees with same degree, using the treeunion operation: Repeatedly pick two trees with the same degree, and merge them: but trees are not sorted by degree, (as oppose to Binomial heaps) and there are many of them how can this be done efficiently ???? (on board) Finish when no two trees with the same degree exist. Recover the new minimum while doing so. Actual Time wi proportional to the number of trees (since every operation reduces the number of tress by one, and takes a constant time) .

Time analysis for consolidation


The consolidation takes actual time t(H) time. In H, (after the consolidation) there is at most one tree for each possible degree of its root. Followed from conclusion 1, t(H) =O(log n). The number of marked nodes is not changed. (H) - (H) = ( t(H) + 2m(H) ( t(H) + 2 m(H) ) = t(H)-t(H) = O(log n)- t(H) The amortized work is therefore t(H)+ ( O(log n) -t(H) ) = O(log n)

Time analysis for Delete


Deletion consists of
first DecreaseKey (amortized time O(1) ) and then ExtractMin (amortized time O(log n) ) Total amortized work: O(log n)

Toward proving lemma CLRS 20.3


Lemma: 20.3: The number of nodes in a tree rooted at v is

deg(v) 1.5 deg(v)

Let F0 = 0 , F1=1 and Fk+2=Fk+1+Fk Lemma 20.2: Proof by induction.


Fk + 2 = 1 + Fi
i =0 k

Lemma 20.1: Let x be a root, and let y1, ,yk denote its children, in the order they joined x. Then deg[yi ] i-2. (i=2,3k). Proof: When yi joined x, its degree was exactly i-1 Since yi jointed, its degree might have decrease by 1.

Fibonacci numbers
Let F0 = 0 , F1=1 and Fk+2=Fk+1+Fk Then Fk
k-2

Proving lemma CLRS 20.3


Let sk denote the minimum number of nodes at a tree of degree k. Lemma (20.3) : sk

Fk k-2

1.5 k-2

Here = (1 +

Proof: Let y1, ,yk denote its children of a node x, in the order they joined x. Then (assuming by induction the claim holds for all i<k)

5) / 2

sk 2 + si 2 2 + Fi 2 = 1 + Fi 2 = Fk + 2
i =2 i =2 i =0

Easily check by induction

Showed all we wanted to Fibonacci heaps

You might also like