Amortized Analysis - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Amortized Analysis

Description:

Amortized analysis guarantees the average performance of each operation in the worst case. ... The total amortized cost of a sequence of operations must be an ... – PowerPoint PPT presentation

Number of Views:487
Avg rating:3.0/5.0
Slides: 25
Provided by: yaotin
Category:

less

Transcript and Presenter's Notes

Title: Amortized Analysis


1
Amortized Analysis
Instructor Yao-Ting Huang
Bioinformatics Laboratory, Department of Computer
Science Information Engineering, National Chung
Cheng University.
2
Amortized Analysis
  • The time required to perform a sequence of
    operations in average over all the operations
    performed.
  • Amortized analysis guarantees the average
    performance of each operation in the worst case.
  • It differs from average-case analysis in that
    probability is not involved.

3
Analyzing Quicksort Average Case
  • So partition generates splits (0n-1, 1n-2,
    2n-3, , n-21, n-10) each with probability
    1/n.
  • If T(n) is the expected running time,

4
Randomized Selection
  • Average case
  • For upper bound, assume ith element always falls
    in larger side of partition

5
Intuition
  • For all n, a sequence of n operations takes worst
    time T(n) in total. The amortize cost of each
    operation is .

6
The aggregate analysis
  • Stack operation
  • - PUSH(S, x)
  • - POP(S)
  • - MULTIPOP(S, k)
  • MULTIPOP(S, k)
  • 1 while not STACK-EMPTY(S) and k ? 0
  • 2 do POP(S)
  • 3 k ? k 1

7
Action of MULTIPOP on a stack S
top ? 23 17 6 39 10 47 ??
initial
top ? 10 47 ?? MULTIPOP(S,4)
?? MULTIPOP(S,7)
8
Amortized Analysis
  • Analysis a sequence of n PUSH, POP, and MULTIPOP
    operation on an initially empty stack.
  • The Multipop is O(n).
  • Overall running time is O(n) .
  • The amortize cost of an operation is

9
Incremental of a binary counter
Use array to implement a binary counter.
10
INCREMENT
  • INCREMENT(A)
  • 1 i ? 0
  • 2 while i lt lengthA and Ai 1
  • 3 do Ai ? 0
  • 4 i ? i 1
  • 5 if i lt lengthA
  • 6 then Ai ? 1

11
Incremental of a binary counter
  • A0 flip each time.
  • A1 flip n/2 times.
  • A2 flip n/4 times.

12
Analysis
  • Amortize Analysis

13
The Accounting Method
  • We assign different charges to different
    operations, with some operations charged more or
    less than the actually cost.
  • The cost we charge an operation is called its
    amortized cost.

14
The Accounting Method
  • When an operations amortized cost exceeds its
    actually cost, the difference is assign to
    specific object in the data structure as credit.
  • Credit can be used later on to help pay for
    operations whose amortized cost is less than
    their actual cost.
  • The total amortized cost of a sequence of
    operations must be an upper bound on the total
    actual cost of the sequence.

15
The Accounting Method
  • If we denote the actual cost of the ith operation
    by ci and the amortized cost of the ith operation
    by , we require
  • for all sequence of n operations.
  • The total credit stored in the data structure is
    the difference between the total actual cost.

16
Stack operation
  • Each PUSH operation stores one credit.
  • The actual cost of POP and MULTIPOP are paid by
    credits saved.
  • Each POP and MULTIPOP has sufficient credits to
    pay.

17
Stack operation
  • The total amortized cost is the cost from n push,
    i.e., (2n).
  • Amount of credits s always non-negative.
  • O(n) is an upper bound on the total actual cost.
  • Amortize cost O(1)

18
Incrementing a binary counter
19
Incrementing a binary counter
Do we have sufficient credits?
20
Incrementing a binary counter
  • Each time, there is exactly one 0 that is changed
    into 1.
  • Each bit is starting from 0 ? 1 and saves one
    credit.
  • Each 1? 0 has credit saved.
  • The credits are never negative!
  • Amortized cost is at most 2 O(1).

21
Java Vector
  • The Java Vector is a class which implements an
    array with dynamic size.
  • E.g., Vector container new Vector()
  • container.add(x).
  • container.add(y).
  • container.add(z).

22
Java Vector
  • Internally, the Vector class dynamically allocate
    memory when necessary.
  • Once the array is full, a new array with double
    size is allocated.
  • The old array elements are copied into the new
    array.
  • See the illustration on the board.

23
Amortized Analysis of the Add Operation
  • Assume the initial array size is 1.
  • Consider adding n elements.
  • Add(x1).
  • Add(x2).
  • Add(xn).

24
Amortized Analysis of the Add Operation
  • Note that some operation may take O(n).
  • How about the overall time complexity?
  • How about the amortized time complexity of one
    operation?
Write a Comment
User Comments (0)
About PowerShow.com