Amortized Complexity Aggregate method. Accounting method. Potential function method.

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

Interval Heaps Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in.
Building an Amortization Schedule How the banks do it and what we need to know…
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Remove min  Pairwise.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Analysis Of Fibonacci Heaps. MaxDegree Let N i = min # of nodes in any min (sub)tree whose root has i children. N 0 = 1. N 1 =
Advanced Data Structures Sartaj Sahni
Kinds Of Complexity Worst-case complexity. Average complexity. Amortized complexity.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
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.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
1 Intro to Induction Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
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]
1 of 30 1 st Derivative The formula for the 1 st derivative of a function is as follows: It’s just the difference between subsequent values and measures.
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.
The Cost of Credit BBI2O Introduction To Business Unit 3: Finance 3.D Credit.
Chapter 5 Using The Accounting Framework: America Online Inc. Mark Higgins Chapter 5 Using The Accounting Framework: America Online Inc. Mark Higgins.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Counting in Binary Amortized and Worst-Case Efficiency © Robert E. Tarjan, 2013.
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction & Algorithm Analysis Lauren Milne Summer 2015.
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 Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.
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.
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.
Recurrence Relations Recurrence relations are a further method of modelling growth. They are used to predict the next value in a number pattern The relation.
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.
Something Smaller Than An Atom? Atomic Structure.
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.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
CS38 Introduction to Algorithms
Hilbert’s Hotel
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Analysis Of Binomial Heaps
Chapter 17: Amortized Analysis I
Table Amortized cost: $3 Insert 5 Actual cost: $1.
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
Exploring the Concept By: J, H, Ma, L, and Me.
Using The Accounting Framework:
CSCE 411 Design and Analysis of Algorithms
Counting Elements of Disjoint Sets: The Addition Rule
CS 332: Algorithms Amortized Analysis Continued
VENUS AND JUPITER.
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Amortized Complexity Aggregate method. Accounting method. Potential function method.

Potential Function P(i) = amortizedCost(i) – actualCost(i) + P(i – 1)  (P(i) – P(i – 1)) =  (amortizedCost(i) –actualCost(i)) P(n) – P(0) =  (amortizedCost(i) –actualCost(i)) P(n) – P(0) >= 0 When P(0) = 0, P(i) is the amount by which the first i operations have been over charged.

Potential Function Example a = x + ( ( a + b ) * c + d ) + y ; actual cost amortized cost potential Potential = stack size except at end.

Accounting Method Guess the amortized cost. Show that P(n) – P(0) >= 0.

Accounting Method Example Guess that amortized complexity of processNextSymbol is 2. Start with P(0) = 0. Can show that P(i) >= number of elements on stack after ith symbol is processed. create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol();

Accounting Method Example Potential >= number of symbols on stack. Therefore, P(i) >= 0 for all i. In particular, P(n) >= 0. a = x + ( ( a + b ) * c + d ) + y ; actual cost amortized cost potential

Potential Method Guess a suitable potential function for which P(n) – P(0) >= 0 for all n. Derive amortized cost of ith operation using  P = P(i) – P(i–1) = amortized cost – actual cost amortized cost = actual cost +  P

Potential Method Example Guess that the potential function is P(i) = number of elements on stack after i th symbol is processed (exception is P(n) = 2). P(0) = 0 and P(i) – P(0) >= 0 for all i. create an empty stack; for (int i = 1; i <= n; i++) // n is number of symbols in statement processNextSymbol();

i th Symbol Is Not ) or ; Actual cost of processNextSymbol is 1. Number of elements on stack increases by 1.  P = P(i) – P(i–1) = 1. amortized cost = actual cost +  P = = 2

i th Symbol Is ) Actual cost of processNextSymbol is #unstacked + 1. Number of elements on stack decreases by #unstacked –1.   P = P(i) – P(i–1) = 1 – #unstacked. amortized cost = actual cost +  P = #unstacked (1 – #unstacked) = 2

i th Symbol Is ; Actual cost of processNextSymbol is #unstacked = P(n–1). Number of elements on stack decreases by P(n–1).   P = P(n) – P(n–1) = 2 – P(n–1). amortized cost = actual cost +  P = P(n–1) + (2 – P(n–1)) = 2

Binary Counter n-bit counter Cost of incrementing counter is number of bits that change. Cost of => is 3. Counter starts at 0. What is the cost of incrementing the counter m times?

Worst-Case Method Worst-case cost of an increment is n. Cost of => is 6. So, the cost of m increments is at most mn.

Aggregate Method Each increment changes bit 0 (i.e., the right most bit). Exactly floor(m/2) increments change bit 1 (i.e., second bit from right). Exactly floor(m/4) increments change bit 2. counter 00000

Aggregate Method Exactly floor(m/8) increments change bit 3. So, the cost of m increments is m + floor(m/2) + floor(m/4) < 2m Amortized cost of an increment is 2m/m = 2. counter 00000

Accounting Method Guess that the amortized cost of an increment is 2. Now show that P(m) – P(0) >= 0 for all m. 1 st increment:  one unit of amortized cost is used to pay for the change in bit 0 from 0 to 1.  the other unit remains as a credit on bit 0 and is used later to pay for the time when bit 0 changes from 1 to 0. bits credits

2 nd Increment.  one unit of amortized cost is used to pay for the change in bit 1 from 0 to 1  the other unit remains as a credit on bit 1 and is used later to pay for the time when bit 1 changes from 1 to 0  the change in bit 0 from 1 to 0 is paid for by the credit on bit 0 bits credits

3 rd Increment.  one unit of amortized cost is used to pay for the change in bit 0 from 0 to 1  the other unit remains as a credit on bit 0 and is used later to pay for the time when bit 1 changes from 1 to 0 bits credits

4 th Increment.  one unit of amortized cost is used to pay for the change in bit 2 from 0 to 1  the other unit remains as a credit on bit 2 and is used later to pay for the time when bit 2 changes from 1 to 0  the change in bits 0 and 1 from 1 to 0 is paid for by the credits on these bits bits credits

Accounting Method P(m) – P(0) =  (amortizedCost(i) –actualCost(i)) = amount by which the first m increments have been over charged = number of credits = number of 1s >= 0

Potential Method Guess a suitable potential function for which P(n) – P(0) >= 0 for all n. Derive amortized cost of ith operation using  P = P(i) – P(i–1) = amortized cost – actual cost amortized cost = actual cost +  P

Potential Method Guess P(i) = number of 1s in counter after ith increment. P(i) >= 0 and P(0) = 0. Let q = # of 1s at right end of counter just before ith increment ( => q = 4). Actual cost of ith increment is 1+q.  P = P(i) – P(i – 1) = 1 – q ( => ) amortized cost = actual cost +  P = 1+q + (1 – q) = 2