fibonacci heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · fibonacci heaps...
TRANSCRIPT
![Page 1: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/1.jpg)
COS 423 Theory of Algorithms • Kevin Wayne • Spring 2007
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 Kozen.
![Page 2: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/2.jpg)
�2
Theorem. Starting from empty Fibonacci heap, any sequence ofa1 insert, a2 delete-min, and a3 decrease-key operations takesO(a1 + a2 log n + a3) time.
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
BinaryHeap
log n
1
log n
n
log n
log n
1
Binomial Heap
log n
log n
log n
log n
log n
log n
1
Fibonacci Heap †
1
1
log n
1
1
log n
1
Relaxed Heap
1
1
log n
1
1
log n
1
Linked List
1
n
n
1
n
n
is-empty 1 1 1 11
Priority Queues Performance Cost Summary
† amortizedn = number of elements in priority queue
![Page 3: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/3.jpg)
�3
Hopeless challenge. O(1) insert, delete-min and decrease-key. Why?
Priority Queues Performance Cost Summary
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
BinaryHeap
log n
1
log n
n
log n
log n
1
Binomial Heap
log n
log n
log n
log n
log n
log n
1
Fibonacci Heap †
1
1
log n
1
1
log n
1
Relaxed Heap
1
1
log n
1
1
log n
1
Linked List
1
n
n
1
n
n
is-empty 1 1 1 11
† amortizedn = number of elements in priority queue
![Page 4: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/4.jpg)
�4
Fibonacci Heaps
History. [Fredman and Tarjan, 1986]� Ingenious data structure and analysis.� Original motivation: improve Dijkstra's shortest path algorithm
from O(E log V ) to O(E + V log V ).
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 delete-min.
V insert, V delete-min, E decrease-key
![Page 5: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/5.jpg)
�5
Fibonacci Heap Structure
![Page 6: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/6.jpg)
�6
723
30
17
35
26 46
24
Heap H39
4118 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.� Set of heap-ordered trees.� Maintain pointer to minimum element.� Set of marked nodes.
roots heap-ordered tree
each parent larger than its children
![Page 7: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/7.jpg)
�7
723
30
17
35
26 46
24
Heap H39
4118 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.� Set of heap-ordered trees.� Maintain pointer to minimum element.� Set of marked nodes.
min
find-min takes O(1) time
![Page 8: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/8.jpg)
�8
723
30
17
35
26 46
24
Heap H39
4118 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.� Set of heap-ordered trees.� Maintain pointer to minimum element.� Set of marked nodes.
min
use to keep heaps flat (stay tuned)
marked
![Page 9: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/9.jpg)
�9
Fibonacci Heaps: Structure
Fibonacci heap.� Set of heap-ordered trees.� Maintain pointer to minimum element.� Set of marked nodes.� Doubly linked list between children of any node
![Page 10: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/10.jpg)
�10
Fibonacci Heaps: Notation
Notation.� n = number of nodes in heap.� 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.
723
30
17
35
26 46
24
39
4118 52
3
44
rank = 3 min
Heap H
trees(H) = 5 marks(H) = 3
marked
n = 14
![Page 11: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/11.jpg)
�11
Fibonacci Heaps: Potential Function
723
30
17
35
26 46
24
Φ(H) = 5 + 2⋅3 = 11
39
4118 52
3
44
min
Heap H
Φ(H) = trees(H) + 2 ⋅ marks(H)potential of heap H
trees(H) = 5 marks(H) = 3
marked
![Page 12: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/12.jpg)
�12
Insert
![Page 13: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/13.jpg)
�13
Fibonacci Heaps: Insert
Insert.� Create a new singleton tree.� Add to root list; update min pointer (if necessary).
723
30
17
35
26 46
24
39
4118 52
3
44
21
insert 21
min
Heap H
![Page 14: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/14.jpg)
�14
Fibonacci Heaps: Insert
Insert.� Create a new singleton tree.� Add to root list; update min pointer (if necessary).
39
41
723
18 52
3
30
17
35
26 46
24
44
21
min
Heap H
insert 21
![Page 15: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/15.jpg)
�15
Fibonacci Heaps: Insert Analysis
Actual cost. O(1)
Change in potential. +1
Amortized cost. O(1)
39
41
7
18 52
3
30
17
35
26 46
24
44
2123
min
Heap H
Φ(H) = trees(H) + 2 ⋅ marks(H)potential of heap H
![Page 16: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/16.jpg)
�16
Code
![Page 17: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/17.jpg)
�17
Heap Union
![Page 18: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/18.jpg)
�18
Delete Min
![Page 19: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/19.jpg)
�19
Linking Operation
Linking operation. Make larger root be a child of smaller root.
39
4118 52
3
4477
56 24
15
tree T1 tree T2
39
4118 52
3
44
77
56 24
15
tree T'
smaller rootlarger root still heap-ordered
![Page 20: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/20.jpg)
�20
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
4118 52
3
44
1723
30
7
35
26 46
24
min
![Page 21: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/21.jpg)
�21
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
min
![Page 22: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/22.jpg)
�22
Delete Min Code
![Page 23: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/23.jpg)
�23
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
mincurrent
![Page 24: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/24.jpg)
�24
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
0 1 2 3
currentmin
rank
![Page 25: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/25.jpg)
�25
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
0 1 2 3
mincurrent
rank
![Page 26: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/26.jpg)
�26
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
![Page 27: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/27.jpg)
�27
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
411723 18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
link 23 into 17
![Page 28: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/28.jpg)
�28
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
4117
23
18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
link 17 into 7
![Page 29: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/29.jpg)
�29
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
417
30
18 52
17
35
26 46
24
44
0 1 2 3
23
current
min
rank
link 24 into 7
![Page 30: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/30.jpg)
�30
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
417
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
![Page 31: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/31.jpg)
�31
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
417
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
![Page 32: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/32.jpg)
�32
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
417
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
![Page 33: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/33.jpg)
�33
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
39
417
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
link 41 into 18
![Page 34: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/34.jpg)
�34
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
3941
7
30
1852
23
17
35
26 46
24
44
0 1 2 3
min
current
rank
![Page 35: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/35.jpg)
�35
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
7
30
52
23
17
35
26 46
24
0 1 2 3
min
rank
3941
18
44
current
![Page 36: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/36.jpg)
�36
Fibonacci Heaps: Delete Min
Delete min.� Delete min; meld its children into root list; update min.� Consolidate trees so that no two roots have same rank.
7
30
52
23
17
35
26 46
24
min
3941
18
44
stop
![Page 37: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/37.jpg)
�37
![Page 38: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/38.jpg)
�38
Fibonacci Heaps: Delete Min Analysis
Delete min.
Actual cost. O(rank(H)) + O(trees(H)) � O(rank(H)) to meld min's children into root list.� O(rank(H)) + O(trees(H)) to update min.� O(rank(H)) + O(trees(H)) to consolidate trees.
Change in potential. O(rank(H)) - trees(H)� trees(H' ) ≤ rank(H) + 1 since no two trees have same rank.� ΔΦ(H) ≤ rank(H) + 1 - trees(H).
Amortized cost. O(rank(H))
Φ(H) = trees(H) + 2 ⋅ marks(H)potential function
![Page 39: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/39.jpg)
�39
Q. Is amortized cost of O(rank(H)) good?
A. Yes, if only insert and delete-min operations.� In this case, all trees are binomial trees.� This implies rank(H) ≤ lg n.
A. Yes, we'll implement decrease-key so that rank(H) = O(log n).
Fibonacci Heaps: Delete Min Analysis
B0 B1 B2 B3
we only link trees of equal rank
![Page 40: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/40.jpg)
�40
Decrease Key
![Page 41: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/41.jpg)
�41
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).
24
46
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
marked node:one child already cut
![Page 42: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/42.jpg)
�42
Case 1. [heap order not violated]� Decrease key of x.� Change heap min pointer (if necessary).
24
46
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
29
35
min
x
decrease-key of x from 46 to 29
![Page 43: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/43.jpg)
�43
Case 1. [heap order not violated]� Decrease key of x.� Change heap min pointer (if necessary).
24
29
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
x
decrease-key of x from 46 to 29
![Page 44: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/44.jpg)
�44
Case 2a. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
29
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
15
35
min
decrease-key of x from 29 to 15
p
x
![Page 45: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/45.jpg)
�45
Case 2a. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
x
![Page 46: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/46.jpg)
�46
Case 2a. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
88
26
21
52
39
18
41
38
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
15
72
x
![Page 47: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/47.jpg)
�47
Case 2a. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
88
26
21
52
39
18
41
38
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
15
72
x
mark parent
24
![Page 48: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/48.jpg)
�48
35
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72 24
Fibonacci Heaps: Decrease Key
5
min
x
p
decrease-key of x from 35 to 5
![Page 49: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/49.jpg)
�49
5
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72 24
Fibonacci Heaps: Decrease Key
min
x
p
decrease-key of x from 35 to 5
![Page 50: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/50.jpg)
�50
Fibonacci Heaps: Decrease Key
24 17
30
23
7
26
21
52
39
18
41
38
24
5
88
15
72
decrease-key of x from 35 to 5
x
p
min
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
![Page 51: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/51.jpg)
�51
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
26
21
52
39
18
41
38
24
5
Fibonacci Heaps: Decrease Key
88
15
72
decrease-key of x from 35 to 5
x
p
second child cut
min
![Page 52: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/52.jpg)
�52
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
26
17
30
23
7
21
52
39
18
41
38
88 24
5
Fibonacci Heaps: Decrease Key
15
72
decrease-key of x from 35 to 5
x pmin
![Page 53: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/53.jpg)
�53
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
24
26
17
30
23
7
21
52
39
18
41
38
88 24
5
Fibonacci Heaps: Decrease Key
15
72
decrease-key of x from 35 to 5
x p
p'
second child cut
min
![Page 54: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/54.jpg)
�54
Case 2b. [heap order violated]� Decrease key of x.� Cut tree rooted at x, meld into root list, and unmark.� If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark(and do so recursively for all ancestors that lose a second child).
26
17
30
23
7
21
52
39
18
41
38
88
5
Fibonacci Heaps: Decrease Key
15 24
72
decrease-key of x from 35 to 5
x p p'min
don't markparent ifit's a root
p''
![Page 55: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/55.jpg)
�55
Decrease-key.
Actual cost. O(c)� O(1) time for changing the key.� O(1) time for each of c cuts, plus melding into root list.
Change in potential. O(1) - c� trees(H') = trees(H) + c.� marks(H') ≤ marks(H) - c + 2.� ΔΦ ≤ c + 2 ⋅ (-c + 2) = 4 - c.
Amortized cost. O(1)
Fibonacci Heaps: Decrease Key Analysis
Φ(H) = trees(H) + 2 ⋅ marks(H)potential function
![Page 56: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/56.jpg)
�56
![Page 57: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/57.jpg)
�57
Analysis Max Degree of Nodes
![Page 58: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/58.jpg)
�58
Analysis Summary
Insert. O(1)Delete-min. O(rank(H)) †Decrease-key. O(1) †
Key lemma. rank(H) = O(log n).
† amortized
number of nodes is exponential in rank
![Page 59: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/59.jpg)
�59
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote its children in the order in which they were linked to x. Then:
Pf. � When yi was linked into x, x had at least i -1 children y1, …, yi-1.� Since only trees of equal rank are linked, at that time
rank(yi) = rank(xi) ≥ i - 1.� Since then, yi has lost at most one child.� Thus, right now rank(yi) ≥ i - 2.
or yi would have been cut
x
y1 y2 yk…
![Page 60: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/60.jpg)
�60
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote its children in the order in which they were linked to x. Then:
Def. Let Fk be smallest possible tree of rank k satisfying property.
F0 F1 F2 F3 F4 F5
1 2 3 5 8 13
x
y1 y2 yk…
![Page 61: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/61.jpg)
�61
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote its children in the order in which they were linked to x. Then:
Def. Let Fk be smallest possible tree of rank k satisfying property.
F4 F5
8 13
F6
8 + 13 = 21
x
y1 y2 yk…
![Page 62: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/62.jpg)
�62
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote its children in the order in which they were linked to x. Then:
Def. Let Fk be smallest possible tree of rank k satisfying property.
Fibonacci fact. Fk ≥ φk, where φ = (1 + √5) / 2 ≈ 1.618.
Corollary. rank(H) ≤ logφ n .golden ratio
x
y1 y2 yk…
![Page 63: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/63.jpg)
�63
Fibonacci Numbers
![Page 64: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/64.jpg)
�64
Fibonacci Numbers: Exponential Growth
Def. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21, …
Lemma. Fk ≥ φk, where φ = (1 + √5) / 2 ≈ 1.618.
Pf. [by induction on k]� Base cases: F0 = 1 ≥ 1, F1 = 2 ≥ φ.� Inductive hypotheses: Fk ≥ φk and Fk+1 ≥ φk + 1
slightly non-standard definition
(φ2 = φ + 1)
(inductive hypothesis)
(definition)
(algebra)
(algebra)
Fn =1 n = 1,2 n = 2,Fn−1 + Fn−2 o . w
![Page 65: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/65.jpg)
�65
Fibonacci Numbers and Nature
pinecone
cauliflower
![Page 66: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/66.jpg)
�66
Union
![Page 67: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/67.jpg)
�67
Fibonacci Heaps: Union
Union. Combine two Fibonacci heaps.
Representation. Root lists are circular, doubly linked lists.
39
41
717
18 52
3
30
23
35
26 46
24
44
21
min min
Heap H' Heap H''
![Page 68: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/68.jpg)
�68
Fibonacci Heaps: Union
Union. Combine two Fibonacci heaps.
Representation. Root lists are circular, doubly linked lists.
39
41
717
18 52
3
30
23
35
26 46
24
44
21
min
Heap H
![Page 69: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/69.jpg)
�69
Fibonacci Heaps: Union
Actual cost. O(1)
Change in potential. 0
Amortized cost. O(1)
Φ(H) = trees(H) + 2 ⋅ marks(H)potential function
39
41
717
18 52
3
30
23
35
26 46
24
44
21
min
Heap H
![Page 70: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/70.jpg)
�70
Delete
![Page 71: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/71.jpg)
�71
Delete node x.� decrease-key of x to -∞.� delete-min element in heap.
Amortized cost. O(rank(H))� O(1) amortized for decrease-key.� O(rank(H)) amortized for delete-min.
Fibonacci Heaps: Delete
Φ(H) = trees(H) + 2 ⋅ marks(H)potential function
![Page 72: Fibonacci Heapsforoughmand.ir/wp-content/uploads/courses/971/ds-971/fib-heap.pdf · Fibonacci Heaps History. [Fredman and Tarjan, 1986] Ingenious data structure and analysis. Original](https://reader034.vdocuments.us/reader034/viewer/2022042417/5f3267d9969e2b02050fd0a9/html5/thumbnails/72.jpg)
�72
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
BinaryHeap
log n
1
log n
n
log n
log n
1
Binomial Heap
log n
log n
log n
log n
log n
log n
1
Fibonacci Heap †
1
1
log n
1
1
log n
1
Relaxed Heap
1
1
log n
1
1
log n
1
Linked List
1
n
n
1
n
n
is-empty 1 1 1 11
Priority Queues Performance Cost Summary
† amortizedn = number of elements in priority queue