Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.

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.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Data Structures Haim Kaplan and Uri Zwick October 2013 Lecture 2 Amortized Analysis “amortized analysis bounds the worst case running time of a sequence.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
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.
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.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
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.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
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]
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.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
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.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
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.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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.
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.
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.
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.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
Introduction to Algorithms Amortized Analysis My T. UF.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
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
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.
Haim Kaplan, Uri Zwick March 2018
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study data structures that are unable to guarantee a good bound on the worst case tirne per operation but will guarantee a good bound on the average time it takes to perform an operation. Example : Consider the stack example described earlier. Clearly, a single multipop(k) operation can take more than O(1) time to execute, in fact the time is min(k, s) where s is the stack-size at that instant. It should be evident, that a sequence of n operations however runs only in O(n) time, yielding an "average" time of O(l) per operation.

Amortized Analysis In an amortized analysis the time required to perform a sequence of data structure operations is averaged over all the 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. Amortized analysis differs from average case analysis in that probability is not involved; an amortized analysis guarantees the average performance of each operation in the worst case.

Formal Schemes for Amortized Analysis Aggregate method. Accounting method. Potential method.

The Aggregate Method Evaluate the overall worst-case cost T(n) (an upper bound) on the total cost of a sequence of n operations. In the worst case, the average cost, or amortized cost, per operation is therefore T(n)/n. Note that the amortized cost for any operation is the same in the aggregate method. On the contrary, the accounting method and the potential method, may assign different amortized costs to different types of operations. Note that T(n)/n is not an average complexity measure in the sense of averaging over lots of different possible inputs. It is a worst-case measure, but expressed in cost per operation.

Example: Two-Stack System Suppose there are two stacks called A and B, manipulated by the following operations: push(S, d): Push a datum d onto stack S. Real Cost = 1. multi-pop(S, k): Removes min(k, |S|) elements from stack S. Real Cost = min(k, |S|). transfer(k): Repeatedly pop elements from stack A and push them onto stack B. until either k elements have been moved, or A is empty. Real Cost = # of elements moved = min(k, |A|).

Illustration B A  n push operations  n elements popped n elements transferred B A

Aggregate Method For a sequence of n operations, there are ≤ n data pushed into either A or B. Therefore there are ≤ n data popped out from either A or B and ≤ n data transferred from A to B. Thus, the total cost of the n operations is ≤ 3n. Thus. Operation Real Cost Amortized Cost Push(A, d) 1 3 Push(B, d) Multipop(A, k) min(k, |A|) Multipop(B, k) min(k, |B|) Transfer(k)

The Accounting Method Assign fixed (possibly different) costs to operations. Compute the aggregate cost based on the fixed costs. There are operations that are overpaid as well as those that are underpaid. The accounting method overcharges some operations early in the sequence, storing the overcharge as "prepaid credit" on specific objects in the data structure. The credit is used later in the sequence to pay for operations that are charged less than they actually cost. Argue that the total underpayment does not exceed the total overpayment.

Illustration Push(A,d)  Pop(A,d)  Pop(B,d)  Transferred()  B A

Accounting Method push(A, d): $3 -- This pays for the push and a pop of the push a transfer and a pop. push(B, d): $2 -- This pays for the push and a pop. multi-pop(S, k): $0 transfer(k): $0 After any n operations you will have 2|A| + |B| dollars in the bank. Thus the bank account never goes negative. Furthermore the amortized cost of the n operations is O(n) (more precisely ≤ 3n).

Comparison Operation Real Cost Aggregate Accounting Potential Push(A, d) 1 3 Push(B, d) 2 Multipop(A, k) min(k, |A|) Multipop(B, k) min(k, |B|) Transfer(k)

The Potential Method D0: the initial data structure on which n operations are performed. Di: the data structure that results after applying the ith operation to data structure Di-1. ci: the actual cost of the ith operation F: a potential function maps each data structure Di to a real number F(Di), which is the potential associated with data structure Di. ĉi: the amortized cost of the ith operation w.r.t. F ĉi = ci + F(Di) - F(Di-1) = ci + DF ĉi = [ci + F(Di) - F(Di-1)] = ci + F(Di) - F(Di-1)

Illustration c1 c2 c3 c4 cn D0 D1 D2 D3 … Dn ĉ1 ĉ2 ĉ3 ĉ4 ĉn F(D0) F(D1) F(D2) F(D3) …

Potential Function Method Let F(A, B) = 2|A| + |B|, then ĉ(push(A, d)) = 1 + DF = 1 + 2 = 3 ĉ(push(B, d)) = 1 + DF = 1 + 1 = 2 ĉ(multi-pop(A, k)) = min(k, |A|) + DF = -min(k, |A|) ĉ(multi-pop(B, k)) = min(k, |B|) + DF = 0 ĉ(transfer(k)) = min(k, |A|) + DF =0 Σĉ = Σc + DF Σc = Σĉ - DF ≤ Σĉ ≤ 3n = O(n). If we can pick F so that F(Di)  F(D0) for all i, and that Σĉ is easy to compute, then Σĉ/n upper-bounds the average cost.

Summary Operation Real Cost Aggregate Accounting Potential Push(A, d) 1 3 Push(B, d) 2 Multipop(A, k) min(k, |A|) -min(k, |A|) Multipop(B, k) min(k, |B|) Transfer(k)

Incrementing a k-bit Counter Incrementing a k-bit nary counter n times starting from 0. Is the cost of increment O(k)? Aggregate Method: The i-th bit is flipped only at every 2i-1th step. So the total number of bit flips is Accounting Method: Set the cost of setting (resetting) a bit to 2 (0). Since a bit cannot be reset unless it is set, the total underpayment is at most the total overpayment. Potential Method: Define F(Di) to be bi, the number of bits 1 in the counter. Let ti be the number of bits that are reset at the ith operation. Then ci = ti +1and ĉ = ti +1+ DF = ti +1+ (1 - ti) = 2. So, the total amortized cost is at most 2n.

Dynamic Tables A dynamic table is a table of variable size, where an expansion (or a contraction is caused when the load factor has become larger (or smaller) than a fixed threshold. Let the expansion threshold be 1 and the expansion rate be 2; i.e., the table size is doubled when an item is to be inserted when the table is full. Let the contraction threshold be 1/4 and the contraction rate be 1/2; i.e., the table size is halved when an item is to be eliminated when the table is exactly one-fourth full.

Implementation of Expansion/Contraction When one of these operations take place we create a new table and move all the elements from the old one to the new one. Suppose that there are n calls of insertion/deletion made, what is the average cost of each operation (in the worst case)?

Expansion/Contraction If the size is kept the same the cost is O(1). If the size is doubled from M to 2M, the actual cost is M+1. The next table size change is after at least M steps for doubling and at least M/2 steps for halving. So the actual cost can be spread over the next M/2 “normal" steps to yield an amortized cost of O(1). If the size is halved from M to M/2, the actual cost is M/4. The next table size change is after at least 3M/4 steps for doubling and at least M/8 steps for halving. So the actual cost can be spread over the next M/8 steps to yield an amortized cost of O(1).