Amortized-Analysis

# Amortized-Analysis - Amortized Analysis Amortized Analysis...

This preview shows pages 1–9. Sign up to view the full content.

Amortized Analysis Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Incrementing a Binary Counter k -bit Binary Counter: A [0. . k - 1] I NCREMENT ( A ) 1. i 0 2. while i < length [ A ] and A [ i ] = 1 3. do A [ i ] 0 reset a bit 4. i i + 1 5. if i < length [ A ] 6. then A [ i ] 1 set a bit = - = 1 0 2 ] [ k i i i A x
k -bit Binary Counter Ctr A[4] A[3] A[2] A[1] A[0] 0 0 0 0 0 0 1 0 0 0 0 1 2 0 0 0 1 0 3 0 0 0 1 1 4 0 0 1 0 0 5 0 0 1 0 1 6 0 0 1 1 0 7 0 0 1 1 1 8 0 1 0 0 0 9 0 1 0 0 1 10 0 1 0 1 0 11 0 1 0 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
k -bit Binary Counter Ctr A[4] A[3] A[2] A[1] A[0] Cost 0 0 0 0 0 0 0 1 0 0 0 0 1 1 2 0 0 0 1 0 3 3 0 0 0 1 1 4 4 0 0 1 0 0 7 5 0 0 1 0 1 8 6 0 0 1 1 0 10 7 0 0 1 1 1 11 8 0 1 0 0 0 15 9 0 1 0 0 1 16 10 0 1 0 1 0 18 11 0 1 0 1 1 19
Worst-case analysis Consider a sequence of n insertions. The worst-case time to execute one insertion is Θ ( k ) . Therefore, the worst-case time for n insertions is n · Θ ( k ) = Θ ( n k ) . WRONG! In fact, the worst-case cost for n insertions is only Θ ( n ) Θ ( n k ) . Let’s see why. Note: You’ll be correct If you’d said O ( n k ). But, it’s an overestimate.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Tighter analysis Ctr A[4] A[3] A[2] A[1] A[0] Cost 0 0 0 0 0 0 0 1 0 0 0 0 1 1 2 0 0 0 1 0 3 3 0 0 0 1 1 4 4 0 0 1 0 0 7 5 0 0 1 0 1 8 6 0 0 1 1 0 10 7 0 0 1 1 1 11 8 0 1 0 0 0 15 9 0 1 0 0 1 16 10 0 1 0 1 0 18 11 0 1 0 1 1 19 A[0] flipped every op n A[1] flipped every 2 ops n /2 A[2] flipped every 4 ops n /2 2 A[3] flipped every 8 ops n /2 3 A[ i ] flipped every 2 i ops n /2 i Total cost of n operations
Tighter analysis (continued)   ) ( 2 2 1 2 1 lg 1 n n n n i i n i i Θ = = < = = = Cost of n increments . Thus, the average cost of each increment operation is Θ ( n )/ n = Θ (1) .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Amortized analysis An amortized analysis is any strategy for analyzing a sequence of operations to show that the average cost per operation is small, even though a single operation within the sequence might be expensive. Even though we’re taking averages, however,
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/03/2011 for the course COT 5407 taught by Professor Staff during the Fall '08 term at FIU.

### Page1 / 29

Amortized-Analysis - Amortized Analysis Amortized Analysis...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online