Introduction to Algorithms

Similar documents
Dynamic tables. Amortized Analysis. Example of a dynamic table. Example of a dynamic table. CS Spring 2008

CMPS 2200 Fall Amortized Analysis. Carola Wenk. Slides courtesy of Charles Leiserson with changes by Carola Wenk

Amortized Analysis. Ric Glassey

Graduate Analysis of Algorithms Prof. Karen Daniels

Union-Find and Amortization

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction. Introduction: Multi-Pop Stack Example. Multi-Pop Stack Cost (clever) Multi-Pop Stack Cost (naïve)

Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms

Graduate Algorithms CS F-12 Amortized Analysis

Introduction to Algorithms 6.046J/18.401J

COMP251: Amortized Analysis

Amortization Analysis

COP 4531 Complexity & Analysis of Data Structures & Algorithms

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Lecture slides by Kevin Wayne. Last updated on Apr 8, :13 AM

Introduction to Algorithms 6.046J/18.401J

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Lecture 11 Unbounded Arrays

Amortized Analysis and Union-Find , Inge Li Gørtz

Today s Outline. CS 561, Lecture 18. Potential Method. Pseudocode. Dynamic Tables. Jared Saia University of New Mexico

1 Static-to-Dynamic Transformations

Problem Set 4 Solutions

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

CSE 373: AVL trees. Michael Lee Friday, Jan 19, 2018

CPS 231 Exam 2 SOLUTIONS

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

Lecture Summary CSC 263H. August 5, 2016

Introduction to Algorithms

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

Data Structures and Algorithms CSE 465

Practice Midterm Exam Solutions

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1

Announcements. Problem Set 3 is due this Tuesday! Midterm graded and will be returned on Friday during tutorial (average 60%)

Amortized Analysis. The time required to perform a sequence of (data structure) operations is averaged over all the operations performed.

Theory and Algorithms Introduction: insertion sort, merge sort

ICS 691: Advanced Data Structures Spring Lecture 3

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation.

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

key h(key) Hash Indexing Friday, April 09, 2004 Disadvantages of Sequential File Organization Must use an index and/or binary search to locate data

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( )

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class

CS:3330 (22c:31) Algorithms

Algorithm Analysis Part 2. Complexity Analysis

16 Greedy Algorithms

The resulting array is written as row i + 1 in our matrix:

Lecture 8 13 March, 2012

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

ECS 153 Discussion Section. April 6, 2015

CS F-12 Amortized Analysis 1

Advanced Database Systems

We have used both of the last two claims in previous algorithms and therefore their proof is omitted.

Lecture 5: Sorting Part A

Activity 13: Amortized Analysis

Dynamic Storage Allocation

Cuckoo Hashing for Undergraduates

Lecture 15 Binary Search Trees

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Einführung in die Programmierung Introduction to Programming

Self Adjusting Data Structures

Introduction to Algorithms 6.046J/18.401J

Advanced Algorithms and Data Structures

Chapter 8 Sorting in Linear Time

Lower Bound on Comparison-based Sorting

Today: Amortized Analysis (examples) Multithreaded Algs.

Habanero Extreme Scale Software Research Project

V Advanced Data Structures

COP 4531 Complexity & Analysis of Data Structures & Algorithms

DATA STRUCTURES AND ALGORITHMS

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Disjoint set (Union-Find)

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Lecture 15 Notes Binary Search Trees

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Class Note #02. [Overall Information] [During the Lecture]

Problem Set 9 Solutions

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

CS2 Algorithms and Data Structures Note 4

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Cilk, Matrix Multiplication, and Sorting

CS2 Algorithms and Data Structures Note 1

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Introduction to Algorithms

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.

V Advanced Data Structures

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

Containers and genericity. A standardized naming scheme. Cursor properties. Lists. Adding a cell. A specific implementation: (singly) linked lists

DATA STRUCTURES AND ALGORITHMS

Introduction to Asymptotic Running Time Analysis CMPSC 122

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Code Generation. Lecture 12

Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Algorithm Design and Analysis

Questions. 6. Suppose we were to define a hash code on strings s by:

CSC236 Week 5. Larry Zhang

Amortized Analysis. Prof. Clarkson Fall Today s music: : "Money, Money, Money" by ABBA

Transcription:

Introduction to Algorithms 6.046J/8.40J/SMA5503 Lecture 4 Prof. Charles E. Leiserson

How large should a hash table be? Goal: Make the table as small as possible, but large enough so that it won t overflow (or otherwise become inefficient). Problem: What if we don t know the proper size in advance? Solution: Dynamic tables. IDEA: Whenever the table overflows, grow it by allocating (via malloc or new) a new, larger table. Move all items from the old table into the new one, and free the storage for the old table. Introduction to Algorithms Day 24 L4.2

Example of a dynamic table. INSERT 2. INSERT overflow Introduction to Algorithms Day 24 L4.3

Example of a dynamic table. INSERT 2. INSERT overflow Introduction to Algorithms Day 24 L4.4

Example of a dynamic table. INSERT 2. INSERT 2 Introduction to Algorithms Day 24 L4.5

Example of a dynamic table. INSERT 2. INSERT 22 3. INSERT overflow Introduction to Algorithms Day 24 L4.6

Example of a dynamic table. INSERT 2. INSERT 3. INSERT overflow 2 Introduction to Algorithms Day 24 L4.7

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 2 Introduction to Algorithms Day 24 L4.8

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 4. INSERT 4 2 3 Introduction to Algorithms Day 24 L4.9

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 2 3 4 overflow Introduction to Algorithms Day 24 L4.0

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT overflow 2 3 4 Introduction to Algorithms Day 24 L4.

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 2 3 4 Introduction to Algorithms Day 24 L4.2

Example of a dynamic table. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 4 5 6. INSERT 6 7. INSERT 7 2 3 Introduction to Algorithms Day 24 L4.3

Worst-case analysis Consider a sequence of n insertions. The worst-case time to execute one insertion is Θ(n). Therefore, the worst-case time for n insertions is n Θ(n) = Θ(n 2 ). WRONG! In fact, the worst-case cost for n insertions is only Θ(n) Θ(n 2 ). Let s see why. Introduction to Algorithms Day 24 L4.4

Tighter analysis Let c i = the cost of the i th insertion i if i is an exact power of 2, = otherwise. i 2 3 4 5 6 7 8 9 0 size i 2 4 4 8 8 8 8 6 6 c i 2 3 5 9 Introduction to Algorithms Day 24 L4.5

Tighter analysis Let c i = the cost of the i th insertion i if i is an exact power of 2, = otherwise. i 2 3 4 5 6 7 8 9 0 size i 2 4 4 8 8 8 8 6 6 c i 2 4 8 Introduction to Algorithms Day 24 L4.6

Tighter analysis (continued) Cost of n insertions = n i= n + c i 3n = Θ( n). lg( n ) j= 0 2 j Thus, the average cost of each dynamic-table operation is Θ(n)/n = Θ(). Introduction to Algorithms Day 24 L4.7

Amortized analysis An amortized analysis is any strategy for analyzing a sequence of operations to show that the average cost per operation is small, even though a single operation within the sequence might be expensive. Even though we re taking averages, however, probability is not involved! An amortized analysis guarantees the average performance of each operation in the worst case. Introduction to Algorithms Day 24 L4.8

Types of amortized analyses Three common amortization arguments: the aggregate method, the accounting method, the potential method. We ve just seen an aggregate analysis. The aggregate method, though simple, lacks the precision of the other two methods. In particular, the accounting and potential methods allow a specific amortized cost to be allocated to each operation. Introduction to Algorithms Day 24 L4.9

Accounting method Charge i th operation a fictitious amortized cost ĉ i, where $ pays for unit of work (i.e., time). This fee is consumed to perform the operation. Any amount not immediately consumed is stored in the bank for use by subsequent operations. The bank balance must not go negative! We must ensure that n i= n c i cˆ i= for all n. Thus, the total amortized costs provide an upper bound on the total true costs. i Introduction to Algorithms Day 24 L4.20

Accounting analysis of dynamic tables Charge an amortized cost of ĉ i =$3for the i th insertion. $ pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $ pays to move a recent item, and $ pays to move an old item. Example: $0 $0 $0 $0 $2 $2 $2 $2 overflow Introduction to Algorithms Day 24 L4.2

Accounting analysis of dynamic tables Charge an amortized cost of ĉ i =$3for the i th insertion. $ pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $ pays to move a recent item, and $ pays to move an old item. Example: $0 $0 $0 $0 $0 $0 $0 $0 overflow Introduction to Algorithms Day 24 L4.22

Accounting analysis of dynamic tables Charge an amortized cost of ĉ i =$3for the i th insertion. $ pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $ pays to move a recent item, and $ pays to move an old item. Example: $0 $0 $0 $0 $0 $0 $0 $0 $2 $2 $2 Introduction to Algorithms Day 24 L4.23

Accounting analysis (continued) Key invariant: Bank balance never drops below 0. Thus, the sum of the amortized costs provides an upper bound on the sum of the true costs. i 2 3 4 5 6 7 8 9 0 size i 2 4 4 8 8 8 8 6 6 c i 2 3 5 9 ĉ i 2* 3 3 3 3 3 3 3 3 3 bank i 2 2 4 2 4 6 8 2 4 *Okay, so I lied. The first operation costs only $2, not $3. Introduction to Algorithms Day 24 L4.24

Potential method IDEA: View the bank account as the potential energy (à laphysics) of the dynamic set. Framework: Start with an initial data structure D 0. Operation i transforms D i to D i. The cost of operation i is c i. Define a potential function Φ : {D i } R, such that Φ(D 0 ) = 0 and Φ(D i ) 0 for all i. The amortized cost ĉ i with respect to Φ is defined to be ĉ i = c i + Φ(D i ) Φ(D i ). Introduction to Algorithms Day 24 L4.25

Understanding potentials ĉ i = c i + Φ(D i ) Φ(D i ) potential difference Φ i If Φ i > 0, then ĉ i > c i. Operation i stores work in the data structure for later use. If Φ i < 0, then ĉ i < c i. The data structure delivers up stored work to help pay for operation i. Introduction to Algorithms Day 24 L4.26

The amortized costs bound the true costs The total amortized cost of n operations is n i= cˆ i = n ( ) i= c i + Φ( D Summing both sides. i ) Φ( D i ) Introduction to Algorithms Day 24 L4.27

The amortized costs bound the true costs The total amortized cost of n operations is n i= cˆ i = = n i= n i= + Φ( D ( ) c c i i + Φ( D n i ) Φ( D ) Φ( D The series telescopes. i 0 ) ) Introduction to Algorithms Day 24 L4.28

The amortized costs bound the true costs The total amortized cost of n operations is n i= cˆ i = = n i= n i= n i= + Φ( D ( ) c c c i i i + Φ( D n i ) Φ( D ) Φ( D i since Φ(D n ) 0 and Φ(D 0 ) = 0. 0 ) ) Introduction to Algorithms Day 24 L4.29

Potential analysis of table doubling Define the potential of the table after the ith insertion by Φ(D i ) = 2i 2 lg i. (Assume that 2 lg 0 = 0.) Note: Φ(D 0 ) = 0, Φ(D i ) 0 for all i. Example: ( Φ = 2 6 2 3 = 4 $0 $0 $0 $0 $2 $2 $2 accounting method) Introduction to Algorithms Day 24 L4.30

Calculation of amortized costs The amortized cost of the i th insertion is ĉ i = c i + Φ(D i ) Φ(D i ) = i + (2i 2 lg i ) (2(i ) 2 lg (i ) ) if i is an exact power of 2, + (2i 2 lg i ) (2(i ) 2 lg (i ) ) otherwise. Introduction to Algorithms Day 24 L4.3

Calculation (Case ) Case : i is an exact power of 2. ĉ i = i + (2i 2 lg i ) (2(i ) 2 lg (i ) ) = i + 2 (2 lg i 2 lg (i ) ) = i + 2 (2(i ) (i )) = i + 2 2i + 2 + i = 3 Introduction to Algorithms Day 24 L4.32

Calculation (Case 2) Case 2: i is not an exact power of 2. ĉ i = + (2i 2 lg i ) (2(i ) 2 lg (i ) ) = + 2 (2 lg i 2 lg (i ) ) = 3 Therefore, n insertions cost Θ(n) in the worst case. Exercise: Fix the bug in this analysis to show that the amortized cost of the first insertion is only 2. Introduction to Algorithms Day 24 L4.33

Conclusions Amortized costs can provide a clean abstraction of data-structure performance. Any of the analysis methods can be used when an amortized analysis is called for, but each method has some situations where it is arguably the simplest. Different schemes may work for assigning amortized costs in the accounting method, or potentials in the potential method, sometimes yielding radically different bounds. Introduction to Algorithms Day 24 L4.34