Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20. Princeton University COS 4 Theory of Algorithms Spring 2002 Kevin Wayne
Priority Queues Operation mae-heap insert find- delete- union decrease-ey delete Lined List 1 1 N N 1 1 N Binary 1 log N 1 log N N log N log N Binomial 1 log N log N log N log N log N log N Heaps Fibonacci 1 1 1 log N 1 1 log N Relaxed is-empty 1 1 1 1 1 1 1 1 log N 1 1 log N amortized this time 2
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-ey and union run in O(1) time. "Lazy" unions. 3
Fibonacci Heaps: Structure Fibonacci heap. Set of -heap ordered trees. 1 24 3 26 46 mared 18 52 H 4
Fibonacci Heaps: Implementation Implementation. Represent trees using left-child, right sibling pointers and circular, doubly lined list. can quicly splice off subtrees Roots of trees connected with circular doubly lined list. fast union Pointer to root of tree with element. fast find- 1 24 3 26 46 18 52 H 5
Fibonacci Heaps: Potential Function Key quantities. Degree[x] = degree of node x. Mar[x] = mar of node x (blac or gray). t(h) = # trees. m(h) = # mared nodes. Φ(H) = t(h) + 2m(H) = potential function. t(h) = 5, m(h) = 3 Φ(H) = 11 degree = 3 1 24 3 26 46 18 52 H 6
Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to left of pointer. Update pointer. Insert 21 21 1 24 3 26 46 18 52 H
Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to left of pointer. Update pointer. Insert 21 1 24 21 3 26 46 18 52 H 8
Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to left of pointer. Update pointer. Running time. O(1) amortized Actual cost = O(1). Change in potential = +1. Amortized cost = O(1). Insert 21 1 24 21 3 26 46 18 52 H 9
Fibonacci Heaps: Union Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly lined lists. 24 1 3 21 H H 26 46 18 52 10
Fibonacci Heaps: Union Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly lined lists. Running time. O(1) amortized Actual cost = O(1). Change in potential = 0. Amortized cost = O(1). 24 1 3 21 H H 26 46 18 52 11
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 24 1 3 26 46 18 52 12
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. current 24 1 18 52 26 46 13
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 1 18 52 26 46 14
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. current 0 1 2 3 24 1 18 52 26 46 15
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 24 1 18 52 26 46 current 16
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 24 1 18 52 26 46 current Merge 1 and trees. 1
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 1 18 52 26 46 Merge and 1 trees. 18
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 18 52 26 46 1 Merge and 24 trees. 19
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 20
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 21
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 22
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 Merge and 18 trees.
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 52 18 24 1 26 46 24
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 52 18 24 1 26 46 25
Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 52 18 24 1 26 46 Stop. 26
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)) wor adding s children into root list and updating. at most D(n) children of node O(D(n) + t(h)) wor consolidating trees. wor 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). 2
Fibonacci Heaps: Delete Min Analysis Is amortized cost of O(D(n)) good? Yes, if only Insert, Delete-, 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) log 2 N Yes, if we support Decrease-ey 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! 28
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 0: -heap property not violated. decrease ey of x to change heap pointer if necessary 18 38 24 1 21 26 46 45 52 88 2 Decrease 46 to 45. 29
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 18 38 24 1 21 26 45 15 52 88 2 Decrease 45 to 15.
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 18 38 24 1 21 26 15 52 88 2 Decrease 45 to 15. 31
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 15 18 38 2 24 1 21 26 52 88 Decrease 45 to 15. 32
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 18 38 2 24 1 21 26 52 5 88 Decrease to 5. 33
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 18 38 2 24 1 21 parent mared 26 52 88 Decrease to 5. 34
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 26 18 38 2 88 24 1 21 parent mared 52 Decrease to 5.
Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 26 24 18 38 2 88 1 21 52 Decrease to 5. 36
Fibonacci Heaps: Decrease Key Analysis Notation. t(h) = # trees in heap H. m(h) = # mared nodes in heap H. Φ(H) = t(h) + 2m(H). Actual cost. O(c) O(1) time for decrease ey. 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 unmars a node last cascading cut could potentially mar a node Φ c + 2(-c + 2) = 4 - c. 3
Fibonacci Heaps: Delete Delete node x. Decrease ey of x to -. Delete element in heap. Amortized cost. O(D(n)) O(1) for decrease-ey. O(D(n)) for delete-. D(n) = max degree of any node in Fibonacci heap. 38
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- tae O(log N) amortized time. Lemma. Let x be a node with degree, and let y 1,..., y denote the children of x in the order in which they were lined to x. Then: Proof. When y i is lined to x, y 1,..., y i-1 already lined to x, degree(x) = i - 1 degree(y i ) = i - 1 since we only lin nodes of equal degree Since then, y i degree has lost at most one child otherwise it would have been cut from x Thus, degree(y i ) = i - 1 or i - 2 ( y i ) 0 i 2 if if i = 1 i 1
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 ey lemma. For any node x, we show that size(x) φ degree(x). size(x) = # node in subtree rooted at x taing base φ logs, degree(x) log φ (size(x)) log φ N. Let s be size of tree rooted at any degree node. trivial to see that s 0 = 1, s 1 = 2 s monotonically increases with Let x* be a degree node of size s, and let y 1,..., y be children in order that they were lined to x*. Assume 2 s = = = size ( x*) 2 + 2 + 2 + 2 + size( y i= 2 i= 2 s i= 2 2 s i= 0 s deg[ y i 2 i i i ] ) 40
Fibonacci Facts Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21,... Slightly nonstandard definition. F = 1 2 F -1 + F -2 if if if = 0 = 1 2 Fact F1. F φ, where φ = (1 + 5) / 2 = 1.618 Fact F2. For 2, 2 F = 2 + F i i= 0 Consequence. s F φ. This implies that size(x) φ degree(x) for all nodes x. s = = = size ( x*) 2 + 2 + 2 + 2 + size( y i= 2 i= 2 s i= 2 2 s i= 0 s deg[ y i 2 i i i ] )
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 42
Fibonacci Facts 43
Fibonacci Numbers and Nature Pinecone Cauliflower
45 Fibonacci Proofs Fact F1. F φ. Proof. (by induction on ) Base cases: F 0 = 1, F 1 = 2 φ. Inductive hypotheses: F φ and F +1 φ +1 Fact F2. Proof. (by induction on ) Base cases: F 2 = 3, F 3 = 5 Inductive hypotheses: + = = 2 0 2 2, For i F F i 2 2 1 1 2 ) ( ) (1 + + + + = = + = + + = F F F ϕ ϕ ϕ ϕ ϕ ϕ ϕ φ 2 = φ + 1 + = = 2 0 2 i F F i + = + + = + = = = + + + i i i F F F F F F 0 2 0 1 1 2 2 2
On Complicated Algorithms "Once you succeed in writing the programs for [these] complicated algorithms, they usually run extremely fast. The computer doesn t need to understand the algorithm, its tas is only to run the programs." R. E. Tarjan 46