




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Amortized complexity calculation
Typology: Lecture notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Amortized Complexity Example
“saving up for later use” when they occasionally become expensive.charging more than necessary when they are cheap — therebytimes. The main ideas is to spread out the cost of operations,Amortized complexity analysis is a different way to estimate run(iii) probabilistic arguments can be difficult.is “average data”, (ii) uniformly random data is usually not average,Average case analysis may be difficult because (i) it is not clear what Worst case analysis of run time complexity is often too pessimistic.
Amortized Complexity Example
elementary operations We choose a set (typically a singleton, but always a small set) of
whose cost can be set to a constant (typically
operations, others use them in combination —In running the algorithm some steps make use of just the elementary1, or other small constants).
aggregate operations
We invent an
accounting
or
potential function
φ
where
is
the state space of the (data) structure and
is the set of natural
numbers.
φ ( T (^) ) denotes the amount of
(cyber) dollars
“deposited” or
“saved up” in state
Amortized Complexity Example
Think of a queue implemented as an array. We have an array
of
size
(^). If we fill it up and need more space we create an array
of
size 2
(^). Then we copy the elements of
into the first half of
Copying
elements costs
dollars.
An accounting functionperformance of pushes?If we start with an array of size 1, how do we analyse the
φ
is updated as follows. Let
1
be the state
of a newly doubled array, only the first half of which is filled.
2
is
φ paying 1 dollar for the cost of doing it, and save 2 dollars. Formally,position of the second half. We charge 3 dollars for this insertion,the next state in which the overflow element is inserted into the first
( T 2 ) =
φ ( T 1 ) + 2. This is done for every push into the new second
half.
Amortized Complexity Example
to pay for this −− we prove there is always enough.
$ $
$ $
$ $
$ $
$ $
Second doubling due to overflow
A B C D E F G H
First doubling filled
copy
A B C D E F G H I
Push −− charge $3 per push. Cost of push = $1.Operations:$ −− amount "deposited" into account Excess $2 is deposited into account.
Double array −− charge $N for copying N elements into first half of doubled array. Use account to
Figure 1: Account updating for push ops
Amortized Complexity Example
In splaying a noderank.nodes, the amount of dollars deposited in each node is equal to itsThe account for a state (tree) is distributed across subaccounts fortextbook explanation.level outlines. Here are a few comments on the details in the The text slides on amortized complexity for splaying are very high
x
to a new position denoted by
x ′ you will notice
decreased, but most are not changed.that some nodes have their account increased and others have theirs
Amortized Complexity Example
zig (1 dollar). If negative, then“donate” dollars to pay for the zig-zig or zig-zag (2 dollars each), or a So, if there is a net positive change, we the affected nodes can
we
pay the difference.
The
invariant
mentioned means this: each node before and after
splaying can
maintain
an account (may be less or more than its old
we account) according to its new rank. But this is only possible because
pay to make up any shortfall.
The text goes on to show that this payment is bounded by
log n
dollars for an arbitrary splay.