260 likes | 510 Views
Fibonacci Heap. Fibonacci Heaps: A collection of heap-ordered trees. trees : rooted but unordered Each node x : P[x] points to its parent child[x] points to any one of its children,children of x are linked together in a circular doubly linked list
E N D
Fibonacci Heaps: A collection of heap-ordered trees. trees: rooted but unordered Each node x: P[x] points to its parent child[x] points to any one of its children,children of x are linked together in a circular doubly linked list degree[x]: number of children in the child list of x mark[x]: indicate whether node x has lost a child since the last time x was mode the child of another node min[H]: points to the root of the tree containing a minimum key n[H]: number of nodes in H
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
min[ H ] min[ H ] (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
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.
Fib-Heap-Insert(H, x) { degree[x] 0 P[x] NIL child[x] NIL left[x] x ; right[x] x mark[x] FALSE concatenate the root list containing x with root list H if min[H] = NIL or key[x]<key[min[H]] then min[H] x n[H] n[H]+1 }
Finding the minimum node: min[H] O(1) Amortized cost O(1) is not changed • Uniting 2 Fibonacci heaps: Fib-Heap-Union(H1, H2) { HMake-Fib-Heap[] min[H] min[H1] concatenate the root list of H2 with the root list of H if (min[H1]=NIL) or (min[H2] NIL and min[H2]<min[H1]) then min[H] min[H2] n[H] n[H1]+n[H2] free the objects H1 and H2 return H }
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
Extracting the minimum node: Fib-Heap-Extract-Min(H) { z min[H] if z NIL then { for each child x of z do { add x to the root list of H P[x] NIL } remove z from the root list of H if z = right[z] then min[H] NIL else min[H] right[z] Consolidate(H) n[H] n[H] – 1 } return z }
Fib-Heap-Link(H, y, x) { remove y from the root list of H; make y a child of x; degree[x]degree[x]+1; mark[y] FALSE; } Consolidate(H) { 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 degree[x] ; while A[d] NIL do { y A[d] if key[x]>key[y] then exchange xy Fib-Heap-Link(H, y, x) A[d] NIL ; d d+1 } A[d] x } min[H] NIL for i 0 to D(n[H]) do if A[i] NIL then { add A[i] to the root list of H ; if min[H]=NIL or key[A[i]]<key[min[H]] then min[H] A[i] } }
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 min[ H ] min[ H ]
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
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
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
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
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
min[ H ] 7 18 (m) 23 24 21 39 17 26 30 46 52 35 38 41
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>key[x] then error “new key is greater than current key” key[x] k y P[x] if yNIL and key[x]<key[y] then { CUT(H, x, y) CASCADING-CUT(H, y) } if key[x]<key[min[H]] then min[H] x }
CUT(H, x, y) { remove x from the child list of y, decrease degree[y] add x to the root list of H P[x] NIL mark[x] FALSE } CASCADING-CUT(H, y) { zP[y] if zNIL then { if mark[y]=FALSE then mark[y] 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) }
min[ H ] 7 18 (a) 23 24 21 39 17 26 30 46 52 35 min[ H ] 15 7 18 (b) 23 24 21 39 38 38 17 26 30 52 41 41 35
min[ H ] 15 5 7 (c) 23 24 17 30 26 min[ H ] 15 5 7 18 18 26 (d) 23 24 21 21 39 39 38 38 17 30 52 52 41 41
min[ H ] 15 5 7 24 26 (e) 23 17 30 18 21 39 38 52 41
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)