CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.

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

CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables.
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Remove min  Pairwise.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
Kinds Of Complexity Worst-case complexity. Average complexity. Amortized complexity.
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.
CS305/503, Spring 2009 Amortized Analysis Michael Barnathan.
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.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
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.
Counting in Binary Amortized and Worst-Case Efficiency © Robert E. Tarjan, 2013.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
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.
Analysis of Algorithms
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.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
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.
CSC401 – Analysis of Algorithms Lecture Notes 2 Asymptotic Analysis Objectives: Mathematics foundation for algorithm analysis Amortization analysis techniques.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
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.
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
Asymptotic Notations Algorithms Lecture 9.
Ch 7: Quicksort Ming-Te Chi
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.
CS 583 Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis

CS 473Lecture 122 Amortized Analysis Key point: The time required to perform a sequence of data structure operations is averaged over all operations performed Amortized analysis can be used to show that –The average cost of an operation is small If one averages over a sequence of operations even though a single operation might be expensive

CS 473Lecture 123 Amortized Analysis vs Average Case Analysis Amortized analysis does not use any probabilistic reasoning Amortized analysis guarantees the average performance of each operation in the worst case

CS 473Lecture 124 Amortized Analysis Techniques The most common three techniques –The aggregate method –The accounting method –The potential method If there are several types of operations in a sequence The aggregate method assigns –The same amortized cost to each operation The accounting method and the potential method may assign –Different amortized costs to different types of operations

CS 473Lecture 125 The Aggregate Method Show that sequence of n operations takes –Worst case time T(n) in total for all n The amortized cost (average cost in the worst case) per operation is therefore T(n)  n This amortized cost applies to each operation –Even when there are several types of operations in the sequence

CS 473Lecture 126 Example: Stack Operations P USH (S, x): pushed object x onto stack P OP (S): pops the top of the stack S and returns the popped object M ULTIPOP (S, k): removes the k top objects of the stack S or pops the entire stack if | S |  k P USH and P OP runs in  (1) time –The total cost of a sequence of n P USH and P OP operations is therefore  (n) The running time of M ULTIPOP (S, k) is –  (min(s, k)) where s  | S |

CS 473Lecture 127 Stack Operations: Multipop M ULTIPOP (S, k) while not StackEmpty(S) and k  0 do t  P OP (S) k  k  1 return Running time:  (min(s, k)) where s  | S |

CS 473Lecture 128 The Aggregate Method: Stack Operations Let us analyze a sequence of n P OP, P USH, and M ULTIPOP operations on an initially empty stack The worst case of a M ULTIPOP operation in the sequence is O(n), since the stack size is at most n Hence, a sequence of n operations costs O(n 2 ) –we may have n M ULTIPOP operations each costing O(n) The analysis is correct, however, –Considering worst-case cost of each operation, it is not tight We can obtain a better bound by using aggregate method of amortized analysis

CS 473Lecture 129 The Aggregate Method: Stack Operations Aggregate method considers the entire sequence of n operations –Although a single M ULTIPOP can be expensive –Any sequence of n P OP, P USH, and M ULTIPOP operations on an initially empty sequence can cost at most O(n) Proof: each object can be popped once for each time it is pushed. Hence the number of times that P OP can be called on a nonempty stack including the calls within M ULTIPOP is at most the number of P USH operations, which is at most n  The amortized cost of an operation is the average O(n)  n  O(1)

CS 473Lecture 1210 Example: Incrementing a Binary Counter Implementing a k-bit binary counter that counts upward from 0 Use array A[0  k  1] of bits as the counter where length[A]  k; A[0] is the least significant bit; A[k  1] is the most significant bit; i.e., x   A[i]2 i k1k1 i  0

CS 473Lecture 1211 Binary Counter: Increment I NCREMENT (A, k) i  0 while i  k and A[i]  1 do A[i]  0 i  i +1 if i  k then A[i]  1 return Initially x  0, i.e., A[i]  0 for i  0,1, , k  1 To add 1 (mod 2 k ) to the counter Essentially same as the one implemented in hardware by a ripple-carry counter A single execution of increment takes  (k) in the worst case in which array A contains all 1’s Thus, n increment operations on an initially zero counter takes O(kn) time in the worst case.  NOT TIGHT 

CS 473Lecture The Aggregate Method: Incrementing a Binary Counter Counter value [7][6][5][4][3][2][1][0] Incre cost Total cost Bits that flip to achieve the next value are shaded

CS 473Lecture 1213 The Aggregate Method: Incrementing a Binary Counter Note that, the running time of an increment operation is proportional to the number of bits flipped However, all bits are not flipped at each I NCREMENT A[0] flips at each increment operation A[1] flips at alternate increment operations A[2] flips only once for 4 successive increment operations  In general, bit A[i] flips  n/2 i  times in a sequence of n I NCREMENTs

CS 473Lecture 1214 The Aggregate Method: Incrementing a Binary Counter Therefore, the total number of flips in the sequence is The amortized cost of each operation is O(n)  n  O(1) lg n i  0  n/2 i    i  0 1/2 i  n  2n 2n

CS 473Lecture 1215 The Accounting Method We assign different charges to different operations with some operations charged more or less than they actually cost The amount we charge an operation is called its amortized cost When the amortized cost of an operation exceeds its actual cost the difference is assigned to specific objects in the data structure as credit Credit can be used later to help pay for operations whose amortized cost is less than their actual cost That is, amortized cost of an operation can be considered as being split between its actual cost and credit (either deposited or used)

CS 473Lecture 1216 The Accounting Method Key points in the accounting method: The total amortized cost of a sequence of operations must be an upper bound on the total actual cost of the sequence This relationship must hold for all sequences of operations Thus, the total credit associated with the data structure must be nonnegative at all times Since it represents the amount by which the total amortized cost incurred so far exceed the total actual cost incurred so far

CS 473Lecture 1217 The Accounting Method: Stack Operations Assign the following amortized costs: Push: 2 Pop: 0 Multipop: 0 Notes: Amortized cost of multipop is a constant (0), whereas the actual cost is variable All amortized costs are O(1), however, in general, amortized costs of different operations may differ asymptotically Suppose we use $1 bill top represent each unit of cost

CS 473Lecture 1218 The Accounting Method: Stack Operations We start with an empty stack of plates When we push a plate on the stack we use $1 to pay the actual cost of the push operation we put a credit of $1 on top of the pushed plate At any time point, every plate on the stack has a $1 of credit on it The $1 stored on the plate is a prepayment for the cost of popping it In order to pop a plate from the stack we take $1 of credit off the plate and use it to pay the actual cost of the pop operation

CS 473Lecture 1219 The Accounting Method: Stack Operations Thus by charging the push operation a little bit more we don’t need to charge anything from the pop & multipop operations We have ensured that the amount of credits is always nonnegative since each plate on the stack always has $1 of credit and the stack always has a nonnegative number of plates Thus, for any sequence of n push, pop, multipop operations the total amortized cost is an upper bound on the total actual cost

CS 473Lecture 1220 The Accounting Method: Stack Operations Incrementing a binary counter: Recall that, the running time of an increment operation is proportional to the number of bits flipped Charge an amortized cost of $2 to set a bit to 1 When a bit is set we use $1 to pay for the actual setting of the bit and we place the other $1 on the bit as credit At any time point, every 1 in the counter has a $1 of credit on it Hence, we don’t need to charge anything to reset a bit to 0, we just pay for the reset with the $1 on it

CS 473Lecture 1221 The Accounting Method: Stack Operations The amortized cost of increment can now be determined the cost of resetting bits within the while loop is paid by the dollars on the bits that are reset At most one bit is set to 1, in an increment operation Therefore, the amortized cost of an increment operation is at most 2 dollars The number of 1’s in the counter is never negative, thus the amount of credit is always nonnegative Thus, for n increment operations, the total amortized cost is O(n), which bounds the actual cost

CS 473Lecture 1222 The Potential Method Accounting method represents prepaid work as credit stored with specific objects in the data structure Potential method represents the prepaid work as potential energy or just potential that can be released to pay for the future operations The potential is associated with the data structure as a whole rather than with specific objects within the data structure

CS 473Lecture 1223 The Potential Method We start with an initial data structure D 0 on which we perform n operations For each i  1, 2, …, n, let C i : the actual cost of the i-th operation D i : data structure that results after applying i-th operation to D i  1  : potential function that maps each data structure D i to a real number  (D i )  (D i ): the potential associated with data structure D i i : amortized cost of the i-th operation w.r.t. function 

CS 473Lecture 1224 The Potential Method actual increase in potential cost due to the operation The total amortized cost of n operations is

CS 473Lecture 1225 The Potential Method If we can ensure that  (D i )   (D 0 ) then the total amortized cost is an upper bound on the total actual cost However,  (D n )   (D 0 ) should hold for all possible n since, in practice, we do not always know n in advance Hence, if we require that  (D i )   (D 0 ), for all i, then we ensure that we pay in advance (as in the accounting method)

CS 473Lecture 1226 The Potential Method If  (D i )   (D i  1 ) > 0, then the amortized cost i represents an overcharge to the i-th operation and the potential of the data structure increases If  (D i )   (D i  1 ) < 0, then the amortized cost i represents an undercharge to the i-th operation and the actual cost of the operation is paid by the decrease in potential Different potential functions may yield different amortized costs which are still upper bounds for the actual costs The best potential fn. to use depends on the desired time bounds

CS 473Lecture 1227 The Potential Method: Stack Operations Define  (S)  | S |, the number of objects in the stack For the initial empty stack, we have  (D 0 )  0 Since |S|  0, stack D i that results after ith operation has nonnegative potential for all i, that is  (D i )  0   (D 0 ) for all i total amortized cost is an upper bound on total actual cost Let us compute the amortized costs of stack operations where ith operation is performed on a stack with s objects

CS 473Lecture 1228 The Potential Method: Stack Operations P USH (S) :  (D i )   (D i  1 )  (s  1)  (s)  1 M ULTIPOP (S, k):  (D i )   (D i  1 )   k'   min{s, k } P OP (S) : The amortized cost of each operation is O(1), and thus the total amortized cost of a sequence of n operations is O(n) C i  C i   (D i )   (D i  1 )  1  1  2 ˆ C i  C i   (D i )   (D i  1 )  k'  k'  0 ˆ C i  0, similarly ˆ

CS 473Lecture 1229 The Potential Method: Incrementing a Binary Counter Define  (D i )  b i, number of 1s in the counter after the ith operation Compute the amortized cost of an I NCREMENT operation wrt  Suppose that ith I NCREMENT resets t i bits then, t i  C i  t i  1 The number of 1s in the counter after the ith operation is b i  1  t i  b i  b i  1  t i  1  b i  b i  1  1  t i The amortized cost is therefore C i  C i   (D i )   (D i  1 )  (t i  1)  (1  t i )  2 ˆ

CS 473Lecture 1230 The Potential Method: Incrementing a Binary Counter If the counter starts at zero, then  (D 0 )  0, the number of 1s in the counter after the ith operation Since  (D i )  0 for all i the total amortized cost is an upper bound on the total actual cost Hence, the worst-case cost of n operations is O(n)

CS 473Lecture 1231 The Potential Method: Incrementing a Binary Counter Assume that the counter does not start at zero, i.,e., b 0  0 Then, after n I NCREMENT operations the number of 1s is b n, where 0  b 0, b n  k Since b 0  k, if we execute at least n   (k) I NCREMENT operations the total actual cost is O(n) No matter what initial value the counter contains  C i   C i   (D n )   (D 0 )   2  b n  b 0  2n  b n  b 0 ˆ i  1 n n n