Presentation by Marty Krogel Chapter 17 Amortized Analysis Presentation by Marty Krogel
Definition Amortized analysis - A way of analyzing the time required to perform a series of operations on a data-structure by averaging the cost over all operations performed.
Chapter Outline 17.1 Aggregate analysis 17.2 The accounting method 17.3 The potential method 17.4 Dynamic tables (Applying the potential method) 17.4.1 Table expansion 17.4.2 Table expansion and contraction
17.1 Aggregate analysis The aggregate analysis spreads the total cost evenly amongst all operations performed. (Later analysis methods assign different costs to different operations.) If the worst-case time of a series of operations is T(n), then the average cost, or amortized cost per operation is T(n)/n.
Stack example (On the whiteboard)
Incrementing binary counter example
Increment binary counter INCREMENT(A) i 0 while i < length[A] and A[i] = 1 do A[i] 0 i i + 1 if i < length[A] then A[i] 1
Binary counter
Cost of INCREMENT
17.2 The accounting method For the accounting method, different operations are assigned different costs, called their amortized cost. When an operation’s amortized cost is greater than it’s actual cost, the difference is assigned as credit to specific objects in a data structure and can be used later by operations who’s amortized cost is less than it’s actual cost.
Amortized cost notation The sum of amortized cost of the n operations is greater than or equal to the sum of the actual cost of the n operations.
Stack example (On the whiteboard)
Incrementing binary counter example (On the whiteboard)
17.3 The potential method Instead of representing the prepaid work as credit, the potential method treats it as “potential energy” or just “potential” which is stored until it is needed later to pay for other operations. Where Di represents the data structure after the ith operation, the potential function F maps the data structure Di to a real number F (Di). Thus, we are able to come up with an equation for the amortized cost of the ith operation.
Potential amortized cost
Stack example (On the whiteboard)
Incrementing binary counter example (On the whiteboard)
17.4 Dynamic tables As we all know, expanding an array involves creating a new array of a larger size and copying over all the elements in it, therefore it takes O(n) time. What if I told you that, using amortized analysis, we can prove that a series of insertion operations on an array has a constant amortized cost, despite requiring a number of array expansions?
Table expansion TABLE-INSERT(T, x) if size[T] = 0 then allocate table[T] with 1 slot size[T] 1 if num[T] = size[T] then allocate new-table with 2 x size[T] slots insert all items in table[T] into new-table free table[T] table[T] new-table size[T] 2 x size[T] insert x into table[T] num[T] num[T] + 1
Table expansion
Table expansion and contraction Since we double the table size when it is full, never dropping below a load factor of ½, one might think to contract the table when the load factor becomes ½. (This doesn’t work.) Consider the case when n operations are performed on a table, where the first n/2 operations are insert (filling up the table). Then the following series is used for the next n/2 operations: Ins,Del,Del,Ins,Ins,Del,Del,Ins,Ins… This would result in n/2 expansions and contractions, each costing Q(n), for a total cost of Q(n2), which leaves us with an amortized cost of Q(n) per operation.
Potential method analysis Solution: Don’t contract the table until the load factor gets below ¼, then reduce the table size by ½. a(T) is the load factor and is equal to num[T] / size[T].
Case 1: TABLE-INSERT when ai-1 ³ ½. ĉi = 3. (Calculated previously) Case 2: TABLE-INSERT when ai-1 < ½. ĉi = ci + Fi – Fi-1 = 1 + (sizei/2 – numi) – (sizei-1/2 – numi-1) = 1 + (sizei/2 – numi) – (sizei/2 – numi –1) = 0. Case 3: TABLE-INSERT when ai-1 < ½, but ai ³ ½. = 1 + (2 · numi –sizei) – (sizei-1/2 – numi-1) = 1 + (2(numi-1+1) – sizei-1) – (sizei-1/2 – numi-1) = 3 · numi-1 – (3/2) sizei-1+ 3 = 3ai-1sizei-1 – (3/2)sizei-1+ 3 = (3/2)sizei-1 – (3/2)sizei-1 + 3 = 3.
Case 4: TABLE-DELETE when ai-1 < ½ but no contraction. ĉi = ci + Fi – Fi-1 = 1 + (sizei /2 – numi) – (sizei-1/2 – numi-1) = 1 + (sizei /2 – numi) – (sizei /2 – (numi +1)) = 2. Case 5: TABLE-DELETE when ai-1 < ½ with contraction. = (numi + 1) + (sizei /2 – numi) – (sizei-1/2 – numi-1) = (numi + 1) + ((numi + 1) – numi) – ((2 · numi + 2) – (numi + 1)) = 1. Case 6: TABLE-DELETE when ai-1 ³ ½, but ai < ½. = 1 + (sizei /2 – numi) – (2 · numi-1 – sizei-1) = 1 + (3/2)sizei – (numi-1 – 1) – 2(numi-1 ) = (3/2)sizei – 3 numi = (3/2)sizei – (3/2)sizei = 0. Case 7: TABLE-DELETE when ai, ai-1 ³ ½. = 1 + (2 · numi – sizei) – (2 · numi-1 – sizei-1) = 1 + 2(numi – (numi+1)) + (sizei – sizei) = 1 – 2 + 0 = – 1