• 沒有找到結果。

# • The time required to perform a sequence of data structure operations is averaged over all the operations performed.

N/A
N/A
Protected

Share "• The time required to perform a sequence of data structure operations is averaged over all the operations performed."

Copied!
54
0
0

(1)

### Amortized Analysis

Lee, Hsiu-Hui

Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.

(2)

(3)

(4)

n n T( )

(5)

### PUSH(S, x) / POP(S) / MULTIPOP(S, k)

MULTIPOP(S, k)

1 while not STACK-EMPTY(S) and k ≠ 0

2 do POP(S)

3 k ← k – 1

(6)

### Action of MULTIPOP on a stack S

MULTIPOP(S,4) MULTIPOP(S,7)

top → 23 17 6 39 10 47

⎯⎯

top → 10 47

⎯⎯ ⎯⎯

(7)

### • PUSH(S, x) / POP(S)

Each runs in O(1) time.

Actual running time for a sequence of n PUSH, POP operations is Θ(n).

### • MULTIPOP(S, k)

pops the top k objects of stack

Total cost : min(s, k) s : stack size

(8)

## 問每一個 operation 在 worst-case 之 amortized cost?

PUSH O(1)

POP O(1)

MULTIPOP O(n) (the stack size is at most n)

2

(see the next)

(9)

### • Each object can be popped at most once for each time it is pushed.

Number of times the POP can be called on a nonempty stack, including calls within MULTIPOP, is at most the number of PUSH operatons, which is at most n.

( ) O(1) .

n n O =

(10)

### EX2-Incrementing a binary counter

A[0] 每 1 次改變一次 A[1] 每 2 次改變一次 A[i] 每 2i 次改變一次

(11)

### 6then A[i] ← 1

length[A]=k Cost : Θ(k)

(12)

### • Amortize Analysis:

The total number of flips in the sequence is

) ) (

is ( operation per

cost amortized

the

1 2 2

1

2 0

log

0

n O n O n

n n

i i n

i

i

=

=

⎥⎦ <

⎢⎣

=

=

(13)

### more or less than the actual cost. The amount we charge an operation is called its amortized cost.

Some are charged more than actual cost.

Some are charged less.

(14)

(15)

### operations.

(16)

If we denote the actual cost of the ith operation by ci and the amortized cost of the ith operation by , we require

(i.e. total amortized cost ≥ total actual cost)

for all sequence of n operations.

The total credit stored in the data structure is the difference between the total actual cost, or

The total credit must be nonnegative at all times.

i

= =

n

n i n

i

i c

c

1 1

ˆ

= =

n

i

i n

i

i c

c

1 1

ˆ

(17)

### EX1-Stack operation

n 個運作之後, total amortized cost = O(n)

0 min{k,s}

MULTIPOP

0 1

POP

2 1

PUSH

actual

cost amortized cost

(18)

### EX2-Incrementing a binary counter

Each time, there is exactly one 0 that is changed into 1.

The number of 1’s in the counter is never negative!

Amortized cost is at most 2 = O(1).

0 1→0 1

2 0→1 1

actual cost amortized cost

(19)

### entire data structure.

Accounting method stores credit with specific objects.

Potential method stores potential in the data structure as a whole.

Can release potential to pay for future operations.

Most flexible of the amortized analysis methods.

(20)

D0

i

i−1

i

(21)

### is defined by

cˆi = ci +Φ(Di)Φ(Di1)

(22)

### • If for all i,

then the amortized cost is always an upper bound on actual cost .

### ∑

=

=

=

Φ

− Φ

+

=

Φ

− Φ

+

=

n

i

n i

n

i

i i

i n

i

i

D D

c

D D

c c

1

0 1

1 1

) (

) (

) ) (

) ( ˆ (

Φ( Di ) ≥ Φ( D0 )

i

i n

i i

n

= =

### ∑ ≥ ∑

1 1

Φ( Di ) ≥ Φ( Di−1)

(23)

(24)

(25)

(26)

## \$ci= 0

(27)

### EX2-Incrementing a binary counter

the number of 1’s in the counter after the ith operations

The ith INCREMENT operation resets bits.

### •

The Counter starts at zero Amortized cost = O(1)

Total amortized cost of n operations is O(n)

i

i

i

1

i

i

i

i

i i

i i

i

i

1

1

1

i

i

i

i1

i

i

(28)

) (

0

n O k

k b

, as

long as

, since

that particular

in Note

=

(29)

### • Scenario

Have a table—maybe a hash table.

Don’t know in advance how many objects will be stored in it.

When it fills, must reallocate with a larger size, copying all objects into the new larger table.

When it gets sufficiently small, might want to reallocate with a smaller size.

(30)

### • Goals

1. O(1) amortized time per operation.

2. Unused space always ≤ constant fraction of allocated space.

### • Load factor α = num / size,

num = # items stored

size = allocated size.

(31)

### =

(32)

TABLE_INSERT(T, x) 1 if size[T] = 0

2 then allocate table[T] with 1 slot 3 size[T] ← 1

4 if num[T] = size[T]

5 then allocate new-table with 2 ⋅ size[T] slots 6 insert all items in table[T] in new-table 7 free table[T]

8 table[T] ← new-table 9 size[T] ← 2 ⋅ size[T]

10 insert x into table[T]

11 num[T] ← num[T] + 1

(33)

i

### = actual cost of ith operation

If not full, ci = 1.

If full, have i - 1 items in the table at the start of the ith operation. Have to copy all i - 1 existing items, then insert ith item⇒ci = i .

i

2

(34)

i

=n

=

i

n j

j

i

1

lg

1

(35)

### Each item pays for 3 elementary insertions;

1. inserting itself in the current table,

2. moving itself when the table is expanded, and

3. moving another item that has already been moved once when the table is expanded.

Charge \$3 per insertion of x.

\$1 pays for x’s insertion.

\$1 pays for x to be moved in the future.

\$1 pays for some other item to be moved.

(36)

1 1

1

i i

i i

i i

i i

i i

i i

(37)

1

i i i

i

(38)

i

i1

i

1 1

1

i i

i i

i i

i

i i

i i

i

i i

i i

(39)

1

i i i

i

(40)

(41)

### 2

(42)

The first n/2 operations are inserted:

The second n/2 operations, we perform

I, D, D, I, I, D, D, …

Total cost of these n operations is . Hence the amortized cost is .

become )

Θ(n2 )

n

1 4 1

2

) Θ(n

(43)

(44)

0 0

0 0

(45)

### • if if

αi−11 2 αi1 < 1 2

i

### <

1 2

\$

( ) ( )

( ) ( ( ))

c c

size num size

num size num size

num

i i i i

i i i

i

i i i

i

= +

= +

= +

Φ Φ 1

1 1

1 2 2

1 1

(46)

(47)

i

2 3 3 2

3

2 3 3 3

2 3 3 3

)) 2 (

( ) )

1 (

2 ( 1

2 ) (

) 2

( 1 ˆ

1 1

1 1

1

1 1

1 1

1 1

1 1

1

+

<

+

=

+

=

− +

+

=

− +

=

Φ

− Φ +

=

i i

i i

i

i i

i i

i i

i i

i i

i i

i i

size size

size size

size num

size num size

num

size num size

num c

c

α

(48)

### 273

Amortized cost of Table insert is O(1).

Amortized cost of Table insert is O(1).

(49)

i−1

1 2

i

i

### −1

\$

( ) ( )

( ) ( ( ))

c c

size num size

num size num size

num

i i i i

i i i

i

i i i

i

= + −

= + − − −

= + − − − +

Φ Φ 1

1 1

1 2 2

1 2 2 1

(50)

(51)

### ( actual cost )

(52)

1

)) 1 (

) 2 2

((

) )

1 ((

) 1 (

2 ) (

2 ) ( ) 1 (

ˆ

1 1

1

=

+

− +

− +

+ +

=

− +

+

=

Φ

− Φ +

=

i i

i i

i

i i

i i

i

i i

i i

num num

num num

num

size num size num

num c c

(53)

i i

1

i

i

i

i

i

1

(54)

i

1

### 2

(c) Draw the graph of as a function of and draw the secant lines whose slopes are the average velocities in part (a) and the tangent line whose slope is the instantaneous velocity

[This function is named after the electrical engineer Oliver Heaviside (1850–1925) and can be used to describe an electric current that is switched on at time t = 0.] Its graph

The question of whether every Cauchy sequence in a given inner product space must converge is very important, just as it is in a metric or normed space.. We give the following name

As discussed in the example (Figure 3.3), the goal of the min-sum algorithm is to find a valid configuration such that the sum of the local costs (over all sites and check sets) is

Calculate the amortized cost of each operation based on the potential function. Calculate total amortized cost based on

Given a shift κ, if we want to compute the eigenvalue λ of A which is closest to κ, then we need to compute the eigenvalue δ of (11) such that |δ| is the smallest value of all of

Reading Task 6: Genre Structure and Language Features. • Now let’s look at how language features (e.g. sentence patterns) are connected to the structure

We explicitly saw the dimensional reason for the occurrence of the magnetic catalysis on the basis of the scaling argument. However, the precise form of gap depends