14-Amortized Analysis and Examples

# 14-Amortized Analysis and Examples - Amortized Analysis...

• Notes
• 11

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

Amortized Analysis Consider a sequence of operations on a data structure and let t i be the cost of the ith operation. An amortized analysis means determining the worst case cost of the sequence. The amortized cost is the worst case cost of the sequence divided by the length of the sequence. Example 1: Binary counter implemented as an array of size k. One operation - increment the counter by 1. We take the cost of an increment to be the number of bits that change. The counter starts at 0. Take k =8. 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 etc It takes 1+ floor(lg i) bits to represent the number i and so no more than this many bits can change when the counter is incremented from i-1 to i. Thus the total cost of n increments will be at most the sum of {1+floor(lg i)} from i=1 to n. But this is O(nlgn). This is a worst case analysis and leads to a bound but this bound can be improved. So the amortized cost is lgn. Aggregate approach - Try to actually add up the costs of a sequence. Here, we see that the 0 th bit changes every time we increment, bit 1 changes the floor(n/2) times, bit 2 changes the floor(n/4) times and in general bit i changes the floor(n/2 i ) times Hence the total cost of n increments = sum of floor(n/2 i ) from i=1 to n. But this is less than the sum of n/2 i over all i> 1 which is 2n. Hence we say the amortized cost of an increment is 2. Potential approach .- assign a “potential”, a number, say Φ(S), to each state S of the data structure. Then we define the amortized cost of the ith operation, a i , by

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

a i = t i + Φ(S i ) - Φ(S i-1 ) where S i , i>0, denotes the state of the data structure after the ith operation and S 0 denotes the initial state. Then, ∑a i = ∑t i + ∑ {Φ(S i ) - Φ(S i-1 )} where the sum is over the sequence of n operations. Notice that this becomes ∑a i = ∑t i + Φ(S f ) - Φ(S 0 ) or ∑t i = ∑a i + {Φ(S 0 ) - Φ(S f )} or ∑a i - {Φ(S f ) - Φ(S 0 )}. This means that whenever the final potential Φ(S f ) exceeds the initial potential Φ(S 0 ), the sum of the a i , ∑a i , is a bound on the total cost. Otherwise, to achieve a bound, requires that a bound for Φ(S 0 ) - Φ(S f ) must to be added to ∑a i to achieve a bound on, ∑t i , the total cost. For our counter example, take the potential to be the number of 1’s in the counter. Then, the amortized cost of an increment is the actual cost 1 plus the change in potential due to the increment. This gives the following table for our example: 0 1 2 3 4 5 6 7 t i Φ a i ∑t i ∑a i 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 2 1 2 0 1 0 0 0 0 0 0 2 1 2 3 4 1 1 0 0 0 0 0 0 1 2 2 4 6 0 0 1 0 0 0 0 0 3 1 2 7 8 1 0 1 0 0 0 0 0 1 2 2 8 10 0 1 1 0 0 0 0 0 2 2 2 10 12 1 1 1 0 0 0 0 0 1 3 2 11 14 0 0 0 1 0 0 0 0 4 1 2 15 16 1 0 0 1 0 0 0 0 1 2 2 16 18 0 1 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0
This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• James Korsh
• Analysis of algorithms, Splay Theorem

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern