Fibonacci Heaps
Total Page:16
File Type:pdf, Size:1020Kb
FIBONACCI HEAPS ‣ preliminaries ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank ‣ meld and delete Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 7/25/17 11:07 AM Priority queues performance cost summary Fibonacci heap operation linked list binary heap binomial heap † MAKE-HEAP O(1) O(1) O(1) O(1) IS-EMPTY O(1) O(1) O(1) O(1) INSERT O(1) O(log n) O(log n) O(1) EXTRACT-MIN O(n) O(log n) O(log n) O(log n) DECREASE-KEY O(1) O(log n) O(log n) O(1) DELETE O(1) O(log n) O(log n) O(log n) MELD O(1) O(n) O(log n) O(1) FIND-MIN O(n) O(1) O(log n) O(1) † amortized Ahead. O(1) INSERT and DECREASE-KEY, O(log n) EXTRACT-MIN. 2 Fibonacci heaps Theorem. [Fredman-Tarjan 1986] Starting from an empty Fibonacci heap, any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations involving n INSERT operations takes O(m + n log n) time. this statement is a bit weaker than the actual theorem Fibonacci Heaps and Their Uses in Improved Network Optimization Algorithms MICHAEL L. FREDMAN University of California, San Diego, L.a Jolla, California AND ROBERT ENDRE TARJAN AT&T Bell Laboratories, Murray HilI, New Jersey 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 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 Theorem. [Fredman–Tarjan 1986] Starting from an empty Fibonacci heap, any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations involving n INSERT operations takes O(m + n log n) time. History. ・Ingenious data structure and application of amortized analysis. ・Original motivation: improve Dijkstra’s shortest path algorithm from O(m log n) to O(m + n log n). ・Also improved best-known bounds for all-pairs shortest paths, assignment problem, minimum spanning trees. 4 FIBONACCI HEAPS ‣ structure ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank ‣ meld and delete SECTION 19.1 Fibonacci heaps Basic idea. ・Similar to binomial heaps, but less rigid structure. ・Binomial heap: eagerly consolidate trees after each INSERT; implement DECREASE-KEY by repeatedly exchanging node with its parent. ・Fibonacci heap: lazily defer consolidation until next EXTRACT-MIN; 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 ・Set of heap-ordered trees. each child no smaller than its parent heap-ordered tree 17 24 23 7 3 root 30 26 46 18 52 41 heap H 35 39 44 7 Fibonacci heap: structure ・Set of heap-ordered trees. ・Set of marked nodes. used to keep trees bushy (stay tuned) min 17 24 23 7 3 30 26 46 18 52 41 heap H marked 35 39 44 8 Fibonacci heap: structure Heap representation. ・Store a pointer to the minimum node. ・Maintain tree roots in a circular, doubly-linked list. min 17 24 23 7 3 30 26 46 18 52 41 heap H 35 39 44 9 Fibonacci heap: representation Node representation. Each node stores: ・A pointer to its parent. ・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 17 24 23 7 3 rank = 3 30 26 46 18 52 41 children are in a heap H 35 circular doubly-linked list 39 44 10 Fibonacci heap: representation Operations we can do in constant time: ・Determine rank of a node. ・Find the minimum element. ・Merge two root lists together. ・Add or remove a node from the root list. ・Remove a subtree and merge into root list. ・Link the root of a one tree to root of another tree. min 17 24 23 7 3 rank = 3 30 26 46 18 52 41 heap H 35 39 44 11 Fibonacci heap: notation notation meaning n number of nodes rank(x) number of children of node x rank(H) max rank of any node in heap H trees(H) number of trees in heap H marks(H) number of marked nodes in heap H n = 14 rank(H) = 3 trees(H) = 5 marks(H) = 3 min rank = 1 17 24 23 7 3 rank = 3 30 26 46 18 52 41 heap H 35 39 44 12 Fibonacci heap: potential function Potential function. Φ(H) = trees(H) + 2 ⋅ marks(H) Φ(H) = 5 + 2⋅3 = 11 trees(H) = 5 marks(H) = 3 min 17 24 23 7 3 30 26 46 18 52 41 heap H 35 39 44 13 FIBONACCI HEAPS ‣ preliminaries ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank ‣ meld and delete SECTION 19.2 Fibonacci heap: insert ・Create a new singleton tree. ・Add to root list; update min pointer (if necessary). insert 21 21 min 17 24 23 7 3 30 26 46 18 52 41 heap H 35 39 44 15 Fibonacci heap: insert ・Create a new singleton tree. ・Add to root list; update min pointer (if necessary). insert 21 min 17 24 23 7 21 3 30 26 46 18 52 41 heap H 35 39 44 16 Fibonacci heap: insert analysis Actual cost. ci = O(1). one more tree; Change in potential. ∆Φ = Φ(Hi) – Φ(Hi–1) = +1. no change in marks Amortized cost. ĉi = ci + ∆Φ = O(1). Φ(H) = trees(H) + 2 ⋅ marks(H) min 17 24 23 7 21 3 30 26 46 18 52 41 heap H 35 39 44 17 FIBONACCI HEAPS ‣ preliminaries ‣ insert ‣ extract the minimum ‣ decrease key ‣ bounding the rank ‣ meld and delete SECTION 19.2 Linking operation Useful primitive. Combine two trees T1 and T2 of rank k. ・Make larger root be a child of smaller root. ・Resulting tree T ʹ has rank k + 1. tree T1 tree T2 tree T′ 15 3 3 56 24 33 18 52 41 15 18 52 41 77 39 44 56 24 33 39 44 77 still heap-ordered 19 Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min. ・Consolidate trees so that no two roots have same rank. min 7 24 23 17 3 30 26 46 18 52 41 35 39 44 20 Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min. ・Consolidate trees so that no two roots have same rank. min 7 24 23 17 18 52 41 30 26 46 39 44 35 21 Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min. ・Consolidate trees so that no two roots have same rank. current min 7 24 23 17 18 52 41 30 26 46 39 44 35 22 Fibonacci heap: extract the minimum ・Delete min; meld its children into root list; update min.