Fibonacci Heaps.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Fibonacci Heaps Especially desirable when the number of calls to Extract-Min & Delete is small (note that all other operations run in O(1) This arises.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
Advanced Data structure
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Fibonacci Heaps. Single Source All Destinations Shortest Paths
Fibonacci Heap.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Binomial heaps, Fibonacci heaps, and applications.
Chapter 9 Heap Structures
Binomial heaps, Fibonacci heaps, and applications
Lecture X Fibonacci Heaps
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
Fibonacci Heaps.
Fibonacci Heaps. Analysis FibonacciAnalysis.ppt Video  iew/cop5536sahni
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1.
Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
CSE332: Data Abstractions Lecture 7: AVL Trees
Chapter 5 : Trees.
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
Chapter 11: Multiway Search Trees
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Binomial heaps, Fibonacci heaps, and applications
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Priority Queues MakeQueue create new empty queue
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Pairing Heaps Actual Complexity.
Lecture 9 Algorithm Analysis
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
© 2013 Goodrich, Tamassia, Goldwasser
Lecture 9 Algorithm Analysis
Chapter 20 Binomial Heaps
Fibonacci Heap.
ערמות בינומיות ופיבונצ'י
Lecture 9 Algorithm Analysis
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
§3 Worst-Case vs. Amortized
Strict Fibonacci Heaps
CS 583 Analysis of Algorithms
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Binary and Binomial Heaps
Binomial heaps, Fibonacci heaps, and applications
Binomial heaps, Fibonacci heaps, and applications
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Pairing Heaps Actual Complexity
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Fibonacci Heaps

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci † Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 this time † amortized

Fibonacci Heaps Fibonacci heap history. Fredman and Tarjan (1986) Ingenious data structure and analysis. Original motivation: O(m + n log n) shortest path algorithm. also led to faster algorithms for MST, weighted bipartite matching Still ahead of its time. Fibonacci heap intuition. Similar to binomial heaps, but less structured. Decrease-key and union run in O(1) time. "Lazy" unions. Reference: CLRS, Kozen Chapter 8-9

Fibonacci Heaps: Structure Set of min-heap ordered trees. set -> unordered min 17 24 23 7 3 30 26 46 marked 18 52 41 35 H 39 44

Fibonacci Heaps: Implementation Represent trees using left-child, right sibling pointers and circular, doubly linked list. can quickly splice off subtrees Roots of trees connected with circular doubly linked list. fast union Pointer to root of tree with min element. fast find-min min 17 24 23 7 3 30 26 46 18 52 41 35 H 39 44

Fibonacci Heaps: Potential Function Key quantities. Degree[x] = degree of node x. Mark[x] = mark of node x (black or gray). t(H) = # trees. m(H) = # marked nodes. (H) = t(H) + 2m(H) = potential function. nodes only change mark in Decrease-Key Can basically ignore marks until then. t(H) = 5, m(H) = 3 (H) = 11 degree = 3 min 17 24 23 7 3 30 26 46 18 52 41 35 H 39 44

Fibonacci Heaps: Insert Create a new singleton tree. Add to left of min pointer. Update min pointer. Insert 21 21 min 17 24 23 7 3 30 26 46 18 52 41 35 H 39 44

Fibonacci Heaps: Insert Create a new singleton tree. Add to left of min pointer. Update min pointer. Insert 21 min 17 24 23 7 21 3 30 26 46 18 52 41 35 H 39 44

Fibonacci Heaps: Insert Create a new singleton tree. Add to left of min pointer. Update min pointer. Running time. O(1) amortized Actual cost = O(1). Change in potential = +1. Amortized cost = O(1). Insert 21 min 17 24 23 7 21 3 30 26 46 18 52 41 35 H 39 44

Fibonacci Heaps: Union Concatenate two Fibonacci heaps. Root lists are circular, doubly linked lists. min min 23 24 17 7 3 21 H' H'' 30 26 46 18 52 41 35 39 44

Fibonacci Heaps: Union Concatenate two Fibonacci heaps. Root lists are circular, doubly linked lists. Running time. O(1) amortized Actual cost = O(1). Change in potential = 0. Amortized cost = O(1). min 23 24 17 7 3 21 H' H'' 30 26 46 18 52 41 35 39 44

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. min 7 24 23 17 3 30 26 46 18 52 41 35 39 44

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. current min 7 24 23 17 18 52 41 39 44 30 26 46 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 current min 7 24 23 17 18 52 41 39 44 30 26 46 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 current min 7 24 23 17 18 52 41 39 44 30 26 46 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min 7 24 23 17 18 52 41 39 44 30 26 46 current 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min 7 24 23 17 18 52 41 39 44 30 26 46 current 35 Merge 17 and 23 trees.

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 current min 7 24 17 18 52 41 23 39 44 30 26 46 35 Merge 7 and 17 trees.

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 24 7 18 52 41 17 30 39 44 26 46 35 23 Merge 7 and 24 trees.

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 18 52 41 24 17 30 39 44 26 46 23 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 18 52 41 24 17 30 39 44 26 46 23 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 18 52 41 24 17 30 39 44 26 46 23 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 18 52 41 24 17 30 39 44 26 46 23 Merge 41 and 18 trees. 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 52 18 24 17 30 41 39 26 46 23 44 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 1 2 3 min current 7 52 18 24 17 30 41 39 26 46 23 44 35

Fibonacci Heaps: Delete Min Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree. min 7 52 18 24 17 30 41 39 26 46 23 44 Stop. 35

Fibonacci Heaps: Delete Min Analysis Notation. D(n) = max degree of any node in Fibonacci heap with n nodes. t(H) = # trees in heap H. (H) = t(H) + 2m(H). Actual cost. O(D(n) + t(H)) O(D(n)) work adding min's children into root list and updating min. at most D(n) children of min node O(D(n) + t(H)) work consolidating trees. work is proportional to size of root list since number of roots decreases by one after each merging  D(n) + t(H) - 1 root nodes at beginning of consolidation Amortized cost. O(D(n)) t(H')  D(n) + 1 since no two trees have same degree. (H)  D(n) + 1 - t(H).

Fibonacci Heaps: Delete Min Analysis Is amortized cost of O(D(n)) good? Yes, if only Insert, Delete-min, and Union operations supported. in this case, Fibonacci heap contains only binomial trees since we only merge trees of equal root degree this implies D(n)  log2 N Yes, if we support Decrease-key in clever way. we'll show that D(n)  log N, where  is golden ratio 2 = 1 +   = (1 + 5) / 2 = 1.618… limiting ratio between successive Fibonacci numbers!

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 0: min-heap property not violated. decrease key of x to k change heap min pointer if necessary min 7 18 38 24 17 23 21 39 41 26 45 46 30 52 Decrease 46 to 45. 35 88 72

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 1: parent of x is unmarked. decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer min 7 18 38 24 17 23 21 39 41 26 15 45 30 52 Decrease 45 to 15. 35 88 72

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 1: parent of x is unmarked. decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer min 7 18 38 24 24 17 23 21 39 41 26 15 30 52 Decrease 45 to 15. 35 88 72

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 1: parent of x is unmarked. decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer min 15 7 18 38 72 24 24 17 23 21 39 41 26 30 52 Decrease 45 to 15. 35 88

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root list cut off link between p[x] and p[p[x]], add p[x] to root list If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat. min 15 7 18 38 72 24 24 17 23 21 39 41 26 30 52 Decrease 35 to 5. 5 35 88

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root list cut off link between p[x] and p[p[x]], add p[x] to root list If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat. min 15 5 7 18 38 72 24 24 17 23 21 39 41 parent marked 26 30 52 Decrease 35 to 5. 88

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root list cut off link between p[x] and p[p[x]], add p[x] to root list If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat. min 15 5 26 7 18 38 72 88 24 24 17 23 21 39 41 parent marked 30 52 Decrease 35 to 5.

Fibonacci Heaps: Decrease Key Decrease key of element x to k. Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root list cut off link between p[x] and p[p[x]], add p[x] to root list If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat. min 15 5 26 24 7 18 38 72 88 17 23 21 39 41 30 52 Decrease 35 to 5.

Fibonacci Heaps: Decrease Key Analysis Notation. t(H) = # trees in heap H. m(H) = # marked nodes in heap H. (H) = t(H) + 2m(H). Actual cost. O(c) O(1) time for decrease key. O(1) time for each of c cascading cuts, plus reinserting in root list. Amortized cost. O(1) t(H') = t(H) + c m(H')  m(H) - c + 2 each cascading cut unmarks a node last cascading cut could potentially mark a node   c + 2(-c + 2) = 4 - c. When marked node y is cut by cascading cut, its mark bit is cleared (2 units of potential). One unit pays for cut, the other for unit increase in potential due to y becoming a root.

Fibonacci Heaps: Delete Delete node x. Decrease key of x to -. Delete min element in heap. Amortized cost. O(D(n)) O(1) for decrease-key. O(D(n)) for delete-min. D(n) = max degree of any node in Fibonacci heap.

Fibonacci Heaps: Bounding Max Degree Definition. D(N) = max degree in Fibonacci heap with N nodes. Key lemma. D(N)  log N, where  = (1 + 5) / 2. Corollary. Delete and Delete-min take O(log N) amortized time. Lemma. Let x be a node with degree k, and let y1, . . . , yk denote the children of x in the order in which they were linked to x. Then: Proof. When yi is linked to x, y1, . . . , yi-1 already linked to x,  degree(x) = i - 1  degree(yi) = i - 1 since we only link nodes of equal degree Since then, yi has lost at most one child otherwise it would have been cut from x Thus, degree(yi) = i - 1 or i - 2

Fibonacci Heaps: Bounding Max Degree Key lemma. In a Fibonacci heap with N nodes, the maximum degree of any node is at most log N, where  = (1 + 5) / 2. Proof of key lemma. For any node x, we show that size(x)  degree(x) . size(x) = # node in subtree rooted at x taking base  logs, degree(x)  log (size(x))  log N. Let sk be min size of tree rooted at any degree k node. trivial to see that s0 = 1, s1 = 2 sk monotonically increases with k Let x* be a degree k node of size sk, and let y1, . . . , yk be children in order that they were linked to x*. 2 comes from node x* (1) and child of x* with degree 1 Assume k  2

Fibonacci Facts Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21, . . . Slightly nonstandard definition. Fact F1. Fk  k, where  = (1 + 5) / 2 = 1.618… Fact F2. Consequence. sk  Fk  k. This implies that size(x)  degree(x) for all nodes x.

Golden Ratio Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21, . . . Definition. The golden ratio  = (1 + 5) / 2 = 1.618… Divide a rectangle into a square and smaller rectangle such that the smaller rectangle has the same ratio as original one. Parthenon, Athens Greece

Fibonacci Facts This image uses shapes derived from the Fibonacci series. The grey shiny base shows the individual quarter-circles which make up the Fibonacci spiral, and increases the height of each quarter-circle as they get smaller in radius. The purple object on the left of the image is a Fibonacci spiral rotated through 90 degrees about a point on the edge common to the square of size 3 and the square of size 13, furthest away from the square of size 2. The axis of rotation is the longest edge of the rectangle made from the squares which make up the spiral. The light blue object to the right is similar to the purple one, but rotated through 239 degrees. The object at the front of the purple one is the Fibonacci spiral plus the squares used in its construction, given a thickness to make them appear to be made from brass strips. The three objects in front of the base are the spiral as it would appear when made from chromed rod. This ray-tracing was done on a 486 DX66 PC, using Imagine 3.0. If you have any comments or suggestions relating to this image, please email Brian Knott at brianknott@wirral.gov.uk)

Fibonacci Numbers and Nature http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html Pinecone Cauliflower

Fibonacci Proofs Fact F1. Fk  k. Proof. (by induction on k) Base cases: F0 = 1, F1 = 2  . Inductive hypotheses: Fk  k and Fk+1  k+1 Fact F2. F2 = 3, F3 = 5 2 =  + 1