Fibonacci Heap Procedure Binary heap worstcase Binomial heap
Fibonacci Heap
Procedure Binary heap (worst-case) Binomial heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP (1) INSERT (lg n) O(lg n) (1) MINIMUM (1) O(lg n) (1) EXTRACT-MIN (lg n) O(lg n) UNION (n) O(lg n) (1) DECREASE-KEY (lg n) (1) DELETE (lg n) O(lg n) p 2.
H. min (a) 23 7 3 18 52 39 17 38 30 24 26 46 35 41 H. min (b) 23 7 3 18 39 52 17 38 41 30 24 26 46 35 p 3.
H. min (a) 23 7 3 18 52 39 17 38 30 24 26 46 35 41 Insert key 21 H. min (b) 23 7 21 18 39 3 52 17 38 41 30 24 26 46 35 p 4.
n Fibonacci Heaps: A collection of heap-ordered trees: rooted but unordered Each node x: x. p points to its parent x. child points to any one of its children of x are linked together in a circular doubly linked list x. Left, x. right: points to its left and right siblings. x. degree: number of children in the child list of x x. mark: indicate whether node x has lost a child since the last time x was mode the child of another node H. min: points to the root of the tree containing a minimum key H. n: number of nodes in H p 5.
n Potential function: (H) = t(H) + 2 m(H) Fibonacci heap # of marked nodes in H # of trees in the rooted list of H D(n): upper bound on the max degree of any node in an n-node Fibonacci heap p 6.
n Unordered binomial tree: U 0: a single node Uk: consists of 2 unordered binomial trees Uk-1 for which the root of one is made into any child of the root of the other 若只用 Make-Heap, Insert, Minimum, Extract-Min & Union. Fibonacci heap 只含 unordered binomial trees. Make-Heap, Make-Fib-Heap(H): Allocate and return the Fibonacci heap object H with H. n=0 and H. min=nil t(H)=0 , m(H)=0 so (H)=0 The amortized cost of Make-Fib-Heap is equal to its O(1) actual cost. p 7.
Fib-Heap-Insert(H, x) 1. x. degree = 0 2. x. p = NIL 3. x. child = NIL 4. x. mark = FALSE 5. if H. min == NIL 6. create a root list for H containing just x 7. H. min = x 8. else insert x into H’s root list 9. if x. key < H. min. key 10. H. min = x 11. H. n = H. n +1 Actual cost: O(1); Amortized cost: O(1) + 1 p 8.
n n 1. 2. 3. 4. 5. 6. 7. Finding the minimum node: min[H] O(1) Amortized cost O(1) is not changed Uniting 2 Fibonacci heaps: Fib-Heap-Union(H 1, H 2) H = Make-Fib-Heap( ) H. min = H 1. min concatenate the root list of H 2 with the root list of H if (H 1. min == NIL) or (H 2. min NIL and H 2. min. key<H 1. min. key) H. min = H 2. min H. n = H 1. n + H 2. n return H p 9.
n (H) - ( (H 1)+ (H 2)) = (t(H)+2 m(H)) – ((t(H 1)+2 m(H 1)) + (t(H 2)+2 m(H 2))) =0 t(H) = t(H 1) + t(H 2) , m(H) = m(H 1) + m(H 2) Thus the amortized cost of Fib-Heap-Union is therefore O(1) actual cost p 10.
Extracting the minimum node: Fib-Heap-Extract-Min(H) 1. z = H. min 2. if z NIL 3. for each child x of z 4. do { add x to the root list of H 5. x. p = NIL } 6. remove z from the root list of H 7. if z == z. right 8. H. min = NIL 9. else H. min = z. right 10. Consolidate(H) 11. H. n = H. n – 1 12. return z n p 11.
Fib-Heap-Link(H, y, x) {1. remove y from the root list of H; 2. make y a child of x; x. degree =x. degree+1; 3. y. mark = FALSE; } Consolidate(H) 1. let A[0. . D(H. n)] be a new array 2. for i = 0 to D(n[H]) do A[i]=NIL 3. for each node w in the root list of H 4. do { x = w ; d = x. degree; 5. while A[d] NIL 6. do { y = A[d] 7. if x. key > y. key exchange x y 8. Fib-Heap-Link(H, y, x) 9. A[d] = NIL ; d = d+1; } 10. A[d] = x; } 11. H. min = NIL 12. for i = 0 to D(H. n) do 13. if A[i] NIL 14. if H. min==NIL 15. create a root list for H containing just A[i]; H. min =A[i]; 16. else insert A[i] into H’s root list 17. if A[i]. key < H. min. key H. min = A[i] p 12.
H. min (a) 23 21 7 18 3 52 39 17 24 26 30 38 46 35 41 H. min (b) 23 7 21 18 39 52 38 17 41 30 24 26 35 46 p 13.
0 1 2 3 4 A (c) 23 7 21 18 52 39 38 w, x 17 41 30 24 26 46 35 0 1 2 3 4 A (d) 23 7 21 18 39 52 38 17 41 30 w, x 24 26 35 46 p 14.
0 1 2 3 4 A (e) 23 w, x 7 21 18 52 39 38 17 41 30 24 26 46 35 0 1 2 3 4 A (f) 7 x 23 21 18 52 38 17 41 30 24 w 39 26 35 46 p 15.
0 1 2 3 4 A 7 (g) x 21 18 17 w 23 38 52 39 41 30 24 26 46 35 0 1 2 3 4 A x (h) 7 24 17 21 23 w 18 39 52 38 41 26 46 30 35 p 16.
0 1 2 3 4 A 7 (i) 24 17 21 23 w, x 18 52 39 38 41 26 46 30 0 35 A 7 (j) 1 2 3 4 24 17 21 23 w, x 18 39 52 38 41 26 46 30 35 p 17.
0 1 2 3 4 A w, x 18 7 (k) 24 17 23 26 46 30 21 38 39 41 52 35 0 1 2 3 4 A 7 (l) 24 17 26 46 30 18 23 21 38 39 w, x 41 52 35 p 18.
H. min 7 (m) 24 17 26 46 30 18 23 21 38 39 41 52 35 p 19.
n Decreasing a key and deleting a node: do not preserve the property that all trees in the Fibonacci heap are unordered binomial trees. Fib-Heap-Decrease-key(H, x, k) 1. if k>x. key 2. error “new key is greater than current key” 3. x. key = k 4. y x. p 5. if y NIL and x. key< y. key 6. { CUT(H, x, y) 7. CASCADING-CUT(H, y) } 8. if x. key< H. min. key 9. H. min = x p 20.
CUT(H, x, y) 1. remove x from the child list of y, decrease y. degree 2. add x to the root list of H 3. x. p = NIL Fib-Heap-Delete(H, x) 4. x. mark = FALSE { Fib-Heap-Decrease-key(H, x, - ) Fib-Heap-Extract-Min(H) CASCADING-CUT(H, y) } 1. z y. p 2. if z NIL 3. if y. mark == FALSE 4. y. mark= TRUE 5. else CUT(H, y, z) 6. CASCADING-CUT(H, z) p 21.
H. min 7 (a) 18 24 17 23 21 26 46 30 38 39 41 52 35 H. min (b) 7 15 24 17 26 35 30 38 18 23 21 39 41 52 p 22.
H. min (c) 15 7 5 18 24 17 26 23 21 30 38 39 41 52 H. min (d) 15 5 26 7 24 17 30 18 23 21 38 39 41 52 p 23.
H. min (e) 15 5 26 7 24 17 30 18 23 21 38 39 41 52 p 24.
n Analysis of Decrease-key: Actual cost : O(c) suppose CASCADING-CUT is called c times n Each recursive call of CASCADING-CUT except for the last one, cuts a marked node and clears the mark bit. n After Decrease-key, there at most t(H)+c trees, and at most m(H)-c+2 marked nodes. Last call of CASCADING-CUT may have marked a node Thus; the potential change is : [t(H)+c+2(m(H)-c+2)] - [t(H)+2 m(H)] = 4 -c Amortized cost: O(c)+4 -c = O(1) By scaling up the units of potential to dominate the constant hidden in O(c) p 25.
n Analysis of Fib-Heap-Extract-Min: H : n-node Fib-Heap Actual cost : O(D(n)) : for-loop in Fib-Heap-Extract-Min D(n)+t(H)-1 : size of the root list Total actual cost: O(D(n))+t(H) Potential before extracting : t(H)+2 m(H) Potential after extracting : D(n)+1+2 m(H) At most D(n)+1 nodes remain on the list and no nodes become marked Thus the amortized cost is at most: O(D(n))+t(H)+[(D(n)+1+2 m(H)) – (t(H)+2 m(H))] = O(D(n)+t(H)-t(H)) = O(D(n)) p 26.
n Bounding the maximum degree: Goal : D(n) log n , = Let size(x) be the number of nodes, including x itself, in the subtree rooted at x p 27.
x y 1 Lemma 1 x : any node in a Fibonacci heap and x. degree=k y 1, …, yk : children of x in the order in which they were linked to x. (from the earliest to the latest) yk Then, y 1. degree 0 and yi. degree i-2 for i=2, 3, …, k Pf: Clearly, y 1. degree 0 For i 2, note that when yi was linked to x, all of y 1, …, yi-1 were children of x, so we MUST have had x. degree i-1. Node yi is linked to x only if x. degree = yi. degree, thus yi. degree i-1 at that time. Since then, node yi has lost at most ONE child, since it would have been cut from x if it had lost two children. We conclude that yi. degree i-2 n y 2 p 28.
Fibonacci number: n Lemma 2: For all integer k 0, pf: By induction on k k=0, F 2=F 1+F 0=1 = 1+F 0 Suppose p 29.
Lemma 3: x: any node in a Fibonacci heap, and let k=x. degree Then, size(x) Fk+2 k pf: Sk : denote the min possible value of size(z) over all nodes z such that z. degree=k. Trivially, S 0=1, S 1=2, and S 2=3 Sk size(x) , size(y 1) 1 size(x) Sk-2 2+ i=2, …, k Si-2 By induction on k that Sk Fk+2 Clearly for k=0 and 1 Assume that k 2 and that Si Fi+2 for i=0, …, k-1 We have Sk 2+ i=2, …, k Si-2 2+ i=2, …, k Fi = 1+ i=0, …, k Fi = Fk+2 Thus, size(x) Sk Fk+2 k n x y 1 yk y 2 S 0 p 30.
n Corollary 4: The max degree D(n) of any node in an n-node Fibonacci heap is O(lg n) pf: x: any node in an n-node Fibonacci heap k=degree[x] n size(x) k log n k Thus the max degree D(n) of any node is O(lg n) p 31.
- Slides: 31