Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.

Slides:



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

Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Kinds Of Complexity Worst-case complexity. Average complexity. Amortized complexity.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
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.
Complexity Analysis (Part I)
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.
How should a computer shuffle?. Intro - 2 Comp 122, Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items;
Amortized Analysis.
1 CSE 326: Data Structures Program Analysis Lecture 3: Friday, Jan 8, 2003.
Cmpt-225 Algorithm Efficiency.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Andreas Klappenecker [based on the slides of Prof. Welch]
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Recursive Algorithms October 29, 2014
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
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.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
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.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
CS 361 – Chapter 1 What is this class about? –Data structures, useful algorithm techniques –Programming needs to address both –You already have done some.
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.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
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.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
8/3/07CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
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.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
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]
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
Stack Memory 1 (also called Call Stack)
CSCE 411 Design and Analysis of Algorithms
Discrete Mathematics CS 2610
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation if considered in a sequence of n operations - In a sequence of n operations, some operations may be cheap, some may be expensive (actual cost) - The amortized cost of an operation equals the total cost of the n operations divided by n.

Think of it this way: –You have a bank account with 1000$ and you want to go shopping and purchase some items… –Some items you buy cost 1$, some items you buy cost 100$ –You purchase 20 items in total, therefore… –…the amortized cost of each purchase is 5$ Amortized Analysis

AMORTIZED ANALYSIS: Amortized Analysis You try to estimate an upper bound of the total work T(n) required for a sequence of n operations… Some operations may be cheap some may be expensive. Overall, your algorithm does T(n) of work for n operations … Therefore, by simple reasoning, the amortized cost of each operation is T(n)/n

Imagine T(n) (the budget) being the number of CPU cycles a computer needs to solve the problem If computer spends T(n) cycles for n operations, each operation needs T(n)/n amortized time Amortized Analysis

We prove amortized run times with the Potential method: –Stack example –Binary counter example Amortized Analysis

Amortized Analysis: Stack Example Consider a stack S that holds up to n elements and it has the following three operations: PUSH(S, x) ………. pushes object x in stack S POP(S) ………. pops top of stack S MULTIPOP(S, k) … pops the k top elements of S or pops the entire stack if it has less than k elements

How much a sequence of n PUSH(), POP() and MULTIPOP() operations cost? –A MULTIPOP() may take O(n) time –Therefore (a naïve way of thinking says that): a sequence of n such operations may take O(n n) = O(n 2 ) time since we may call n MULTIPOP() operations of O(n) time each Amortized Analysis: Stack Example With potential method (amortized analysis) we can show a better run time of O(1) per operation!

Potential method: Let the potential of a stack be the number of elements in the stack. Amortized Analysis: Stack Example Actual cost PUSH 1 POP 1 MULTIPOP min(k, S) Amortized cost PUSH 2 POP 0 MULTIPOP 0 Potential diff +1 -min(k,S) + =

The Potential Method Potential is the accumulation of credits –it is associated with the entire data structure rather than individual objects;  (D i ) is a real number associated with the structure D i –the amortized cost is –the total amortized cost is –if we insure that  (D i ) >=  (D 0 ) then the potential never becomes negative –it is often convenient to let  (D 0 ) = 0 then it only needs to be shown that all  (D i ) are nonnegative

Stack Operations - 1 The potential function is the size of the stack the total amortized cost of n operations with respect to  is an upper bound for the actual cost The push operation So The pop operation – the difference in potential for the pop operation is -1 – so the amortized cost is 1 + (-1) = 0 = 2

Stack Operations - 2 The multipop operation where k’ = min(k,s) so Total amortized cost –Each operation has an amortized cost of O(1) –For n operations, the total amortized cost is O(n) –Since the potential function meets all of our requirements, the total cost is a valid upper bound

–A sequence of n POP(), MULTIPOP(), and PUSH() operations needs a total of 2n amortized cost –Each operation costs T(n)/n = 2n/n = O(1) amortized time Accounting Method

Let n-bit counter A[n-1]…A[0] (counts from 0 to 2 n) : –How much work does it take to increment the counter n times starting from zero? –Work T(n): how many bits do you need to flip (0  1 and 1  0) as you increment the counter … Binary Counter Example

INCREMENT(A) 1. i=0; 2. while i < length(A) and A[i]=1 do 3. A[i]=0; 4. i=i+1; 5. if i < length(A) then 6. A[i] = 1 This procedure resets the first i-th sequence of 1 bits and sets A[i] equal to 1 (ex  0100, 0101  0110, 0111  1000)

4-bit counter: Counter value COUNTER Bits flipped (work T(n)) highlighted are bits that flip at each increment A3A2A1A0A3A2A1A0 Binary Counter Example

A naïve approach says that a sequence of n operations on a n-bit counter needs O(n 2 ) work –Each INCREMENT() takes up to O(n) time. n INCREMENT() operations can take O(n 2 ) time Amortized analysis with potential method –We show that amortized cost per INCREMENT() is only O(1) and the total work O(n) Binary Counter Example

Incrementing a Binary Counter - 1 Potential function - the number of 1s in the count after the i th operation Amortized cost of the i th increment operation –suppose that t i bits are reset and one bit is set –since the actual cost is t i + 1, we have Therefore, for n operations, the total cost is O(n)

Incrementing a Binary Counter - 2 Suppose the counter is not initially zero –there are b 0 initial 1s and after n increments b n 1s –But the amortized cost for c are each 2, so Total cost since b 0 < k, if we executed at least n =  (k) increment operations, then the total cost is no more than O(n) no matter what the starting value of the counter