Professional Documents
Culture Documents
Suppose you are given a sequence of operations (starting with Show that (Di ) (D0 ).
an empty data-structure) that operate on at most n elements,
and let ki denote the number of occurences of opi () within this Then
k k k
sequence. Then the actual running time must be at most
X X X
P ci ci + (Dk ) (D0 ) = ci
i ki ti (n). i=1 i=1 i=1
18 41 52 26 46 30
Every node stores a boolean value x. marked that specifies
whether x is marked or not.
39 44 35
39 44 35
39 44 35
Running time:
Actual cost O(1).
No change in potential.
Hence, amortized cost is O(1).
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
Harald Rcke 350 Harald Rcke 351
x is inserted next to the min-pointer as D(min) is the number of
8.3 Fibonacci Heaps this is our entry point into the root-list. 8.3 Fibonacci Heaps children of the node that
stores the minimum.
S. insert(x)
Create a new tree containing x. S. delete-min(x)
Insert x into the root-list. Delete minimum; add child-trees to heap;
Update min-pointer, if necessary. time: D(min) O(1).
Update min-pointer; time: (t + D(min)) O(1).
min
7 3 x 23 24 17
min
18 41 52 26 46 30
7 3 23 24 17
39 44 35
18 41 52 26 46 30
Running time: 39 44 35
Actual cost O(1).
Change in potential is +1.
Amortized cost is c + O(1) = O(1).
S. delete-min(x)
Delete minimum; add child-trees to heap; Consolidate:
time: D(min) O(1). 0 1 2 3
Update min-pointer; time: (t + D(min)) O(1).
current
min x
7 x
18 x
41 x
52 23 24 17
7 18 41 52 23 24 17 39 44 26 46 30
39 44 26 46 30 35
min
35
During the consolidation we traverse the root list. Whenever we discover two
trees that have the same degree we merge these trees. In order to efficiently
Consolidate root-list so that no roots have the same degree. check whether two trees have the same degree, we use an array that contains
for every degree value d a pointer to a tree left of the current pointer whose root
Time t O(1) (see next slide). has degree d (if such a tree exist).
Consolidate: Consolidate:
0 1 2 3 0 1 2 3
current current
min x
7 x
18 x
41 x
52 23 24 17 min x
7 x x
18 x
52 23 24 17
39 44 26 46 30 41 39 26 46 30
35 44 35
Consolidate: Consolidate:
0 1 2 3 0 1 2 3
current current
min x
7 x x
18 x
52 23 24 17 min x
7 x
18 x 23 24 17
41 39 26 46 30 52 41 39 26 46 30
44 35 44 35
Consolidate: Consolidate:
0 1 2 3 0 1 2 3
current current
min x
7 x
18 x x
23 x
24 17 min x
7 x x
18 x
23 x 17
52 41 39 26 46 30 52 24 41 39 30
44 35 26 46 44
35
Consolidate: Consolidate:
0 1 2 3 0 1 2 3
current
min x
7 x x
18 x
23 x
17 min x
7 x
18 x
23 x
52 24 41 39 30 17 52 24 41 39
26 46 44 30 26 46 44
35 35
min min
7 18 38 7 18 38
24 17
12 23 21 39 41 24 17
12 23 21 39 41
26 45 30 52 26 45
19 30 52
35 74 72 35 74 72
40 40
Case 1: decrease-key does not violate heap-property Case 2: heap-property is violated, but parent is not marked
Just decrease the key-value of element referenced by h. Decrease key-value of element x reference by h.
Nothing else to do. If the heap-property is violated, cut the parent edge of x,
and make x into a root.
Adjust min-pointers, if necessary.
Mark the (previous) parent of x (unless its a root).
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
Harald Rcke 357 Harald Rcke 357
Fibonacci Heaps: decrease-key(handle h, v) Fibonacci Heaps: decrease-key(handle h, v)
min min
19 7 18 38 19 7 18 38
72 24 17
12 23 21 39 41 72 24 17
12 23 21 39 41
26 45 30 52 26 45 30 52
35 74 72 35
4 74 72
40 40
Case 2: heap-property is violated, but parent is not marked Case 3: heap-property is violated, and parent is marked
Decrease key-value of element x reference by h. Decrease key-value of element x reference by h.
If the heap-property is violated, cut the parent edge of x, Cut the parent edge of x, and make x into a root.
and make x into a root. Adjust min-pointers, if necessary.
Adjust min-pointers, if necessary. Continue cutting the parent until you arrive at an unmarked
Mark the (previous) parent of x (unless its a root). node.
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
Harald Rcke 357 Harald Rcke 357
min
74 40 72 24 17
12 23 21 39 41
Decrease key-value of element x reference by h.
Cut the parent edge of x, and make x into a root.
26 45 30 52
Adjust min-pointers, if necessary. Marking a node can be viewed as a
35 74 72 first step towards becoming a
Execute the following: root. The first time x loses a child
40 p parent[x]; it is marked; the second time it
loses a child it is made into a root.
Case 3: heap-property is violated, and parent is marked while (p is marked)
Decrease key-value of element x reference by h. pp parent[p];
cut of p; make it into a root; unmark it;
Cut the parent edge of x, and make x into a root.
p pp;
Adjust min-pointers, if necessary.
if p is unmarked and not a root mark it;
Continue cutting the parent until you arrive at an unmarked
node.
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
Harald Rcke 357 Harald Rcke 358
Fibonacci Heaps: decrease-key(handle h, v) Delete node
Actual cost:
Constant cost for decreasing the value.
H. delete(x):
Constant cost for each of ` cuts.
decrease value of x to .
Hence, cost is at most c2 (` + 1), for some constant c2 .
delete-min.
Amortized cost:
t 0 = t + `, as every cut creates one new root. Amortized cost: O(Dn )
m0 m (` 1) + 1 = m ` + 2, since all but the first cut O(1) for decrease-key.
unmarks a node; the last cut may mark a node.
O(Dn ) for delete-min.
` + 2(` + 2) = 4 ` t andt0 :number of
trees before and after
Amortized cost is at most
operation.
c2 (`+1)+c(4`) (c2 c)`+4c +c2 = O(1) , m and m0 : number of
marked nodes before
if c c2 . and after operation.
Proof
Lemma 2
When yi was linked to x, at least y1 , . . . , yi1 were already
Let x be a node with degree k and let y1 , . . . , yk denote the
linked to x.
children of x in the order that they were linked to x. Then
(
Hence, at this time degree(x) i 1, and therefore also
0 if i = 1 degree(yi ) i 1 as the algorithm links nodes of equal
degree(yi )
i2 if i > 1 degree only.
Since, then yi has lost at most one child.
Therefore, degree(yi ) i 2.
The marking process is very important for the proof of
this lemma. It ensures that a node can have lost at most
one child since the last time it became a non-root node.
When losing a first child the node gets marked; when
losing the second child it is cut from the parent and
made into a root.
k
Facts:
X
sk = 2 + size(yi )
i=2
1. Fk k .
k Pk2
2. For k 2: Fk = 2 + Fi .
X
2+ si2 i=0
i=2 The above facts can be easily proved by induction. From this it
k2
X follows that sk Fk k , which gives that the maximum degree
=2+ si
in a Fibonacci heap is logarithmic.
i=0
k=0: 1 = F0 0 = 1
k=1: 2 = F1 1 1.61 2
z }| {
k-2,k-1 k: Fk = Fk1 + Fk2 k1 + k2 = k2 ( + 1) = k
k=2: 3 = F2 = 2 + 1 = 2 + F0
Pk3 Pk2
k-1 k: Fk = Fk1 + Fk2 = 2 + i=0 Fi + Fk2 = 2 + i=0 Fi