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

Similar documents
DATA STRUCTURES I, II, III, AND IV

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

Data Structures and Algorithm. Xiaoqing Zheng

Lecture 7: Amortized analysis

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

Graduate Analysis of Algorithms Dr. Haim Levkowitz

Today: Amortized Analysis

Lecture Notes for Chapter 17: Amortized Analysis

Amortized analysis. Amortized analysis

Amortized Analysis (chap. 17)

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

Tutorial 4. Dynamic Set: Amortized Analysis

Amortized Complexity Main Idea

Algorithms and Data Structures

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

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

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Splay Trees. CMSC 420: Lecture 8

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

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

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

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

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

2. ALGORITHM ANALYSIS

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

Amortized Complexity Verified

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

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

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

Algorithm Design and Analysis

CS361 Homework #3 Solutions

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

Average Case Analysis of Marking Algorithms

Mon Tue Wed Thurs Fri

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

Algorithm Design and Analysis

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Analysis of Algorithms Fall Basics of Algorithm Analysis Computational Tractability

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

Linear Time Selection

Lecture VI AMORTIZATION

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 473: Algorithms. Ruta Mehta. Spring University of Illinois, Urbana-Champaign. Ruta (UIUC) CS473 1 Spring / 32

6.1 Occupancy Problem

Disjoint Sets : ADT. Disjoint-Set : Find-Set

9. PSPACE 9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

Divide-Conquer-Glue Algorithms

9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

CS 580: Algorithm Design and Analysis

Problem Set 4 Solutions

INF2220: algorithms and data structures Series 1

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Part I: Definitions and Properties

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Week 5: Quicksort, Lower bound, Greedy

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

CS Data Structures and Algorithm Analysis

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

6. DYNAMIC PROGRAMMING I

Algorithm Theory - Exercise Class

Hashing. Algorithm : Design & Analysis [09]

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

7 Dictionary. Ernst Mayr, Harald Räcke 126

Lower Bounds for Dynamic Connectivity (2004; Pǎtraşcu, Demaine)

Introduction to Randomized Algorithms III

1 Maintaining a Dictionary

Lecture 14: Nov. 11 & 13

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

EENG/INFE 212 Stacks

Algorithms lecture notes 1. Hashing, and Universal Hash functions

Biased Quantiles. Flip Korn Graham Cormode S. Muthukrishnan

Review Of Topics. Review: Induction

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

Analysis of Algorithms I: Perfect Hashing

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

5. DIVIDE AND CONQUER I

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

CS Analysis of Recursive Algorithms and Brute Force

Lecture 1 : Data Compression and Entropy

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

Algorithms for Data Science

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

5. DIVIDE AND CONQUER I

Math 391: Midterm 1.0 Spring 2016

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Introduction to Hashtables

Tight Bounds for Sliding Bloom Filters

CSE 21 Practice Exam for Midterm 2 Fall 2017

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Data Structures 1 NTIN066

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Nov 30. Final In-Class Exam. ( 7:05 PM 8:20 PM : 75 Minutes )

Lecture 17: Designing Sequential Systems Using Flip Flops

CSE 202 Dynamic Programming II

Transcription:

AMORTIZED ANALYSIS binary counter multipop stack dynamic table Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 1/24/17 11:31 AM

Amortized analysis Worst-case analysis. Determine worst-case running time of a data structure operation as function of the input size. Amortized analysis. Determine worst-case running time of a sequence of data structure operations as a function of the input size. can be too pessimistic if the only way to encounter an expensive operation is if there were lots of previous cheap operations Ex. Starting from an empty stack implemented with a dynamic table, any sequence of n push and pop operations takes O(n) time in the worst case. 9

Amortized analysis: applications Splay trees. Dynamic table. Fibonacci heaps. Garbage collection. Move-to-front list updating. Push-relabel algorithm for max flow. Path compression for disjoint-set union. Structural modifications to red-black trees. Security, databases, distributed computing,... SIAM J. ALG. DISC. METH. Vol. 6, No. 2, April 1985 1985 Society for Industrial and Applied Mathematics 016 AMORTIZED COMPUTATIONAL COMPLEXITY* ROBERT ENDRE TARJANt Abstract. A powerful technique in the complexity analysis of data structures is amortization, or averaging over time. Amortized running time is a realistic but robust complexity measure for which we can obtain surprisingly tight upper and lower bounds on a variety of algorithms. By following the principle of designing algorithms whose amortized complexity is low, we obtain "self-adjusting" data structures that are simple, flexible and efficient. This paper surveys recent work by several researchers on amortized complexity. ASM(MOS) subject classifications. 68C25, 68E05 10

AMORTIZED ANALYSIS binary counter multipop stack dynamic table CHAPTER 17

Binary counter Goal. Increment a k-bit binary counter (mod 2 k ). Representation. aj = j th least significant bit of counter. Counter value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 Cost model. Number of bits flipped. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] 12

Binary counter Goal. Increment a k-bit binary counter (mod 2 k ). Representation. aj = j th least significant bit of counter. Counter value Theorem. Starting from the zero counter, a sequence of n INCREMENT operations flips O(n k) bits. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 Pf. At most k bits flipped per increment. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] 13

Aggregate method (brute force) Aggregate method. Sum up sequence of operations, weighted by their cost. Counter value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 Total cost 0 1 3 4 7 8 10 11 15 16 18 19 22 23 25 26 31 14

Binary counter: aggregate method Starting from the zero counter, in a sequence of n INCREMENT operations: Bit 0 flips n times. Bit 1 flips n / 2 times. Bit 2 flips n / 4 times. Theorem. Starting from the zero counter, a sequence of n INCREMENT operations flips O(n) bits. Pf. Bit j flips n / 2 j times. The total number of bits flipped is k 1 j=0 n 2 j < n = 2n j=0 1 2 j Remark. Theorem may be false if initial counter is not zero. 15

Accounting method (banker's method) Assign (potentially) different charges to each operation. Di = data structure after i th operation. ci = actual cost of i th operation. can be more or less than actual cost ĉi = amortized cost of i th operation = amount we charge operation i. When ĉi > ci, we store credits in data structure Di to pay for future ops; when ĉi < ci, we consume credits in data structure Di. Initial data structure D0 starts with zero credits. Key invariant. The total number of credits in the data structure 0. i=1 ĉ i i=1 c i 0 16

Accounting method (banker's method) Assign (potentially) different charges to each operation. Di = data structure after i th operation. ci = actual cost of i th operation. ĉi = amortized cost of i th operation = amount we charge operation i. When ĉi > ci, we store credits in data structure Di to pay for future ops; when ĉi < ci, we consume credits in data structure Di. Initial data structure D0 starts with zero credits. Key invariant. The total number of credits in the data structure 0. i=1 ĉ i i=1 c i 0 Theorem. Starting from the initial data structure D0, the total actual cost of any sequence of n operations is at most the sum of the amortized costs. Pf. The amortized cost of the sequence of operations is: can be more or less than actual cost Intuition. Measure running time in terms of credits (time = money). n i=1 ĉ i n i=1 c i. 17

Binary counter: accounting method Credits. One credit pays for a bit flip. Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting. Flip bit j from 0 to 1: charge two credits (use one and save one in bit j). increment 7 6 5 4 3 2 1 0 0 1 0 0 1 1 1 01 18

Binary counter: accounting method Credits. One credit pays for a bit flip. Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting. Flip bit j from 0 to 1: charge two credits (use one and save one in bit j). Flip bit j from 1 to 0: pay for it with the one credit saved in bit j. increment 7 6 5 4 3 2 1 0 0 1 0 10 01 01 10 10 19

Binary counter: accounting method Credits. One credit pays for a bit flip. Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting. Flip bit j from 0 to 1: charge two credits (use one and save one in bit j). Flip bit j from 1 to 0: pay for it with the one credit saved in bit j. 7 6 5 4 3 2 1 0 0 1 0 1 0 0 0 0 20

Binary counter: accounting method Credits. One credit pays for a bit flip. Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting. Flip bit j from 0 to 1: charge two credits (use one and save one in bit j). Flip bit j from 1 to 0: pay for it with the one credit saved in bit j. Theorem. Starting from the zero counter, a sequence of n INCREMENT operations flips O(n) bits. Pf. the rightmost 0 bit Each increment operation flips at most one 0 bit to a 1 bit (so the total amortized cost is at most 2n). The invariant is maintained. number of credits in each bit 0. 21

Potential method (physicist's method) Potential function. Φ(Di) maps each data structure Di to a real number s.t.: Φ(D0) = 0. Φ(Di) 0 for each data structure Di. Actual and amortized costs. ci = actual cost of i th operation. ĉi = ci + Φ(Di) Φ(Di 1) = amortized cost of i th operation. 22

Potential method (physicist's method) Potential function. Φ(Di) maps each data structure Di to a real number s.t.: Φ(D0) = 0. Φ(Di) 0 for each data structure Di. Actual and amortized costs. ci = actual cost of i th operation. ĉi = ci + Φ(Di) Φ(Di 1) = amortized cost of i th operation. Theorem. Starting from the initial data structure D0, the total actual cost of any sequence of n operations is at most the sum of the amortized costs. Pf. The amortized cost of the sequence of operations is: n ĉ i = n (c i + (D i ) (D i 1 ) i=1 i=1 = n c i + (D n ) (D 0 ) i=1 n i=1 c i 23

Binary counter: potential method Potential function. Let Φ(D) = number of 1 bits in the binary counter D. Φ(D0) = 0. Φ(Di) 0 for each Di. increment 7 6 5 4 3 2 1 0 0 1 0 0 1 1 1 01 24

Binary counter: potential method Potential function. Let Φ(D) = number of 1 bits in the binary counter D. Φ(D0) = 0. Φ(Di) 0 for each Di. increment 7 6 5 4 3 2 1 0 0 1 0 10 01 10 10 10 25

Binary counter: potential method Potential function. Let Φ(D) = number of 1 bits in the binary counter D. Φ(D0) = 0. Φ(Di) 0 for each Di. 7 6 5 4 3 2 1 0 0 1 0 1 0 0 0 0 26

Binary counter: potential method Potential function. Let Φ(D) = number of 1 bits in the binary counter D. Φ(D0) = 0. Φ(Di) 0 for each Di. Theorem. Starting from the zero counter, a sequence of n INCREMENT operations flips O(n) bits. Pf. Suppose that the i th increment operation flips ti bits from 1 to 0. The actual cost ci ti + 1. The amortized cost ĉi = ci + Φ(Di) Φ(Di 1) ci + 1 ti 2. operation sets one bit to 1 (unless counter resets to zero) 27

Famous potential functions Fibonacci heaps. (H) = 2 trees(h) + 2 marks(h) Splay trees. (T ) = x T log 2 size(x) Move-to-front. (L) = 2 inversions(l, L ) Preflow push. (f) = height(v) v : excess(v) > 0 Red-black trees. (T ) = w(x) x T w(x) = 0 x 1 x 0 x 2 x 28

AMORTIZED ANALYSIS binary counter multipop stack dynamic table SECTION 17.4

Multipop stack Goal. Support operations on a set of elements: PUSH(S, x): push object x onto stack S. POP(S): remove and return the most-recently added object. MULTIPOP(S, k): remove the most-recently added k objects. MULTIPOP (S, k) FOR i = 1 TO k POP (S). Exceptions. We assume POP throws an exception if stack is empty. 30

Multipop stack Goal. Support operations on a set of elements: PUSH(S, x): push object x onto stack S. POP(S): remove and return the most-recently added object. MULTIPOP(S, k): remove the most-recently added k objects. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n 2 ) time. Pf. Use a singly-linked list. PoP and PUSH take O(1) time each. MULTIPOP takes O(n) time. overly pessimistic upper bound top 1 4 1 3 31

Multipop stack: aggregate method Goal. Support operations on a set of elements: PUSH(S, x): push object x onto stack S. POP(S): remove and return the most-recently added object. MULTIPOP(S, k): remove the most-recently added k objects. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n) time. Pf. An object is popped at most once for each time it is pushed onto stack. There are n PUSH operations. Thus, there are n POP operations (including those made within MULTIPOP). 32

Multipop stack: accounting method Credits. One credit pays for a push or pop. Accounting. PUSH(S, x): charge two credits. use one credit to pay for pushing x now store one credit to pay for popping x at some point in the future No other operation is charged a credit. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n) time. Pf. The algorithm maintains the invariant that every object remaining on the stack has 1 credit number of credits in data structure 0. 33

Multipop stack: potential method Potential function. Let Φ(D) = number of objects currently on the stack. Φ(D0) = 0. Φ(Di) 0 for each Di. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n) time. Pf. [Case 1: push] Suppose that the i th operation is a PUSH. The actual cost ci = 1. The amortized cost ĉi = ci + Φ(Di) Φ(Di 1) = 1 + 1 = 2. 34

Multipop stack: potential method Potential function. Let Φ(D) = number of objects currently on the stack. Φ(D0) = 0. Φ(Di) 0 for each Di. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n) time. Pf. [Case 2: pop] Suppose that the i th operation is a POP. The actual cost ci = 1. The amortized cost ĉi = ci + Φ(Di) Φ(Di 1) = 1 1 = 0. 35

Multipop stack: potential method Potential function. Let Φ(D) = number of objects currently on the stack. Φ(D0) = 0. Φ(Di) 0 for each Di. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n) time. Pf. [Case 3: multipop] Suppose that the i th operation is a MULTIPOP of k objects. The actual cost ci = k. The amortized cost ĉi = ci + Φ(Di) Φ(Di 1) = k k = 0. 36

AMORTIZED ANALYSIS binary counter multipop stack dynamic table SECTION 17.4

Dynamic table Goal. Store items in a table (e.g., for hash table, binary heap). Two operations: INSERT and DELETE. too many items inserted expand table. too many items deleted contract table. Requirement: if table contains m items, then space = Θ(m). Theorem. Starting from an empty dynamic table, any intermixed sequence of n INSERT and DELETE operations takes O(n 2 ) time. Pf. A single INSERT or DELETE takes O(n) time. overly pessimistic upper bound 38

Dynamic table: insert only Initialize empty table of capacity 1. INSERT: if table is full, first copy all items to a table of twice the capacity. insert old capacity new capacity insert cost copy cost 1 1 1 1 2 1 2 1 1 3 2 4 1 2 4 4 4 1 5 4 8 1 4 6 8 8 1 7 8 8 1 8 8 8 1 9 8 16 1 8 Cost model. Number of items written (due to insertion or copy). 39

Dynamic table: insert only (aggregate method) Theorem. [via aggregate method] Starting from an empty dynamic table, any sequence of n INSERT operations takes O(n) time. Pf. Let ci denote the cost of the i th insertion. c i = i i 1 1 Starting from empty table, the cost of a sequence of n INSERT operations is: n i=1 c i n + < n+2n lg n j=0 2 j = 3n 40

Dynamic table: insert only (accounting method) WLOG, can assume the table fills from left to right. 1 2 3 4 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 41

Dynamic table: insert only (accounting method) Accounting. INSERT: charge 3 credits (use 1 credit to insert; save 2 with new item). Theorem. [via accounting method] Starting from an empty dynamic table, any sequence of n INSERT operations takes O(n) time. Pf. The algorithm maintains the invariant that there are 2 credits with each item in right half of table. When table doubles, one-half of the items in the table have 2 credits. This pays for the work needed to double the table. 42

Dynamic table: insert only (potential method) Theorem. [via potential method] Starting from an empty dynamic table, any sequence of n INSERT operations takes O(n) time. Pf. Let Φ(Di) = 2 size(di) capacity(di). number of elements capacity of array 1 2 3 4 5 6 43

Dynamic table: insert only (potential method) Theorem. [via potential method] Starting from an empty dynamic table, any sequence of n INSERT operations takes O(n) time. Pf. Let Φ(Di) = 2 size(di) capacity(di). number of elements Case 1. [does not trigger expansion] size(di) capacity(di 1). Actual cost ci = 1. Φ(Di) Φ(Di 1) = 2. capacity of array Amortized costs ĉi = ci + Φ(Di) Φ(Di 1) = 1 + 2 = 3. Case 2. [triggers expansion] size(di) = 1 + capacity(di 1). Actual cost ci = 1 + capacity(di 1). Φ(Di) Φ(Di 1) = 2 capacity(di) + capacity(di 1) = 2 capacity(di 1). Amortized costs ĉi = ci + Φ(Di) Φ(Di 1) = 1 + 2 = 3. 44