Академический Документы
Профессиональный Документы
Культура Документы
Some algorithms involve repeated calls to one or more data structures Example: Heapsort
repeatedly insert keys into a priority queue (heap) repeatedly remove the smallest key from the heap
When analyzing the running time of the overall algorithm, need to sum up the time spent in all the calls to the data structure When different calls take different times, how can we accurately calculate the total time?
CPSC 411, Fall 2008: Set 6 2
Heapsort Example
Each of the n calls to insert into the heap operates on a heap with at most n elements Inserting into a heap with n elements takes O(log n) time So total time spent doing the insertions is O(n log n) time But maybe this is an over-estimate!
different insertions take different amounts of time many of the insertions are on significantly smaller heaps
Amortized Analysis
Purpose is to accurately compute the total time spent in executing a sequence of operations on a data structure Three different approaches:
aggregate method: brute force accounting method: assign costs to each operation so that it is easy to sum them up while still ensuring that result is accurate potential method: a more sophisticated version of the accounting method
Operations are:
Push(S,x) Pop(S) Multipop(S,k) - pop the top k elements time for Push is O(1) time for Pop is O(1) time for Multipop is O(min(|S|,k))
CPSC 411, Fall 2008: Set 6 5
Operation:
Implementation:
Aggregate Method
Show that a sequence of n operations takes T(n) time We can then say that the amortized cost per operation is T(n)/n Makes no distinction between operation types
In a sequence of n operations, the stack never holds more than n elements. So cost of a multipop is O(n) So worst-case cost of any sequence of n operations is O(n2). But this is an over-estimate!
Key idea: total number of elements popped in the entire sequence is at most the total number of Pushes done.
Maximum number of Pushes is n. So time for entire sequence is O(n). And amortized cost per operation is O(n)/n = O(1).
CPSC 411, Fall 2008: Set 6 9
Worst-case time for an increment is O(k), occurs when all k bits are flipped But in a sequence of n operations, not all of them will cause all k bits to flip
bit 0 flips with every increment bit 1 flips with every 2nd increment bit 2 flips with every 4th increment bit k flips with every 2k-th increment
CPSC 411, Fall 2008: Set 6 10
So total cost of the sequence is O(n). Amortized cost per operation is O(n)/n = O(1).
11
Accounting Method
Assign a cost, called the "amortized cost", to each operation Assignment must ensure that the sum of all the amortized costs in a sequence is at least the sum of all the actual costs
Accounting Method
if amortized cost > actual cost then store extra as a credit with an object in the data structure if amortized cost < actual cost then use the stored credits to make up the difference
Never allowed to go into the red! Must have enough credit saved up to pay for any future underestimates.
CPSC 411, Fall 2008: Set 6 13
Aggregate method:
first analyze entire sequence then calculate amortized cost per operation first assign amortized cost per operation check that they are valid (never go into the red) then compute cost of entire sequence of operations
Accounting method:
14
Assign these amortized costs: Push - 2 Pop - 0 Multipop - 0 For Push, actual cost is 1. Store the extra 1 as a credit, associated with the pushed element. Pay for each popped element (either from Pop or Multipop) using the associated credit
CPSC 411, Fall 2008: Set 6 15
There is always enough credit to pay for each operation (never go into red). Each amortized cost is O(1) So cost of entire sequence of n operations is O(n).
16
Idea: 1 is used to pay for flipping a 0 to 1. The extra 1 is stored with the bit to pay for the next change (when it is flipped back to 0)
CPSC 411, Fall 2008: Set 6 17
0 0
0
1
0 0
1
1
0
1
0 0
0 1
0 0
1 1
0 1 1
0 0
0 0
1
0 0 0 1
1
1 0 0
CPSC 411, Fall 2008: Set 6
1
1
1
1
1
1
18
All changes from 1 to 0 are paid for with previously stored credit (never go into red) Amortized time per operation is O(1) total cost of sequence is O(n)
19
Potential Method
Similar to accounting method Amortized costs are assigned in a more complicated way
based on a potential function and the current state of the data structure
Must ensure that sum of amortized costs of all operations in the sequence is at least the sum of the actual costs of all operations in the sequence.
20
Potential Method
Define potential function which maps any state of the data structure to a real number
Notation: D0 - initial state of data structure Di - state of data structure after i-th operation ci - actual cost of i-th operation mi - amortized cost of i-th operation
21
Potential Method
Define amortized cost of i-th operation: mi = ci + (Di) (Di1) This is the actual cost plus the change in the potential from previous state to current state Sum of all the amortized costs is sum of all the actual costs plus (Dn) (D0)
22
Potential Function
23
(D0) = 0, since stack is initially empty (Di) 0, since can't have a negative number of elements in the stack
24
25
All operations have O(1) amortized time So cost of entire sequence is O(n)
26
(D0) = 0, since counter is initially all 0's (Di) 0, since can't have a negative number of 1's in the counter
27
mi = ci + (Di) (Di-1) x 1's are changed to 0 = (x+1) + (bx+1) b and one 0 is changed to 1 =2 All ops have O(1) amortized time So total cost of sequence is O(n)
28