Fibonacci Heap
Group Minus One Second December 6, 2016
Group Minus One Second Fibonacci Heap
Fibonacci Heap Group Minus One Second December 6, 2016 Group Minus - - PowerPoint PPT Presentation
Introduction Fibonacci Heap Time Complexity Analysis Fibonacci Heap Group Minus One Second December 6, 2016 Group Minus One Second Fibonacci Heap Introduction Fibonacci Heap Time Complexity Analysis Outline Introduction Motivation
Fibonacci Heap
Group Minus One Second December 6, 2016
Group Minus One Second Fibonacci HeapOutline
Introduction Motivation Performance Goal Fibonacci Heap Structure Rank Insert Delete Min Decrease Key Time Complexity Analysis Amortized Analysis Insert Complexity Delete Min Complexity Decrease Key Complexity
Group Minus One Second Fibonacci HeapMotivation
I Design a data structure that supports the following operations
I Insert(x) I DeleteMin() I DecreaseKey(u, v)12 5 7 3 15 2 9 21 30
Group Minus One Second Fibonacci HeapMotivation
I Design a data structure that supports the following operations
I Insert(x) I DeleteMin() I DecreaseKey(u, v)12 5 7 3 15 2 9 21 30 Insert(10) 10
Group Minus One Second Fibonacci HeapMotivation
I Design a data structure that supports the following operations
I Insert(x) I DeleteMin() I DecreaseKey(u, v)12 5 7 3 15 2 9 21 30 DeleteMin() 10
Group Minus One Second Fibonacci HeapMotivation
I Design a data structure that supports the following operations
I Insert(x) I DeleteMin() I DecreaseKey(u, v)12 5 7 3 15 9 21 30 DecreaseKey(21, 17) 10 17
Group Minus One Second Fibonacci HeapPerformance Goal
I Our performance goals of this data structure
I Insert(x): O(1) I DeleteMin(): O(log n) I DecreaseKey(u, v): O(1) Group Minus One Second Fibonacci HeapStructure
I Fibonacci heap is essentially a set of heap-ordered trees.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 roots heap-ordered tree
Group Minus One Second Fibonacci HeapRank
I The rank of a tree in a Fibonacci heap is the number of
children of the root.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 rank = 1 rank = 2 rank = 0 rank = 0 rank = 3
Group Minus One Second Fibonacci HeapInsert
I To insert a new value x into Fibonacci Heap H, simply create
a new tree that contains only x and add it to H.
I Example: Insert(21)
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapInsert
I To insert a new value x into Fibonacci Heap H, simply create
a new tree that contains only x and add it to H.
I Example: Insert(21)
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDelete Min
I The minimum must be one of the roots. I But there can be too many(Ω(n)) roots! I Example: DeleteMin() for the heap below
17 24 23 7 3 21 46
Group Minus One Second Fibonacci HeapDelete Min
I We introduce consolidation, a process of merging trees of the
same rank.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21 merge
Group Minus One Second Fibonacci HeapDelete Min
I We introduce consolidation, a process of merging trees of the
same rank.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21 merge
Group Minus One Second Fibonacci HeapDelete Min
I We introduce consolidation, a process of merging trees of the
same rank.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21 merge
Group Minus One Second Fibonacci HeapDelete Min
I We introduce consolidation, a process of merging trees of the
same rank.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21 merge
Group Minus One Second Fibonacci HeapDelete Min
I We introduce consolidation, a process of merging trees of the
same rank.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDelete Min
I After consolidation, we simply remove the minimum value and
leave the remaining trees in the heap.
17 24 30 26 46 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I Case 1: we can finish the operation immediately I Example: decreaseKey(46, 36)
17 24 30 26 46 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I Case 1: we can finish the operation immediately I Example: decreaseKey(46, 36)
17 24 30 26 36 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I Case 2: we have to cut the whole subtree rooted at u I Example: DecreaseKey(26, 16)
17 24 30 26 46 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I Case 2: we have to cut the whole subtree rooted at u I Example: DecreaseKey(26, 16)
17 24 30 16 46 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I Case 2: we have to cut the whole subtree rooted at u I Example: DecreaseKey(26, 16)
17 24 30 16 46 35 23 7 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDecrease Key
I But wait! Under this strategy, something undesirable might
happen.
I The example below shows that this strategy can hurt the
performance of DeleteMin operation.
I Example:
Group Minus One Second Fibonacci HeapDecrease Key with Marks
One node is marked if it has lost one of its children.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 1: no violation of heap property, finish.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 2: simply remove the subtree and mark the parent node.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 2: simply remove the subtree and mark the parent node.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 2: simply remove the subtree and mark the parent node.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 3: recursively remove all subtrees whose parent is marked.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 3: recursively remove all subtrees whose parent is marked.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 3: recursively remove all subtrees whose parent is marked.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 3: recursively remove all subtrees whose parent is marked.
Group Minus One Second Fibonacci HeapDecrease Key with Marks
Case 3: recursively remove all subtrees whose parent is marked.
Group Minus One Second Fibonacci HeapAmortized Analysis
I We need a new technique to analyze the time complexity of
Fibonacci heap
I Consider the following question:
I Drop: Dropping a single coin onto a table costs 1 unit of time I Clean: Collecting each coin into a bag costs 1 unit of time.Emptying the bag costs 1 unit of time.
I Question: What is the amortized time complexity of CleanAmortized Analysis
I Observation: A coin that is cleaned must have been dropped
before.
I So why not prepay the cost of clean of each coin as the cost
I Drop: Dropping a single coin onto a table costs 2 units of
time
I Clean: Collecting each coin into a bag costs no time.
Emptying the bag costs 1 unit of time.
I The amortized time complexity of Clean operation is O(1).
Group Minus One Second Fibonacci HeapInsert Complexity
I In addition to the cost of creating a tree($1), we also add a
merge credit (a prepaid constant cost,$1) to each new tree.
I Since no other operations are needed, each Insert operation
still takes $2 = O(1) time.
17 24 23 7 3 21 46 $1 $1 $1 $1 $1 $1 $1
Group Minus One Second Fibonacci HeapDelete Min Complexity
I Recall that DeleteMin opeartion consists of two parts:
I Consolidate the heap: for each rank i ∈ [0, k], merge twotrees of the same rank i.
I Linearly search for the minimum: traverse all k + 1 roots. I k is the largest rank in the heap.I We will make and prove a few claims.
Group Minus One Second Fibonacci HeapDelete Min Complexity
I Claim 1: Merge operations are free. I Proof: Their costs are paid by merge credits.
17 46 $1 $1 17 46 $1 The other $ 1 is usd to pay for the cost of merge
Group Minus One Second Fibonacci HeapDelete Min Complexity
I Claim 2: Consolidation operations are of O(k) where k is the
largest rank.
I Proof: For each rank i ∈ [0, k], consolidation calls merge to
generate new trees, which is free. So the only cost is the time consumed while travsering k + 1 ranks.
Group Minus One Second Fibonacci HeapDelete Min Complexity
I Claim 3: Searching for minimum is of O(k) where k is the
largest rank.
I Proof: After consolidation there are at most k + 1 roots. So
a linear traversal will take O(k) time.
17 24 30 26 46 35 23 7 3 18 52 41 44 39 21
Group Minus One Second Fibonacci HeapDelete Min Complexity
I Combining all three claims, we can see that the complexity of
DeleteMin is O(k) where k is the largest rank.
I One last question: why k = O(log n)?
I This is equivalent to ”what is the smallest number of nodes ina tree of rank i?”
Group Minus One Second Fibonacci HeapDelete Min Complexity
Some results for different ranks. Let Si be the smallest number of nodes in a tree of rank i. We can see that S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...
rank = 0
Group Minus One Second Fibonacci HeapDelete Min Complexity
Some results for different ranks. Let Si be the smallest number of nodes in a tree of rank i. We can see that S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...
rank = 1
Group Minus One Second Fibonacci HeapDelete Min Complexity
Some results for different ranks. Let Si be the smallest number of nodes in a tree of rank i. We can see that S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...
rank = 2
Group Minus One Second Fibonacci HeapDelete Min Complexity
Some results for different ranks. Let Si be the smallest number of nodes in a tree of rank i. We can see that S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...
rank = 3
Group Minus One Second Fibonacci HeapDelete Min Complexity
Some results for different ranks. Let Si be the smallest number of nodes in a tree of rank i. We can see that S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...
rank = 4
Group Minus One Second Fibonacci HeapDelete Min Complexity
I We can get a recurrence relation from the above observation:
Si = Si−1 + Si−2, S0 = 1, S1 = 2 which is exactly the Fibonacci sequence, and hence the heap’s name.
I Since Si ≥ ci where c = 1+ √ 5 2
, the number of nodes in each tree grows expoentially with the tree’s rank.
I Therefore, there can be at most O(log n) ranks in a Fibonacci
heap.
Group Minus One Second Fibonacci HeapDecrease Key Complexity
I By similar argument (using the amortized analysis), we can
prove that the time complexity of DecreaseKey(u, v) is O(1).
I We will omit this part due to time limit. I You can refer to the lecture note on the course website.
Group Minus One Second Fibonacci Heap