amortized analysis
play

Amortized Analysis Chapter 17 1 CPTR 430 Algorithms Amortized - PowerPoint PPT Presentation

Amortized Analysis Chapter 17 1 CPTR 430 Algorithms Amortized Analysis Amortized Analysis Amortized analysis averages the time required to perform a sequence of operations on a data structure over all the operations performed An


  1. Amortized Analysis Chapter 17 1 CPTR 430 Algorithms Amortized Analysis

  2. Amortized Analysis ■ Amortized analysis averages the time required to perform a sequence of operations on a data structure over all the operations performed ■ An individual operation may itself be relatively expensive, but its average cost (amortized cost) may be small ■ Common types of amortized analysis: ❚ Aggregate analysis ❚ Accounting method ❚ Potential method 2 CPTR 430 Algorithms Amortized Analysis

  3. Sample Applications The sample applications are simple enough to see how the principles of amortized analysis apply ■ Augmented stack ■ Binary counter 3 CPTR 430 Algorithms Amortized Analysis

  4. Augmented Stack A normal stack with an additional operation: multiPop() ■ push(x) —pushes element x onto the stack, as usual ■ pop() —pops the top element off of the stack and returns the popped element, as usual ■ multiPop(k) pops the top k elements off of the stack ❚ Returns true if k elements could be popped ❚ Returns false if fewer than k elements were popped because the stack had fewer than k elements when the method was called 4 CPTR 430 Algorithms Amortized Analysis

  5. The Implementation public class Stack { private Object[] stack; // Array of elements private int top; // Next available position public Stack(int capacity) { stack = new Object[capacity]; top = 0; // Stack is initially empty } public boolean isEmpty() { return top == 0; } public int size() { return top; } public void push(Object newElement) { if ( top < stack.length ) { stack[top++] = newElement; } } public Object pop() { Object result = null; if ( top > 0 ) { result = stack[--top]; } return result; } public boolean multiPop(int k) { while ( !isEmpty() && k != 0 ) { pop(); k--; } return k == 0; // k elements successfully popped off? } } 5 CPTR 430 Algorithms Amortized Analysis

  6. ✁ ✂ � � ✁ ☎ � ✄ ✁ � ✁ ✁ ✁ � � ✁ ✁ � � Analysis of Stack Operations ■ push(x) — O 1 ■ pop() — O 1 ■ MultiPop(k) — min n k , where n is the size of the stack—worst case is O n ■ Consider a sequence of n stack operations on an initially empty stack ❚ The worst-case cost of a multiPop() is O n , since the stack may contain n elements ❚ The worst-case time for any stack operation is thus O n n 2 ❚ A sequence on n stack operations, worst-case, would be O ( O n multiPop() operations each costing O n ) ■ This bound is correct but not tight (Why?) 6 CPTR 430 Algorithms Amortized Analysis

  7. ✞ ✟ ✠ ✡ ☛ ☛ ☛ ☛ ☛ ✡ ✟✠ ✞ ☛ ✝ � ✂ ✁ Binary Counter ■ Uses a fixed number of bits, k ■ The bits are stored in an array with length k k 1 ∑ 2 i ✄✆☎ ■ Lowest order bit is at index 0 , so the counter’s value = A i i 0 1 (modulo 2 k counter) ■ Counts upward from 0 to k ■ Flipping a bit counts as work ■ More work occurs when carries must be done k bits ■ Incrementing the maximum representable value 1111 1 (rolling it k bits 0000 0 ) requires k bit flips over to 7 CPTR 430 Algorithms Amortized Analysis

  8. The Implementation public class BinaryCounter { private int[] bitString; public BinaryCounter(int bits) { // Java automatically initializes the contents to zero bitString = new int[bits]; } public String toString() { String result = ""; for ( int i = bitString.length - 1; i >= 0; i-- ) { result += bitString[i]; } return result; } public void increment() { int i = 0; while ( i < bitString.length && bitString[i] == 1 ) { bitString[i] = 0; i++; } if ( i < bitString.length ) { bitString[i] = 1; } } } 8 CPTR 430 Algorithms Amortized Analysis

  9. � ✁ ✁ � Analysis of the Binary Counter ■ A single execution of the increment() methods takes worst-case time O k (when the array contains all 1s) ■ Thus a sequence of n increments on a counter object that is initially zero will take O nk time, in the worst case ■ This bound is correct, but not tight (Why?) 9 CPTR 430 Algorithms Amortized Analysis

  10. � ✁ ✁ � � Aggregate Analysis ■ In aggregate analysis, we show that for all n , a sequence of n operations takes worst-case time T n in total ■ The worst case amortized cost (or average cost) is thus T n n ■ This amortized cost is then applied to each operation even though different operations may have very different run times individually 10 CPTR 430 Algorithms Amortized Analysis

  11. � � ✁ � ✁ Aggregate Analysis of Stack Operations ■ Aggregate analysis can give a tighter upper bound for the sequence of n stack operations ■ A single multiPop() operation can be expensive, but any sequence of n push() , pop() , and multiPop() operations on an initially empty stack can cost at most O n (Why?) ■ Each item can be popped off at most once for each time it is pushed the number of pop() calls, including the calls from within multiPop() is at most the number of push() operations ■ The number of push() operations is at most n , so any sequence of n push() , pop() , and multiPop() operations on an initially empty stack takes at most O n time 11 CPTR 430 Algorithms Amortized Analysis

  12. ✁ � ✁ � � ✁ � � � ✁ Aggregate Analysis of Stack Operations (cont.) ■ The number of push() operations is at most n , so any sequence of n push() , pop() , and multiPop() operations on an initially empty stack takes at most O n time ■ The average cost of an operation: O n n O 1 ■ Aggregate analysis assigns the amortized cost of each operation to be the average cost ■ The amortized cost of all three stack operations (even multiPop() ) is O 1 12 CPTR 430 Algorithms Amortized Analysis

  13. ✄ ✁ ☛ ✁ ☛ � ☛ � ✄ � ✁ � � � � � ✁ ✂ � ✁ ✄ Aggregate Analysis of the Binary Counter ■ Notice that not all bits flip every time increment() is called ❚ bitString[0] flips every time A sequence of n increments causes it to flip n times ❚ bitString[1] flips every other time A sequence of n increments causes it to flip n 2 times ❚ bitString[2] flips every fourth time A sequence of n increments causes it to flip n 4 times 2 i ■ For i 0 1 lg n n , bit bitString[i] flips times in a sequence of n increments starting with a zero counter ■ For i lg n , bit bitString[i] does not flip at all 13 CPTR 430 Algorithms Amortized Analysis

  14. ✡ � ✠ ✞ ✟ � ✂ ✁ ✁ � � ✁ � � ✁ ✁ � ✁ Aggregate Analysis of the Binary Counter (cont.) The total number of flips in the sequence is ∞ lg n n 1 ∑ ∑ n 2 n 2 i 2 i i 0 i 0 (See Page 1060) The worst-case time for a sequence of n increment() operations on an initially zero counter is thus O n The average cost per operation (amortized cost) is therefore O n n O 1 14 CPTR 430 Algorithms Amortized Analysis

  15. Aggregate Analysis of the Binary Counter (cont.) Counter Bit Current Total Value 7 6 5 4 3 2 1 0 Cost Cost 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 2 1 2 0 0 0 0 0 0 1 0 1 3 3 0 0 0 0 0 0 1 1 3 4 4 0 0 0 0 0 1 0 0 1 7 5 0 0 0 0 0 1 0 1 2 8 6 0 0 0 0 0 1 1 0 1 10 7 0 0 0 0 0 1 1 1 4 11 8 0 0 0 0 1 0 0 0 1 15 9 0 0 0 0 1 0 0 1 2 16 10 0 0 0 0 1 0 1 0 1 18 11 0 0 0 0 1 0 1 1 3 19 12 0 0 0 0 1 1 0 0 1 22 13 0 0 0 0 1 1 0 1 2 23 14 0 0 0 0 1 1 1 0 1 25 15 0 0 0 0 1 1 1 1 5 26 16 0 0 0 1 0 0 0 0 1 31 15 CPTR 430 Algorithms Amortized Analysis

  16. The Accounting Method ■ The accounting method charges differing amounts to different operations ■ Some operations may be charged more or less than they actually cost ■ The amount charged to an operation is its amortized cost ■ Operations charged more than their actual cost build credit stored within a specific object ■ This credit can be used to help pay for operations that are charged less than their actual cost ■ Note that this approach is different from aggregate analysis in which all operations are charged the same amortized cost 16 CPTR 430 Algorithms Amortized Analysis

  17. The Accounting Method (cont.) ■ We want the worst case analysis to show that the amortized (average) cost per operation is small, even though a given operation may in and of itself be expensive (This is exactly the point of aggregate analysis, also) ■ We must show that the total amortized cost of a sequence of operations is an upper bound on the total actual cost of the sequence of operations ❚ This relationship must hold for all sequences ❚ Therefore, the applied credit can never be negative ❚ Should the credit be allowed to become negative, then the cost of the amortized sequence to that point would be less than the actual cost to that point, and so the amortized cost would violate the upper-bound relationship 17 CPTR 430 Algorithms Amortized Analysis

Recommend


More recommend