CSE332: Data Abstractions Lecture 25: Amortized Analysis Sandra B. Fan Winter 2011 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Data Structures and Algorithms (60-254)
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
CS 171: Introduction to Computer Science II Hashing and Priority Queues.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
An Approach to Generalized Hashing Michael Klipper With Dan Blandford Guy Blelloch.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
MA/CSSE 473 Day 27 Hash table review Intro to string searching.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions Kevin Quinn Fall 2015.
Stacks And Queues Chapter 18.
CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality Lauren Milne Summer 2015.
CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality Nicki Dell Spring 2014.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Internal and External Sorting External Searching
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
CSE373: Data Structures & Algorithms Lecture 10: Amortized Analysis and Disjoint Sets Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms Lecture Supplement: Amortized Analysis Hunter Zahn Summer 2016 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Priority Queues
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
CSE373: Data Structures & Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE 326: Data Structures: Midterm Review
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Haim Kaplan, Uri Zwick March 2018
Presentation transcript:

CSE332: Data Abstractions Lecture 25: Amortized Analysis Sandra B. Fan Winter

Announcements Homework 8 due NOW Pick up Homeworks 1-7 after lecture from TAs Project 3 due last night 11pm – Unless you are using late days Final exam is Tues, March 15, 2:30-4:20pm HERE – Info on “Final Exam” link on CSE 332 webpage 2

Amortized Analysis This lecture: – What does amortized mean? – How can we prove an amortized bound? Will do a couple simple examples – The text has more complicated examples and proof techniques – The idea of how amortized describes average cost is essential 3

Imagine you want to take your date to a nice restaurant on Friday It will cost you $50 to eat there You earn $100 a week at your crappy part- time job, and you normally spend $20 a day on food at school. If you didn’t eat some days, you could do it, but then you’d be starved. Solution? 4

So, save up your costs! Instead of spending $20 a day on food on Monday thru Thursday, spend $12.50 a day on food, and save up the rest. After 4 days, you have your $50 to spend on your expensive date! 5

Amortized Analysis If you live cheaply, every meal (operation) is cheap and you’re fine, like an algorithm with an operation that is O(1) most of the time. But sometimes, the algorithm has an expensive operation (like your fancy $50 date, or an O(n) run time) Good thing you saved up for your expensive operation!!! 6

Amortized Analysis Recall our plain-old stack implemented as an array that doubles its size if it runs out of room – How can we claim push is O( 1 )? Sure, most calls will take O(1), but some will take O(n) to resize – We can’t claim O(1) as guaranteed run-time, but we can claim it’s an O( 1 ) amortized bound (Very) rough idea: Resizing won’t happen ‘very often’ – Amortized bounds are not a hand-wavy concept though It’s a provable bound for the running time, not necessarily for every operation, but over some sequence of operations Don’t use amortized to mean ‘we don’t really expect it to happen much’ 7

Amortized Complexity If a sequence of M operations takes O( M f(n) ) time, we say the amortized runtime is O( f(n) ) The worst case time per operation can be larger than f(n) – For example, maybe f(n)=1, but the worst-case is n But the worst-case for any sequence of M operations is O( M f(n) ) – Best case could, of course, be better Amortized guarantee ensures the average time per operation for any sequence is O( f(n) ) 8

Amortized Complexity If a sequence of M operations takes O( M f(n) ) time, we say the amortized runtime is O( f(n) )  Another way to think about it: If every possible sequence of M operations runs in O(M*f(n)) time, we have an amortized bound of O(f(n))  A good way to convince yourself that an amortized bound does or does not hold: Try to come up with a worst-possible sequence of operations  Ex: Do BST inserts have an amortized bound of O(logM)?  We can come up with a sequence of M inserts that takes O(M 2 ) time (M in-order inserts); so, no – O(MlogM) is not an amortized bound 9

Amortized != Average Case  The ‘average case’ is generally probabilistic  What data/series of operations are we most likely to see?  Ex: Average case for insertion in BST is O(logn); worst case O(n)  For ‘expected’ data, operations take about O(logn) each  We could come up with a huge # of operations performed in a row that each have O(n) time  O(logn) is not an amortized bound for BST operations  Amortized bounds are not probabilistic  It’s not ‘we expect …’; it’s ‘we are guaranteed...’  If the amortized bound is O(logn), then there does not exist a long series of operations whose average cost is greater than O(logn) 10

Amortized != Average Case Example Consider a hashtable using separate chaining We generally expect O(1) time lookups; why not O(1) amortized? – Imagine a worst-case where all n elements are in one bucket – Lookup one will likely take n/2 time – Because we can concoct this worst-case scenario, it can’t be an amortized bound But the expected case is O(1) because of the expected distribution keys to different buckets (given a decent hash function, prime table size, etc.) 11

Example #1: Resizing stack From lecture 1: A stack implemented with an array where we double the size of the array if it becomes full Claim: Any sequence of push / pop / isEmpty is amortized O( 1 ) Though resizing, when it occurs, will take O(n) Need to show any sequence of M operations takes time O( M )  Recall the non-resizing work is O( M ) (i.e., M* O( 1 ))  Need to show that resizing work is also O( M ) (or less)  The resizing work is proportional to the total number of element copies we do for the resizing  We’ll show that: After M operations, we have done < 2M total element copies (So number of copies per operation is bounded by a constant) 12

Amount of copying How much copying gets done?  Take M=100  Say we start with an empty array of length 32 and finish with 100 elements  Resizes: 64, then 128  Copy from old array: 32, then 64 (Each resize has half that many copies)  Total is 32+64=96 copies, 96< 2M 13

Amount of copying Show: after M operations, we have done < 2M total element copies Let n be the size of the array after M operations  Every time we’re too full to insert, we resize via doubling  Total element copies = INITIAL_SIZE + 2*INITIAL_SIZE + … + n/8 + n/4 + n/2 < n  In order to get it to resize to size n, we need at least half that many pushes : M  n/2  So 2M  n > number of element copies 14

Is it still amortized O(1) if array resizes by constant amount (say 1000) instead of doubling? 15

Constant Amount Resizing  If array grows by a constant amount (say 1000)  Every 1000 inserts, we do additional O(n) work copying  So work per insert is roughly O(1)+O(n/1000) = O(n)  After O( M ) operations, you may have done  ( M 2 ) copies  So, not amortized O( 1 ) the way doubling was 16

Is it O(1) amortized if array shrinks to fit when ½ empty? 17

Shrink when ½ empty Worst case scenario:  When just over half full, pop once and shrink  push once and grow  pop once and shrink, …  Each time we grow and shrink, we do O(n) copies to the new, resized array  So, not amortized O( 1 ) like when doubling 18

What about shrinking to ½ size when ¾ empty? 19

Shrinking to ½ size when ¾ empty?  If array shrinks when ¾ empty, it is amortized O( 1 )  Proof is more complicated, but basic idea remains: by the time an expensive operation occurs, many cheap ones occurred 20

Example #2: Queue with two stacks A queue implementation using only stacks (as on recent homework) 21 class Queue { Stack in = new Stack (); Stack out = new Stack (); void enqueue(E x){ in.push(x); } E dequeue(){ if(out.isEmpty()) { while(!in.isEmpty()) { out.push(in.pop()); } return out.pop(); } CBACBA in out enqueue: A, B, C

Example #2: Queue with two stacks A clever and simple queue implementation using only stacks 22 class Queue { Stack in = new Stack (); Stack out = new Stack (); void enqueue(E x){ in.push(x); } E dequeue(){ if(out.isEmpty()) { while(!in.isEmpty()) { out.push(in.pop()); } return out.pop(); } in out dequeue BCBC Output: A

Example #2: Queue with two stacks A clever and simple queue implementation using only stacks 23 class Queue { Stack in = new Stack (); Stack out = new Stack (); void enqueue(E x){ in.push(x); } E dequeue(){ if(out.isEmpty()) { while(!in.isEmpty()) { out.push(in.pop()); } return out.pop(); } in out enqueue D, E BCBC EDED Output: A

Example #2: Queue with two stacks A clever and simple queue implementation using only stacks 24 class Queue { Stack in = new Stack (); Stack out = new Stack (); void enqueue(E x){ in.push(x); } E dequeue(){ if(out.isEmpty()) { while(!in.isEmpty()) { out.push(in.pop()); } return out.pop(); } in out dequeue twice EDED Output: A, B, C

Example #2: Queue with two stacks A clever and simple queue implementation using only stacks 25 class Queue { Stack in = new Stack (); Stack out = new Stack (); void enqueue(E x){ in.push(x); } E dequeue(){ if(out.isEmpty()) { while(!in.isEmpty()) { out.push(in.pop()); } return out.pop(); } in out dequeue again E Output: A, B, C, D

Analysis  dequeue is not O( 1 ) worst-case because out might be empty and in may have lots of items; need to copy them over in O(n) time  But if the stack operations are (amortized) O( 1 ), then any sequence of queue operations is amortized O( 1 )  How much total work is done per element?  1 push onto in  1 pop off of in  1 push onto out  1 pop off of out  So the total work should be 4n; just shifted around  When you reverse n elements, there were n earlier O( 1 ) enqueue operations you got ‘for cheap’ 26

Amortized bounds not limited to array- based data structures Splay Tree: Another balance BST data structure – Comparable in many ways to AVL tree – Like BST & AVL trees, operations are a function of the height – Height for splay tree can be O(n) – Nonetheless, we have O(logn) amortized bound on operations – A single operation may need to work through a O(n) height tree But splay tree operations (even lookups) shift around the tree – a worst-case tree of height O(n) is guaranteed to be ‘fixed’ over the course of sufficiently many operations 27

Amortized useful?  When the average per operation is all we care about (i.e., sum over all operations), amortized is perfectly fine  This is the usual situation  If we need every operation to finish quickly, amortized bounds are too weak  In a concurrent setting  Time-critical real-time applications  While amortized analysis is about averages, we are averaging cost- per-operation on worst-case input  Contrast: Average-case analysis is about averages across possible inputs. Example: if all initial permutations of an array are equally likely, then quicksort is O( n log n ) on average even though on some inputs it is O( n 2 )) 28

Not always so simple  Proofs for amortized bounds can be much more complicated  For more complicated examples, the proofs need much more sophisticated invariants and “potential functions” to describe how earlier cheap operations build up “energy” or “money” to “pay for” later expensive operations  See Chapter 11  But complicated proofs have nothing to do with the code! 29

Hope you enjoyed CSE 332! Collect your homework from TAs outside Good luck on the Final Exam on Tuesday! 30