Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed.

Slides:



Advertisements
Similar presentations
October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Advertisements

1 Milk Market Situation Brussels, 23 February 2012.
Lebanese Energy Statistics: A Decade in Review Dr. Joseph Al Assad.
COMPASS® ReportsCOMPASS® Reports Customized List Report Placement Summary Report KDE:OAA:js & pp:2/6/20121
Milk Market Situation Brussels, 19 January Market Situation – 19 January !!! Data from some Member States are confidential and are NOT included.
Smart Data Pricing (SDP) Soumya Sen Joint Work with: Sangtae Ha, Carlee Joe-Wong, Mung Chiang Innovating Data Plans Soumya Sen, WITE
Solving Manufacturing Equipment Monitoring Through Efficient Complex Event Processing Tilmann Rabl, Kaiwen Zhang, Mohammad Sadoghi, Navneet Kumar Pandey,
Hearing Complex Sounds
The Starter Recertification Clinics 9/23/2012 & 9/29/2012.
Design and construction of a mid-IR SPIDER apparatus 09/10/2012 Malte Christian Brahms Imperial College London 09/10/20121.
CDLPA Spring 2012 Plenary Session Legal Aid Ontario Presentation Executive Summary Thursday, May 10, 2012 Kingston, Ontario Bob.
© Dr Kelvyn Youngman, May Two Diagonals – The Change Matrix Here we have the change matrix of the two principals of The Goal; Jonah and Alex Dissatisfaction.
Milk Market Situation Brussels, 20 September 2012.
Selecting Multiple Commodity Codes. 30/03/ Click on STATISTICS, then BUILD YOUR OWN TABLES from the drop-down menu. 2. Click on Data by Commodity.
Mexico’s Competitive Position in the New Global Economy Gordon Hanson UC San Diego and NBER November 2012.
© Dr Kelvyn Youngman, Aug Efrat's Layer 2 At the conference I graphed the 5 layers of resistance onto the change matrix as above 3. Have reservations.
Revised: August “Why don’t you just take your meds?” CIT Officer Wendi Shackelford Anchorage (Alaska) Police Department.
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Remove min  Pairwise.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
Tirgul 9 Amortized analysis Graph representation.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Lecture 3 Tuesday, 2/10/09 Amortized Analysis.
Amortized Anaysis of Algorihms, A.Yazici, Spring 2007CEng Amortized Analysis of Algorithms Adnan YAZICI Dept. of Computer Engineering Middle East.
Amortized Analysis (chap. 17) Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of.
Amortized Analysis.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Andreas Klappenecker [based on the slides of Prof. Welch]
17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
Ajinkya Nene, Lynbrook CS Club. 04/21/2014. Definition Allows you to figure the worst-case bound for the performance of an algorithm (most useful for.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Interval Trees.
Counting in Binary Amortized and Worst-Case Efficiency © Robert E. Tarjan, 2013.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 5: Advanced Design Techniques.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Amortized Complexity Aggregate method. Accounting method. Potential function method.
Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.
CS 361 – Chapter 1 What is this class about? –Data structures, useful algorithm techniques –Programming needs to address both –You already have done some.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
1 Chapter 17: Amortized Analysis I. 2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
CMSC 341 Amortized Analysis.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential.
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
Introduction to Algorithms Amortized Analysis My T. UF.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
1 Chapter 17: Amortized Analysis II. 2 About this lecture Previous lecture shows Aggregate Method This lecture shows two more methods: (2)Accounting Method.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
CS38 Introduction to Algorithms
Complexity analysis.
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Chapter 17: Amortized Analysis I
Presentation by Marty Krogel
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Chapter 17 Amortized Analysis Lee, Hsiu-Hui
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
CSCE 411 Design and Analysis of Algorithms
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed

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/20122Amortized Analysis

Methods of Amortized Analysis Accounting Method: we overcharge some operations early and use them to as prepaid charge later. 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. Potential Method: we maintain credit as potential energy associated with the structure as a whole. 4/15/20123Amortized 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/20124Amortized Analysis

3 ops: Push(S,x)Pop(S) Multi- pop(S,k) Worst-case cost: O(1) O(min(|S|,k) = O(n) Amortized cost: O(1) per operation Aggregate Analysis: Stack Example 4/15/20125Amortized Analysis

Sequence of n push, pop, Multipop operations Worst-case cost of Multipop is O(n) Have n operations Therefore, worst-case cost of sequence is O(n 2 ) Observations Each object can be popped only once per time that its pushed Have <= n pops, including those in Multipop Therefore total cost = O(n) Average over n operations => O(1) per operation on average Notice that no probability involved ……. Aggregate Analysis: Stack Example 4/15/20126Amortized Analysis

2. Accounting Method Charge i th operation a fictitious amortized cost ĉ i, where $1 pays for 1 unit of work (i.e., time). 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/20127Amortized Analysis

3 ops: Push(S,x)Pop(S)Multi-pop(S,k) Assigned cost: 200 Actual cost:11min(|S|,k) Push(S,x) pays for possible later pop of x. ….. Accounting Method: Stack Example 4/15/20128Amortized Analysis

….. Accounting Method: Stack Example When pushing an object, pay $2 $1 pays for the push $1 is prepayment for it being popped by either pop or Multipop Since each object has $1, which is credit, the credit can never go negative Therefore, total amortized cost = O(n), is an upper bound on total actual cost 4/15/20129Amortized Analysis

….. Accounting Method: Binary Counter k-bit Binary Counter: A[0..k 1] I NCREMENT (A) 1. i 0 2. while i < length[A] and A[i] = 1 3. do A[i] 0 reset a bit 4. i i if i < length[A] 6. then A[i] 1 set a bit Introduction 4/15/201210Amortized Analysis

Consider a sequence of n increments. The worst-case time to execute one increment is (k). Therefore, the worst-case time for n increments is n · (k) = (n k). WRONG! In fact, the worst-case cost for n increments is only (n) (n k). Lets see why. Note: Youd be correct if youd said O(n k). But, its an overestimate. ….. Accounting Method: Binary Counter 4/15/201211Amortized Analysis

CtrA[4]A[3]A[2]A[1]A[0]Cost A[0] flipped every op n A[1] flipped every 2 ops n/2 A[2] flipped every 4 ops n/2 2 A[3] flipped every 8 ops n/2 3 … … … … … A[i] flipped every 2 i ops n/2 i Total cost of n operations ….. Accounting Method: Binary Counter 4/15/201212Amortized Analysis

Cost of n increments Thus, the average cost of each increment operation is (n)/n = (1). ….. Accounting Method: Binary Counter 4/15/201213Amortized Analysis

Example: 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) $1 0 1 $ $1 0 1 $1 1 $ $1 1 $1 0 0 Cost = $2 ….. Accounting Method: Binary Counter 4/15/201214Amortized Analysis

When Incrementing, Amortized cost for line 3 = $0 Amortized cost for line 6 = $2 Amortized cost for INCREMENT(A) = $2 Amortized cost for n INCREMENT(A) = $2n =O(n) 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 if i < length[A] 6. then A[i] 1 set a bit ….. Accounting Method: Binary Counter 4/15/201215Amortized 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 D i–1 to D i. The cost of operation i is c i. Define a potential function : {D i } R, such that (D 0 ) = 0 and (D i ) 0 for all i. The amortized cost ĉ i with respect to is defined to be ĉ i = c i + (D i ) – (D i–1 ). 4/15/201216Amortized Analysis

Like the accounting method, but think of the credit as potential stored with the entire data structure. Accounting method stores credit with specific objects while potential method stores potential in the data structure as a whole. Can release potential to pay for future operations Most flexible of the amortized analysis methods ). ….. Potential Method 4/15/201217Amortized Analysis

ĉ i = c i + (D i ) – (D i–1 ) potential difference i If i > 0, then ĉ i > c i. Operation i stores work in the data structure for later use. If i < 0, then ĉ i < c i. The data structure delivers up stored work to help pay for operation i. ….. Potential Method 4/15/201218Amortized Analysis

The total amortized cost of n operations is Summing both sides telescopically. ….. Potential Method since (D n ) 0 and (D 0 ) = 0. 4/15/201219Amortized Analysis

….. Potential Method: Stack Example Define: (D i ) = #items in stackThus, (D 0 )=0. Plug in for operations: Push:ĉ i = c i + (D i ) - (D i-1 ) = 1 + j - (j-1) = 2 Pop:ĉ i = c i + (D i ) - (D i-1 ) = 1 + (j-1) - j = 0 Multi-pop:ĉ i = c i + (D i ) - (D i-1 ) = k + (j-k) - jk=min(|S|,k) = 0 4/15/201220Amortized Analysis

….. Potential Method: Binary Counter Define the potential of the counter after the i th operation by (D i ) = b i, the number of 1s in the counter after the i th operation. Note: (D 0 ) = 0, (D i ) 0 for all i. Example: $1 0 1 $1 0 Accounting method ( 4/15/201221Amortized Analysis

….. Potential Method The amortized cost of the i th INCREMENT is ĉ i = c i + (D i ) – (D i–1 ) = (t i + 1) + (1 t i ) = 2 Assume ith INCREMENT resets t i bits (in line 3). Actual cost c i = (t i + 1) Number of 1s after ith operation: b i = b i–1 – t i + 1 Therefore, n INCREMENTs cost (n) in the worst case. 4/15/201222Amortized Analysis

4/15/201223Amortized Analysis

4/15/201224Amortized Analysis

4/15/201225Amortized Analysis