CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.

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

Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed.
CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables.
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Fundamentals of Python: From First Programs Through Data Structures
1 Chapter 17: Amortized Analysis III. 2 Dynamic Table Sometimes, we may not know in advance the #objects to be stored in a table We may allocate space.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
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.
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Delete min  Pairwise.
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.
Andreas Klappenecker [based on the slides of Prof. Welch]
Tirgul 11 Solutions for questions from T2, T3 DFS & BFS - reminder Some Hashing Reminder : don’t forget to run ~dast/bin/testEx3.csh on your jar file before.
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.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
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.
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.
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.
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.
Amortized Algorithm Analysis COP3503 July 25, 2007 Andy Schwartz.
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.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
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.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
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.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Complexity analysis.
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Analysis Of Binomial Heaps
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
Lecture 16 Amortized Analysis
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
Amortized Analysis and Heaps Intro
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting Method

CS333 / Cutler Amortized Analysis 2 Amortized Analysis Amortized analysis computes the average time required to perform a sequence of n operations on a data structure Often worst case analysis is not tight and the amortized cost of an operation is less that its worst case. Analogy (making coffee)

CS333 / Cutler Amortized Analysis 3 Applications of amortized analysis Vectors/ tables Disjoint sets Priority queues Heaps, Binomial heaps, Fibonacci heaps Hashing

CS333 / Cutler Amortized Analysis 4 Difference between amortized and average cost To do averages we need to use probability For amortized analysis no such assumptions are needed We compute the average cost per operation for any mix of n operations

CS333 / Cutler Amortized Analysis 5 Operations on Data Structures A data structure has a set of operations associated with it. Example: A stack with –push(), pop() and MultiPop(k). Often some operations may be slow while others are fast. push() and pop() are fast. MultiPop(k) may be slow. Sometimes the time of a single operation can vary

CS333 / Cutler Amortized Analysis 6 Methods Aggregate analysis- the total amount of time needed for the n operations is computed and divided by n Accounting - operations are assigned an amortized cost. Objects of the data structure are assigned a credit Potential – The prepaid work (money in the “bank”) is represented as “potential” energy that can be released to pay for future operations

CS333 / Cutler Amortized Analysis 7 Aggregate analysis n operations take T(n) time Amortized cost of an operation is T(n)/n

CS333 / Cutler Amortized Analysis 8 Stack - aggregate analysis A stack with operations Push, Pop and Multipop. Multipop(S, k) while not empty(S) and k>0 do Pop(S); k:=k-1 end while

CS333 / Cutler Amortized Analysis 9 Stack - aggregate analysis Push and Pop are O(1) (to move 1 data element) Multipop is O(min(s, k)) where s is the size of the stack and k the number of elements to pop. Assume a sequence of n Push, Pop and Multipop operations

CS333 / Cutler Amortized Analysis 10 Stack - aggregate analysis Each object can be popped only once for each time it is pushed So the total number of times Pop can be called ( directly or from Multipop) is bound by the number of Pushes <=n.

CS333 / Cutler Amortized Analysis 11 Stack - aggregate analysis A sequence of n Push and Pop operations is therefore O(n) and the amortized cost of each is O(n)/n=O(1)

CS333 / Cutler Amortized Analysis 12 Stack Example: Op/Moves <=2 Start push a a push b a b push c a b c Multipop(3) a b pop b a pop a OperationStack a a b a b c pop c 6 Operation: 6 Moves 4 Operation: 6 Moves

CS333 / Cutler Amortized Analysis 13 Accounting Method Charge each operation an (invented) amortized cost. –Often different from actual run time cost. Some operations may have an amortized cost larger than runtime, others may have less. –Unlike businesses we do not want to make a profit –We want to cover the actual cost Amount charged but not used in performing an operation is stored with objects of the data structure

CS333 / Cutler Amortized Analysis 14 Accounting method Later operations can use stored amount to pay for their actual cost Credit balance must not go negative (always enough to pay for performance of future operations)

CS333 / Cutler Amortized Analysis 15 Stack - amortized analysis We assign the amortized costs: $2 for Push $0 for both Pop and Multipop For a sequence of n Push and Pop operations the total amortized cost is at most 2n or O(n)

CS333 / Cutler Amortized Analysis 16 Stack - amortized analysis Each time we do a Push we pay $1 for the actual cost of the Push and the element has a credit of $1. Each time an element is popped we take the $1 credit to pay for it Thus the balance is always nonnegative

CS333 / Cutler Amortized Analysis 17 k bit binary counter Increment (A) i = 0 while i < length[A] and A[i] = 1 A[i] = 0 i ++ if i < length[A] A[i] = 1 Initially the counter contains 0 Eventually it becomes 2 k -1 Next it is reset to k -1 A

CS333 / Cutler Amortized Analysis 18 Aggregate analysis Count number of times a bit is flipped. Let number increments n = 2 k (if n < 2 k analysis similar) A[0] flipped n times A[1] flipped n/2 1 times … A[k - 1] flipped n/2 k-1 times Bit D No Flips bit counter k=3

CS333 / Cutler Amortized Analysis 19 Accounting method Charge amortized cost of $2 to set a bit to 1 When a bit is set to 1, pay $1 for actual cost and store $1 with bit Note: at all times a bit with value 1 has $1 When a bit is reset to 0 use $1 to pay for actual cost $2 per Increment operation

CS333 / Cutler Amortized Analysis 20 Accounting method Let the value stored in the counter be: After increment and a payment of $2 = $1 +$1: $1 $1 $0 $0 $1 $0 $0 $1 $1 $1 $1 $1 $0 $0 $1 $0 $1 $0 $0 $

CS333 / Cutler Amortized Analysis 21 Dynamic table (object table, hash table, vector, etc) The table is dynamic We can’t predict its maximum size We would like to avoid allocating a lot of unused space (reasonable load balance) May not be able to avoid table overflow. Overflow should not cause run time failure

CS333 / Cutler Amortized Analysis 22 java.util.Vector A built in class which is a “growable” array. The user can set: –initialCapacity - the initial capacity of the vector. –capacityIncrement - the amount by which the capacity is increased when the vector overflows. The default for capacityIncrement is to double the size

CS333 / Cutler Amortized Analysis 23 Dynamic table Idea: Allocate more memory as needed. Duplicate the size of the table after each overflow. After each duplication must copy elements from old to new table We assume for now the only operation is insert and calculate amortized cost Table sizes: 1, 2, 4, 8, …, 2 k

CS333 / Cutler Amortized Analysis 24 Aggregate method Op. Size Cost The table before after / Copy

CS333 / Cutler Amortized Analysis 25 Let the number of inserts be 2 k < n  2 k+1 (Note: 2*2 k = 2 k+1 < 2n) At this point the size of the table is 2 k+1 After n inserts: –The total cost for copy operations only is k = 2 k < 2n –The total cost for n inserts (without copy) is n. Total < 3n Therefore the amortized time is O(1). Aggregate Analysis 2k2k 2k2k n

CS333 / Cutler Amortized Analysis 26 Accounting analysis Charge each insert $3. When the table is not full, use $1 for the cost of insert, and store $2 with element When the table doubles from m to 2m: –m/2 elements that never moved before have $2 credit, –m/2 elements which already moved have $0 –After copy all m elements have $0 credit

CS333 / Cutler Amortized Analysis 27 Accounting analysis $0 $2 $0 $2 Size = 4 2 copied elements with $0 2 new elements with $2 $0 Size =8 4 copied elements with $0 4 new elements with $2 Size = 8 4 copied elements with $0

CS333 / Cutler Amortized Analysis 28 java.util.Vector fixed increment Let the number of inserts n satisfy c 0 +(m-1)c < n  c 0 +mc So (n- c 0 )/c  m <1+ (n- c 0 )/c and m =  (n) At this point the size of the array is c 0 +mc After the nth insert: –The total time for copy operations is ? –The total time for n inserts (without copy) is n.

CS333 / Cutler Amortized Analysis 29 java.util.Vector fixed increment m-1 Cost for m vector copies = mc 0 + c  ( … +(m-1)) = mc 0 + c  m(m-1)/2=  (m 2 ) =  (n 2 ) c0c0 c0c0 c0c0 c0c0 ccc c c  Initial capacity c 0 Capacity increment c

CS333 / Cutler Amortized Analysis 30 java.util.Vector fixed increment After the nth insert: –The total time for copy operations is  (n 2 ) –The total time for n inserts (without copy) is n. Average insert time is  (n)