Amortized algorithms, Lecture notes of Design and Analysis of Algorithms

Amortized complexity calculation

Typology: Lecture notes

2016/2017

Uploaded on 05/24/2017

jibin4dj
jibin4dj 🇮🇳

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Comp2711 S1 2006 Amortized Complexity Example 1
'
&
$
%
Amortized Complexity Main Idea
Worst case analysis of run time complexity is often too pessimistic.
Average case analysis may be difficult because (i) it is not clear what
is “average data”, (ii) uniformly random data is usually not average,
(iii) probabilistic arguments can be difficult.
Amortized complexity analysis is a different way to estimate run
times. The main ideas is to spread out the cost of operations,
charging more than necessary when they are cheap thereby
“saving up for later use” when they occasionally become expensive.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Amortized algorithms and more Lecture notes Design and Analysis of Algorithms in PDF only on Docsity!

Amortized Complexity Example

Amortized Complexity – Main Idea

“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

The Accounting Method

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

φ

S →

N

where

S

is

the state space of the (data) structure and

N

is the set of natural

numbers.

φ ( T (^) ) denotes the amount of

(cyber) dollars

“deposited” or

“saved up” in state

T

Amortized Complexity Example

Example: Extendable Arrays — 6.1.

Think of a queue implemented as an array. We have an array

A

of

size

N

(^). If we fill it up and need more space we create an array

B

of

size 2

N

(^). Then we copy the elements of

A

into the first half of

B

Copying

N

elements costs

N

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

T

1

be the state

of a newly doubled array, only the first half of which is filled.

T

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

Comments on the Textbook Explanations I

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

Comments on the Textbook Explanations II

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

O

log n

dollars for an arbitrary splay.