Download Amortized Analysis - Lecture Slides | CS 5633 and more Study notes Algorithms and Programming in PDF only on Docsity!
CS 5633 Analysis of Algorithms
1
3/13/
CS 5633 -- Spring 2008^ Amortized Analysis^ Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 5633 Analysis of Algorithms
Dynamic tablesProblem: 3/13/
We may not know the proper size in
Task:^ Store a dynamic set in a table/array. Elementscan only be inserted, and all inserted elements arestored in one contiguous part in the array. The tableshould be as small as possible, but large enough sothat it won’t overflow.advance! IDEA:^ Whenever the table overflows, “grow” it byallocating (via
malloc
or^ new ) a new, larger table.
Solution: Move all items from the old table into the new one,and free the storage for the old table.
Dynamic tables.
CS 5633 Analysis of Algorithms
3
Example of a dynamic table 1. INSERT 3/13/
2. INSERT
overflow
CS 5633 Analysis of Algorithms 3/13/
Example of a dynamic table 1. INSERT2. INSERT^
overflow
CS 5633 Analysis of Algorithms
5
3/13/
Example of a dynamic table 1. INSERT2. INSERT
CS 5633 Analysis of Algorithms
Example of a dynamic table 1. INSERT2. INSERT 3/13/
3. INSERT
overflow
CS 5633 Analysis of Algorithms
7
Example of a dynamic table 1. INSERT2. INSERT3. INSERT 3/13/
overflow
CS 5633 Analysis of Algorithms
Example of a dynamic table 1. INSERT2. INSERT3. INSERT 3/13/
CS 5633 Analysis of Algorithms
13
Example of a dynamic table 1. INSERT2. INSERT3. INSERT4. INSERT6. INSERT 3/13/
5. INSERT
7. INSERT
CS 5633 Analysis of Algorithms
Worst-case analysis Consider a sequence of 3/13/
n^ insertions. The
worst-case time to execute one insertion is Ο( n ). Therefore, the worst-case time for
n
insertions is
n^ ·^ Ο( n ) =
2 Ο( n ).
WRONG!
In fact, the worst-case cost for
n^ insertions is only
Θ( n )^ ≪
2 Ο( n ).
Let’s see why.
CS 5633 Analysis of Algorithms
15
Tighter analysis i 3/13/
size^^1 i^
Let^ c = the cost of the i^
i^ th insertion
c^ i
CS 5633 Analysis of Algorithms
Tighter analysis Let c 3/13/
= the cost of the i
i^ th insertion
=^ 1 + cost to double array size i 1 2 3
size^^1 i^
?^?^
?^?^
?^?^
?^?^
?^?
c^ i
CS 5633 Analysis of Algorithms
17
Tighter analysis Let c 3/13/
= the cost of the i
i^ th insertion
1 + cost to double array size= i 1 2 3
size^^1 i^
c^ i
CS 5633 Analysis of Algorithms
Tighter analysis Let c 3/13/
= the cost of the i
i^ th insertion
1 + cost to double array size= i 1 2 3
size^^1 i^
c^ i^
CS 5633 Analysis of Algorithms
19
Tighter analysis (continued) 3/13/
c 1 )^1 lg( n^ ^ ^2 n^0 j 3 n^ )( n
j
n =∑ i i = − +≤∑= ≤ Θ=
Cost of^
n^ insertions
Thus, the average cost of each dynamic-tableoperation is
Θ( n )/ n^
=^ Θ(1).
CS 5633 Analysis of Algorithms
Amortized analysis An amortized analysis 3/13/
is any strategy for
analyzing a
sequence
of operations:
• compute the total cost of the sequence, OR• amortized cost of an operation = averagecost per operation, averaged over the numberof operations in the sequence• amortized cost can be small, even though asingle operation within the sequence might beexpensive
CS 5633 Analysis of Algorithms
25
Accounting analysis ofdynamic tables Charge an amortized cost of Example: 3/13/
ĉ = $3 for the i^
i^ th
insertion.• $1 pays for the immediate insertion.• $2 is stored for later table doubling.When the table doubles, $1 pays to move arecent item, and $1 pays to move an old item.
overflow
$0 $0$0 $0$0 $0$0 $0^ $
CS 5633 Analysis of Algorithms
Accounting analysis ofdynamic tables Charge an amortized cost of Example: 3/13/
ĉ = $3 for the i^
i^ th
insertion.• $1 pays for the immediate insertion.• $2 is stored for later table doubling.When the table doubles, $1 pays to move arecent item, and $1 pays to move an old item.$0 $0$0 $0$0 $0$
CS 5633 Analysis of Algorithms
27
Accounting analysis(continued)Key invariant: 3/13/
Bank balance never drops below 0.
Thus, the sum of the amortized costs provides anupper bound on the sum of the true costs.^ i^
size^^1 i^
c^^1 i^
ĉ^2 i^
bank^ i^
*Okay, so I lied. The first operation costs only $2, not $3.
CS 5633 Analysis of Algorithms Incrementing a Binary CounterGiven: A 3/13/ k -bit binary counter
A [0,1,…, k
-1], initialized with
0,0,…,0. The counter supports the following
INREMENT
operation:INCREMENT
( A )^ // increases counter by 1
i^ ←^0 while^ i <length(
A )^ and^ A [ i ]=1^ do A [ i ]^ ←^0 i ++ if i <length( A )^ then A [ i ]^ ←^1
-^ Question:
In a sequence of
n^ INCREMENT operations, what is
the amortized runtime of one
INCREMENT operation?
CS 5633 Analysis of Algorithms
29
Binary Counter Example Initial counter 3/13/
After 1 increment
After 2 increments
After 3 increments
After 4 increments
After 5 increments
After 6 increments
After 7 increments
After 8 increments
After 9 increments
Example for
k =8 and^ n =9:
- The worst-case runtime of one I
NCREMENT
operation is O(
k )
- For^ n^ operations the total is O(
(^1) nk ) →^00 →^1 flipflip$1$1$1$1$1$2^ $1$1$1^ $1$1$3$
CS 5633 Analysis of Algorithms
Accounting Method • Charge $2 to set a bit to 1 (0 3/13/
→1 flip) ¾^ $1 pays for the actual flip ¾^ Store $1 on the bit as credit to be used later when this bit isflipped back to 0• Charge $0 to set a bit to 0 (
→0 flip) ¾^ Every 1 in the counter has $1 credit on it, which is used topay for this flip
CS 5633 Analysis of Algorithms
31
Binary Counter Example Initial counter 3/13/
After 1 increment
After 2 increments
After 3 increments
After 4 increments
After 5 increments
After 6 increments
After 7 increments
After 8 increments
After 9 increments
Example for
k =8 and^ n =9:
1 →^00 →^1 flipflip$1$1$1$1$1$2^ $1$1$1$1$1$3$
CS 5633 Analysis of Algorithms
Accounting Method • Charge $2 to set a bit to 1 (0 3/13/
→1 flip) ¾^ $1 pays for the actual flip ¾^ Store $1 on the bit as credit to be used later when this bit isflipped back to 0• Charge $0 to set a bit to 0 (
→0 flip) ¾^ Every 1 in the counter has $1 credit on it, which is used topay for this flip ⇒ Since each I
NCREMENT
operation is composed of one 0
→1 flip
and possibly multiple 1
→0 flips, the asymptotic runtime of one I^ NCREMENT
operation is O(1).