Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fibonacci Heap.

Similar presentations


Presentation on theme: "Fibonacci Heap."— Presentation transcript:

1 Fibonacci Heap

2 Binary heap (worst-case) Binomial heap (worst-case)
Procedure Binary heap (worst-case) Binomial heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n) O(lg n) MINIMUM EXTRACT-MIN UNION  (n) DECREASE-KEY DELETE

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

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

5 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

6 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

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

8 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.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1

9 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

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

11 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

12 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]

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

14 (c) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 A w,x (d)

15 A (e) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 w,x (f) x w

16 A (g) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 x w (h)

17 35 (i) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x (j)

18 35 (k) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x 35 (l) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x

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

20 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

21 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) }

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

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

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

25 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)

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

27 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

28 x : any node in a Fibonacci heap and x.degree=k
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 x y1 y2 yk

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

30 x: any node in a Fibonacci heap, and let k=x.degree
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 x y1 y2 yk  S0  Sk-2

31 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)


Download ppt "Fibonacci Heap."

Similar presentations


Ads by Google