Algorithm analysis Design and Microprocessor - AmortizedAnalysis, Study notes of Design and Analysis of Algorithms

In this document description about Amortized Analysis, Three Methods of Amortized Analysis, Example for amortized analysis, Aggregate Analysis , Analysis of INCREMENT(A), Accounting Method.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Amortized Analysis
Not just consider one operation, but a sequence of
operations on a given data structure.
Average cost over a sequence of operations.
Probabilistic analysis:
Average case running time: average over all possible inputs for
one algorithm (operation).
If using probability, called expected running time.
Amortized analysis:
No involvement of probability
Average performance on a sequence of operations, even some
operation is expensive.
Guarantee average performance of each operation among the
sequence in worst case.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Algorithm analysis Design and Microprocessor - AmortizedAnalysis and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

Amortized Analysis

  • (^) Not just consider one operation, but a sequence of operations on a given data structure.
  • (^) Average cost over a sequence of operations.
  • (^) Probabilistic analysis:
    • (^) Average case running time: average over all possible inputs for one algorithm (operation).
    • (^) If using probability, called expected running time.
  • (^) Amortized analysis:
    • (^) No involvement of probability
    • (^) Average performance on a sequence of operations, even some operation is expensive.
    • (^) Guarantee average performance of each operation among the sequence in worst case.

Three Methods of Amortized Analysis

  • (^) Aggregate analysis:
    • (^) Total cost of n operations/ n ,
  • (^) Accounting method:
    • (^) Assign each type of operation an (different) amortized cost
    • (^) overcharge some operations,
    • (^) store the overcharge as credit on specific objects,
    • (^) then use the credit for compensation for some later operations.
  • (^) Potential method:
    • (^) Same as accounting method
    • (^) But store the credit as “potential energy” and as a whole.

Aggregate Analysis

• In fact, a sequence of n operations on an

initially empty stack cost at most O ( n ). Why?

Each object can be POP only once (including in MULTIPOP) for each time it is PUSHed. #POPs is at most #PUSHs, which is at most n. Thus the average cost of an operation is O ( n )/ n = O (1). Amortized cost in aggregate analysis is defined to be average cost.

Another example: increasing a binary counter

  • (^) Binary counter of length k , A[0.. k -1] of bit array.
  • (^) INCREMENT(A) 1. i  0 2. while i < k and A[ i ]=
  1. do A[ i ] 0 (flip, reset)
  2. ii + 5. if i < k 6. then A[ i ] 1 (flip, set)

Amortized (Aggregate) Analysis of INCREMENT(A) Observation: The running time determined by #flips but not all bits flip each time INCREMENT is called. A[0] flips every time, total n times. A[1] flips every other time,  n /2 times. A[2] flips every forth time,  n /4 times. …. for i =0,1,…, k -1, A[ i ] flips  n / i  times. Thus total #flips is  i = k -  n / i  < ni =  1/ i = 2 n.

Amortized Analysis of INCREMENT(A)

  • (^) Thus the worst case running time is O ( n ) for a sequence of n INCREMENTs.
  • (^) So the amortized cost per operation is O (1).

Accounting Method (cont.)

  • (^) Conditions:
    • (^) suppose actual cost is ci for the i th operation in the sequence, and amortized cost is ci ',
    • i =1 n^ ci '  i =1 n^ ci should hold.
      • (^) since we want to show the average cost per operation is small using amortized cost, we need the total amortized cost is an upper bound of total actual cost.
      • (^) holds for all sequences of operations.
    • Total credits is  i =1 n^ ci ' -  i =1 n^ ci , which should be nonnegative, - Moreover,  i =1 t^ ci ' -  i =1 t^ ci ≥0 for any t >0.

Accounting Method: Stack Operations

  • (^) Actual costs:
    • (^) PUSH :1, POP :1, MULTIPOP: min( s , k ).
  • (^) Let assign the following amortized costs:
    • PUSH:2, POP: 0, MULTIPOP: 0.
  • (^) Similar to a stack of plates in a cafeteria.
    • (^) Suppose $1 represents a unit cost.
    • (^) When pushing a plate, use one dollar to pay the actual cost of the push and leave one dollar on the plate as credit.
    • (^) Whenever POPing a plate, the one dollar on the plate is used to pay the actual cost of the POP. (same for MULTIPOP).
    • (^) By charging PUSH a little more, do not charge POP or MULTIPOP.
  • (^) The total amortized cost for n PUSH, POP, MULTIPOP is O ( n ), thus O (1) for average amortized cost for each operation.
  • (^) Conditions hold: total amortized cost ≥total actual cost, and amount of credits never becomes negative.

The Potential Method

• Same as accounting method:

something prepaid is used later.

• Different from accounting method

  • (^) The prepaid work not as credit, but as “potential energy”, or “potential”.
  • (^) The potential is associated with the data structure as a whole rather than with specific objects within the data structure.

The Potential Method (cont.)

  • (^) Initial data structure D 0 ,
  • (^) n operations, resulting in D 0 , D 1 ,…, Dn with costs c 1 , c 2 ,…,

cn.

  • A potential function : { Di }  R (real numbers)

 ( Di ) is called the potential of Di.

  • Amortized cost ci' of the i th operation is:
    • ci' = ci + ( Di ) - ( Di -1). (actual cost + potential change)

  i =1 n^ ci' =  i =1 n^ ( ci + ( Di ) - ( Di -1))

  • (^) =  i =1 nci + ( Dn ) - ( D 0 )

Potential method: stack operation

  • (^) Potential for a stack is the number of objects in the stack.
  • (^) So ( D 0 )=0, and ( Di )  0
  • (^) Amortized cost of stack operations:
    • (^) PUSH:
      • Potential change: ( Di )- ( Di -1) =( s +1)- s =1.
      • (^) Amortized cost: ci' = ci + ( Di ) - ( Di -1)=1+1=2.
    • (^) POP:
      • Potential change: ( Di )- ( Di -1) =( s -1) – s = -1.
      • (^) Amortized cost: ci' = ci + ( Di ) - ( Di -1)=1+(-1)=0.
    • (^) MULTIPOP( S , k ): k' =min( s , k )
      • (^) Potential change: ( Di )- ( Di -1) = – k'.
      • (^) Amortized cost: ci' = ci + ( Di ) - ( Di -1)= k' +(- k' )=0.
  • (^) So amortized cost of each operation is O (1), and total amortized cost of n operations is O ( n ).
  • (^) Since total amortized cost is an upper bound of actual cost, the worse case cost of n operations is O ( n ).

Potential method: binary counter

  • (^) Define the potential of the counter after the i th INCREMENT is ( Di ) = bi , the number of 1’s. clearly, ( Di )0.
  • (^) Let us compute amortized cost of an operation
    • Suppose the i th operation resets ti bits.
    • (^) Actual cost ci of the operation is at most ti +1.
    • (^) If bi= 0 , then the i th operation resets all k bits, so bi -1= ti =k.
    • If bi> 0, then bi = bi -1- ti +
    • (^) In either case, bibi -1- ti +1.
    • So potential change is ( Di ) - ( Di -1)  bi -1- ti +1- bi -1=1- ti.
    • (^) So amortized cost is: ci' = ci + ( Di ) - ( Di -1)  ti +1+1- ti =2.
  • (^) The total amortized cost of n operations is O ( n ).
  • (^) Thus worst case cost is O ( n ).

Dynamic table

• Load factor α = num/size , where num =

items stored, size = allocated size.

• If size = 0, then num = 0. Call α = 1.

• Never allow α > 1.

• Keep α > a constant fraction  goal (2).

Dynamic table: expansion with insertion

  • (^) Table expansion
  • (^) Consider only insertion.
  • (^) When the table becomes full, double its size and reinsert all existing items.
  • (^) Guarantees that α ≥ 1 / 2.
  • (^) Each time we actually insert an item into the table, it’s an elementary insertion.