amortized analysis

35
Amortized Analysis Michael Tsai 2011/4/29

Upload: fynn

Post on 23-Feb-2016

108 views

Category:

Documents


0 download

DESCRIPTION

Amortized Analysis. Michael Tsai 2011/4/29. 為什麼需要 Amortied Analysis. O(f(n)). running time. =O(n f(n)) ??. 估得太鬆了 !. n operations. Amortized Analysis. Amortized: Reduce or extinguish (a debt) by money regularly put aside 攤 銷 可以想成是把一連串的 operation 一起考慮它的花費 ( 也就是執行時間 ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Amortized Analysis

Amortized AnalysisMichael Tsai2011/4/29

Page 2: Amortized Analysis

2

為什麼需要 Amortied AnalysisO(f(n))

n operations=O(n f(n)) ??

runn

ing

time

估得太鬆了 !

Page 3: Amortized Analysis

3

Amortized Analysis Amortized:

Reduce or extinguish (a debt) by money regularly put aside

攤銷 可以想成是把一連串的 operation 一起考慮它的花費 ( 也就是執行時間 ) 一起考慮的時候有時候估計比較準確 (bound比較緊 )

Page 4: Amortized Analysis

4

Aggregated Analysis T(n): n 個 operation 最差狀況下所需花的時間 因此 , 每個 operation 攤分的所需時間

(amortized cost) 為 T(n)/n

在 aggregated analysis 中 , 我們不區分不同 operation 的所需時間 ( 也就是amortized cost 對不同 operation 都一樣 )

Page 5: Amortized Analysis

5

一 : Stack Stack 的基本動作 : Push(S,x) Pop(S) Multipop(S,k)while not STACK_EMPTY(S) and k>0POP(S)k=k-1 使用平常的方法 :

𝑂 (1)𝑂 (1)

min (𝑠 ,𝑘 ) ¿𝑂 (𝑛)

栗子

Page 6: Amortized Analysis

6

Aggregated Analysis 但是事實上,雖然單一 Multipop operation 要花很多時間 任何 n 個連續的 operation 最多只會花 . 因為 stack 裡面最多只會放 n 個 (n 個 push 後 ) 而不管是 pop 或 Multipop, 最多真正 pop 的數目也只會是 n 因此總共花的時間不會超過 .

所以平均每個 operation 所花時間為 在 aggregated analysis 裡面 , 我們把每個

operation 的 amortized cost 設定為 average cost. 在這個例子裡 , 也就說三種 operation 的 amortized

cost 都是 .

Page 7: Amortized Analysis

7

二 : 二進位計數器 A[0..k-1] 放 二進位數的 bits k 位數INCREMENT(A)i=0while A[i]==1 and i<A.lengthA[i]=0i=i+1if i<A.lengthA[i]=1

n 個 operation 要花多少時間 ?

A[5]

A[4]

A[3]

A[2]

A[1]

A[0]

1 0 0 0 1 1

栗子

Page 8: Amortized Analysis

8

使用平常的方法 執行一次 INCREMENT 最多花 最糟的狀況 : 每一位都是 1, 全部清成 0 因此 n 次 INCREMENT 要花 正確的 bound, 但是太鬆

Page 9: Amortized Analysis

9

Aggregated Analysis

Counter

A[7]

A[6]

A[5]

A[4]

A[3]

A[2]

A[1]

A[0]

Cost

0 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 12 0 0 0 0 0 0 1 0 33 0 0 0 0 0 0 1 1 44 0 0 0 0 0 1 0 0 75 0 0 0 0 0 1 0 1 86 0 0 0 0 0 1 1 0 107 0 0 0 0 0 1 1 1 118 0 0 0 0 1 0 0 0 15

翻 n 次翻

翻翻

第 i 位數翻

Page 10: Amortized Analysis

10

Aggregated Analysis 所以 n 個 operation 的 cost 應為 :

Amortized cost for each operation:

Page 11: Amortized Analysis

11

The accounting method “ 發明”一種給每個 operation 不同”虛擬

cost” 的方法 ( 用這種方法可能分析 T(n) 比較容易 ) 每個 operation 給不一樣的 amortized cost 每個 operation 的 amortized cost 可能比實際的

cost 少一點或多一點 可以把 amortized cost > 實際 cost 想成是存錢 可以把 amortized cost < 實際 cost 想成是花錢 有些 operation 可能多存一些錢 , 供給後面的其他花錢的 operation 使用

Page 12: Amortized Analysis

12

限制 如果我們想要用 amortized cost 總和來分析 T(n)最大有多少 , 則必須滿足 這樣的話 , 如果是則也是 注意上面的條件是”任何 n 個 operation” 都要符合喔 ! 如果不符合 : 則不能保障”如果是則也是” ( 也就是說 , 不可以使某個 operation 多花的錢大於之前存的錢 )

Page 13: Amortized Analysis

13

假設我們設定以下的 amortized cost: Push: 2 Pop: 0 Multipop: 0 這樣 , push 先存下的錢 , 足夠讓 pop 和

multipop 花嗎 ? Pop&Multipop 的 amortized cost 設成 0, 因此會花先存下來的錢

一 : Stack

Page 14: Amortized Analysis

14

存錢和花錢的故事

1

2

Push: 付兩塊錢一塊錢付掉 push 本身花的時間

1

一塊錢先存起來( 跟著 push 進去 stack 的 item)

2

Push: 付兩塊錢一塊錢付掉 push 本身花的時間

一塊錢先存起來( 跟著 push 進去 stack 的 item)

存起來的錢 ,是用來給之後pop 出來的花的( 不管是 pop or Multipop)

也就是說 , 不管 n 個 operation的順序是怎麼樣 ,永遠不會有情形是花錢花超過已經存下來的錢 .每個已經在 stack 裡面的 item 都已經預先存下了錢給 pop or Multipop 用 .

the total amortized cost is always a upper bound of the total actual cost.

Page 15: Amortized Analysis

15

Total Amortized Cost 然後呢 ? Total amortized cost=2*push operation數目 =O(n) 因此 total actual cost 也是 O(n)

Page 16: Amortized Analysis

16

假設我們這樣定義 amortized cost: 設定一個 bit 為 1 的時候 : 2 其他都是 0

這樣的設計會不會有錢不夠花的狀況 ?

二 : 二進位計數器

Page 17: Amortized Analysis

17

存錢和花錢的故事 Part IIA[5]

A[4]

A[3]

A[2]

A[1]

A[0]

0 0 0 0 0 0A[5]

A[4]

A[3]

A[2]

A[1]

A[0]

0 0 0 0 0 1

A[0]

A[5]

A[4]

A[3]

A[2]

A[1]

A[0]

0 0 0 0 1 0A[1]

設 A[0]=1: 付兩塊錢一塊錢付掉 push 本身花的時間

一塊錢先存起來( 跟著 A[1] 的 bit 1)

設 A[0]=0: 不付錢用存起來的一塊錢付

設 A[1]=1: 付兩塊錢一塊錢付掉 push 本身花的時間

一塊錢先存起來( 跟著 A[0] 的 bit 1)

也就是說 , 不管 n 個 operation 的順序是怎麼樣 , 永遠不會有情形是花錢花超過已經存下來的錢 . 每個 bit=1 都已經預先存下了錢給 set bit=0 的時候用 .the total amortized cost is always a upper bound of the total actual cost.

Page 18: Amortized Analysis

18

Total Amortized Cost Total amortized cost=n 個 INCREMENT 設定 bit=1 的次數 每次最多只設定一個 bit 為 1 因此 n 次最多設定 n 個 bit 為 1 Total amortized cost=O(n) Total actual cost=O(n)

攝於地下室 , 期中考前一天

Page 19: Amortized Analysis

19

The potential method 比較 : Accounting method 是使用”先存後花”的方式 Potential method 則是用 potential 來計算”之前的 operation 使目前的資料結構有殘存多少能量” 物理定義 :Potential energy is the

energy stored in a body or in a system due to its position in a force field or due to its configuration*

*http://en.wikipedia.org/wiki/Potential_energy

Page 20: Amortized Analysis

20

定義 做完第 i 個 operation 之後的 data structure ( 一開始為結束 n 個 operation 後為 ) : 的 potential ( 自己定義 ) 則第 i 個 operation 的 amortized cost 為 amortized cost=actual cost 加上

potential 的改變

Page 21: Amortized Analysis

21

限制 要怎麼確定 total amortized cost 是 total

actual cost 的 upper bound 呢 ? 要確定 :

也就是 通常不知道 n 是多少 所以也就是必須對於任何 i

通常會希望 ,

Page 22: Amortized Analysis

22

概念 𝑐 𝑖=𝑐𝑖+Φ (𝐷𝑖 )−Φ (𝐷𝑖 −1 )

Φ (𝐷𝑖 )−Φ (𝐷 𝑖−1 )≥0 amortized cost actual cost則可以想成是存錢 . ( 存到 potential 裡面去 )

Φ (𝐷𝑖 )−Φ (𝐷 𝑖−1 )<0 amortized cost actual cost則可以想成是花錢 . ( 從 potential 裡面拿出錢來 )

Page 23: Amortized Analysis

23

定義 : stack 裡面有多少個 item 則 ( 一開始 stack 沒有東西 ) 且 total amortized cost 是 total actual cost的 upper bound

一 : Stack

Page 24: Amortized Analysis

24

Amortized costs 如果第 i 個 operation 是 push, 目前 stack 有 s 個

item 則 如果第 i 個 operation 是 pop, 目前 stack 有 s 個 item 則 如果第 i 個 operation 是 Multipop(S,k) , 目前 stack有 s 個 item ( 請同學練習看看 ) 設 則

Page 25: Amortized Analysis

25

Total actual cost 因為三個 operation 的 amortized cost 都是

O(1) 因此 n 個 operation ( 由三種 operation 組成 ) 的 amortized cost 也是 O(1) Total amortized cost 是 total actual cost的 upper bound 因此 total actual cost 也是 O(n)

Page 26: Amortized Analysis

26

定義 : A[] 裡面有幾個 bit 是 1 假設為做完第 i 個 operation 後 A[] 裡面 bit 是 1 的數目 假設第 i 個 operation 把個 bit 從 1 設成 0 則第 i 個 operation 的實際花費最多為 ( 個 10, 最多 1 個 01) ( 想想什麼時候沒有 01) 如果 , 則 如果 , 則

二 : 二進位計數器

Page 27: Amortized Analysis

27

Amortized costs

如果一開始 counter=0, 則 ( 所有 i) total amortized cost 為 total actual cost之 upper bound n 個 INCREMENT 之 total amortized cost為 O(n) 所以 total actual cost 也是 O(n)

Page 28: Amortized Analysis

28

變形 如果一開始我們不從 0 開始 count 呢 ? , 不一定成立 , total amortized cost 不一定是 total actual cost 的 upper bound 使用原本的定義的話

因為只有 k 位數 , 所以當 k= 的時候 我們知道 total actual cost =!

Page 29: Amortized Analysis

29

來一個實際一點的例子… 大家很不愛用 malloc, realloc, delete 陣列一開始就開很大 浪費記憶體空間 Dynamic table: 怎麼隨時動態調整 table 大小 , 使得不會有太多空間浪費 , 平均起來也不會浪費很多時間 ? load-factor: ( 跟 hash table 的定義一樣 )

Page 30: Amortized Analysis

30

只有 insert 的 caseTable_Insert(T,x)if T.size==0allocate T.table with 1 slotT.size=1if T.num==T.sizeallocate new-table with 2*T.size slotsinsert all items in T.table into new-tablefree T.tableT.table=new-tableT.size=2*T.sizeinsert x into T.tableT.num=T.num+1

這邊 insert 了 T.size次

這邊 insert 了 1 次假設主要花費時間是這些insertion

Page 31: Amortized Analysis

31

原本的方法 (非 amortized analysis)

如果原本的 table還有空間 , 則 如果原本的 table 沒有空間 , 則 (1 個新的 insertion 跟 i-1 個搬移 insert 到新的 table) 所以一個 operation 的 cost 為 n 個 operation 的 cost 為 一樣 ; 正確但是不 tight

Page 32: Amortized Analysis

32

Aggregate analysis

所以 n 個 operation 的 cost=3n=O(n) 平均每個 operation 的 cost=3=O(1)

, if i-1 is an exact power of 2otherwise

Page 33: Amortized Analysis

33

Accounting method 我們設計每個 table-insert 的 amortized

cost 是 3size/2 insert 的時候付 3 塊錢一塊錢把最新的 item insert 到 table 的時候花掉一塊錢給之前已經在裡面的其中一個 item ( 準備當之後滿了需要移動的時候可以花掉 )一塊錢給自己 ( 準備當之後滿了需要移動的時候可以花掉 )當全滿的時候 , 則每一個 item 都有預存了一塊錢可以拿來搬移

(insert 到新的 table)

Page 34: Amortized Analysis

34

Potential method 定義 則 Table-insert 沒有造成把 table 變大的時候 :

當 Table-insert造成把 table 變大的時候 :

Page 35: Amortized Analysis

35

Reading assignment Section 17.4.2 可以長大也可以縮小的 dynamic table 作業會有 17.4-2 (延續你的閱讀的題目 )