fibonacci heap

Click here to load reader

Post on 22-Feb-2016




0 download

Embed Size (px)


Fibonacci Heap. 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. H. min. 23. 7. 3. 17. 24. ( a). 26. 30. 52. 18. 38. 46. 21. ( b). 23. 7. 3. 17. 24. 35. 39. - PowerPoint PPT Presentation


Fibonacci Heap

Fibonacci Heap p#.ProcedureBinary heap(worst-case) Binomial heap (worst-case)Fibonacci heap (amortized)MAKE-HEAP (1) (1) (1)INSERT (lg n)O(lg n) (1)MINIMUM (1)O(lg n) (1)EXTRACT-MIN (lg n) (lg n)O(lg n)UNION (n)O(lg n) (1)DECREASE-KEY (lg n) (lg n) (1)DELETE (lg n) (lg n)O(lg n)p#.(a)23741392430173818522634635H.min(b)23741392430173818522634635H.minp#.(a)23741392430173818522634635H.minH.min(b)2374139243017381852263463521Insert key 21 p#.Fibonacci Heaps:A collection of heap-ordered trees.trees: rooted but unorderedEach node x: x.p points to its parent x.child points to any one of its childrenchildren of x are linked together in a circular doubly linked listx.Left, x.right: points to its left and right number of children in the child list of xx.mark: indicate whether node x has lost a child since the last time x was mode the child of another nodeH.min: points to the root of the tree containing a minimum keyH.n: number of nodes in Hp#.Potential function:D(n): upper bound on the max degree of any node in an n-node Fibonacci heapFibonacci heap# of trees in the rooted list of H# of marked nodes in H (H) = t(H) + 2m(H)p#.Unordered binomial tree:U0: a single nodeUk: 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#.Fib-Heap-Insert(H, x) = 0x.p = NILx.child = NILx.mark = FALSEif H.min == NIL create a root list for H containing just x H.min = xelse insert x into Hs root list if x.key < H.min.key H.min = xH.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1p#.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.minconcatenate the root list of H2 with the root list of Hif (H1.min == NIL) or (H2.min NIL and H2.min.key y.key exchange xyFib-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 Hs root list if A[i].key < H.min.key H.min = A[i] p#.(a)2374139243017381852263463521(b)237242646353918413830172152H.minH.minp#.(c)2372426463539184138301721520 1 2 3 4Aw,x0 1 2 3 4A(d)237242646353918413830172152w,xp#.0 1 2 3 4A(e)237242646353918413830172152w,x(f)2370 1 2 3 4A242646353918413830172152xwp#.0 1 2 3 4A(g)237242646353918413830172152xw35(h)0 1 2 3 4A2373918413830172426462152xwp#.0 1 2 3 435(i)A2373918413830172426462152w, x0 1 2 3 435(j)A2373918413830172426462152w, xp#.0 1 2 3 435(k)A2373918413830172426462152w, x0 1 2 3 435(l)A2373918413830172426462152w, xp#.35(m)2373918413830172426462152H.minp#.Decreasing a key and deleting a node:do not preserve the property that all trees in theFibonacci heap are unordered binomial trees.

Fib-Heap-Decrease-key(H, x, k)if k>x.key error new key is greater than current keyx.key = ky x.pif yNIL and x.key< y.key { CUT(H, x, y) CASCADING-CUT(H, y) }if x.key< H.min.key H.min = xp#.CUT(H, x, y)1. remove x from the child list of y, decrease y.degree2. add x to the root list of H3. x.p = NIL4. x.mark = FALSE

CASCADING-CUT(H, y)z y.pif zNIL 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) }p#.35(a)2373918413830172426462152H.min(b)153526237413830172439182152H.minp#.(c)1526237413830172439182152H.min5(d)15413839182152237301724H.min526p#.(e)154138391821522373017526H.min24p#.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 nodeThus; the potential change is :[t(H)+c+2(m(H)-c+2)] - [t(H)+2m(H)]= 4-cAmortized cost: O(c)+4-c = O(1)By scaling up the units of potential to dominate the constant hidden in O(c)p#.Analysis of Fib-Heap-Extract-Min:H : n-node Fib-HeapActual cost :O(D(n)) : for-loop in Fib-Heap-Extract-MinD(n)+t(H)-1 : size of the root listTotal actual cost:O(D(n))+t(H)Potential before extracting : t(H)+2m(H)Potential after extracting : D(n)+1+2m(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+2m(H)) (t(H)+2m(H))]= O(D(n)+t(H)-t(H))= O(D(n))p#.Bounding the maximum degree:

Goal : D(n) logn , =

Let size(x) be the number of nodes, including x itself, in the subtree rooted at x

p#.Lemma 1x : any node in a Fibonacci heap and, , yk : children of x in the order in which they were linked to x. (from the earliest to the latest)Then, 0 and i-2 for i=2,3,,kPf:Clearly, 0For i 2, note that when yi was linked to x, all of y1, , yi-1were children of x, so we MUST have had i-1.Node yi is linked to x only if =,thus 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 i-2xy1y2ykp#.Lemma 2:For all integer k0, pf:By induction on kk=0, F2=F1+F0=1 = 1+F0SupposeFibonacci number:

p#.Lemma 3:x: any node in a Fibonacci heap, and let k=x.degreeThen, size(x) Fk+2 kpf:Sk : denote the min possible value of size(z) over all nodes z such that, S0=1, S1=2, and S2=3Sk size(x) , size(y1) 1size(x) Sk 2+i=2,,k Si-2By induction on k that SkFk+2 Clearly for k=0 and 1Assume that k2 and that SiFi+2 for i=0,,k-1We have Sk 2+i=2,,k Si-2 2+i=2,,k Fi = 1+ i=0,,k Fi = Fk+2Thus, size(x) Sk Fk+2 kxy1y2yk S0 Sk-2p#.Corollary 4:The max degree D(n) of any node in an n-nodeFibonacci heap is O(lg n)pf:x: any node in an n-node Fibonacci heapk=degree[x]n size(x) klogn kThus the max degree D(n) of any node is O(lg n)p#.

View more