Fib On 1
Fib On 1
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
delete-min union decrease-key delete is-empty Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key
O(|V|2)
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)
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
If theNode is not a root and new key < parent key, remove subtree rooted at theNode from its sibling list.
8
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
5 6
5 6
T 7
T 7
theNode
9 8
Decrease key by 2.
5 6
5 6
Note a node that moves to the root lists loose its mark (becomes unmarked).
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
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
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 */
10
10
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
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
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