Chapter 4 Amortized Analysisac.informatik.uni-freiburg.de/teaching/ws17_18/algo1718/...โข...
Transcript of Chapter 4 Amortized Analysisac.informatik.uni-freiburg.de/teaching/ws17_18/algo1718/...โข...
Chapter 4
Amortized Analysis
Algorithm TheoryWS 2017/18
Fabian Kuhn
Algorithm Theory, WS 2017/18 Fabian Kuhn 2
Amortization
โข Consider sequence ๐1, ๐2, โฆ , ๐๐ of ๐ operations(typically performed on some data structure ๐ท)
โข ๐๐: execution time of operation ๐๐โข ๐ป โ ๐๐ + ๐๐ +โฏ+ ๐๐: total execution time
โข The execution time of a single operation might
vary within a large range (e.g., ๐ก๐ โ [1, ๐ ๐ ])
โข The worst case overall execution time might still be small
average execution time per operation might be small inthe worst case, even if single operations can be expensive
Algorithm Theory, WS 2017/18 Fabian Kuhn 3
Analysis of Algorithms
โข Best case
โข Worst case
โข Average case
โข Amortized worst case
What is the average cost of an operationin a worst case sequence of operations?
Algorithm Theory, WS 2017/18 Fabian Kuhn 4
Example 1: Augmented Stack
Stack Data Type: Operations
โข ๐. push(๐ฅ) : inserts ๐ฅ on top of stack
โข ๐.pop() : removes and returns top element
Complexity of Stack Operations
โข In all standard implementations: ๐ 1
Additional Operation
โข ๐บ.multipop(๐) : remove and return top ๐ elements
โข Complexity: ๐ ๐
โข What is the amortized complexity of these operations?
Algorithm Theory, WS 2017/18 Fabian Kuhn 5
Augmented Stack: Amortized Cost
Amortized Cost
โข Sequence of operations ๐ = 1, 2, 3,โฆ , ๐
โข Actual cost of op. ๐: ๐๐โข Amortized cost of op. ๐ is ๐๐ if for every possible seq. of op.,
๐ =
๐=1
๐
๐ก๐ โค
๐=1
๐
๐๐
Actual Cost of Augmented Stack Operations
โข ๐. push ๐ฅ , ๐. pop(): actual cost ๐ก๐ = ๐(1)
โข ๐.multipop ๐ : actual cost ๐ก๐ = ๐ ๐
โข Amortized cost of all three operations is constantโ The total number of โpoppedโ elements cannot be more than the total
number of โpushedโ elements: cost for pop/multipop โค cost for push
Algorithm Theory, WS 2017/18 Fabian Kuhn 6
Augmented Stack: Amortized Cost
Amortized Cost
๐ =
๐
๐ก๐ โค
๐
๐๐
Actual Cost of Augmented Stack Operations
โข ๐. push ๐ฅ , ๐. pop(): actual cost ๐ก๐ โค ๐
โข ๐.multipop ๐ : actual cost ๐ก๐ โค ๐ โ ๐
Algorithm Theory, WS 2017/18 Fabian Kuhn 7
Example 2: Binary Counter
Incrementing a binary counter: determine the bit flip cost:Operation Counter Value Cost
00000
1 00001 1
2 00010 2
3 00011 1
4 00100 3
5 00101 1
6 00110 2
7 00111 1
8 01000 4
9 01001 1
10 01010 2
11 01011 1
12 01100 3
13 01101 1
Algorithm Theory, WS 2017/18 Fabian Kuhn 8
Accounting Method
Observation:
โข Each increment flips exactly one 0 into a 1
00100๐1111 โน 00100๐0000
Idea:
โข Have a bank account (with initial amount 0)
โข Paying ๐ฅ to the bank account costs ๐ฅ
โข Take โmoneyโ from account to pay for expensive operations
Applied to binary counter:
โข Flip from 0 to 1: pay 1 to bank account (cost: 2)
โข Flip from 1 to 0: take 1 from bank account (cost: 0)
โข Amount on bank account = number of ones We always have enough โmoneyโ to pay!
Algorithm Theory, WS 2017/18 Fabian Kuhn 9
Accounting Method
Op. Counter Cost To Bank From Bank Net Cost Credit
0 0 0 0 0
1 0 0 0 0 1 1
2 0 0 0 1 0 2
3 0 0 0 1 1 1
4 0 0 1 0 0 3
5 0 0 1 0 1 1
6 0 0 1 1 0 2
7 0 0 1 1 1 1
8 0 1 0 0 0 4
9 0 1 0 0 1 1
10 0 1 0 1 0 2
Algorithm Theory, WS 2017/18 Fabian Kuhn 10
Potential Function Method
โข Most generic and elegant way to do amortized analysis!โ But, also more abstract than the othersโฆ
โข State of data structure / system: ๐ โ ๐ฎ (state space)
Potential function ๐ฝ:๐ข โ โโฅ๐
โข Operation ๐:โ ๐๐: actual cost of operation ๐
โ ๐บ๐: state after execution of operation ๐ (๐0: initial state)
โ ๐ฝ๐ โ ฮฆ(๐๐): potential after exec. of operation ๐
โ ๐๐: amortized cost of operation ๐:
๐๐ โ ๐๐ +๐ฝ๐ โ๐ฝ๐โ๐
Algorithm Theory, WS 2017/18 Fabian Kuhn 11
Potential Function Method
Operation ๐:
actual cost: ๐ก๐ amortized cost: ๐๐ = ๐ก๐ +ฮฆ๐ โฮฆ๐โ1
Overall cost:
๐ โ
๐=1
๐
๐ก๐ =
๐
๐
๐๐ +ฮฆ0 โฮฆ๐
Algorithm Theory, WS 2017/18 Fabian Kuhn 12
Binary Counter: Potential Method
โข Potential function:๐ฝ:๐ง๐ฎ๐ฆ๐๐๐ซ ๐จ๐ ๐จ๐ง๐๐ฌ ๐ข๐ง ๐๐ฎ๐ซ๐ซ๐๐ง๐ญ ๐๐จ๐ฎ๐ง๐ญ๐๐ซ
โข Clearly, ฮฆ0 = 0 and ฮฆ๐ โฅ 0 for all ๐ โฅ 0
โข Actual cost ๐ก๐: 1 flip from 0 to 1
๐ก๐ โ 1 flips from 1 to 0
โข Potential difference: ฮฆ๐ โฮฆ๐โ1 = 1 โ ๐ก๐ โ 1 = 2 โ ๐ก๐
โข Amortized cost: ๐๐ = ๐ก๐ +ฮฆ๐ โฮฆ๐โ1 = 2
Algorithm Theory, WS 2017/18 Fabian Kuhn 13
Example 3: Dynamic Array
โข How to create an array where the size dynamically adapts to the number of elements stored?โ e.g., Java โArrayListโ or Python โlistโ
Implementation:
โข Initialize with initial size ๐0โข Assumptions: Array can only grow by appending new elements
at the end
โข If array is full, the size of the array is increased by a factor ๐ฝ > 1
Operations (array of size ๐ต):
โข read / write: actual cost ๐ 1
โข append: actual cost is ๐(1) if array is not full, otherwisethe append cost is ๐ ๐ฝ โ ๐ (new array size)
Algorithm Theory, WS 2017/18 Fabian Kuhn 14
Example 3: Dynamic Array
Notation:
โข ๐: number of elements stored
โข ๐: current size of array
Cost ๐๐ of ๐๐๐ append operation: ๐ก๐ = แ1 if ๐ < ๐
๐ฝ โ ๐ if ๐ = ๐
Claim: Amortized append cost is ๐ 1
Potential function ๐ฝ?
โข should allow to pay expensive append operations by cheap ones
โข when array is full, ฮฆ has to be large
โข immediately after increasing the size of the array, ฮฆ should be small again
Algorithm Theory, WS 2017/18 Fabian Kuhn 15
Dynamic Array: Potential Function
Cost ๐๐ of ๐๐๐ append operation: ๐ก๐ = แ1 if ๐ < ๐
๐ฝ โ ๐ if ๐ = ๐
Algorithm Theory, WS 2017/18 Fabian Kuhn 16
Dynamic Array: Amortized Cost
Cost ๐๐ of ๐๐๐ append operation: ๐ก๐ = แ1 if ๐ < ๐
๐ฝ โ ๐ if ๐ = ๐