data structures binomial heaps fibonacci heapsfibonacci heaps a list of heap-ordered trees 29 59 87...
TRANSCRIPT
Data Structures
Haim Kaplan & Uri Zwick
December 2013
Binomial Heaps
Fibonacci Heaps
1
Fibonacci
Heaps
Lazy
Binomial
Heaps
Binomial
Heaps
Binary
Heaps
O(1) O(1) O(log n) O(log n) Insert
O(1) O(1) O(1) O(1) Find-min
O(log n) O(log n) O(log n) O(log n) Delete-min
O(1) O(log n) O(log n) O(log n) Decrease-key
O(1) O(1) O(log n) – Meld
Heaps / Priority queues
Worst case Amortized
Delete can be implemented using Decrease-key + Delete-min
Decrease-key in O(1) time important for Dijkstra and Prim
Binomial Heaps [Vuillemin (1978)]
3
4
Binomial Trees
B0 B1 B2 B3
B4
5
Binomial Trees
B0 B1 B2 B3
B4
6
Binomial Trees
B0 B1 B2 B3
Bk
Bk−1
Bk−1 Bk−1 Bk−2
…
Bk
7
Binomial Trees
B0 Bk
Bk−1
Bk−1
Bk−1 Bk−2
…
Bk
8
Min-heap Ordered Binomial Trees
45
67
40
58
20
31
15
35 18
45
5
11
17
38
2
25
key of child key of parent
9
Tournaments Binomial Trees
G
H
F
E
A
B
D
C F D
D
B H C E D F
D G F A
A G
The children of x are the items that lost matches with x,
in the order in which the matches took place.
Binomial Heap
A list of binomial trees, at most one of each rank
45
67
40
58
20
31
15
35
9
33
23
Pointer to root with minimal key
Each number n can be written
in a unique way as a sum of
powers of 2
11 = (1011)2 = 8+2+1
At most
log2 (n+1) trees
11
Ordered forest Binary tree
45
67
40
58
20
31
15
35
9
33
23
next – next “sibling”
info key
child next
x
2 pointers per node
child – leftmost child
Forest Binary tree
45
67
40
58
20
31
15
35
9
33
23
40
45 20
67 58 31 35
9
15 33
23
Heap order
“half ordered”
Binomial heap representation
45
67
40
58
20
31
15
35
9
33
23
n first
2 pointers per node
No explicit rank information
Q
min
info key
child next
x
“Structure V”
[Brown (1978)]
How do we determine ranks?
Alternative representation
45
67
40
58
20
31
15
35
9
33
23
n first
Reverse sibling pointers
Make lists circular
Q
min Avoids reversals during meld
“Structure R”
[Brown (1978)]
info key
child next
15
Bk a
Bk−1
b
Bk−1
Linking binomial trees
a ≤ b
O(1) time
x
y
16
Linking in first
representation
Linking in second
representation
Linking binomial trees
Melding binomial heaps
Q1:
Q2:
Link trees of same degree
18
Q1: B0 B1 B3
Q2: B0 B2 B3
Melding binomial heaps Link trees of same degree
B1 B2 B3
B3 B4
Like adding binary numbers
O(log n) time
Maintain a pointer to the minimum
19
Insert
45
67
40
58
20
31
9
35
15
33
23
New item is a one tree binomial heap
Meld it to the original heap
O(log n) time
11
20
Delete-min
45
67
40
58
20
31
9
35
15
33
23
When we delete the
minimum, we get a
binomial heap
21
Delete-min
45
67
40
58
20
31
35
15
33
23
When we delete the
minimum, we get a
binomial heap
Meld it to the original heap
O(log n) time
(Need to reverse list of
roots in first representation)
22
Decrease-key using “sift-up”
45
67
40
58
20
31
15
35 18
45
5
11
17
38
2
25
I
Decrease-key(Q,I,7)
23
45
67
40
58
20
7
15
35 18
45
5
11
17
38
2
25
Decrease-key using “sift-up”
I
Need parent pointers
(not needed before)
24
45
67
40
58
7
20
15
35 18
45
5
11
17
38
2
25
Decrease-key using “sift-up”
I
Need to update the node pointed by I
25
45
67
40
58
7
20
15
35 18
45
5
11
17
38
2
25
Decrease-key using “sift-up”
I
Need to update the node pointed by I
45
67
40
58
15
20
7
35 18
45
5
11
17
38
2
25
Decrease-key using “sift-up”
I
How can we do it?
27
Adding parent pointers
45
67
40
58
20
31
15
35
9
33
23
n first
Q
min
item child next
x
parent
28
Adding a level of indirection
45
67
40
58
20
31
15
35
9
33
23
node
info key
I
Nodes and items
are distinct entities
n first
Q
min
“Endogenous vs. exogenous”
Heaps / Priority queues
Worst case Amortized
Fibonacci
Heaps
Lazy
Binomial
Heaps
Binomial
Heaps
Binary
Heaps
O(1) O(1) O(log n) O(log n) Insert
O(1) O(1) O(1) O(1) Find-min
O(log n) O(log n) O(log n) O(log n) Delete-min
O(1) O(log n) O(log n) O(log n) Decrease-key
O(1) O(1) O(log n) – Meld
Lazy Binomial Heaps
30
Binomial Heaps
A list of binomial trees,
at most one of each rank, sorted by rank
(at most O(log n) trees)
Pointer to root with minimal key
Lazy Binomial Heaps
An arbitrary list of binomial trees
(possibly n trees of size 1)
Pointer to root with minimal key
32
45
67
40
58
20
31
15
35
9
33
10
Pointer to root with minimal key
Lazy Binomial Heaps
An arbitrary list of binomial trees
29 59 87 19
20
33
Update the pointer to root with minimal key
Lazy Meld
Concatenate the two lists of trees
O(1) worst case time
Update the pointer to root with minimal key
Lazy Insert
Add the new item to the list of roots
O(1) worst case time
May need (n) time to find the new minimum
Lazy Delete-min ?
Remove the minimum root and meld ?
45
67
40
58
20
31
9
35
15
33
10 29 59 19
20
Consolidating / Successive Linking
45
67
40
58
20
31
35 15
33
10 29 59 19
20
0 1 2
…
3
45
67
40
58
20
31
35 15
33
10
29 59 19
20
0 1 2
…
3
Consolidating / Successive Linking
45
67
40
58
20
31
35 15
33
59 19
20
0 1 2
…
3
10
29
Consolidating / Successive Linking
45
67
40
58
20
31
35 59 19
20
0 1 2
…
3
15
33
10
29
Consolidating / Successive Linking
45
67
40
58
20
31
35
59
19
20
0 1 2
…
3
15
33
10
29
Consolidating / Successive Linking
20
31
35
59
19
20
0 1 2
…
3
45
67
40
58
15
33
10
29
Consolidating / Successive Linking
20
31
35
59
19
20
0 1 2
…
3
45
67
40
58
15
33
10
29
Consolidating / Successive Linking
20
31
19
20
0 1 2
…
3
45
67
40
58
15
33
10
29
35
59
Consolidating / Successive Linking
19
20
0 1 2
…
3
45
67
40
58
15
33
10
29
35
59
20
31
Consolidating / Successive Linking
19
20
0 1 2
…
3
45
67
40
58
15
33
10
29
35
59
20
31
At the end of the process, we obtain a non-lazy
binomial heap containing at most log (n+1) trees,
at most one of each rank
Consolidating / Successive Linking
At the end of the process, we obtain a non-lazy
binomial heap containing at most log n trees,
at most one of each degree
Worst case cost – O(n)
Amortized cost – O(log n)
Potential = Number of Trees
Consolidating / Successive Linking
Cost of Consolidating
T0 – Number of trees before
L – Number of links
T1 – Number of trees after
T1 = T0−L (Each link reduces the number of tree by 1)
Total number of trees processed – T0+L
(Each link creates a new tree)
Total cost = O( (T0+L) + L + log2n )
= O( T0+ log2n )
Putting trees into buckets
or finding trees to link with Linking
Handling
the buckets
As L ≤ T0
Amortized Cost of Consolidating
(Scaled) actual cost = T0 + log2n
Change in potential = = T1−T0
Amortized cost = (T0 + log2n ) + (T1−T0)
= T1 + log2n
≤ 2 log2n As T1 ≤ log2n
Another view: A link decreases the potential by 1.
This can pay for handling all the trees involved in the link.
The only “unaccounted” trees are those that
were not the input nor the output of a link operation.
Amortized
cost
Change in
potential Actual cost
O(1) 1 O(1) Insert
O(1) 0 O(1) Find-min
O(log n) k1+T1−T0 O(k+T0+log n) Delete-min
O(log n) 0 O(log n) Decrease-key
O(1) 0 O(1) Meld
Lazy Binomial Heaps
Rank of
deleted root
Heaps / Priority queues
Worst case Amortized
Fibonacci
Heaps
Lazy
Binomial
Heaps
Binomial
Heaps
Binary
Heaps
O(1) O(1) O(log n) O(log n) Insert
O(1) O(1) O(1) O(1) Find-min
O(log n) O(log n) O(log n) O(log n) Delete-min
O(1) O(log n) O(log n) O(log n) Decrease-key
O(1) O(1) O(log n) – Meld
One-pass successive linking
A tree produced by a link is immediately
put in the output list and not linked again
Worst case cost – O(n)
Amortized cost – O(log n)
Potential = Number of Trees
Exercise: Prove it!
45
67
40
58
20
31
35 15
33
10 29 59 19
20
0 1 2
… 3
One-pass successive Linking
45
67
40
58
20
31
35 15
33
10
29 59 19
20
0 1 2
… 3
One-pass successive Linking
45
67
40
58
20
31
35 15
33
59 19
20
0 1 2
… 3
One-pass successive Linking
10
29
Output list:
Fibonacci Heaps [Fredman-Tarjan (1987)]
54
55
Decrease-key in O(1) time?
45
67
40
58
20
31
15
35 18
45
5
11
17
38
2
25 x
Decrease-key(Q,x,30)
56
45
67
30
58
20
31
15
35 18
45
5
11
17
38
2
25 x
Decrease-key(Q,x,10)
Decrease-key in O(1) time?
57
45
67
10
58
20
31
15
35 18
45
5
11
17
38
2
25 x
Heap order violation
Decrease-key in O(1) time?
58
45
67
10
58
20
31
15
35 18
45
5
11
17
38
2
25 x
Cut the edge
Decrease-key in O(1) time?
59
45
67
10
58
20
31
15
35 18
45
5
11
17
38
2
25 x
Tree no longer binomial
Decrease-key in O(1) time?
60
40
58
20
31
15
35
9
33
10
Pointer to root with minimal key
Fibonacci Heaps
A list of heap-ordered trees
29 59 87 19
25
Not all trees may appear in Fibonacci heaps
22
61
Fibonacci heap representation
45
67
40
58
20
15
35
9
33
23
4 pointers + rank + mark bit per node
min
Q
0 1
3
2 0 0
1 0
0
0
Doubly linked lists of children
Arbitrary order of children
child points to an arbitrary child
Explicit ranks = degrees
62
Fibonacci heap representation
45
67
40
58
20
15
35
9
33
23
4 pointers + rank + mark bit per node
x
info
rank
child next prev
parent
mark
key
min
Q
0 1
3
2 0 0
1 0
0
0
63
Are simple cuts enough?
Ranks not necessarily O(log n)
A binomial tree of rank k
contains at least 2k
We may get trees of rank k
containing only k+1 nodes
Analysis breaks down
64
Cascading cuts
Invariant: Each node looses at most
one child after becoming a child itself
To maintain the invariant, use a mark bit
Each node is initially unmarked.
When a non-root node looses its first child,
it becomes marked
When a marked node looses a second child,
it is cut from its parent
65
Cascading cuts
Invariant: Each node looses at most
one child after becoming a child itself
When xy is cut:
x becomes unmarked
If y is unmarked, it becomes marked
If y is marked, it is cut from its parent
Our convention: Roots are unmarked
66
Cascading cuts
Cut x from its parent y
Perform a cascading-cut
process starting at x
67
Cascading cuts
…
…
…
68
Cascading cuts
… …
…
69
Cascading cuts
… …
…
70
Cascading cuts
… …
…
71
Cascading cuts
… …
…
72
Cascading cuts
… …
…
73
Cascading cuts
… …
…
74
Cascading cuts
… …
…
75
Cascading cuts
… …
76
Number of cuts
A decrease-key operation may trigger many cuts
Proof in a nutshell:
Number of times a second child is lost is at most
the number of times a first child is lost
Lemma 1: The first d decrease-key
operations trigger at most 2d cuts
Potential = Number of marked nodes
Number of cuts
Potential = Number of marked nodes
Amortized
number of cuts ≤ c + (1(c1)) = 2
78
Trees formed by cascading cuts
Lemma 2: Let x be a node of rank k and
let y1,y2,…,yk be the current children of x,
in the order in which they were linked to x.
Then, the rank of yi is at least i2.
Proof: When yi was linked to x,
y1,…yi1 were already children of x.
At that time, the rank of x and yi was at least i1.
As yi is still a child of x, it lost at most one child.
79
Lemma 3: A node of rank k in a Fibonacci Heap
has at least Fk+2 ≥ k descendants, including itself.
n 0 1 2 3 4 5 6 7 8 9
Fn 0 1 1 2 3 5 8 13 21 34
Trees formed by cascading cuts
80
Lemma 3: A node of rank k in a Fibonacci Heap
has at least Fk+2 ≥ k descendants, including itself.
Let Sk be the minimum
number of descendants of a
node of rank at least k
…
k
k3 k2
0 0 1
Trees formed by cascading cuts
81
Corollary: In a Fibonacci heap containing n items,
all ranks are at most logn ≤ 1.4404 log2n
Lemma 3: A node of rank k in a Fibonacci Heap
has at least Fk+2 ≥ k descendants, including itself.
Ranks are again O(log n)
Are we done?
Trees formed by cascading cuts
82
A cut increases the number of trees…
We need a potential function that gives good
amortized bounds on both successive linking
and cascading cuts
Putting it all together
Are we done?
Potential = #trees + 2 #marked
Cost of Consolidating
T0 – Number of trees before
L – Number of links
T1 – Number of trees after
T1 = T0−L (Each link reduces the number of tree by 1)
Total number of trees processed – T0+L
(Each link creates a new tree)
Total cost = O( (T0+L) + L + logn )
= O( T0+ logn )
Putting trees into buckets
or finding trees to link with Linking
Handling
the buckets
As L ≤ T0
Cost of Consolidating
T0 – Number of trees before
L – Number of links
T1 – Number of trees after
T1 = T0−L (Each link reduces the number of tree by 1)
Total number of trees processed – T0+L
(Each link creates a new tree)
Total cost = O( (T0+L) + L + logn )
= O( T0+ logn )
Only change:
logn instead of log2n
As L ≤ T0
Amortized
cost Marks Trees Actual cost
O(1) 0 1 O(1) Insert
O(1) 0 0 O(1) Find-min
O(log n) ≤ 0 k1+T1−T0 O(k+T0+log n) Delete-min
O(1) ≤ 2c c O(c) Decrease-
key
O(1) 0 0 O(1) Meld
Fibonacci heaps
Rank of
deleted root Number of cuts
performed
Heaps / Priority queues
Worst case Amortized
Fibonacci
Heaps
Lazy
Binomial
Heaps
Binomial
Heaps
Binary
Heaps
O(1) O(1) O(log n) O(log n) Insert
O(1) O(1) O(1) O(1) Find-min
O(log n) O(log n) O(log n) O(log n) Delete-min
O(1) O(log n) O(log n) O(log n) Decrease-key
O(1) O(1) O(log n) – Meld
Consolidating / Successive linking