Fibonacci Heaps Lecture slides adapted from: • Chapter 20 of Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein. • Chapter 9 of The Design and Analysis of Algorithms by Dexter Koze COS 423 Theory of Algorithms • Kevin Wayne • Spring 2007 Adapted by Cheng Li and Virgil Pavlu
Fibonacci Heaps History. [Fredman and Tarjan, 1986] V insert, V extract-min, E decrease-key Ingenious data structure and analysis. Original motivation: improve Dijkstra's shortest path algorithm (module 12) from to Basic idea. Similar to binomial heaps, but less rigid structure. Binomial heap: eagerly consolidate trees after each insert. Fibonacci heap: lazily defer consolidation until next extract-min. 2
Fibonacci Heaps: Structure each parent smaller than its children Fibonacci heap. Set of heap-ordered trees. Maintain pointer to minimum element. Set of marked nodes. roots heap-ordered tree 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39 3
Fibonacci Heaps: Structure Fibonacci heap. Set of heap-ordered trees. Maintain pointer to minimum element. Set of marked nodes. find-min takes O(1) time min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39 4
Fibonacci Heaps: Structure Fibonacci heap. Set of heap-ordered trees. Maintain pointer to minimum element. Set of marked nodes. use to keep heaps flat (stay tuned) min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39 5
Fibonacci Heaps: Notation Notation. = number of nodes in heap. degree(x) = number of children of node x. = upper bound on the maximum degree of any node. In fact, . The proof (omitted) uses Fibonacci numbers. = number of trees in heap H. = number of marked nodes in heap H. min t(H) = 5 m(H) = 3 n = 14 degree = 3 17 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39 6
Fibonacci Heaps: Potential Function potential of heap H : min t(H) = 5 m(H) = 3 Φ (H) = 5 + 2 ⋅ 3 = 11 17 24 23 7 3 Heap H 30 26 46 18 52 41 35 marked 44 39 This lecture is not a complete treatment of Fibonacci heaps; in order to implement (code) and use them, more details are necessary (see book). Our main purpose here is to understand how the potential function works. Next: analyze change in potential and amortized costs for heaps operations: • Insert (easy, required) • Extract min (medium, required) • Decrease Key (difficult, optional) • Union (easy, required) • Delete (medium, required) 7
Insert 8
Fibonacci Heaps: Insert 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 35 Heap H 44 39 9
Fibonacci Heaps: Insert 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 35 Heap H 44 39 10
Fibonacci Heaps: Insert Analysis Actual cost. H’ = the heap after insert potential of heap H Change in potential. Amortized cost. min 17 24 23 7 21 3 30 26 46 18 52 41 35 Heap H 44 39 11
Extract-Min 12
Linking Operation Linking operation. Make larger root be a child of smaller root. larger root still heap-ordered smaller root 3 3 15 18 52 41 15 18 52 41 56 24 39 44 39 44 77 56 24 tree T 1 tree T 2 77 tree T' 13
Fibonacci Heaps: Extract-Min Extract-min. Delete min; meld its children into root list; update min. 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 14
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. min 7 24 23 17 18 52 41 39 44 30 26 46 35 15
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. min current 7 24 23 17 18 52 41 39 44 30 26 46 35 16
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 min current 7 24 23 17 18 52 41 39 44 30 26 46 35 17
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 min current 7 24 23 17 18 52 41 39 44 30 26 46 35 18
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 min 7 24 23 17 18 52 41 39 44 current 30 26 46 35 19
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 min 7 24 23 17 18 52 41 current 39 44 30 26 46 35 link 23 into 17 20
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 min 7 24 17 18 52 41 current 23 39 44 30 26 46 35 link 17 into 7 21
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 24 7 18 52 41 17 30 39 44 26 46 35 23 link 24 into 7 22
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 18 52 41 24 17 30 39 44 26 46 23 35 23
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 18 52 41 24 17 30 39 44 26 46 23 35 24
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 18 52 41 24 17 30 39 44 26 46 23 35 25
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 18 52 41 24 17 30 39 44 26 46 23 link 41 into 18 35 26
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 52 18 41 24 17 30 39 26 46 23 44 35 27
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. degree 0 1 2 3 current min 7 52 18 41 24 17 30 39 26 46 23 44 35 28
Fibonacci Heaps: Extract-Min Extract-Min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same degree. min 7 52 18 41 24 17 30 39 26 46 23 44 stop 35 29
Fibonacci Heaps: Extract-Min Analysis Extract-Min. potential function Actual cost. to meld min's children into root list. (at most children of min) to update min.(the size of the root list is at most ) to consolidate trees.(one of the roots is linked to another in each merging, and thus the total number of iterations is at most the number of roots in the root list.) Change in potential: (at most roots with distinct degrees remain and no nodes become marked during the operation) Amortized cost: 30
Decrease Key 31
Fibonacci Heaps: Decrease Key Intuition for deceasing the key of node x. If heap-order is not violated, just decrease the key of x. Otherwise, cut tree rooted at x and meld into root list. To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it). min 7 18 38 marked node: one child already cut 24 17 23 21 39 41 26 46 30 52 35 88 72 32
Fibonacci Heaps: Decrease Key Case 1. [heap order not violated] Decrease key of x. Change heap min pointer (if necessary). min 7 18 38 24 17 23 21 39 41 26 46 29 30 52 x decrease-key of x from 46 to 29 35 88 72 33
Fibonacci Heaps: Decrease Key Case 1. [heap order not violated] Decrease key of x. Change heap min pointer (if necessary). min 7 18 38 24 17 23 21 39 41 26 29 30 52 x decrease-key of x from 46 to 29 35 88 72 34
Recommend
More recommend