Presentation is loading. Please wait.

Presentation is loading. Please wait.

Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential.

Similar presentations


Presentation on theme: "Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential."— Presentation transcript:

1 Amortized Analysis

2 p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential method Aggregate method A sequence of n operations takes worst-case time T(n) in total. In the worst case, the amortized cost (average cost) per operation is T(n)/n

3 p3. Eg.1 [Stack operation] PUSH(S,x): pushes object x onto stack S POP(S): pops the top of stack S and return the popped object Each runs in O(1) time ( 假設只需一單位時間 ) 故 n 個 PUSH 和 POP operations 需  (n) time 考慮 MULTIPOP(S, k): pops the top k objects of stack S MULTIPOP(S, k) While not STACK-EMPTY(S) and k  0 do { POP(S) k  k-1 } 故執行一 MULTIPOP 需 min{s, k} 個步驟

4 p4. 問題 : 執行上述 PUSH, POP, MULTIPOP n 次 假設 initial stack 為 empty 試問每一 operation 在 worst-case 之 amortized cost? Each object can be popped at most once for each time it is pushed. 上述問題只需 O(n) time O(n)/n = O(1) 每個 operation 平均所需的時間

5 p5. Eg.2 [Incrementing a binary counter] A k-bit binary counter, counts upward from 0 A[k-1] A[k-2]…A[1] A[0] highest order bit lowest order bit Increment(A) { i  0; while i<length[A] and A[i]=1 do A[i]  0; i  i+1; if i<length[A] then A[i]  1; } Ripple-carry counter A[7]A[6]A[5]A[4]A[3]A[2]A[1]A[0] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0

6 p6. 問題 : 執行 n 次 ( 依序 ) Increment operations. 在 worst-case 每一 operation 之 amortized cost 為何 ? A[0] 每 1 次改變一次 A[1] 每 2 次改變一次 A[i] 每 2 i 次改變一次 故 amortized cost of each operation 為 O(n)/n = O(1)

7 p7. Potential method D 0 : initial data structure c i : actual cost of the i-th operation D i : data structure after applying the i-th operation to D i-1  : potential function  (D i ) is a real number : the amortized cost of the i-th operation w.r.t potential function is 若  (D n )   (D 0 ), 則 total amortized cost 為 total actual cost 之一 upper bound

8 p8. Eg. 1 [Stack operation] 定義 potential function  : stack size  (D 0 )=0, 亦知  (D i )  0 i-th op. PUSH: MULTIPOP(S, k): 令 k’=min{k,S} POP: 同理 所以一序列 n 個 op 之 total amortized cost 為 O(n)

9 p9. Eg. 2 [Incrementing a binary counter] 對 counter 而言其 potential function 值為在 i-th op 後 被設定為 1 的 bit 個數 b i = # of 1’s in the counter after the i-th operation Suppose that the i-th Increment op. resets t i bits Actual cost  t i +1 # of 1’s in the counter after the i-th op.: b i  b i-1 – t i + 1

10 p10. Implement a Queue with two Stacks: Stack A : Dequeue Stack B : Enqueue S B : stack size of B Enqueue(x) { PUSH(B, x) } Dequeue(Q) { if Empty(Q) then return “empty queue”; else if Empty(A) then while not Empty(B) do PUSH(A, POP(B); POP(A); } Stack AStack B frontrear

11 p11.  (D i )=2[stack size of B after the i-th operation] Enqueue: Dequeue:

12 p12. i100117 Table T:

13 p13. Table expansion: Table-Insert(T, x) { if size[T]=0 then allocate table[T] with 1 slot size[T]=1; if num[T]=size[T] then { allocate new-table with 2*size[T] slots;  Expansion insert all items in table[T] into new-table; free table[T]; table[T] = new-table; size[T] = 2*size[T];} insert x into table[T]; num[T]= num[T] + 1;}

14 p14. Define potential function No expansion after the ith op: With expansion after the ith op:

15 p15. Table expansion and contraction: I, D, I, I, D, D, I, I, I………. : A sequence of n operations. Preserve two properties:. Load factor is bounded below by a constant.. The amortized cost of a table op is bounded above by a const. Strategy:.Double the table size when an item is inserted into a full table..Halve the table size when a deletion causes the table to become less than ¼ full.

16 p16. Define potential function: If the i-th operation is Table-Insert: If the load factor >= ½, then the analysis is the same as before.

17 p17.

18 p18. If the i-th operation is Table-Delete:

19 p19. Exercise! 17.4-2.


Download ppt "Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential."

Similar presentations


Ads by Google