Data Structures Haim Kaplan and Uri Zwick February 2010 Lecture 2 Amortized Analysis “amortized analysis finds the average running time per operation over.

Slides:



Advertisements
Similar presentations
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Foundation of Computing Systems Lecture 2 Linked Lists.
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 Using C++ 2E
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.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
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.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistant: Yahav Nussbaum Website: Exam: 80% Theoretical Assingnments:
1 Abstract Data Types Stack, Queue Amortized analysis Cormen: Ch 10, 17 (11, 18)
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.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
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.
CS305/503, Spring 2009 Amortized Analysis Michael Barnathan.
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]
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.
Data Structures Hashing Uri Zwick January 2014.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
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.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum view.php?id=
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
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.
Amortized Algorithm Analysis COP3503 July 25, 2007 Andy Schwartz.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Linked List by Chapter 5 Linked List by
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Hanoch Levi and Daniel Cohen-Or Nov 2011 Lecture 1 Abstract Data Types Lists, Stacks, Queues, Deques Arrays, Linked Lists Amortized Analysis.
Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need.
Hashing TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA Course: Data Structures Lecturer: Haim Kaplan and Uri Zwick.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
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.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
Amortized Analysis.
Abstract Data Types Stack, Queue Amortized analysis
Data Structures Lecture 4 AVL and WAVL Trees Haim Kaplan and Uri Zwick
Data Structures Lectures: Haim Kaplan, Uri Zwick Exam: 80%
Abstract Data Types Stack, Queue Amortized analysis
Data Structures Website: Lectures: Haim Kaplan and Uri Zwick
Haim Kaplan, Uri Zwick March 2018
CS 332: Algorithms Amortized Analysis Continued
Lecture-Hashing.
Presentation transcript:

Data Structures Haim Kaplan and Uri Zwick February 2010 Lecture 2 Amortized Analysis “amortized analysis finds the average running time per operation over a worst-case sequence of operations.”worst-case

A ferry tale Cost of moving ferry = C Cost for first 7 passengers = 0 Cost for 8-th passenger = C Average/amortized cost for each passenger = C/8

A ferry tale (Chapter 2) amort(IN) = 1 amort(OUT) = 1 amort(IN) = 2 amort(OUT) = 0 amort(IN) = 3 amort(OUT) = −1 Suppose C=8 Assuming the island is initially empty

Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M n=M What do we do when the array is full? We cannot extend the array Allocate a larger array and copy What should be the size of the new array?

Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M If we start with an empty array and increase its length by 1, then the time to do n Insert-Last is about: n=M Average/amortized time per operation = O(n)

Implementing lists using (circular) arrays with doubling When the array is full, double its size and copy What is the cost of n Insert-Last operations? Assume, for simplicity that n=2 k cost of insertion cost of copying The average/amortized cost of each operation is O(1)

Implementing lists using (circular) arrays with doubling When the array is full, double its size and copy n=2 k seems to be ‘fortunate’ case. What if n=2 k +1 ? Last operation causes copying. cost of insertion cost of copying The amortized cost of each operation is still O(1)

Worst-case bounds Suppose that a data structure supports k different types of operations T 1, T 2,…,T k Let worst(T i ), the maximal time that a single operation of type T i may take. Let op 1, op 2,… op n be a sequence of operations that includes n i operations of type T i. Sometimes, this bound is very loose.

Amortized bounds We say that amort(T i ) is an amortized bound on the cost of an operation of type T i iff for every valid sequence of operations op 1, op 2,… op n that includes n i operations of type T i we have Note: Amortized bounds are bounds, they are not unique

Coin operated computer A token pays for a constant number of operations Each operation may buy tokens and 1.Use them immediately 2.Leave tokens for subsequent operations 3.Use tokens left by previous operations Number of tokens bought is clearly an upper bound on the number of operations performed

The Accounting method Saving for a rainy day - “Keep something, esp. money, for a time in the future when it might be needed” Each “normal” insert operations buys three tokens. It uses one of them to insert the new item. It leaves the other two in the “bank” When the array is full, the bank contains enough tokens to pay for the copying! What about the cost of allocating the new array?

The Accounting method Theorem: If the array contains M/2+k items, where k≥0, then the bank contains at least 2k tokens. Easy proof by induction Corollary: When the array is full, the bank contains enough tokens to pay for copying all the items into a new array Amortized cost of an operation ≡ number of tokens bought by the operation Note: Tokens are only used in the analysis! The data structure doesn’t really manipulate them.

Implementing lists using arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 n M M n amort(Insert-Last) = 3 amort(Delete-Last) = 1 amort(Insert(i)) = n−i+3 amort(Retrieve(i)) = 1

The Potential method Very similar to the accounting method The difference: Instead of specifying in advance how many tokens each operation should buy, specify how many tokens should be in the bank in each state of the data structure Potential ≡  ≡ Balance of bank account

The Potential method Bank account initially empty No overdraft! ≥ 0

Potentials for expanding arrays When array is not full 1 ≤ 2 When array is full M+1 2−M At most 3 in both cases!

Potentials for expanding arrays 1 ≤ The amortized cost of Delete-Last is sometimes negative. When?

Not only doubling Trade-offs between time and space Suppose that instead of doubling, we multiply the size of the array by 1+α, where α>0 Amortized cost of Insert-Last is Find an appropriate potential function

Expanding and shrinking arrays What do we do when a very large array contains very few elements? When n=M, double the size When n=M/2, half the size ? Both worst-case and amortized costs are O(n)

Expanding and shrinking arrays What do we do when a very large array contain very few elements? When n=M, double the size When n=M/4, half the size ! Amortized cost now O(1) Which potential function should we use?

Expanding and shrinking arrays (Dynamic arrays) We always have M/4 < n < M

Lazy deletions from singly linked lists Delete(A) simply sets A.item to null Retrieve-Node(L,i) deletes empty nodes while scanning for the i-th item worst(Delete) = O(1) worst(Retrieve-Node(i)) = unbounded worst(Insert-After) = O(1) L a0a0 a n-1 a1a1 …

Lazy deletions from singly linked lists L a0a0 a n-1 a1a1 … amort(Insert-After) = O(1) amort(Delete) = O(1) amort(Retrieve-Node(i)) = O(i+1)  ≡ Number of deleted items

De-Amortization In some cases, but not all, amortized bounds can be converted into worst-case bounds For example, in dynamic arrays, instead of leaving two tokens for future operations, we can actually move two elements Can we do something similar with lazy singly linked lists?

De-Amortized dynamic arrays L.medium L.large L.small L.medium is always up to date If L.medium is full then L.large is up to date If L.medium is ¼ full then L.small is up to date Updates may need to be performed on all three lists

Amortized vs. Worst-case Amortization gives worst-case bounds for a whole sequence of operations In many cases, this is what we really care about In some cases, such as real-time applications, we need each individual operation to be fast In such cases, amortization is not good enough