n ops take time t n push s x o 1 avg cost per op t n n
play

} n ops take time T(n) PUSH(S,x) --- O(1) Avg cost per op = - PDF document

Stack Example Stack Example Amortized Analysis: 3 Methods Amortized Analysis: 3 Methods Aggregate Consider a stack data structure with 3 ops } n ops take time T(n) PUSH(S,x) --- O(1) Avg cost per op = T(n)/n (Amortized


  1. Stack Example Stack Example Amortized Analysis: 3 Methods Amortized Analysis: 3 Methods • Aggregate Consider a stack data structure with 3 ops } • n ops take time T(n) • PUSH(S,x) --- O(1) • Avg cost per op = T(n)/n (Amortized Cost) • Imprecise – no seperate cost for each item • POP(S) --- O(1) s • Accounting --- (Book-Keeping scheme for counting ops • MULTIPOP(S,k) --- Min(s,k) • Charge each op an amortized cost Stack S • Store in “bank” amount not used • Later ops can used “banked” work • Potential • “Stored work” viewed as “potential energy” • Most flexible & powerful point of view Stack Example (cont.) Aggregate Method Stack Example (cont.) Aggregate Method • If the stack S is initially empty, Non-Tight Bound then an object can be Popped only after it has been pushed. So if Assume O(n) ops. Then the worst there are n ops, then case is a MultiPop of n objects. ≤ ≤ ≤ ≤ Hence, T(n) = O(n 2 ). # Pops # Pushes n • Moreover, counts all Pops, # Pops even those executed by all MultiPop Ergo, T(n)/n =O(n) cost per op. ops. So the total work T(n) for n ops is at most n+n = O(n). Hence, the amortized cost per operation is = T n ( )/ n O (1) 1

  2. Incrementing a Counter: Aggregate Analysis Incrementing a Counter: Aggregate Analysis Incrementing a Counter: Aggregate Analysis Incrementing a Counter: Aggregate Analysis We have a k-bit register which counts up from 0. [ [ ] ] [ ] [ ] [ ] [ ] [ ] [ ] Least Sig A k − 1 A 2 A 1 A 0 bit Most Sig � bit − = ∑ k 1 [ ] [ ] Value of counter i A i 2 = i 0 There are two types of bit flip ops: • Reset to O • Set to 1 Cursory Analysis: Non- -Tight Bound Tight Bound Better Analysis: Tighter Bound Cursory Analysis: Non Better Analysis: Tighter Bound Observation: If register is initially • The computation work for a single bit = set to 0, then after a seq of n ops, O(k), since worst case is k-1 resets and bit A[i] flips exactly: 1 set. { ≤  times, if    / 2 i   i lg n  n  • Hence, work for n ops = O(nk) >  0 times, if    i lg n ∞   lg n ∑ n ∑ Hence, = = < < − = i T n ( ) n 2 2 n   • Ergo, Amortized cost =T(n)/n =O(k)   i 2 = = = = i 0 i 0 = ⇒ = So, T n ( ) O n ( ) T n ( )/ n O (1) 2

  3. Accounting Method Accounting Method Stack Example: Accounting Method Stack Example: Accounting Method Assign Amortized Costs in such a way that Actual Costs: you overcharge on some ops, and undercharge Push 1 on others. Pop 1 Overcharge = Actual Cost + Credit MultiPop Min(k,s) Undercharge = Actual Cost –Credit Amortized Costs: ± Amortized Cost = Actual Cost Credit Push 2 But always, Pop 0 ≤ Total Actual Cost Total Amortized Cost MultiPop 0 Stack Example: Accounting Method Incrementing Counter: Accounting Method Stack Example: Accounting Method Incrementing Counter: Accounting Method Amortized Costs: Associate Credit with Data Structure Set Bit to 1 --- $2 Push Push Push Pop Push Push MultiP Reset Bit to 0 -- $0 P=$ P=$ P=$ P=$ P=$ P=$ P=$ P=$ Note. A bit cannot be reset unless it has P=$ P=$ P=$ P=$ P=$ P=$ P=$ been set sometime in the past. So if we pay $1 $2 $3 $2 $3 $4 $0 Total Credit $2 for each Set (which is $1 too much), $4 $1 $2 $3 $5 $6 $10 Tot Act. Cost then there will be $1 left to pay for a Reset, should it occur. Therefore, the total $2 $4 $6 $6 $8 $10 $10 Total ≥ Amortized available credit will always be 0 Cost ≤ ≤ Actual Cost Total Amortized Cost = Total Actual Cost Total Amortized Cost ≤ ≤ = = 2#Sets ( ( ) ) 2 n O n ( ) = = = = ⇒ = 2 # Push O n ( ) T n ( ) O n ( ) 3

  4. The Potential Method The Potential Method Example: A Dynamic Table Example: A Dynamic Table To be Completed Example: A Dynamic Table(Cont Table(Cont.) .) Example: A Dynamic Table(Cont Table(Cont.) .) Example: A Dynamic Example: A Dynamic 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend