1 / 19

Fibonacci Heaps

Fibonacci Heaps. CS 583 Analysis of Algorithms. Outline. Operations on Binomial Heaps Inserting a node Extracting the node with minimum key Decreasing a key Deleting a key Fibonacci Heaps Definitions Amortized analysis Inserting a node Union Self-test 20.2-2.

jillrbrown
Download Presentation

Fibonacci Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fibonacci Heaps CS 583 Analysis of Algorithms CS583 Fall'06: Binomial and Fibonacci Heaps

  2. Outline • Operations on Binomial Heaps • Inserting a node • Extracting the node with minimum key • Decreasing a key • Deleting a key • Fibonacci Heaps • Definitions • Amortized analysis • Inserting a node • Union • Self-test • 20.2-2 CS583 Fall'06: Binomial and Fibonacci Heaps

  3. Binomial Heaps: Inserting a Node This procedure inserts node x into binomial heap H, assuming that x is already allocated with key[x]. Binomial-Heap-Insert(H, x) 1 H2 = Make-Binomial-Heap() // create empty heap 2 p[x] = NIL 3 child[x] = NIL 4 sibling[x] = NIL 5 degree[x] = 0 6 head[H2] = x 7 H = Binomial-Heap-Union(H, H2) The procedure makes a one node heap H2 in (1) time and unites it with the n-node binomial heap H in O(lg n) time. CS583 Fall'06: Binomial and Fibonacci Heaps

  4. Extracting Minimum Node This procedure extracts the node with the minimum key from heap H and returns a pointer to the extracted node. Binomial-Heap-Extract-Min(H) 1 x = <Find the root with the minimum key in the root list of H and remove it from H> 2 H2 = Make-Binomial-Heap() // Remove x (root) from the tree, and // prepare the linked list as B0, B1, ... , Bk-1 3 <reverse the order of x’s children> 4 head[H2] = <head of x’s children> 5 H = Binomial-Heap-Union(H,H2) 6 return x Each of lines 1,3,4,5 takes O(lg n) time if H has n nodes. Hence the above procedure runs in O(lg n) time. CS583 Fall'06: Binomial and Fibonacci Heaps

  5. Decreasing a Key This procedure decreases the key of a node x in a heap H to a new value k. Binomial-Heap-Decrease(H, x, k) 1 if k > key[x] 2 throw “new key is greater than the current key” 3 key[x] = k 4 y = x 5 z = p(y) // Bubble up the key in the heap 6 while z <> NIL and key[y] < key[z] 7 <exchange key[y] with key[z]> 8 <exchange satellite info y with z> 9 y = z 10 z = p[y] The maximum depth of x is lg n, hence the while loop 6 iterates at most lg n times. Each iteration takes (1) time, hence the running time of the algorithm is O(lg n). CS583 Fall'06: Binomial and Fibonacci Heaps

  6. Deleting a Key Deleting a key is a straightforward combination of existing procedures. The procedure takes a heap H, and the node x. Binomial-Heap-Delete(H, x) // Make x’s key the smallest 1 Binomial-Heap-Decrease-Key(H, x, -) // Remove x from H 2 Binomial-Heap-Extract-Min(H) Both subroutines take O(lg n), hence the running time of the above procedure is O(lg n). CS583 Fall'06: Binomial and Fibonacci Heaps

  7. Fibonacci Heaps • Fibonacci heaps support meargeable-heap operations: • Insert, Minimum, Extract-Min, Union, Decrease-Key, Delete. • They have the advantage over other heap types in that operations that do not involve deleting an element run in (1) time. • They are theoretically desirable in applications that involve small number of Extract-Min and Delete operations relative to the total number of operations. • Practically, their complexity and constant factors make them less desirable than binary heaps for most applications. CS583 Fall'06: Binomial and Fibonacci Heaps

  8. Fibonacci Heaps: Definition • A Fibonacci heap is a collection of min-heap trees. • The trees are not constrained to be binomial trees. • The trees are unordered, i.e. children nodes order does not matter. • Each node x contains the following fields: • p[x] pointer to its parent. • child[x] pointer to any of its children. • all children nodes are linked in a circular doubly linked list using left[.] and right[.] pointers. • degree[x] – the number of x’s children. • mark[x] indicates whether the node x has lost a child since the last time x was made a child of another node. CS583 Fall'06: Binomial and Fibonacci Heaps

  9. Definition (cont.) • A Fibonacci heap H is accessed by a pointer min[H] to the root of a tree containing a minimum key. • This node is called the minimum node. • The roots of all trees in the Fibonacci heap H are linked together into a circular doubly linked list. • The list is called the root list of H. • Roots are linked using left and right pointers. • The order of trees within a root list is arbitrary. • The number of nodes currently in H is kept in n[H]. CS583 Fall'06: Binomial and Fibonacci Heaps

  10. Amortized Analysis • In amortized analysis, the time required to perform a sequence of data-structure operations is averaged over all operations performed. • It differs from the average-case analysis in that probability is not involved. • It guarantees the average performance of each operation in the worst case. • The potential method of amortized analysis represents the prepaid work as “potential” that can be released for future operations. • The total “work” on a data structure is spread unevenly over the course of an algorithm. CS583 Fall'06: Binomial and Fibonacci Heaps

  11. The Potential Method • Start with an initial data structure D0 on which n operations are performed. • Let ci, i= 1,...,n be the actual cost of the ith operation. • Di is the data structure that results after applying the ith operation on Di-1. • A potential function maps each data structure Di to a real number (Di), which is a potential associated with Di. • The amortized cost cai of the ith operation is: • cai = ci + (Di) - (Di-1) • It is thus an actual cost of operation plus the increase in potential due to the operation. CS583 Fall'06: Binomial and Fibonacci Heaps

  12. The Potential Method (cont.) • The total amortized cost of n operations is: • i=1,ncai = i=1,n(ci + (Di) - (Di-1)) = i=1,nci + (Dn) - (D0) • When (Dn)>(D0), the total amortized cost is an upper bound on the total actual cost. • Intuitively, when (Di)-(Di-1) > 0, the amortized cost of the ith operation represents an overcharge. • Otherwise, the amortized cost represents an undercharge, and the actual cost is paid by decrease in potential. • Different potential functions may yield different amortized costs, but still be upper bound on the actual costs. CS583 Fall'06: Binomial and Fibonacci Heaps

  13. Fibonacci Heap: Potential Function • For a given Fibonacci heap H, we denote: • t(H) – the number of trees in the root list. • m(H) – the number of marked nodes in H. • The potential of Fibonacci heap H is defined as: • (H) = t(H) + 2 m(H) • Assume that a Fibonacci heap application begins with no heaps. • The initial potential is 0. • By above equation, the potential >= 0 in all subsequent time. • Hence an upper bound on total amortized cost is also an upper bound on total actual cost. CS583 Fall'06: Binomial and Fibonacci Heaps

  14. Mergeable-Heap Operations • For basic mergeable-heap operations (Make-Heap, Insert, Minimum, Extract-Min, Union): • Each Fibonacci heap is a collection of “unordered” binomial trees. • An unordered binomial tree is like a binomial tree, and is defined recursively. • The tree U0 contains a single node. • The tree Uk consists of two unordered binomial trees Uk-1 for which the root of one is made into any child of the root of another. The following property is different from the ordered binomial trees. • For the tree Uk, the root has degree k, which is greater than of any other node. The children of the root are U0,U1,....Uk-1 in some order. CS583 Fall'06: Binomial and Fibonacci Heaps

  15. Creating a New Heap • The Make-Fib-Heap procedure allocates and returns the Fibonacci heap object H, where: • n[H] = 0 • min[H] = NIL • there are no trees in H. • The potential is determined by t(H) = 0 and m(H) = 0: • (H) = 0 • Hence the amortized cost is the same as actual cost, -- (1). CS583 Fall'06: Binomial and Fibonacci Heaps

  16. Inserting a Node This procedure inserts node x into a Fibonacci heap assuming that x is allocated with a key[x]. Fib-Heap-Insert(H,x) // Create a linked list containing x only 1 degree[x] = 0 2 p[x] = NIL 3 child[x] = NIL 4 left[x] = x 5 right[x] = x 6 mark[x] = FALSE 7 <concatenate x-list with root list H> 8 if min[H] = NIL or key[x] < key[min[H]] 9 min[H] = x 10 n[H]++ CS583 Fall'06: Binomial and Fibonacci Heaps

  17. Inserting Node: Performance • Let H be the input heap, and H2 be the resulting heap. Then • t(H2) = t(H) + 1 • m(H2) = m(H) • The increase in potential is: • ((t(H)+1) + 2 m(H)) – (t(H) + 2 m(H)) = 1 • The amortized cost is • Actual cost + 1 = (1) + 1 = (1) CS583 Fall'06: Binomial and Fibonacci Heaps

  18. Uniting Two Fibonacci Heaps This procedure unites heaps H1 and H2, destroying them in the process. It simply concatenates root lists and determines the new minimum node. Fib-Heap-Union(H1,H2) 1 H = Make-Fib-Heap() 2 min[H] = min[H1] 3 <concatenate the root lists H2 and H> 4 if (min[H1] = NIL) or (min[H2] <> NIL and key[min[H2]] < key[min[H1]]) 5 min[H] = min[H2] 6 n[H] = n[H1] + n[H2] 7 <free objects H1 and H2> 8 return H CS583 Fall'06: Binomial and Fibonacci Heaps

  19. Union: Performance • The components of potential are: • t(H) = t(H1) + t(H2) • m(H) = m(H1) + m(H2) • The change in potential is: • (H) – ((H1) + (H2)) = 0 • The amortized cost is therefore equal to the actual cost. • The actual cost is (1) = amortized cost. CS583 Fall'06: Binomial and Fibonacci Heaps

More Related