Professional Documents
Culture Documents
17.amortized Analysis: Hsu, Lih-Hsing
17.amortized Analysis: Hsu, Lih-Hsing
Amortized analysis
Hsu, Lih-Hsing
Computer Theory Lab.
Chapter 17 P.2
Computer Theory Lab.
Chapter 17 P.3
Computer Theory Lab.
Chapter 17 P.4
Computer Theory Lab.
MULTIPOP(S, k)
1 while not STACK-EMPTY(S) and k 0
2 do POP(S)
3 kk–1
Chapter 17 P.5
Computer Theory Lab.
top 23
17
6
39
10 top 10
47 47
initial MULTIPOP(S,4) MULTIPOP(S,7)
Chapter 17 P.6
Computer Theory Lab.
Chapter 17 P.8
Computer Theory Lab.
INCREMENT
INCREMENT(A)
1 i0
2 while i < length[A] and A[i] = 1
3 do A[i] 0
4 ii+1
5 if i < length[A]
6 then A[i] 1
Chapter 17 P.9
Computer Theory Lab.
Analysis:
O(n k) (k is the word length)
Amortize Analysis:
log n n 1
2i n 2i
i 0 i 0
2n
O ( n)
the amortize cost is O(1)
n
Chapter 17 P.10
Computer Theory Lab.
Chapter 17 P.11
Computer Theory Lab.
Chapter 17 P.12
Computer Theory Lab.
Chapter 17 P.13
Computer Theory Lab.
Chapter 17 P.14
Computer Theory Lab.
Stack operation
PUSH 1 PUSH 2
POP 1 POP 0
Chapter 17 P.15
Computer Theory Lab.
10 1 10 0
Chapter 17 P.16
Computer Theory Lab.
Chapter 17 P.17
Computer Theory Lab.
Chapter 17 P.18
Computer Theory Lab.
n n
cˆ (c ( D ) ( D
i 1
i
i 1
i i i 1 ))
n
ci ( Dn ) ( D0 )
i 1
n n
ci ci
If ( Di ) ( D0 ) then i 1 i 1 .
If ( Di ) ( Di 1 ) then the potential
increases.
Chapter 17 P.19
Computer Theory Lab.
Stack operations
( Di ) the number of objects in the
stack of the ith operation.
( D0 ) 0
( D ) 0
i
Chapter 17 P.20
Computer Theory Lab.
PUSH
( Di ) ( Di 1 ) ( s 1) s 1
ci ci ( Di ) ( Di 1 ) 1 1 2
Chapter 17 P.21
Computer Theory Lab.
MULTIPOP
k' min{ k , s }
( Di ) ( Di 1 ) k'
ci ci ( Di ) ( Di 1 ) k' k' 0
Chapter 17 P.22
Computer Theory Lab.
POP
ci 0
Chapter 17 P.23
Computer Theory Lab.
( Di ) ( Di 1 ) (bi 1 ti 1) bi 1 1 ti
cˆi ci ( Di ) ( Di1 ) (ti 1) (1 ti ) 2
Amortized cost = O(1)
Chapter 17 P.24
Computer Theory Lab.
Chapter 17 P.25
17.4 Dynamic tables
Computer Theory Lab.
Chapter 17 P.27
Computer Theory Lab.
TABLE_INSERT
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
Chapter 17 P.28
Computer Theory Lab.
Aggregate method:
i 1 j 1
amortized cost = 3
Chapter 17 P.29
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.30
Computer Theory Lab.
Potential method:
(T ) 2 num[T ] size[T ]
sizei sizei 1 (not expansion)
cˆi ci i i 1
1 (2 numi sizei ) (2 numi 1 sizei 1 )
1 (2 numi sizei ) (2(numi 1) sizei )
3
Chapter 17 P.31
Computer Theory Lab.
Example:
sizei sizei 1 16 , numi 13
cˆi ci i i 1
1 (2 13 16) (2 12 16)
1 (2 13 16) (2 12 16)
3
Chapter 17 P.32
Computer Theory Lab.
cˆi ci i i 1
numi (2 numi sizei ) (2 numi 1 sizei 1 )
numi (2 numi (2 numi 2))
(2(numi 1) (numi 1))
numi 2 (numi 1)
3
Chapter 17 P.33
Computer Theory Lab.
Example:
sizei / 2 sizei 1 numi 1 16
cˆi ci i i 1
17 (2 17 32) (2 16 16)
17 (2 17 (2 17 2)) (2 16 16)
17 2 16
3
Amortized cost = 3
Chapter 17 P.34
Computer Theory Lab.
Chapter 17 P.35
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.
1
Set load factor
2
Chapter 17 P.36
Computer Theory Lab.
n
The first 2 operations are inserted. The
n
second 2 operations, we perform I, D, D, I,
I, D, D, …
Total cost of these n operations is ( n 2
).
Hence the amortized cost is ( n ).
1
Set load factor 4 (as TABLE_DELETE)
(after the contraction, the load factor
become 1 )
2
Chapter 17 P.37
Computer Theory Lab.
1
2 num [T ] size[T ] if (T )
(T ) 2
size[T ] 1
num[T ] if (T )
2 2
Chapter 17 P.38
Computer Theory Lab.
Initial
num0 0
size0 0
0 1
0 0
Chapter 17 P.39
Computer Theory Lab.
TABLE-INSERT
1
if i1 , same as before.
2
1
if i1
2
1
if i
2
ci ci i i 1
sizei sizei 1
1( numi ) ( numi 1 )
2 2
sizei sizei
1( numi ) ( ( numi 1 ))
2 2
Chapter 17 0 P.40
Computer Theory Lab.
Example:
Chapter 17 P.41
Computer Theory Lab.
1
If i
2
cˆi ci i i 1
sizei 1
1 (2numi sizei ) ( numi 1 )
2
sizei 1
1 (2(numi 1 1) sizei 1 ) ( (numi 1 ))
2
3
3numi 1 sizei 1 3
2
3
3 i 1 sizei 1 sizei 1 3
2
3 3
sizei 1 sizei 1 3
2 2
Chapter 17 3 P.42
Computer Theory Lab.
Example:
Chapter 17 P.43
Computer Theory Lab.
TABLE-DELETE
1
If i1
2
i does not cause a contraction (i.e.,
sizei sizei 1
ci ci i i 1
sizei sizei 1
1( numi ) ( numi 1 )
2 2
sizei sizei
1( numi ) ( ( numi 1 ))
2 2
2
Chapter 17 P.44
Computer Theory Lab.
Example:
Chapter 17 P.45
Computer Theory Lab.
i causes a contraction
ci numi 1 ( actual cost )
sizei sizei 1
numi 1
2 4
Chapter 17 P.46
Computer Theory Lab.
cˆi ci i i 1
sizei sizei 1
( numi 1) ( numi ) ( numi 1 )
2 2
( numi 1) (( numi 1) numi )
(( 2numi 2) ( numi 1))
1
Chapter 17 P.47
Computer Theory Lab.
Example:
sizei sizei 1
numi 1 4
2 4
ci ci i i 1
8 16
( 3 1) ( 3 ) ( 4 )
2 2
1
Chapter 17 P.48
Computer Theory Lab.
1
if i1 (Exercise 18.4.3)
2
Amortized cost O(1).
Chapter 17 P.49