Chapter 4 Amortized Analysisac.informatik.uni-freiburg.de/teaching/ws17_18/algo1718/...โ€ข...

Post on 27-May-2020

10 views 0 download

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 ๐‘› = ๐‘