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

amortized analysis
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

4/18/2013 1

1

Amortized Analysis

Chapter 14

CPTR 318

Amortized Analysis

 Amortized analysis averages the time

required to perform a sequence of operations

  • n 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

Some Simple Examples

 Two simple applications which illustrate how

the principles of amortized analysis apply:

 Augmented stack  Binary counter

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 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

template <typename T> class MultiStack { vector<T> stack; // Vector of elements int top; // Top of stack public: MultiStack(int capacity): stack(vector<T>(capacity)), top(-1) {} bool isEmpty() const { return top < 0; } int size() const { return top + 1; } void push(const T& newElement) { int size = stack.size(); if ( top < size - 1 ) { stack[++top] = newElement; } } T pop() { T result; if ( top >= 0 ) { result = stack[top--]; } return result; } vector<T> multipop(int k) { vector<T> result; while ( !isEmpty() && k != 0 ) { result.push_back(pop()); k--; } return result; } };

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)

slide-2
SLIDE 2

4/18/2013 2 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)

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)

Analysis of Stack Operations

 push(x) O(1)  pop() O(1)  multipop(k) O(min(n, k)), where n

is the size of the stack

 worst case is O(n)

Analysis of Stack Operations

 push(x) O(1)  pop() O(1)  multipop(k) O(min(n, k)), where n

is the size of the stack

 worst case is O(n)

Analysis of Stack Operations

 push(x)—O(1)  pop()—O(1)  multipop(k)—O(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)  A sequence on n stack operations, worst-case, would be

O(n2) → n multipop operations each costing O(n)

 This bound is correct but not tight (Why?)

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

slide-3
SLIDE 3

4/18/2013 3 Stack Charges

 All three operations have effective costs

  • f O(1)

 Can we pay for any sequence of stack

  • perations by charging simply the amortized

costs?

Operation Actual Cost Amortized Cost push 1 2 pop 1 multipop min(n, k)

Stack Example (cont.)

Every push operation is not only charged its actual cost (1), but an additional cost of 1 for prepayment for any future pop operation, whether it be from a single pop, or multiple pops from within multipop

This credit is associated with the object pushed onto the stack

The operations pop and multipop need be charged nothing, as their cost has already been paid

When an object is popped off, the credit associated with that object pays for the pop operation

Thus for any sequence of n push, pop, and multipop operations, the total amortized cost is an upper bound on the total actual cost

The total amortized cost is O(n), and so is the total actual cost

Operation Actual Cost Amortized Cost Credit (Empty stack)

  • 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 4 pop() 1 3 push(7) 1 2 4 push(10) 1 2 5 push(8) 1 2 6 multipop(5) 5 1 Total 15 16

5

Binary Counter Counter Complexity Counter Amortization

slide-4
SLIDE 4

4/18/2013 4 Counter Amortized Analysis