Amortized-Analysis

Amortized-Analysis - Amortized Analysis Amortized Analysis...

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

View Full Document Right Arrow Icon
Amortized Analysis Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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) .
Background image of page 7

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

View Full DocumentRight Arrow Icon
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,
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online