Amortized Analysis Chapter 17 Amortized Analysis What is
- Slides: 50
Amortized Analysis Chapter 17
Amortized Analysis What is Amortized Analysis ? In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed. No involvement of probability Average performance on a sequence of operations, even some operation is expensive. Guarantee average performance of each operation among the sequence in worst case. Amortized analysis is not just an analysis tool, it is also a way of thinking about designing algorithms. 4/15/2012 Amortized Analysis 2
Amortized Analysis Methods of Amortized Analysis q Aggregate Method: we determine an upper bound T(n) on the total sequence of n operations. The cost of each will then be T(n)/n. q Accounting Method: we overcharge some operations early and use them to as prepaid charge later. q Potential Method: we maintain credit as potential energy associated with the structure as a whole. 4/15/2012 Amortized Analysis 3
Amortized Analysis 1. Aggregate Method Show that for all n, a sequence of n operations take worst-case time T(n) in total In the worst case, the average cost, or amortized cost , per operation is T(n)/n. The amortized cost applies to each operation, even when there are several types of operations in the sequence. 4/15/2012 Amortized Analysis 4
Amortized Analysis Aggregate Analysis: Stack Example 3 ops: Push(S, x) Worst-case cost: 4/15/2012 Pop(S) Multipop(S, k) O(1) O(min(|S|, k) = O(n) O(1) Amortized cost: O(1) per operation Amortized Analysis 5
Amortized Analysis ……. Aggregate Analysis: Stack Example Sequence of n push, pop, Multipop operations q Worst-case cost of Multipop is O(n) q Have n operations q Therefore, worst-case cost of sequence is O(n 2) Observations q Each object can be popped only once per time that it’s pushed q Have <= n pushes => <= n pops, including those in Multipop q Therefore total cost = O(n) q Average over n operations => O(1) per operation on average Notice that no probability involved 4/15/2012 Amortized Analysis 6
Amortized Analysis 2. Accounting Method Charge i th operation a fictitious amortized cost ĉi, where $1 pays for 1 unit of work (i. e. , time). q Assign different charges (amortized cost ) to different operations § Some are charged more than actual cost § Some are charged less This fee is consumed to perform the operation. Any amount not immediately consumed is stored in the bank for use by subsequent operations. The bank balance (the credit) must not go negative! We must ensure that for all n. Thus, the total amortized costs provide an upper bound on the total true costs. 4/15/2012 Amortized Analysis 7
Amortized Analysis …. . Accounting Method: Stack Example 3 ops: Push(S, x) Pop(S) Multi-pop(S, k) • Assigned cost: 2 0 0 • Actual cost: 1 1 min(|S|, k) Push(S, x) pays for possible later pop of x. 4/15/2012 Amortized Analysis 8
Amortized Analysis …. . Accounting Method: Stack Example When pushing an object, pay $2 q $1 pays for the push q $1 is prepayment for it being popped by either pop or Multipop q Since each object has $1, which is credit, the credit can never go negative q Therefore, total amortized cost = O(n), is an upper bound on total actual cost 4/15/2012 Amortized Analysis 9
Amortized Analysis …. . Accounting Method: Binary Counter Introduction k-bit Binary Counter: A[0. . k 1] INCREMENT(A) 1. i 0 2. while i < length[A] and A[i] = 1 3. do A[i] 0 ⊳ reset a bit 4. i i+1 5. if i < length[A] 6. then A[i] 1 ⊳ set a bit 4/15/2012 Amortized Analysis 10
Amortized Analysis …. . Accounting Method: Binary Counter Consider a sequence of n increments. The worst-case time to execute one increment is Q(k). Therefore, the worst-case time for n increments is n · Q(k) = Q(n k). In fact, the worst-case cost for n increments is only Q(n) ≪ Q(n k). WRONG! Let’s see why. 4/15/2012 Note: You’d be correct if you’d said O(n k). But, it’s an overestimate. Amortized Analysis 11
Amortized Analysis …. . Accounting Method: Binary Counter Ctr A[4] A[3] A[2] A[1] A[0] Cost 0 0 0 0 1 1 2 0 0 0 1 0 3 Total cost of n operations 3 0 0 0 1 1 4 A[0] flipped every op 4 0 0 1 0 0 7 5 0 0 1 8 6 0 0 1 1 0 10 7 0 0 1 11 8 0 1 0 0 0 15 9 0 1 0 0 1 16 10 0 1 0 18 11 0 1 1 19 n A[1] flipped every 2 ops n/2 A[2] flipped every 4 ops n/22 A[3] flipped every 8 ops n/23 … … … A[i] flipped every 2 i ops n/2 i 4/15/2012 Amortized Analysis 12
Amortized Analysis …. . Accounting Method: Binary Counter Cost of n increments Thus, the average cost of each increment operation is Q(n)/n = Q(1). 4/15/2012 Amortized Analysis 13
Amortized Analysis …. . Accounting Method: Binary Counter Charge an amortized cost of $2 every time a bit is set from 0 to 1 • $1 pays for the actual bit setting. • $1 is stored for later re-setting (from 1 to 0). At any point, every 1 bit in the counter has $1 on it… that pays for resetting it. (reset is “free”) Example: 0 0 0 1$1 0 4/15/2012 0 0 0 1$1 1$1 Cost = $2 0 0 0 1$1 0 Cost = $2 0 Amortized Analysis 14
Amortized Analysis …. . Accounting Method: Binary Counter INCREMENT(A) 1. i 0 2. while i < length[A] and A[i] = 1 3. do A[i] 0 ⊳ reset a bit 4. i i+1 5. if i < length[A] 6. then A[i] 1 ⊳ set a bit When Incrementing, q Amortized cost for line 3 = $0 q Amortized cost for line 6 = $2 Amortized cost for INCREMENT(A) = $2 Amortized cost for n INCREMENT(A) = $2 n =O(n) 4/15/2012 Amortized Analysis 15
Amortized Analysis 3. Potential Method IDEA: View the bank account as the potential energy (as in physics) of the dynamic set. FRAMEWORK: Start with an initial data structure D 0. Operation i transforms Di– 1 to Di. The cost of operation i is ci. Define a potential function F : {Di} R, such that F(D 0 ) = 0 and F(Di ) ³ 0 for all i. The amortized cost ĉi with respect to F is defined to be ĉi = ci + F(Di) – F(Di– 1). 4/15/2012 Amortized Analysis 16
Amortized Analysis …. . Potential Method Like the accounting method, but think of the credit as potential stored with the entire data structure. q Accounting method stores credit with specific objects while potential method stores potential in the data structure as a whole. q Can release potential to pay for future operations Most flexible of the amortized analysis methods ). 4/15/2012 Amortized Analysis 17
Amortized Analysis …. . Potential Method ĉi = ci + F(Di) – F(Di– 1) potential difference DFi q If DFi > 0, then ĉi > ci. Operation i stores work in the data structure for later use. q If DFi < 0, then ĉi < ci. The data structure delivers up stored work to help pay for operation i. 4/15/2012 Amortized Analysis 18
Amortized Analysis …. . Potential Method The total amortized cost of n operations is Summing both sides telescopically. since F(Dn) ³ 0 and F(D 0 ) = 0. 4/15/2012 Amortized Analysis 19
Amortized Analysis …. . Potential Method: Stack Example Define: (Di) = #items in stack Thus, (D 0)=0. Plug in for operations: Push: Pop: Multi-pop: 4/15/2012 ĉi = ci + (Di) - (Di-1) = 1 + j - (j-1) =2 ĉi = ci + (Di) - (Di-1) = 1 + (j-1) - j =0 ĉi = ci + (Di) - (Di-1) = k’ + (j-k’) - j =0 Amortized Analysis k’=min(|S|, k) 20
Amortized Analysis …. . Potential Method: Binary Counter Define the potential of the counter after the ith operation by F(Di) = bi, the number of 1’s in the counter after the ith operation. Note: • F(D 0 ) = 0, • F(Di) ³ 0 for all i. Example: 0 0 0 1 0 ( 0 0 0 1$1 0 4/15/2012 Accounting method) Amortized Analysis 21
Amortized Analysis …. . Potential Method Assume ith INCREMENT resets ti bits (in line 3). Actual cost ci = (ti + 1) Number of 1’s after ith operation: bi = bi– 1 – ti + 1 The amortized cost of the i th INCREMENT is ĉi = ci + F(Di) – F(Di– 1) = (ti + 1) + (1 ti) =2 Therefore, n INCREMENTs cost Q(n) in the worst case. 4/15/2012 Amortized Analysis 22
Amortized Analysis 4/15/2012 Amortized Analysis 23
Dynamic Tables
Amortized analyses: Dynamic Table • A nice use of amortized analysis • Table-insertion, table-deletion. • Scenario: – – – A table –maybe a hash table Do not know how large in advance May expend with insertion May contract with deletion Detailed implementation is not important • Goal: – O(1) amortized cost. – Unused space always ≤ constant fraction of allocated space. 4/15/2012 Amortized Analysis 25
Table expansion • TABLE-INSERT • TABLE-DELETE (discuss later) • load-factor • Chapter 17 (load factor) P. 26
Table Insert TABLE_INSERT(T, x) 1 if size[T] = 0 2 then allocate table[T] with 1 slot 3 size[T] 1 4 if num[T] = size[T] 5 then allocate new-table with 2 size[T] slots 6 insert all items in table[T] in new-table 7 free table[T] 8 table[T] new-table 9 size[T] 2 size[T] 10 insert x into table[T] 11 num[T] + 1 Chapter 17 P. 27
Aggregate method: • amortized cost = 3 Chapter 17 P. 28
Accounting method: • each item pays for 3 elementary insertions; 1. inserting itself in the current table, 2. moving itself when the table is expanded, and 3. moving another item that has already been moved once when the table is expanded. Chapter 17 P. 29
Potential method: (not expansion) • Chapter 17 (not expansion) P. 30
Potential method: Example Chapter 17 P. 31
Potential method: (expansion) • Chapter 17 (expansion) P. 32
Potential method: Example (expansion) • Amortized cost = 3 Chapter 17 P. 33
Effect of Insertion on Table Potential 4/15/2012 Amortized Analysis 34
Table expansion and contraction • To implement a TABLE-DELETE operation, it is desirable to contract the table when the load factor of the table becomes too small, so that the waste space is not exorbitant. Chapter 17 P. 35
Goal • • • The load factor of the dynamic table is bounded below by a constant. The amortized cost of a table operation is bounded above by a constant. Set load factor Chapter 17 P. 36
The first operations are inserted. The second operations, we perform I, D, D, I, I, D, D, … • Total cost of these n operations is. Hence the amortized cost is. • Set load factor (as TABLE_DELETE) (after the contraction, the load factor become ) • Chapter 17 P. 37
4/15/2012 Amortized Analysis 38
Chapter 17 P. 39
• Initial Chapter 17 P. 40
Table Insert • If • if , same as before. if Chapter 17 P. 41
Table Insert (Example) Chapter 17 P. 42
Table Insert • If Chapter 17 P. 43
Table Insert Amortized cost of Table insert is O(1). Chapter 17 P. 44
Table Delete • If does not cause a contraction (i. e. , Chapter 17 P. 45
Table Delete (Example) Chapter 17 P. 46
Table Delete • Chapter 17 causes a contraction P. 47
Table Delete Chapter 17 P. 48
Table Delete (Example) Chapter 17 P. 49
Amortized Analysis END 4/15/2012 Amortized Analysis 50
- Amortized analysis
- Dynamic arrays and amortized analysis
- Bottom up splay tree
- Amortized analysis ppt
- Aggregate method amortized analysis
- Amortized analysis
- Amortized time big o
- Amortized complexity
- Amortized supersampling
- Amortized complexity
- What are structured analysis tools?
- Cuckoo sandbox online
- Discourse and content analysis
- Differences between contrastive analysis and error analysis
- Contrastive analysis error analysis and interlanguage
- What is fact finding in system analysis and design
- Content analysis and task analysis
- Time structuring in transactional analysis
- Sad vs ooad
- Lex
- Sources of content analysis
- Feasibility
- Kmo and bartlett's test interpretation
- Chapter 4 things fall apart
- What season is approaching the great gatsby chapter 8
- Foreshadowing in chapter 7 of the great gatsby
- Describe two ways nick differs other guests party
- Chapter 3 gatsby summary
- 4. how does myrtle behave as the party progresses?
- Why doesn't holden like the disciples
- System analysis and design chapter 2
- Chapter 14 financial statement analysis solutions
- Maus chapter 5
- Short summary of chapter 8 lord of the flies
- Lotf sparknotes chapter 4
- Lord of the flies theme
- Inventory costing and capacity analysis
- Chapter 4 job analysis and the talent management process
- Frankenstein ch 19
- Flexible budgets and performance analysis
- Subramanyam financial statement analysis
- How to calculate break even sales
- Deflection
- Cables in structural analysis
- Presentation of data chapter 4
- The internal loading can be found by
- Chapter 3 analysis of financial statements
- Chapter 3 cost volume profit analysis
- Chapter 15 document and handwriting analysis
- What are the 3 types of handwriting forgery?
- Determinate and indeterminate structure