Fibonacci Heap

# Fibonacci Heap

## Fibonacci Heap

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Fibonacci Heap

2. H.min H.min (b) (a) 23 23 7 7 3 3 17 17 24 24 26 26 46 30 30 52 52 18 18 38 38 46 35 35 39 39 41 41

3. H.min 23 7 3 17 24 (a) 26 30 52 18 38 46 21 (b) 23 7 3 17 24 35 39 41 26 30 52 18 38 46 35 39 41 Insert key 21 H.min

4. Fibonacci Heaps: A collection of heap-ordered trees. trees: rooted but unordered Each node x: x.p points to its parent x.child points to any one of its children 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

5. Potential function: # of marked nodes in H  (H) = t(H) + 2m(H) # of trees in the rooted list of H Fibonacci heap D(n): upper bound on the max degree of any node in an n-node Fibonacci heap

6. Unordered binomial tree: U0: 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.

7. Fib-Heap-Insert(H, x) • x.degree = 0 • x.p = NIL • x.child= NIL • x.mark= FALSE • if H.min == NIL • create a root list for H containing just x • H.min = x • else insert x into H’s root list • if x.key < H.min.key • H.min = x • H.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1

8. Finding the minimum node: min[H]  O(1) Amortized cost O(1)  is not changed • Uniting 2 Fibonacci heaps: Fib-Heap-Union(H1, H2) • H =Make-Fib-Heap( ) • H.min = H1.min • concatenate the root list of H2 with the root list of H • if (H1.min == NIL) or (H2.min  NIL and H2.min.key<H1.min.key) • H.min = H2.min • H.n = H1.n + H2.n • return H

9. t(H) = t(H1) + t(H2) , m(H) = m(H1) + m(H2) • (H) - ((H1)+(H2)) = (t(H)+2m(H)) – ((t(H1)+2m(H1)) + (t(H2)+2m(H2))) = 0 Thus the amortized cost of Fib-Heap-Union is therefore O(1) actual cost

10. Extracting the minimum node: Fib-Heap-Extract-Min(H) • z = H.min • if z  NIL • for each child x of z • do { add x to the root list of H • x.p = NIL } • remove z from the root list of H • if z == z.right • H. min = NIL • else H.min = z.right • Consolidate(H) • H.n = H.n – 1 • return z

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) • let A[0..D(H.n)] be a new array • for i = 0 to D(n[H]) do A[i]=NIL • for each node w in the root list of H • do { x = w ; d = x.degree; • while A[d]  NIL • do { y = A[d] • if x.key > y.key exchange xy • Fib-Heap-Link(H, y, x) • A[d] = NIL ; d = d+1; } • A[d] = x; } • H.min = NIL • for i = 0 to D(H.n) do • if A[i]  NIL • if H.min==NIL • create a root list for H containing just A[i]; H.min =A[i]; • else insert A[i] into H’s root list • if A[i].key < H.min.key H.min = A[i]

12. 21 (a) 23 7 3 17 24 21 (b) 23 7 17 24 52 26 30 52 18 38 46 26 30 18 38 46 35 39 41 35 39 41 H.min H.min

13. w,x 0 1 2 3 4 0 1 2 3 4 A A w,x 21 21 (d) (c) 23 23 7 7 17 17 24 24 52 52 26 26 30 30 18 18 38 38 46 46 35 35 39 39 41 41

14. w,x 0 1 2 3 4 0 1 2 3 4 A A 21 (e) 23 7 17 17 24 52 x 7 (f) 24 21 52 26 30 30 18 18 38 38 46 w 23 26 46 35 39 39 41 41 35

15. x 7 (g) 24 21 52 w 23 26 46 0 1 2 3 4 A 35 0 1 2 3 4 A 17 x 7 21 52 (h) 30 18 18 38 38 w 23 24 17 39 39 41 41 26 30 46 35

16. 0 1 2 3 4 A w, x 7 21 52 (i) 23 24 17 26 30 46 0 1 2 3 4 35 A w, x 7 21 52 (j) 23 24 18 18 38 38 17 26 30 46 39 39 41 41 35

17. 0 1 2 3 4 A w, x 7 18 (k) 23 24 21 39 17 26 30 46 52 35 0 1 2 3 4 A w, x 7 18 (l) 38 38 23 24 21 39 17 26 30 46 41 41 52 35

18. H.min 7 18 (m) 23 24 21 39 17 26 30 46 52 35 38 41

19. 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) • if k>x.key • error “new key is greater than current key” • x.key = k • y x.p • if yNIL and x.key< y.key • { CUT(H, x, y) • CASCADING-CUT(H, y) } • if x.key< H.min.key • H.min = x

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 4. x.mark = FALSE CASCADING-CUT(H, y) • zy.p • if zNIL • if y.mark == FALSE • y.mark= TRUE • else CUT(H, y, z) • CASCADING-CUT(H, z) Fib-Heap-Delete(H, x) { Fib-Heap-Decrease-key(H, x, -) Fib-Heap-Extract-Min(H) }

21. H.min 7 18 (a) 23 24 21 39 17 26 30 46 52 35 H.min 15 7 18 (b) 23 24 21 39 38 38 17 26 30 52 41 41 35

22. H.min 15 5 7 (c) 23 24 17 30 26 H.min 15 5 7 18 18 26 (d) 23 24 21 21 39 39 38 38 17 30 52 52 41 41

23. H.min 15 5 7 24 26 (e) 23 17 30 18 21 39 38 52 41

24. Analysis of Decrease-key: Actual cost : O(c) suppose CASCADING-CUT is called c times • Each recursive call of CASCADING-CUT except for the last one, cuts a marked node and clears the mark bit. • After Decrease-key, there are 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)+2m(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)

25. At most D(n)+1 nodes remain on the list and no nodes become marked • 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)+2m(H) Potential after extracting :  D(n)+1+2m(H) Thus the amortized cost is at most: O(D(n))+t(H)+[(D(n)+1+2m(H)) – (t(H)+2m(H))] = O(D(n)+t(H)-t(H)) = O(D(n))

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

27. x yk y1 y2 • Lemma 1 x : any node in a Fibonacci heap and x.degree=k y1, …, yk : children of x in the order in which they were linked to x. (from the earliest to the latest) Then, y1.degree  0 and yi.degree  i-2 for i=2,3,…,k Pf: Clearly, y1.degree  0 For i  2, note that when yi was linked to x, all of y1, …, 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

28. Fibonacci number: • Lemma 2: For all integer k0, pf: By induction on k k=0, F2=F1+F0=1 = 1+F0 Suppose

29. x yk y1 y2  Sk-2  S0 • 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, S0=1, S1=2, and S2=3 Sk size(x) , size(y1)  1 size(x)  Sk  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

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