Docsity

Amortized Analysis: An Approach to Analyzing Data Structures - Prof. Jared C. Saia, Study notes of Computer Science

Toggle sidebar
bg1
CS 561, Lecture 16
Jared Saia
University of New Mexico
Amortized Analysis
“I will gladly pay you Tuesday for a hamburger today” - Welling-
ton Wimpy
•In amortized analysis, time required to perform a sequence of
data structure operations is averaged over all the operations
performed
•Typically used to show that the average cost of an operation
is small for a sequence of operations, even though a single
operation can cost a lot
1
Amortized analysis
Amortized analysis is not average case analysis.
•Average Case Analysis: the expected cost of each operation
•Amortized analysis: the average cost of each operation in
the worst case
•Probability is not involved in amortized analysis
2
Types of Amortized Analysis
•Aggregate Analysis
•Accounting or Taxation Method
•Potential method
•We’ll see each method used for 1) a stack with the additional
operation MULTIPOP and 2) a binary counter
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Amortized Analysis: An Approach to Analyzing Data Structures - Prof. Jared C. Saia and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 16

Jared Saia University of New Mexico

Amortized Analysis

“I will gladly pay you Tuesday for a hamburger today” - Welling- ton Wimpy

  • In amortized analysis, time required to perform a sequence of data structure operations is averaged over all the operations performed
  • Typically used to show that the average cost of an operation is small for a sequence of operations, even though a single operation can cost a lot

Amortized analysis

Amortized analysis is not average case analysis.

  • Average Case Analysis: the expected cost of each operation
  • Amortized analysis: the average cost of each operation in the worst case
  • Probability is not involved in amortized analysis

Types of Amortized Analysis

  • Aggregate Analysis
  • Accounting or Taxation Method
  • Potential method
  • We’ll see each method used for 1) a stack with the additional operation MULTIPOP and 2) a binary counter

Aggregate Analysis

  • We get an upperbound T (n) on the total cost of a sequence of n operations. The average cost per operation is then T (n)/n, which is also the amortized cost per operation

Stack with Multipop

  • Recall that a standard stack has the operations PUSH and POP
  • Each of these operations runs in O(1) time, so let’s say the cost of each is 1
  • Now for a stack S and number k, let’s add the operation MULTIPOP which removes the top k objects on the stack
  • Multipop just calls Pop either k times or until the stack is empty

Multipop

  • Q: What is the running time of Multipop(S,k) on a stack of s objects?
  • A: The cost is min(s,k) pop operations
  • If there are n stack operations, in the worst case, a single Multipop can take O(n) time

Multipop Analysis

  • Let’s analyze a sequence of n push, pop, and multipop op- erations on an initially empty stack
  • The worst case cost of a multipop operation is O(n) since the stack size is at most n, so the worst case time for any operation is O(n)
  • Hence a sequence of n operations costs O(n^2 )

Binary Counter

  • Initially x = 0 so A[i] = 0 for all i = 0, 1 ,... , k − 1
  • To add 1 to the counter, we use a simple procedure which scans the bits from right to left, zeroing out 1’s until it finally find a zero bit which it flips to a 1

Increment

Increment(A){ i = 0; while(i<k && A[i]=1){ A[i] = 0; i++; } if (i<k) A[i] = 1; }

Analysis

  • It’s not hard to see that in the worst case, the increment procedure takes time Θ(k)
  • Thus a sequence of n increments takes time O(nk) in the worst case
  • Note that again this bound is correct but overly pessimistic
    • not all bits flip each time increment is called!

Aggregate Analysis

  • In fact, we can show that a sequence of n calls to Increment has a worst case time of O(n)
  • A[0] flips every time Increment is called, A[1] flips over every other time, A[2] flips over every fourth time,...
  • Thus if there are n calls to increment, A[0] flips n times, A[1] flips bn/ 2 c times, A[2] flips bn/ 4 c times

Aggregate Analysis

  • In general, for i = 0,... blog nc, bit A[i] flips

⌊ n/ 2 i

⌋ times in a sequence of n calls to Increment on an initially zero counter

  • For i > blog nc, bit A[i] never flips
  • Total number of flips in the sequence of n calls is thus

blog∑ nc

i=

⌊ (^) n 2 i

⌋ < n

∑^ ∞ i=

2 i^

= 2 n (2)

Aggregate Analysis

  • Thus the worst-case time for a sequence of n Increment operations on an initially empty counter is O(n)
  • The average cost of each operation in the worst case then is O(n)/n = O(1)

Accounting or Taxation Method

  • The second method is called the accounting method in the book, but a better name might be the taxation method
  • Suppose it costs us a dollar to do a Push or Pop
  • We can then measure the run time of our algorithm in dollars (Time is money!)

Taxation Method for Multipop

  • Instead of paying for each Push and Pop operation when they occur, let’s tax the pushes to pay for the pops
  • I.e. we tax the push operation 2 dollars, and the pop and multipop operations 0 dollars
  • Then each time we do a push, we spend one dollar of the tax to pay for the push and then save the other dollar of the tax to pay for the inevitable pop or multipop of that item
  • Note that if we do n operations, the total amount of taxes we collect is then 2n

Taxation Scheme

  • Let’s tax the algorithm 2 dollars to set a bit to 1
  • Now we need to show that: 1) this scheme has low total taxes and 2) we will collect enough taxes to pay for all of the bit flips
  • Showing overall taxes are low is easy: Each time Increment is called, it sets at most one bit to a 1
  • So we collect exactly 2 dollars in taxes each time increment is called
  • Thus over n calls to Increment, we collect 2n dollars in taxes

Taxation Scheme

  • We now need to show that our taxation scheme has enough money to pay for the costs of all operations
  • This is easy: Each time we set a bit to a 1, we collect 2 dollars in tax. We use one dollar to pay for the cost of setting the bit to a 1, then we store the extra dollar on that bit
  • When the bit gets flipped back from a 1 to a 0, we use the dollar already on that bit to pay for the cost of the flip!

Binary Counter

  • We’ve shown that we can use the 2 tax each time a bit is set to a 1 to pay for all operations which flip a bit
  • Moreover we know that this taxation scheme collects 2n dol- lars in taxes over n calls to Increment
  • Hence we’ve shown that the amortized cost per call to In- crement is O(1)

In Class Exercise

  • A sequence of Pushes and Pops is performed on a stack whose size never exceeds k
  • After every k operations, a copy of the entire stack is made for backup purposes
  • Show that the cost of n stack operations, including copying the stack, is O(n)

In Class Exercise

  • A sequence of Pushes and Pops is performed on a stack whose size never exceeds k
  • After every k operations, a copy of the entire stack is made for backup purposes
  • Show that the cost of n stack operations, including copying the stack, is O(n)

In Class Exercise

  • Q1: What is your taxation scheme?
  • Q2: What is the maximum amount of taxes this scheme collects over n operations?
  • Q3: Show that your taxation scheme can pay for the costs of all operations