17.amortized analysis hsu, lih-hsing. computer theory lab. chapter 17p.2 the time required to...
Post on 22-Dec-2015
216 views
TRANSCRIPT
17.Amortized analysis
Hsu, Lih-Hsing
Chapter 17 P.2
Computer Theory Lab.
The time required to perform a sequence of data structure operations in average over all the operations performed.
Average performance of each operation in the worst case.
Chapter 17 P.3
Computer Theory Lab.
For all n, a sequence of n operations takes worst time T(n) in total. The amortize cost of each operation is
.n
nT )(
Chapter 17 P.4
Computer Theory Lab.
Three common techniques
aggregate analysis accounting method potential method
Chapter 17 P.5
Computer Theory Lab.
17.1 The aggregate analysis
Stack operation - PUSH(S, x) - POP(S) - MULTIPOP(S, k)
MULTIPOP(S, k) 1 while not STACK-EMPTY(S) and k 02 do POP(S)3 k k – 1
Chapter 17 P.6
Computer Theory Lab.
Action of MULTIPOP on a stack S
top 23 17 6 39 10 47
initial
top 10 47
MULTIPOP(S,4)
MULTIPOP(S,7)
Chapter 17 P.7
Computer Theory Lab.
Analysis a sequence of n PUSH, POP, and MULTIPOP operation on an initially empty stack.
O(n2) O(n) (better bound) The amortize cost of an operation is
.
O n
nO
( )( ) 1
Chapter 17 P.8
Computer Theory Lab.
Incremental of a binary counter
Chapter 17 P.9
Computer Theory Lab.
INCREMENT
INCREMENT(A)1 i 0
2 while i < length[A] and A[i] = 1
3 do A[i] 0
4 i i + 1
5 if i < length[A]
6 then A[i] 1
Chapter 17 P.10
Computer Theory Lab.
Analysis:
O(n k) (k is the word length) Amortize Analysis:
)1()(
iscost amortize the
2
2
1
2 0
log
0
On
nO
n
nn
ii
n
ii
Chapter 17 P.11
Computer Theory Lab.
17.2 The accounting method We assign different charges to different
operations, with some operations charged more or less than the actually cost. The amount we charge an operation is called its amortized cost.
Chapter 17 P.12
Computer Theory Lab.
When an operation’s amortized cost exceeds its actually cost, the difference is assign to specific object in the data structure as credit. Credit can be used later on to help pay for operations whose amortized cost is less than their actual cost.
Chapter 17 P.13
Computer Theory Lab.
If we want analysis with amortized costs to show that in the worst cast the average cost per operation is small, the total amortized cost of a sequence of operations must be an upper bound on the total actual cost of the sequence.
Moreover, as in aggregate analysis, this relationship must hold for all sequences of operations.
Chapter 17 P.14
Computer Theory Lab.
If we denote the actual cost of the ith operation by ci and the amortized cost of the ith operation by , we require
for all sequence of n operations. The total credit stored in the data structure
is the difference between the total actual cost, or .
ic
n
ni
n
ii cc
11
ˆ
n
i i
n
i icc
11ˆ
Chapter 17 P.15
Computer Theory Lab.
Stack operation
Amortize cost: O(1)
PUSH 1 PUSH 2
POP 1 POP 0
MULTIPOP min{k,s} MULTIPOP 0
Chapter 17 P.16
Computer Theory Lab.
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).
01 1 01 2
10 1 10 0
Chapter 17 P.17
Computer Theory Lab.
17.3 The potential method
initial data structure D0 .
Di : the data structure of the result after applying the i-th
operation to the data structure Di1.
Ci : actual cost of the i-th operation.
Chapter 17 P.18
Computer Theory Lab.
A p o t e n t i a l f u n c t i o n m a p s e a c h
d a t a s t r u c t u r e D i t o a r e a l n u m b e r
( )D i , w h i c h i s t h e p o t e n t i a l a s s o c i a t e d
w i t h d a t a s t r u c t u r e D i .
T h e a m o r t i z e d c o s t c i o f t h e i - t h
o p e r a t i o n w i t h r e s p e c t t o p o t e n t i a l
i s d e f i n e d b y )()(ˆ1
iiii
DDcc .
Chapter 17 P.19
Computer Theory Lab.
If then . If then the potential incre
ases.
n
ini
n
iiii
n
ii
DDc
DDcc
10
11
1
)()(
))()((ˆ
( ) ( )D Di 0c ci
i
ni
i
n
1 1
( ) ( )D Di i 1
Chapter 17 P.20
Computer Theory Lab.
Stack operations
the number of objects in the stack of the ith operation.
( )Di
( )D0 0( )Di 0
Chapter 17 P.21
Computer Theory Lab.
PUSH
( ) ( ) ( )D D s si i 1 1 1
( ) ( )c c D Di i i i 1 1 1 2
Chapter 17 P.22
Computer Theory Lab.
MULTIPOP
k k s' min{ , } ( ) ( ) 'D D ki i 1 ( ) ( ) ' 'c c D D k ki i i i 1 0
Chapter 17 P.23
Computer Theory Lab.
POP
The amortized cost of each of these operations is O(1).
ci 0
Chapter 17 P.24
Computer Theory Lab.
Incrementing a binary counter the number of 1’s in the counte
r after the ith operations = . = the ith INCREMENT operation rese
ts bits. Amortized cost = O(1)
( )Di bi
titi
1)(1
iiii
tbbDiiiiii
tbtbDD 1)1()()(111
2)1()1()()(ˆ1
iiiiiittDDcc
Chapter 17 P.25
Computer Theory Lab.
Even if the counter does not start at zero:
001
011
22
)()(ˆ
bbnbb
DDcc
nn
n
i
n
n
ii
n
ii
17.4 Dynamic tables
Chapter 17 P.27
Computer Theory Lab.
17.4.1 Table expansion
TABLE-INSERT TABLE-DELETE (discuss later) load-factor
(load factor)
( ) ( ( ) )T T 12
( )[ ]
[ ]T
num T
size T
Chapter 17 P.28
Computer Theory Lab.
TABLE_INSERT TABLE_INSERT(T, x)1 if size[T] = 02 then allocate table[T] with 1 slot3 size[T] 14 if num[T] = size[T]5 then allocate new-table with 2 size[T] slots6 insert all items in table[T] in new-table7 free table[T]8 table[T] new-table9 size[T] 2 size[T] 10 insert x into table[T]11 num[T] num[T] + 1
Chapter 17 P.29
Computer Theory Lab.
Aggregate method:
amortized cost = 3
otherwise1
2 ofpower exact an is 1 if i-ici
n
i
n
j
j
i nnnnc1
lg
1322
Chapter 17 P.30
Computer Theory Lab.
Accounting method: 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.
Chapter 17 P.31
Computer Theory Lab.
Potential method:
(not expansion)
][][2)( TsizeTnumT size sizei i 1
3
))1(2()2(1
)2()2(1
ˆ
11
1
iiii
iiii
iiii
sizenumsizenum
sizenumsizenum
cc
Chapter 17 P.32
Computer Theory Lab.
Example:
size size numi i i 1 16 13,
3
)16122()16132(1
)16122()16132(1
ˆ 1
iiii cc
Chapter 17 P.33
Computer Theory Lab.
(expansion)
size size numi i i/ 2 11
3
)1(2
))1()1(2(
))22(2(
)2()2(
ˆ
11
1
ii
ii
iii
iiiii
iiii
numnum
numnum
numnumnum
sizenumsizenumnum
cc
Chapter 17 P.34
Computer Theory Lab.
Example:
Amortized cost = 3
size size numi i i/ 2 1 161
3
16217
)16162())2172(172(17
)16162()32172(17
ˆ 1
iiii cc
Chapter 17 P.35
Computer Theory Lab.
17.4.2 Table expansion and contraction
To implement a TABLE-DELETE operation, it is desirable to contract the table when the load factor of the table becomes too small, so that the waste space is not exorbitant.
Chapter 17 P.36
Computer Theory Lab.
Goal:
The load factor of the dynamic table is bounded below by a constant.
The amortized cost of a table operation is bounded above by a constant.
Set load factor12
Chapter 17 P.37
Computer Theory Lab.
The first operations are inserted. The second operations, we perform I, D, D, I, I, D, D, …
Total cost of these n operations is . Hence the amortized cost is .
Set load factor (as TABLE_DELETE) (after the contraction, the load factor
become )
n
2n
2
( )n2
( )n14
1
2
Chapter 17 P.38
Computer Theory Lab.
2
1)(][
2
][2
1)(][][2
)(TifTnum
Tsize
TifTsizeTnumT
Chapter 17 P.39
Computer Theory Lab.
Initial
0
1
0
0
0
0
0
0
size
num
Chapter 17 P.40
Computer Theory Lab.
TABLE-INSERT
if , same as before.
if if
i 112
i 112
i 12
( ) ( )
( ) ( ( ))
c c
sizenum
sizenum
sizenum
sizenum
i i i i
ii
ii
ii
ii
1
111
2 2
12 2
1
0
Chapter 17 P.41
Computer Theory Lab.
Example:
size size numi i i 1 16 6,
( ) ( )c ci i i i
1 116
26
16
25
0
Chapter 17 P.42
Computer Theory Lab.
If i 12
3
32
3
2
3
32
33
32
33
))(2
())1(2(1
)2
()2(1
ˆ
11
111
11
11
11
11
1
ii
iii
ii
ii
ii
ii
ii
iiii
sizesize
sizesize
sizenum
numsize
sizenum
numsize
sizenum
cc
Chapter 17 P.43
Computer Theory Lab.
Example:
size size numi i i 1 16 8,
( ) ( )
c ci i i i
1
1 2 8 1616
27
3
Amortized cost of Table insert is O(1).Amortized cost of Table insert is O(1).
Chapter 17 P.44
Computer Theory Lab.
TABLE-DELETE
If does not cause a contraction
(i.e.,
i 11
2isize sizei i 1
( ) ( )
( ) ( ( ))
c c
sizenum
sizenum
sizenum
sizenum
i i i i
ii
ii
ii
ii
1
111
2 2
12 2
1
2
Chapter 17 P.45
Computer Theory Lab.
Example:
size size numi i i 1 16 6,
( ) ( )c ci i i i
1 116
26
16
27
2
Chapter 17 P.46
Computer Theory Lab.
causes a contraction i
c num
size sizenum
i i
i ii
1
2 411
( )actual cost
Chapter 17 P.47
Computer Theory Lab.
1
))1()22((
))1(()1(
)2
()2
()1(
ˆ
1
1
1
ii
iii
i
i
i
i
i
iiii
numnum
numnumnum
numsize
numsize
num
cc
Chapter 17 P.48
Computer Theory Lab.
Example:
size sizenumi i
i2 41 41
( ) ( ) ( )
c ci i i i
1
3 18
23
16
24
1
Chapter 17 P.49
Computer Theory Lab.
if (Exercise 18.4.3) Amortized cost O(1).
i 11
2