amortized analysis
play

Amortized Analysis performed An individual operation may itself be - PDF document

4/18/2013 Amortized Analysis Amortized analysis averages the time required to perform a sequence of operations on a data structure over all the operations Amortized Analysis performed An individual operation may itself be relatively


  1. 4/18/2013 Amortized Analysis  Amortized analysis averages the time required to perform a sequence of operations on a data structure over all the operations Amortized Analysis performed  An individual operation may itself be relatively expensive, but its average cost (amortized Chapter 14 cost) may be small CPTR 318 1 Some Simple Examples Augmented Stack A normal stack with an additional operation: multipop()  Two simple applications which illustrate how  push(x) — pushes element x onto the stack, as usual the principles of amortized analysis apply:  pop() — pops the top element off of the stack and  Augmented stack returns the popped element, as usual  Binary counter  multipop(k) pops the top k elements off of the stack  Returns a vector containing the popped elements  Notice, cannot just adjust the top-of-stack pointer; each element must be inserted into the result vector Augmented Stack Implementation Analysis of Stack Operations template <typename T> T pop() class MultiStack { vector<T> stack; // Vector of elements {  push(x) — O(1) int top; // Top of stack T result; public: if ( top >= 0 ) { result = stack[top--]; MultiStack(int capacity): stack(vector<T>(capacity)), }  pop() — O(1) top(-1) {} return result; bool isEmpty() const } { return top < 0; vector<T> multipop(int k)  multipop(k) — min( n , k ) , where n is } { int size() const vector<T> result; { while ( !isEmpty() && k != 0 ) the size of the stack { return top + 1; } result.push_back(pop()); void push(const T& newElement) k--; { }  worst case is O( n ) int size = stack.size(); return result; if ( top < size - 1 ) { } stack[++top] = newElement; }; } } 1

  2. 4/18/2013 Analysis of Stack Operations Analysis of Stack Operations  push(x) O(1)  push(x) O(1)  pop() — O(1)  pop() O(1)  multipop(k) — min( n , k ) , where n is  multipop(k) — min( n , k ) , where n is the size of the stack the size of the stack  worst case is O( n )  worst case is O( n ) Analysis of Stack Operations Analysis of Stack Operations  push(x) O(1)  push(x) O(1)  pop() O(1)  pop() O(1)  multipop(k) O(min( n , k )) , where n  multipop(k) O(min( n , k )) , where n is the size of the stack is the size of the stack  worst case is O( n )  worst case is O( n ) Analysis of Stack Operations Accounting Method  push(x) — O(1)  The accounting method charges differing amounts to  pop() — O(1) different operations  multipop(k) — O(min( n , k )) , where n is the size of the  Some operations may be charged more or less than they stack — worst case is O( n ) actually cost  Consider a sequence of n stack operations on an initially  The amount charged to an operation is its amortized cost empty stack  Operations charged more than their actual cost build  The worst-case cost of a multipop is O( n ) , since the stack may credit stored within a specific object contain n elements  This credit can be used to help pay for operations that  The worst-case time for any stack operation is thus O( n ) are charged less than their actual cost  A sequence on n stack operations, worst-case, would be O( n 2 ) → n multipop operations each costing O( n )  This bound is correct but not tight (Why?) 2

  3. 4/18/2013 Stack Charges Stack Example (cont.) Operation Actual Cost Amortized Cost Every push operation is not only charged its actual cost (1), but an  push 1 2 additional cost of 1 for prepayment for any future pop operation, pop whether it be from a single pop , or multiple pops from within 1 0 multipop multipop min(n, k) 0 This credit is associated with the object pushed onto the stack  The operations pop and multipop need be charged nothing, as  All three operations have effective costs  their cost has already been paid of O(1) When an object is popped off, the credit associated with that object   Can we pay for any sequence of stack pays for the pop operation Thus for any sequence of n push , pop , and multipop operations,  operations by charging simply the amortized the total amortized cost is an upper bound on the total actual cost costs? The total amortized cost is O( n ) , and so is the total actual cost  Binary Counter Operation Actual Amortized Credit Cost Cost (Empty stack) - - 0 push(5) 1 2 1 push(3) 1 2 2 push(11) 1 2 3 push(12) 1 2 4 push(3) 1 2 5 pop() 1 0 4 pop() 1 0 3 push(7) 1 2 4 push(10) 1 2 5 push(8) 1 2 6 multipop(5) 5 5 0 1 Total 15 16 Counter Complexity Counter Amortization 3

  4. 4/18/2013 Counter Amortized Analysis 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend