1 Chapter 17: Amortized Analysis II. 2 About this lecture Previous lecture shows Aggregate Method This lecture shows two more methods: (2)Accounting Method.

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.
McGraw-Hill/Irwin ©2008 The McGraw-Hill Companies, All Rights Reserved CHAPTER6CHAPTER6 CHAPTER6CHAPTER6 Residential Financial Analysis.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
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.
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.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
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.
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]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 4 Image Slides.
Copyright © The McGraw-Hill Companies, Inc
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
McGraw-Hill/IrwinCopyright © 2009 by The McGraw-Hill Companies, Inc. All Rights Reserved. Chapter 9 Hypothesis Testing.
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.
CREDIT: Day 2. Types of Credit Credit Cards Loans.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Home. Copyright © by The McGraw-Hill Companies, Inc. All rights reserved.Glencoe Accounting Financial Claims in Accounting Property and Financial Claims.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
Chapter 3 Digital Logic Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 3-2 Combinational vs.
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.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 3 (26/01/2006) Instructor: Haifeng YU.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Section Concepts 2.4 Definitions of b 0 and b –n Slide 1 Copyright (c) The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
17.16 Synthesis of Thyroid Hormone (TH) Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slide number: 1.
Copyright © 2015, 2011, and 2007 Pearson Education, Inc. 1 Chapter 12 Business and Consumer Loans Section 4 Personal Property Loans.
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.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Three Statics of Structures Reactions.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7.
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4-1 CHAPTER 4 Counting Techniques.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
Chapter 4 Stacks
Andreas Klappenecker [partially based on the slides of Prof. Welch]
CS38 Introduction to Algorithms
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
Haim Kaplan, Uri Zwick March 2018
CSCE 411 Design and Analysis of Algorithms
Chapter 9: Medians and Order Statistics
EGR 2131 Unit 12 Synchronous Sequential Circuits
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

1 Chapter 17: Amortized Analysis II

2 About this lecture Previous lecture shows Aggregate Method This lecture shows two more methods: (2)Accounting Method (3) Potential Method

3 Accounting Method In real life, a bank account allows us to save our excess money, and the money can be used later when needed We also have an easy way to check the savings In amortized analysis, the accounting method is very similar …

4 Accounting Method Each operation pays an amortized cost if amortized cost  actual cost, we save the excess in the bank Else, we use savings to help the payment Often, savings can be checked easily based on the objects in the current data structure Lemma: For a sequence of operations, if we have enough to pay for each operation, total actual cost  total amortized cost

5 Super Stack (Take 2) Recall that apart from PUSH/POP, a super stack, supports: SUPER-POP(k): pop top k items in k time Let us now assign the amortized cost for each operation as follows: PUSH = $2 POP or SUPER-POP = $0

6 Super Stack (Take 2) Questions: Which operation “saves money to the bank” when performed? Which operation “needs money from the bank” when performed? How to check the savings in the bank ?

7 Super Stack (Take 2) Does our bank have enough to pay for each SUPER-POP operation? Ans. When SUPER-POP is performed, each popped item donates its corresponding $1 to help the payment  Enough $$ to pay for each SUPER-POP

8 Super Stack (Take 2) Conclusion: Amortized cost of PUSH = 2 Amortized cost of POP/SUPER-POP = 0 Meaning: For any sequence of operations with #PUSH = n 1, #POP = n 2, #SUPER-POP = n 3, total actual cost  2n 1

9 Binary Counter (Take 2) Let us use accounting method to analyze increment operation in a binary counter, whose initial count = We assign amortized cost for each increment = $2 Recall: actual cost = #bits flipped

10 Binary Counter (Take 2) Observation: In each increment operation, at most one bit is set from 0 to 1 (whereas at most the remaining bits are set from 1 to 0) count = count = count = count = 5 E.g.,

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12 Binary Counter (Take 2) Observation: Savings = # of 1’s in the counter To show amortized cost = $2 is enough, we use $1 to pay for flipping some bit x from 0 to 1, and store the excess $1 For other bits being flipped (from 1 to 0), each donates its corresponding $1 to help in paying the operation  Enough to pay for each increment

13 Binary Counter (Take 2) Conclusion: Amortized cost of increment = 2 Meaning: For n increments (with initial count = 0) total actual cost  2n Question: What’s wrong if initial count  0?

14 Accounting Method (Remarks) In contrast to the aggregate method, the accounting method may assign different amortized costs to different operations Another thing: To help the analysis, we usually link each excess $ to a specific object in the data structure (such as an item in a stack, or a bit in a binary counter)  called the credit stored in the object

15 Potential Method In physics, an object at a higher place has more potential energy (due to gravity) than an object at a lower place More potential Less potential

16 Potential Method The potential energy can usually be measured by some function of the status of the object (in fact, its height) In amortized analysis, the potential method is very similar … It uses a potential function to measure the potential of a data structure, based on its current status

17 Potential Method Thus, potential of a data structure may increase or decrease after an operation The potential is similar to the $ in the accounting method, which can be used to help in paying an operation

18 Potential Method Each operation pays an amortized cost, and If potential increases by d after an operation, we need: amortized cost  actual cost + d If potential decreases by d after an operation, we need: amortized cost  actual cost - d

19 Potential Method To combine the above, we let  = potential function D i = data structure after i th operation c i = actual cost of i th operation  i = amortized cost of i th operation Then, we always need:  i  c i +  D i  D i-1 

20 Potential Method Because smaller amortized cost gives better (tighter) analysis, so in general, we set:  i = c i +  D i  D i-1  Consequently, after n operations, total amortized cost = total actual cost +  D n  D 0 

21 Potential Method Any  such that  D i  D 0  for all i should work, as it implies total amortized cost at any time  total actual cost at any time Our target is to find the best such  so that amortized cost can be minimized

22 Super Stack (Take 3) Let us now use potential method to analyze the operations on a super stack Define  such that for a super stack S  S  = #items in S Thus we have:  D 0  = 0, and  D i  D 0  for all i

23 Super Stack (Take 3) PUSH increases potential by 1  amortized cost of PUSH = = 2 POP decreases potential by 1  amortized cost of POP = 1 + (-1) = 0 SUPER-POP(k) decreases potential by k  amortized cost of SUPER-POP = k + (-k) = 0 [Assume: Stack has enough items before POP/SUPER-POP]

24 Super Stack (Take 3) Conclusion: Because  D 0  = 0, and  D i  D 0  for all i,  total amortized cost  total actual cost Then, by setting amortized cost for each operation accordingly (according to what??) : amortized cost = O(1)

25 Binary Counter (Take 3) Let us now use potential method to analyze the increment in a binary counter Define  such that for a binary counter B  B  = #bits in B which are 1 Thus we have:  D 0  = 0, and  D i  D 0  for all i Assume: initial count = 0

26 Binary Counter (Take 3) From our previous observation, at most 1 bit is set from 0 to 1, the corresponding increase in potential is at most 1 Now, suppose the i th operation resets t i bits from 1 to 0  actual cost c i = t i + 1  potential change = (-t i ) + 1  amortized cost  i = c i + potential change = 2, for all i

27 Binary Counter (Take 3) Conclusion: Because  D 0  = 0, and  D i  D 0  for all i,  total amortized cost  total actual cost Then, by setting amortized cost for each operation accordingly: amortized cost = 2 = O(1)

28 Potential Method (Remarks) Potential method is very similar to the accounting method: we can save something ($/potential) now, which can be used later It usually gives a neat analysis, as the cost of each operation is very specific However, finding a good potential function can be extremely difficult (like magic)

29 Homework Exercises: , , (Due: Nov. 28) Practice ay home: , , , , Ch 19 Fibonacci heaps (Self-study)