FIBONACCI HEAPS ‣ Preliminaries ‣ Insert ‣ Extract the Minimum
Total Page:16
File Type:pdf, Size:1020Kb
Priority queues performance cost summary FIBONACCI HEAPS operation linked list binary heap binomial heap Fibonacci heap † MAKE-HEAP O(1) O(1) O(1) O(1) ‣ preliminaries IS-EMPTY O(1) O(1) O(1) O(1) ‣ insert INSERT O(1) O(log n) O(log n) O(1) ‣ extract the minimum EXTRACT-MIN O(n) O(log n) O(log n) O(log n) ‣ decrease key DECREASE-KEY O(1) O(log n) O(log n) O(1) ‣ bounding the rank ‣ meld and delete DELETE O(1) O(log n) O(log n) O(log n) MELD O(1) O(n) O(log n) O(1) Lecture slides by Kevin Wayne FIND-MIN O(n) O(1) O(log n) O(1) http://www.cs.princeton.edu/~wayne/kleinberg-tardos † amortized Ahead. O(1) INSERT and DECREASE-KEY, O(log n) EXTRACT-MIN. Last updated on Apr 15, 2013 6:59 AM 2 Fibonacci heaps Fibonacci heaps Theorem. [Fredman-Tarjan 1986] Starting from an empty Fibonacci heap, Theorem. [Fredman-Tarjan 1986] Starting from an empty Fibonacci heap, any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations involving n INSERT operations takes O(m + n log n) time. involving n INSERT operations takes O(m + n log n) time. this statement is a bit weaker than the actual theorem History. Fibonacci Heaps and Their Uses in Improved Network ・Ingenious data structure and application of amortized analysis. Optimization Algorithms ・Original motivation: improve Dijkstra's shortest path algorithm MICHAEL L. FREDMAN University of California, San Diego, L.a Jolla, California from O(m log n) to O(m + n log n). AND Also improved best-known bounds for all-pairs shortest paths, ROBERT ENDRE TARJAN ・ AT&T Bell Laboratories, Murray HilI, New Jersey assignment problem, minimum spanning trees. Abstract. In this paper we develop a new data structure for implementing heaps (priority queues). Our structure, Fibonacci heaps (abbreviated F-heaps), extends the binomial queues proposed by Vuillemin and studied further by Brown. F-heaps support arbitrary deletion from an n-item heap in qlogn) amortized time and all other standard heap operations in o( 1) amortized time. Using F-heaps we are able to obtain improved running times for several network optimization algorithms. In particular, we obtain the following worst-case bounds, where n is the number of vertices and m the number of edges in the problem graph: ( 1) O(n log n + m) for the single-source shortest path problem with nonnegative edge lengths, improved from O(mlogfmh+2)n); (2) O(n*log n + nm) for the all-pairs shortest path problem, improved from O(nm lo&,,,+2,n); (3) O(n*logn + nm) for the assignment problem (weighted bipartite matching), improved from O(nmlog0dn+2)n); (4) O(mj3(m, n)) for the minimum spanning tree problem, improved from O(mloglo&,,.+2,n), where j3(m, n) = min {i 1log% 5 m/n). Note that B(m, n) 5 log*n if m 2 n. Of these results, the improved bound for minimum spanning trees is the most striking, although all the results give asymptotic improvements for graphs of appropriate densities. Categories and Subject Descriptors: E.l [Data]: Data Structures--trees; graphs; F.2.2 [Analysis of Algorithms and Problem Complexity]: Nonnumerical Algorithms and Problems-computations on discrete structures; sorting and searching; G.2.2 [Discrete Mathematics]: Graph Theory-graph algo- 3 4 rithms; network problems; trees General Terms: Algorithms, Theory Additional Key Words and Phrases: Heap, matching, minimum spanning tree, priority queue, shortest Pati ’ A preliminary version of this paper appeared in the Proceedings of the 25th IEEE Symposium on the Foundations of Computer Science (Singer Island, Fla., Oct. 24-26). IEEE, New York, 1984, pp. 338- 346, 0 IEEE. Any portion of this paper that appeared in the preliminary version is reprinted with permission. This research was supported in part by the National Science Foundation under grant MCS 82-0403 1. Authors’ addresses: M. L. Fredman, Electrical Engineering and Computer Science Department, Uni- versity of California, San Diego, La Jolla, CA 92093; R. E. Tarjan, AT&T Bell Laboratories, Murray Hill, NJ 07974. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1987 ACM 0004-541 l/87/0700-0596 $1.50 Journal ofthe Association for Computing Machinery, Vol. 34, No. 3, July 1987, Pages 596-615. Fibonacci heaps Basic idea. FIBONACCI HEAPS ・Similar to binomial heaps, but less rigid structure. ・Binomial heap: eagerly consolidate trees after each INSERT; ‣ structure implement DECREASE-KEY by repeatedly exchanging node with its parent. ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank ‣ meld and delete ・Fibonacci heap: lazily defer consolidation until next EXTRACT-MIN; SECTION 19.1 implement DECREASE-KEY by cutting off node and splicing into root list. Remark. Height of Fibonacci heap is Θ(n) in worst case, but it doesn't use sink or swim operations. 6 Fibonacci heap: structure Fibonacci heap: structure ・Set of heap-ordered trees. ・Set of heap-ordered trees. ・Set of marked nodes. each child no smaller than its parent used to keep trees bushy (stay tuned) heap-ordered tree min 17 24 23 7 3 root 17 24 23 7 3 30 26 46 30 26 46 18 52 41 18 52 41 heap H heap H marked 35 35 39 44 39 44 7 8 Fibonacci heap: structure Fibonacci heap: representation Heap representation. Node representation. Each node stores: ・Store a pointer to the minimum node. ・A pointer to its parent. ・Maintain tree roots in a circular, doubly-linked list. ・A pointer to any of its children. ・A pointer to its left and right siblings. ・Its rank = number of children. ・Whether it is marked. min min 17 24 23 7 3 17 24 23 7 3 rank = 3 30 26 46 30 26 46 18 52 41 18 52 41 children are in a heap H heap H 35 35 circular doubly-linked list 39 44 39 44 9 10 Fibonacci heap: representation Fibonacci heap: notation Operations we can do in constant time: notation meaning ・Find the minimum element. n number of nodes ・Merge two root lists together. ・Determine rank of a root node. rank(x) number of children of node x ・Add or remove a node from the root list. rank(H) max rank of any node in heap H ・Remove a subtree and merge into root list. trees(H) number of trees in heap H ・Link the root of a one tree to root of another tree. marks(H) number of marked nodes in heap H n = 14 rank(H) = 3 trees(H) = 5 marks(H) = 3 min min 17 24 23 7 3 rank = 3 rank = 1 17 24 23 7 3 rank = 3 30 26 46 30 26 46 18 52 41 18 52 41 heap H heap H 35 35 39 44 39 44 11 12 Fibonacci heap: potential function Potential function. FIBONACCI HEAPS Φ(H) = trees(H) + 2 ⋅ marks(H) ‣ preliminaries ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank Φ(H) = 5 + 2⋅3 = 11 trees(H) = 5 marks(H) = 3 min ‣ meld and delete 17 24 23 7 3 SECTION 19.2 30 26 46 18 52 41 heap H 35 39 44 13 Fibonacci heap: insert Fibonacci heap: insert ・Create a new singleton tree. ・Create a new singleton tree. ・Add to root list; update min pointer (if necessary). ・Add to root list; update min pointer (if necessary). insert 21 insert 21 21 min min 17 24 23 7 3 17 24 23 7 21 3 30 26 46 30 26 46 18 52 41 18 52 41 heap H heap H 35 35 39 44 39 44 15 16 Fibonacci heap: insert analysis Actual cost. ci = O(1). FIBONACCI HEAPS one more tree; Change in potential. ∆Φ = Φ(Hi) – Φ(Hi–1) = +1. no change in marks ‣ preliminaries Amortized cost. ĉi = ci + ∆Φ = O(1). ‣ insert ‣ extract the minimum Φ(H) = trees(H) + 2 ⋅ marks(H) ‣ decrease key ‣ bounding the rank min ‣ meld and delete 17 24 23 7 21 3 SECTION 19.2 30 26 46 18 52 41 heap H 35 39 44 17 Linking operation Fibonacci heap: extract the minimum Useful primitive. Combine two trees T1 and T2 of rank k. ・Delete min; meld its children into root list; update min. ・Make larger root be a child of smaller root. ・Consolidate trees so that no two roots have same rank. ・Resulting tree T ' has rank k + 1. tree T1 tree T2 tree T' 15 3 3 min 7 24 23 17 3 56 24 33 18 52 41 15 18 52 41 77 39 44 56 24 33 39 44 30 26 46 18 52 41 77 still heap-ordered 35 39 44 19 20 Fibonacci heap: extract the minimum Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min. ・Delete min; meld its children into root list; update min. ・Consolidate trees so that no two roots have same rank. ・Consolidate trees so that no two roots have same rank. current min 7 24 23 17 18 52 41 min 7 24 23 17 18 52 41 30 26 46 39 44 30 26 46 39 44 35 35 21 22 Fibonacci heap: extract the minimum Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min.