You are on page 1of 10

Amortized Analysis Potential Method

Introduce a potential for the data structure.


(Di ) is the potential after the i-th operation.
Definition 1 Amortized cost of the i-th operation is
A data structure with operations op1 (), . . . , opk () has amortized
running times t1 , . . . , tk for these operations if the following ci = ci + (Di ) (Di1 ) .
holds.

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

This means the amortized costs can be used to derive a bound


on the total cost.

8.3 Fibonacci Heaps


Harald Rcke 340 Harald Rcke 341

Example: Stack Example: Stack


Stack
S. push() Use potential function (S) = number of elements on the stack.
S. pop()
Amortized cost:
S. multipop(k): removes k items from the stack. If the S. push(): cost
stack currently contains less than k items it empties the
stack. Cpush = Cpush + = 1 + 1 2 .
Note that the analysis
The user has to ensure that pop and multipop do not becomes wrong if pop() or

generate an underflow. S. pop(): cost multipop() are called on an


empty stack.
Cpop = Cpop + = 1 1 0 .
Actual cost:
S. push(): cost 1. S. multipop(k): cost
S. pop(): cost 1.
Cmp = Cmp + = min{size, k} min{size, k} 0 .
S. multipop(k): cost min{size, k} = k.

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 342 Harald Rcke 343
Example: Binary Counter Example: Binary Counter
Choose potential function (x) = k, where k denotes the
Incrementing a binary counter:
number of ones in the binary representation of x.
Consider a computational model where each bit-operation costs
one time-unit.
Amortized cost:

Incrementing an n-bit binary counter may require to examine


Changing bit from 0 to 1:
n-bits, and maybe change them.
C01 = C01 + = 1 + 1 2 .
Actual cost:
Changing bit from 0 to 1: cost 1.
Changing bit from 1 to 0:

Changing bit from 1 to 0: cost 1. C10 = C10 + = 1 1 0 .


Increment: cost is k + 1, where k is the number of
consecutive ones in the least significant bit-positions (e.g, Increment: Let k denotes the number of consecutive ones in
001101 has k = 1). the least significant bit-positions. An increment involves k
(1 0)-operations, and one (0 1)-operation.

8.3 Fibonacci Heaps Hence, the amortized cost is kC10 + C01 2.


Harald Rcke 344

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

Collection of trees that fulfill the heap property.

Structure is much more relaxed than binomial heaps.


Additional implementation details:
Every node x stores its degree in a field x. degree. Note that
min this can be updated in constant time when adding a child to
x.
7 3 23 24 17

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 346 Harald Rcke 347
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
The potential function:
t(S) denotes the number of trees in the heap.
m(S) denotes the number of marked nodes.
We use the potential function (S) = t(S) + 2m(S). We assume that one unit of potential can pay for a constant
amount of work, where the constant is chosen big enough (to
take care of the constants that occur).
min

To make this more explicit we use c to denote the amount of


7 3 23 24 17
work that a unit of potential can pay for.
18 41 52 26 46 30

39 44 35

The potential is (S) = 5 + 2 3 = 11.

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 348 Harald Rcke 349

In the figure below the dashed edges are


8.3 Fibonacci Heaps 8.3 Fibonacci Heaps replaced by red edges.
The minimum of the left heap becomes
S. merge(S 0 ) the new minimum of the merged heap.
S. minimum() Merge the root lists.
Access through the min-pointer. Adjust the min-pointer
Actual cost O(1).
min min
No change in potential.
7 3 23 24 17 5
Amortized cost O(1).
18 41 52 26 46 30 11

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).

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 352 Harald Rcke 353

D(min) is the number of


8.3 Fibonacci Heaps children of the node that 8.3 Fibonacci Heaps
stores the minimum.

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).

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 353 Harald Rcke 354
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 354 Harald Rcke 354

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 354 Harald Rcke 354
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 354 Harald Rcke 354

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 354 Harald Rcke 354
t and t 0 denote the number of trees before and
8.3 Fibonacci Heaps after the delete-min() operation, respectively. 8.3 Fibonacci Heaps
Dn is an upper bound on the degree (i.e., num-
ber of children) of a tree node.
Actual cost for delete-min()
At most Dn + t elements in root-list before consolidate.
Actual cost for a delete-min is at most O(1) (Dn + t). If the input trees of the consolidation procedure are binomial
Hence, there exists c1 s.t. actual cost is at most c1 (Dn + t). trees (for example only singleton vertices) then the output will
be a set of distinct binomial trees, and, hence, the Fibonacci
Amortized cost for delete-min() heap will be (more or less) a Binomial heap right after the
t 0 Dn + 1 as degrees are different after consolidating. consolidation.
Therefore Dn + 1 t;
If we do not have delete or decrease-key operations then
We can pay c (t Dn 1) from the potential decrease.
Dn log n.
The amortized cost is
c1 (Dn + t) c (t Dn 1)
(c1 + c)Dn + (c1 c)t + c 2c(Dn + 1) O(Dn )
for c c1 .

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 355 Harald Rcke 356

Fibonacci Heaps: decrease-key(handle h, v) Fibonacci Heaps: decrease-key(handle h, v)

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

Fibonacci Heaps: decrease-key(handle h, v) Fibonacci Heaps: decrease-key(handle h, v)

min

24 26 4 19 7 18 38 Case 3: heap-property is violated, and parent is marked

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.

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 359 Harald Rcke 360

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps

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.

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 361 Harald Rcke 362
8.3 Fibonacci Heaps 8.3 Fibonacci Heaps
Let sk be the minimum possible size of a sub-tree rooted at
Definition 3
a node of degree k that can occur in a Fibonacci heap.
Consider the following non-standard Fibonacci type sequence:
sk monotonically increases with k

s0 = 1 and s1 = 2. 1

if k = 0
Fk = 2 if k = 1
Let x be a degree k node of size sk and let y1 , . . . , yk be its
F F
+ k2 if k 2

children. k1

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

8.3 Fibonacci Heaps 8.3 Fibonacci Heaps


Harald Rcke 363 Harald Rcke 364

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

8.3 Fibonacci Heaps


Harald Rcke 365

You might also like