Lecture Notes for Chapter 17: Amortized Analysis

Similar documents
Graduate Analysis of Algorithms Dr. Haim Levkowitz

Lecture 7: Amortized analysis

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

Today: Amortized Analysis

Amortized Analysis (chap. 17)

Data Structures and Algorithm. Xiaoqing Zheng

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Amortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person

Amortized analysis. Amortized analysis

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

Tutorial 4. Dynamic Set: Amortized Analysis

Algorithms and Data Structures

CSCB63 Winter Week10 - Lecture 2 - Hashing. Anna Bretscher. March 21, / 30

CSE 548: Analysis of Algorithms. Lecture 8 ( Amortized Analysis )

N/4 + N/2 + N = 2N 2.

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Average Case Analysis of Marking Algorithms

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

DATA STRUCTURES I, II, III, AND IV

Analysis of Algorithms I: Perfect Hashing

CHAPTER 17 Amortized Analysis

1 Maintaining a Dictionary

Data Structures and Algorithm. Xiaoqing Zheng

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Hash Tables. Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing. CS 5633 Analysis of Algorithms Chapter 11: Slide 1

Lecture 6 September 21, 2016

Splay Trees. CMSC 420: Lecture 8

Fundamental Algorithms

Hashing, Hash Functions. Lecture 7

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by:

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CS361 Homework #3 Solutions

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Generating Function Notes , Fall 2005, Prof. Peter Shor

Hash Tables. Given a set of possible keys U, such that U = u and a table of m entries, a Hash function h is a

Counters. We ll look at different kinds of counters and discuss how to build them

An Optimal Algorithm for l 1 -Heavy Hitters in Insertion Streams and Related Problems

12 Hash Tables Introduction Chaining. Lecture 12: Hash Tables [Fa 10]

CSC321 Lecture 20: Reversible and Autoregressive Models

Problem Set 4 Solutions

15-451/651: Design & Analysis of Algorithms September 13, 2018 Lecture #6: Streaming Algorithms last changed: August 30, 2018

Algorithms lecture notes 1. Hashing, and Universal Hash functions

6.1 Occupancy Problem

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Name Solutions Linear Algebra; Test 3. Throughout the test simplify all answers except where stated otherwise.

Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, === Homework submission instructions ===

arxiv: v2 [cs.dc] 28 Apr 2013

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Hashing. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing. Philip Bille

Hashing. Hashing. Dictionaries. Dictionaries. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Hashing. Algorithm : Design & Analysis [09]

CSCB63 Winter Week 11 Bloom Filters. Anna Bretscher. March 30, / 13

Lecture 6: Introducing Complexity

Designing Information Devices and Systems I Spring 2019 Lecture Notes Note 3

Final Review Sheet. B = (1, 1 + 3x, 1 + x 2 ) then 2 + 3x + 6x 2

Lecture 13: Linear programming I

Introduction to Algorithms

? 11.5 Perfect hashing. Exercises

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) =

Amortized Complexity Main Idea

Lecture 12 : Recurrences DRAFT

Lecture 3: Big-O and Big-Θ

Grades 7 & 8, Math Circles 10/11/12 October, Series & Polygonal Numbers

Turing Machine Variants

Hash Table Analysis. e(k) = bp(k). kp(k) = n b.

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Searching, mainly via Hash tables

INTRODUCTION TO HASHING Dr. Thomas Hicks Trinity University. Data Set - SSN's from UTSA Class

Lecture 2: Divide and conquer and Dynamic programming

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

MATH 310, REVIEW SHEET 2

Solution suggestions for examination of Logic, Algorithms and Data Structures,

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

CS341 info session is on Thu 3/1 5pm in Gates415. CS246: Mining Massive Datasets Jure Leskovec, Stanford University

Lecture 17: Floyd-Hoare Logic for Partial Correctness

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Math 416, Spring 2010 The algebra of determinants March 16, 2010 THE ALGEBRA OF DETERMINANTS. 1. Determinants

Problem 1: (Chernoff Bounds via Negative Dependence - from MU Ex 5.15)

ARE211, Fall2012. Contents. 2. Linear Algebra (cont) Vector Spaces Spanning, Dimension, Basis Matrices and Rank 8

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

Nondeterministic finite automata

Lecture 2. Frequency problems

1 Divide and Conquer (September 3)

COEN 312 DIGITAL SYSTEMS DESIGN - LECTURE NOTES Concordia University

Splay Trees. Splay Trees. Splay Trees. Splay Trees

1 Difference between grad and undergrad algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 4: Constructing the Integers, Rationals and Reals

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

Proving languages to be nonregular

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

CS 161 Summer 2009 Homework #2 Sample Solutions

Transcription:

Lecture Notes for Chapter 17: Amortized Analysis Chapter 17 overview Amortized analysis Analyze a sequence of operations on a data structure. Goal: Show that although some individual operations may be expensive, on average the cost per operation is small. Average in this context does not mean that we re averaging over a distribution of inputs. No probability is involved. We re talking about average cost in the worst case. Organization We ll look at 3 methods: aggregate analysis accounting method potential method Using 3 examples: stack with multipop operation binary counter dynamic tables (later on) Aggregate analysis Stack operations PUSH(S, x): O(1) each O(n) for any sequence of n operations. POP(S): O(1) each O(n) for any sequence of n operations.

17-2 Lecture Notes for Chapter 17: Amortized Analysis MULTIPOP(S, k) while S is not empty and k > 0 do POP(S) k k 1 Running time of MULTIPOP: Linear in # of POP operations. Let each PUSH/POP cost 1. # of iterations of while loop is min(s, k), where s = # of objects on stack. Therefore, total cost = min(s, k). Sequence of n PUSH, POP, MULTIPOP operations: Worst-case cost of MULTIPOP is O(n). Have n operations. Therefore, worst-case cost of sequence is O(n 2 ). Observation Each object can be popped only once per time that it s pushed. Have n PUSHes n POPs, including those in MULTIPOP. Therefore, total cost = O(n). Average over the n operations O(1) per operation on average. Again, notice no probability. Showed worst-case O(n) cost for sequence. Therefore, O(1) per operation on average. This technique is called aggregate analysis. Binary counter k-bit binary counter A[0..k 1] of bits, where A[0] is the least signiþcant bit and A[k 1] is the most signiþcant bit. Counts upward from 0. k 1 Value of counter is A[i] 2 i. i=0 Initially, counter value is 0, so A[0..k 1] = 0. To increment, add 1 (mod 2 k ): INCREMENT(A, k) i 0 while i < k and A[i] = 1 do A[i] 0 i i + 1 if i < k then A[i] 1

Lecture Notes for Chapter 17: Amortized Analysis 17-3 Example: k = 3 [Underlined bits ßip. Show costs later.] counter A value 2 1 0 cost 0 000 0 1 001 1 2 010 3 3 011 4 4 100 7 5 101 8 6 110 10 7 111 11 0 000 14.. 15 Cost of INCREMENT = (# of bits ßipped). Analysis: Each call could ßip k bits, so n INCREMENTs takes O(nk) time. Observation Not every bit ßips every time. [Show costs from above.] bit ßips how often times in n INCREMENTs 0 every time n 1 1/2 the time n/2 2 1/4 the time n/4. i 1/2 i the time n/2 i. i k never 0 k 1 Therefore, total # of ßips = n/2 i < n i=0 1/2 i i=0 ( ) 1 = n 1 1/2 = 2n. Therefore, n INCREMENTs costs O(n). Average cost per operation = O(1).

17-4 Lecture Notes for Chapter 17: Amortized Analysis Accounting method Assign different charges to different operations. Some are charged more than actual cost. Some are charged less. Amortized cost = amount we charge. When amortized cost > actual cost, store the difference on speciþc objects in the data structure as credit. Use credit later to pay for operations whose actual cost > amortized cost. Differs from aggregate analysis: In the accounting method, different operations can have different costs. In aggregate analysis, all operations have same cost. Need credit to never go negative. Otherwise, have a sequence of operations for which the amortized cost is not an upper bound on actual cost. Amortized cost would tell us nothing. Let c i = actual cost of ith operation, ĉ i = amortized cost of ith operation. n n Then require ĉ i c i for all sequences of n operations. Total credit stored = Stack n ĉ i n c i 0. }{{} had better be operation actual cost amortized cost PUSH 1 2 POP 1 0 MULTIPOP min(k, s) 0 Intuition: When pushing an object, pay $2. $1 pays for the PUSH. $1 is prepayment for it being popped by either POP or MULTIPOP. Since each object has $1, which is credit, the credit can never go negative. Therefore, total amortized cost, = O(n), is an upper bound on total actual cost.

Lecture Notes for Chapter 17: Amortized Analysis 17-5 Binary counter Charge $2 to set a bit to 1. $1 pays for setting a bit to 1. $1 is prepayment for ßipping it back to 0. Have $1 of credit for every 1 in the counter. Therefore, credit 0. Amortized cost of INCREMENT: Cost of resetting bits to 0 is paid by credit. At most 1 bit is set to 1. Therefore, amortized cost $2. For n operations, amortized cost = O(n). Potential method Like the accounting method, but think of the credit as potential stored with the entire data structure. Accounting method stores credit with speciþc objects. Potential method stores potential in the data structure as a whole. Can release potential to pay for future operations. Most ßexible of the amortized analysis methods. Let D i = data structure after ith operation, D 0 = initial data structure, c i = actual cost of ith operation, ĉ i = amortized cost of ith operation. Potential function : D i R (D i ) is the potential associated with data structure D i. ĉ i = c i + (D i ) (D i 1 ) = c i + (D i ) }{{}. increase in potential due to ith operation n Total amortized cost = = = ĉ i n (c i + (D i ) (D i 1 )) (telescoping sum: every term other than D 0 and D n is added once and subtracted once) n c i + (D n ) (D 0 ).

17-6 Lecture Notes for Chapter 17: Amortized Analysis If we require that (D i ) (D 0 ) for all i, then the amortized cost is always an upper bound on actual cost. In practice: (D 0 ) = 0, (D i ) 0 for all i. Stack = # of objects in stack (= # of $1 bills in accounting method) D 0 = empty stack (D 0 ) = 0. Since # of objects in stack is always 0, (D i ) 0 = (D 0 ) for all i. operation actual cost amortized cost PUSH 1 (s + 1) s = 1 1+ 1 = 2 where s = # of objects initially POP 1 (s 1) s = 1 1 1 = 0 MULTIPOP k = min(k, s) (s k ) s = k k k = 0 Therefore, amortized cost of a sequence of n operations = O(n). Binary counter = b i = # of 1 s after ith INCREMENT Suppose ith operation resets t i bits to 0. c i t i + 1 (resets t i bits, sets 1 bit to 1) If b i = 0, the ith operation reset all k bits and didn t set one, so b i 1 = t i = k b i = b i 1 t i. If b i > 0, the ith operation reset t i bits, set one, so b i = b i 1 t i + 1. Either way, b i b i 1 t i + 1. Therefore, (D i ) (b i 1 t i + 1) b i 1 = 1 t i. ĉ i = c i + (D i ) (t i + 1) + (1 t i ) = 2. If counter starts at 0, (D 0 ) = 0. Therefore, amortized cost of n operations = O(n). Dynamic tables A nice use of amortized analysis.

Lecture Notes for Chapter 17: Amortized Analysis 17-7 Scenario Have a table maybe a hash table. Don t know in advance how many objects will be stored in it. When it Þlls, must reallocate with a larger size, copying all objects into the new, larger table. When it gets sufþciently small, might want to reallocate with a smaller size. Details of table organization not important. Goals 1. O(1) amortized time per operation. 2. Unused space always constant fraction of allocated space. Load factor α = num/size, where num = # items stored, size = allocated size. If size = 0, then num = 0. Call α = 1. Never allow α>1. Keep α>aconstant fraction goal (2). Table expansion Consider only insertion. When the table becomes full, double its size and reinsert all existing items. Guarantees that α 1/2. Each time we actually insert an item into the table, it s an elementary insertion. TABLE-INSERT(T, x) if size[t ] = 0 then allocate table[t ] with 1 slot size[t ] 1 if num[t ] = size[t ] expand? then allocate new-table with 2 size[t ] slots insert all items in table[t ] into new-table free table[t ] table[t ] new-table size[t ] 2 size[t ] insert x into table[t ] num[t ] num[t ] + 1 num[t ] elem insertions 1 elem insertion Initially, num[t ] = size[t ] = 0.

17-8 Lecture Notes for Chapter 17: Amortized Analysis Running time: Charge 1 per elementary insertion. Count only elementary insertions, since all other costs together are constant per call. c i = actual cost of ith operation If not full, c i = 1. If full, have i 1 items in the table at the start of the ith operation. Have to copy all i 1 existing items, then insert ith item c i = i. n operations c i = O(n) O(n 2 ) time for n operations. Of course, we don t always expand: { i if i 1 is exact power of 2, c i = 1 otherwise. Total cost = n c i lg n n + j=0 2 j = n + 2 lg n +1 1 2 1 < n + 2n = 3n Therefore, aggregate analysis says amortized cost per operation = 3. Accounting method Charge $3 per insertion of x. $1 pays for x s insertion. $1 pays for x to be moved in the future. $1 pays for some other item to be moved. Suppose we ve just expanded, size = m before next expansion, size = 2m after next expansion. Assume that the expansion used up all the credit, so that there s no credit stored after the expansion. Will expand again after another m insertions. Each insertion will put $1 on one of the m items that were in the table just after expansion and will put $1 on the item inserted. Have $2m of credit by next expansion, when there are 2m items to move. Just enough to pay for the expansion, with no credit left over!

Lecture Notes for Chapter 17: Amortized Analysis 17-9 Potential method (T ) = 2 num[t ] size[t ] Initially, num = size = 0 = 0. Just after expansion, size = 2 num = 0. Just before expansion, size = num = num have enough potential to pay for moving all items. Need 0, always. Always have size num 1 2 size 2 num size 0. Amortized cost of ith operation: num i = num after ith operation, size i = size after ith operation, i = after ith operation. If no expansion: size i = size i 1, num i = num i 1 +1, c i = 1. Then we have ĉ i = c i + i i 1 = 1 + (2 num i size i ) (2 num i 1 size i 1 ) = 1 + (2 num i size i ) (2(num i 1) size i ) = 1 + 2 = 3. If expansion: size i = 2 size i 1, size i 1 = num i 1 = num i 1, c i = num i 1 +1 = num i. Then we have ĉ i = c i + i + i 1 = num i + (2 num i size i ) (2 num i 1 size i 1 ) = num i + (2 num i 2(num i 1)) (2(num i 1) (num i 1)) = num i + 2 (num i 1) = 3.

17-10 Lecture Notes for Chapter 17: Amortized Analysis 32 24 size i num i 16 Φ i 8 0 0 8 16 24 32 i Expansion and contraction When α drops too low, contract the table. Allocate a new, smaller one. Copy all items. Still want α bounded from below by a constant, amortized cost per operation = O(1). Measure cost in terms of elementary insertions and deletions. Obvious strategy : Double size when inserting into a full table (when α = 1, so that after insertion α would become > 1). Halve size when deletion would make table less than half full (when α = 1/2, so that after deletion α would become < 1/2). Then always have 1/2 α 1. Suppose we Þll table. Then insert double 2 deletes halve 2 inserts double 2 deletes halve Not performing enough operations after expansion or contraction to pay for the next one. Simple solution: Double as before: when inserting with α = 1 after doubling, α = 1/2. Halve size when deleting with α = 1/4 after halving, α = 1/2. Thus, immediately after either expansion or contraction, have α = 1/2. Always have 1/4 α 1.

Lecture Notes for Chapter 17: Amortized Analysis 17-11 Intuition: Want to make sure that we perform enough operations between consecutive expansions/contractions to pay for the change in table size. Need to delete half the items before contraction. Need to double number of items before expansion. Either way, number of operations between expansions/contractions is at least a constant fraction of number of items copied. { 2 num[t ] size[t ] if α 1/2, (T ) = size[t ]/2 num[t ] if α<1/2. T empty = 0. α 1/2 num 1 size 2 num size 0. 2 α<1/2 num < 1 size 0. 2 Intuition: measures how far from α = 1/2 we are. α = 1/2 = 2 num 2 num = 0. α = 1 = 2 num num = num. α = 1/4 = size /2 num = 4 num /2 num = num. Therefore, when we double or halve, have enough potential to pay for moving all num items. Potential increases linearly between α = 1/2 and α = 1, and it also increases linearly between α = 1/2 and α = 1/4. Since α has different distances to go to get to 1 or 1/4, starting from 1/2, rate of increase of differs. For α to go from 1/2 to1,num increases from size /2 tosize, for a total increase of size /2. increases from 0 to size. Thus, needs to increase by 2 for each item inserted. That s why there s a coefþcient of 2 on the num[t ] term in the formula for when α 1/2. For α to go from 1/2to1/4, num decreases from size /2tosize /4, for a total decrease of size /4. increases from 0 to size /4. Thus, needs to increase by 1 for each item deleted. That s why there s a coefþcient of 1 onthe num[t ] term in the formula for when α<1/2. Amortized costs: more cases insert, delete α 1/2, α<1/2 (use α i, since α can vary a lot) size does/doesn t change Insert: α i 1 1/2, same analysis as before ĉ i = 3. α i 1 < 1/2 no expansion (only occurs when α i 1 = 1).

17-12 Lecture Notes for Chapter 17: Amortized Analysis If α i 1 < 1/2 and α i < 1/2: ĉ i = c i + i + i 1 = 1 + (size i /2 num i ) (size i 1 /2 num i 1 ) = 1 + (size i /2 num i ) (size i /2 (num i 1)) = 0. If α i 1 < 1/2 and α i 1/2: ĉ i = 1 + (2 num i size i ) (size i 1 /2 num i 1 ) = 1 + (2(num i 1 +1) size i 1 ) (size i 1 /2 num i 1 ) = 3 num i 1 3 2 size i 1 +3 = 3 α i 1 size i 1 3 2 size i 1 +3 < 3 2 size i 1 3 2 size i 1 +3 = 3. Therefore, amortized cost of insert is < 3. Delete: If α i 1 < 1/2, then α i < 1/2. If no contraction: ĉ i = 1 + (size i /2 num i ) (size i 1 /2 num i 1 ) = 1 + (size i /2 num i ) (size i /2 (num i +1)) = 2. If contraction: ĉ i = (num i +1 }{{} ) + (size i /2 num i ) (size i 1 /2 num i 1 ) move + delete [size i /2 = size i 1 /4 = num i 1 = num i +1] = (num i +1) + ((num i +1) num i ) ((2 num i +2) (num i +1)) = 1. If α i 1 1/2, then no contraction. If α i 1/2: ĉ i = 1 + (2 num i size i ) (2 num i 1 size i 1 ) = 1 + (2 num i size i ) (2 num i +2 size i ) = 1. If α i < 1/2, since α i 1 1/2, have num i = num i 1 1 1 2 size i 1 1 = 1 2 size i 1.

Lecture Notes for Chapter 17: Amortized Analysis 17-13 Thus, ĉ i = 1 + (size i /2 num i ) (2 num i 1 size i 1 ) = 1 + (size i /2 num i ) (2 num i +2 size i ) = 1 + 3 2 size i 3 num i 1 + 3 ( ) 1 2 size i 3 2 size i 1 = 2. Therefore, amortized cost of delete is 2.