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.

Slides:



Advertisements
Similar presentations
CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables.
Advertisements

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.
Hash Tables.
Greedy Algorithms Amihood Amir Bar-Ilan University.
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.
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.
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
Cuckoo Hashing : Hardware Implementations Adam Kirsch Michael Mitzenmacher.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
Data Structures Haim Kaplan and Uri Zwick February 2010 Lecture 2 Amortized Analysis “amortized analysis finds the average running time per operation over.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
CSE 250: Data Structures Week 12 March 31 – April 4, 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.
Amortized Anaysis of Algorihms, A.Yazici, Spring 2007CEng Amortized Analysis of Algorithms Adnan YAZICI Dept. of Computer Engineering Middle East.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
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.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Quick Review of material covered Apr 8 B+-Tree Overview and some definitions –balanced tree –multi-level –reorganizes itself on insertion and deletion.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Andreas Klappenecker [based on the slides of Prof. Welch]
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
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.
Amortized Algorithm Analysis COP3503 July 25, 2007 Andy Schwartz.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.
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.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
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.
Insertion sort Loop invariants Dynamic memory
Dynamic Hashing (Chapter 12)
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
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
Haim Kaplan, Uri Zwick March 2018
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

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 for the table, say with malloc(), at the beginning When more objects are inserted, the space allocated before may not be enough

3 Dynamic Table The table must then be reallocated with a larger size, say with realloc(), and all objects from original table must be copied over into the new, larger table Similarly, if many objects are deleted, we may want to reallocate the table with a smaller size (to save space) Any good choice for the reallocation size?

4 Load Factor For a table T, we define the load factor, denoted by LF(T), to be the ratio of #items stored in T and the size of T That is, LF is between 0 and 1, and the fuller the T, the larger its LF To measure how good the space usage in a reallocation scheme, we can look at the load factor it guarantees

5 Load Factor Obviously, we have a reallocation scheme that guarantees a load factor of 1: Rebuild table for each indel (insert/delete) However, n indels can cost  (n 2 ) time In general, we want to trade some space for time efficiency Can we ensure any n indels cost  (n) time, and a not-too-bad load factor?

6 Handling Insertion Suppose we have only insertion operations Our reallocation scheme is as follows: If T is full before insertion of an item, we expand T by doubling its size It is clear that at any time, LF(T) is at least 0.5 Question: How about the insertion cost?

7 Handling Insertion Observe that the more items are stored, the closer the next expansion will come Let num(T) = #items currently stored in T Let size(T) = size of T To reflect this observation, we define a potential function  such that  T  = 2 num(T) – size(T)

8 Handling Insertion The function  has some nice properties: Immediately before an expansion,  T  = num(T)  this provides enough cost to copy items into new table Immediately after an expansion,  T  = 0  this resets everything, and simplify the analysis Its value is always non-negative (the table is always at least half full)

9 Amortized Cost of Insertion Now, what will be amortized insertion cost? Notation c i = actual cost of i th operation  i = amortized cost of i th operation num i = #items in T after i th operation size i = size of T after i th operation  i  =  T  after i th operation There are two cases …

10 Case 1: No Expansion If i th insertion does not cause expansion:  i = c i +  i  -  i-1 = 1 + (2num i – size i ) - (2num i-1 – size i-1 ) = 1 + 2num i - 2num i-1 = 3

11 Case 2: With Expansion If i th insertion causes an expansion: size i =2size i-1 and size i-1 =num i-1 = num i – 1  i = c i +  i  -  i-1 = num i + (2num i – size i ) - (2num i-1 – size i-1 ) = num i + (2num i – 2(num i – 1)) - (2(num i – 1) – (num i – 1)) = num i + 2- (num i – 1) = 3 Conclusion: amortized cost for insertion = O(1)

12 Handling Insertion & Deletion Suppose we have both insertion & deletion Can we still maintain a reallocation scheme with LF(T) is at least 0.5 ? To do so, when table is full, we need to expand as usual, AND when table is just below half-full, we need to contract immediately

13 Handling Insertion & Deletion Will the following scheme work? If T is full before insertion of an item, we expand T by doubling its size If T is below half-full after deletion, we contract T by halving its size In worst-case, n indels cost  (n 2 ) time

14 Slight Modification The previous scheme fails because we are too greedy … (contracting too early) Let us modify our scheme slightly: If T is full before insertion of an item, we expand T by doubling its size If T is below (1/4)-full after deletion, we contract T by halving its size At any time, LF(T) is at least 0.25

15 Handling Insertion & Deletion Now, using this scheme, If table is more than (1/2)-full, we should start worrying about the next expansion  watch for insertion If table is less than (1/2)-full, we should start worrying about the next contraction  watch for deletion This gives us some intuition of how to define the potential function

16 New Potential Function Our new potential function  is a bit strange (it has two parts): If table is at least half full:  T  = 2 num(T) – size(T) If table is less than half full:  T  = size(T)/2 – num(T) Can you compute the amortized cost for each operation?

17 Nice Properties The function  has some nice properties: Immediately before a resize,  T  = num(T)  this provides enough cost to copy items into new table At half-full or immediately after resize,  T  = 0  this resets everything, and simplify the analysis Its value is always non-negative

18 Amortized Insertion Cost If i th operation = insertion If it causes an expansion:  i = same as before = 3 If it does not cause expansion: if T at least half full, (LF i-1 < ½, LF i ≥ ½)  i = same as before = 3 if T less than half full,  i = c i + (size i /2 – num i ) - (size i-1 /2 – num i-1 ) = 1 + (-1) = 0

19 Amortized Deletion Cost If i th operation = deletion If it causes a contraction:  i = c i +  i  -  i -1 = c i + (size i /2 – num i ) - (size i-1 /2 – num i-1 ) = (num i + 1)+((num i + 1)-num i ) – ((2(num i +1)) – (num i +1)) = 1 If it does not cause a contraction: if T less than half full, (LF i-1 < ½ )  i = c i + (size i /2 – num i ) - (size i-1 /2 – num i-1 ) = = 2

20 Amortized Deletion Cost (cont) If it does not cause a contraction: if T at least half full, (LF i-1 ≥ ½)  i = c i +  i  -  i -1 = c i + (2num i – size i ) - (2num i-1 – size i-1 ) = = -1 This operation will not cause any problem

21 Conclusion The amortized insertion or deletion cost in our new scheme = O(1) Meaning: Any n operations in total cost O(n) time Remark: There can be other reallocation schemes with O(1) load factor and O(1) amortized cost (Try to think about it !)

22 Homework Problem: 17-2 (Due: Dec. 7) Practice ay home: , , , , Second Mid-Term Exam. Dec. 9 Ch 19 Fibonacci heaps (Self-study)

23 Quiz 3 Show that the second smallest of n elements can be found with n + lg n - 2 comparisons in the worst case. Give an O(n)-time Dynamic Programming algorithm to compute the n-th Fibonacci number.