Lecture 16 Amortized Analysis

Slides:



Advertisements
Similar presentations
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Advertisements

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.
ALGORITHMS Introduction. Definition Algorithm: Any well-defined computational procedure that takes some value or set of values as input and produces some.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Tirgul 9 Amortized analysis Graph representation.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
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 Algorithm Analysis COP3503 July 25, 2007 Andy Schwartz.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Using Credit Wisely Ch. 14. Understanding Costs  Before you can compute the cost of credit, you have to know four things:  The amount you are borrowing.
Credit Ratings: How to Determine Your Score Section 6-6.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
An Array-Based Implementation of the ADT List
Personal Finance Personal Loans
22C:21 Problem 2 (Set 1) Solution outline.
Lecture 2: Divide and Conquer
Amortized Analysis.
MORTGAGE & RETIREMENT BINGO
Obtaining Credit.
Chapter 7 Raising money to repay debts: Making good choices and
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Outline lecture Revise arrays Entering into an array
Data Structures I (CPCS-204)
Personal Finance (part II)
Lecture 13 Shortest Path.
Personal Finance.
Unit 4 - Good Debt, Bad Debt:
CSCE 411 Design and Analysis of Algorithms
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.
Cse 373 May 15th – Iterators.
More Stacks Growable stacks Amortized analysis
Unit 4 - Good Debt, Bad Debt:
Personal Finance.
Top 5 Tips for Splitting Your Expenses with Your Partner.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Interest and Investment
Chapter 6: Consumer Credit
Calculating Interest Interest can grow in a couple different ways, and its important to know how much you will be paying back when you borrow money for.
Collections will Severely Damage your Credit Scores
Linked List Intro CSCE 121 J. Michael Moore.
Budgeting and Saving By Mr. Brown
Lectures on Graph Algorithms: searching, testing and sorting
Haim Kaplan, Uri Zwick March 2018
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
More Stacks Growable stacks Amortized analysis
Unit 4 - Good Debt, Bad Debt:
Lecture 2: Divide and Conquer
Amortized Analysis and Heaps Intro
Section 7.7 Simple Interest
Personal Finance Banking and Saving.
Banking and Credit.
$100 $300 $100 $400 $100 $300 $200 $100 $100 $200 $500 $200 $500 $200 $300 $200 $500 $300 $500 $300 $400 $400 $400 $500 $400.
Lecture 12 Shortest Path.
Linked List Intro CSCE 121.
CS 113: Data Structures and Algorithms
Estimating Algorithm Performance
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 20 Hashing Amortized Analysis
Lecture 21 Amortized Analysis
Review of MST Algorithms Disjoint-Set Union Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Lecture 16 Amortized Analysis

“Amortized” verb (used with object), amortized, amortizing. 1. Finance. to liquidate or extinguish (a mortgage, debt, or other obligation), especially by periodic payments to the creditor or to a sinking fund. to write off a cost of (an asset) gradually. Definition from Dictionary.com

Amortized Analysis in Algorithms Scenario: Operation A is repeated many times in an algorithm. In some cases, Operation A is very fast. In some other cases, Operation A can be very slow. Idea: If the bad cases don’t happen very often, then the average cost of Operation A can still be small.

Amortized Analysis in disguise MergeSort For each iteration, steps 4-5 can take different time Worst case: O(n) per iteration  O(n2)? The total amount of time 4-5 can take is O(n). Merge(b[], c[]) a[] = empty i = 1 FOR j = 1 to length(c[]) WHILE b[i] < c[j] a.append(b[i]); i = i+1 a.append(c[j]); j = j+1 RETURN a[]

Amortized Analysis in disguise DFS For each vertex, the number of edges can be different. If a graph has m = 5n edges, and there is one vertex connected to n/2 other vertices. Worst case for a vertex: O(n)  O(n2)? No: the total amount of time is proportional to the number of edges.

Dynamic Array problem Design a data-structure to store an array. Items can be added to the end of the array. At any time, the amount of memory should be proportional to the length of the array. Example: ArrayList in java, vector in C++ Goal: Design a data-structure such that adding an item has O(1) amortized running time.

Why naïve approach does not work 1 2 3 4 5 6 7 a.add(8) 1 2 3 4 5 6 7 8 Need to allocate a new piece of memory, copy the first 7 elements and add 8. a.add(9) 1 2 3 4 5 6 7 8 9 Need to allocate a new piece of memory, copy the first 8 elements and add 9. Running Time for n add operation = O(n2)!

Designing the Data-Structure We don’t want to allocate new memory every time. Idea: when allocating new memory, allocate a larger space. Init() capacity = 1 length = 0 allocate an array a[] of 1 element Add(x) IF length < capacity THEN a[length] = x length = length+1 ELSE capacity = capacity * 2 allocate a new array a[] of capacity elements copy the current array to the new array

How to Analyze? There are 3 basic techniques to analyze the amortized cost of operations. Aggregate Method Accounting (charging) method Potential Argument

Aggregate Method Idea: Compute the total cost of n operations, divide the total cost by n. Conceptually simple Can be difficult to compute for more complicated problems. What’s used in analyzing MergeSort and DFS.

Accounting (charging) method Idea: Have a bank account (of running time) save money in order to pay for the more expensive operations (old fashioned: no credit card, no interest) Major step: Design a way of “charging” the expensive operations to the normal operations. Make sure the bank account always have money.

Potential Argument Recall: Law of physics

Potential Argument Define a “potential function” Φ,Φ≥0. When executing an expensive operation, the potential function should drop (Potential turns into energy) When executing a normal (light) operation, the potential function should increase (Energy turns into potential)

Potential Argument Amortized cost of an operation: Suppose an operation took (real) time Ti, changed the status from xi to xi+1 𝐴𝑖=𝑇𝑖 −Φ 𝑥 𝑖 +Φ( 𝑥 𝑖+1 ) Claim: 𝑖=1 𝑛 𝑇 𝑖 = 𝑖=1 𝑛 𝐴 𝑖 +Φ 𝑥 1 −Φ( 𝑥 𝑛+1 ) Amortized Cost Real Cost Potential Before Potential After