Introduction to Algorithms

Size: px
Start display at page:

Download "Introduction to Algorithms"

Transcription

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

2 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

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

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

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

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

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

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

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

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

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

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

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

14 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

15 Tighter analysis Let c i = the cost of the i th insertion i if i is an exact power of 2, = otherwise. i size i c i Introduction to Algorithms Day 24 L4.5

16 Tighter analysis Let c i = the cost of the i th insertion i if i is an exact power of 2, = otherwise. i size i c i Introduction to Algorithms Day 24 L4.6

17 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

18 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

19 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

20 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

21 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

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 overflow Introduction to Algorithms Day 24 L4.22

23 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

24 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 size i c i ĉ i 2* bank i *Okay, so I lied. The first operation costs only $2, not $3. Introduction to Algorithms Day 24 L4.24

25 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

26 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

27 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

28 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

29 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

30 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: ( Φ = = 4 $0 $0 $0 $0 $2 $2 $2 accounting method) Introduction to Algorithms Day 24 L4.30

31 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

32 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 i = 3 Introduction to Algorithms Day 24 L4.32

33 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

34 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

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

Dynamic tables. Amortized Analysis. Example of a dynamic table. Example of a dynamic table. CS Spring 2008 CS 56 -- Spring 008 Amortized Analysis Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk CS 56 Analysis of Algorithms Dynamic tables Task: Store a dynamic set in a table/array.

More information

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

CMPS 2200 Fall Amortized Analysis. Carola Wenk. Slides courtesy of Charles Leiserson with changes by Carola Wenk CMPS 2200 Fall 2017 Amortized Analysis Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 11/15/17 CMPS 2200 Intro. to Algorithms 1 Dynamic tables Task: Store a dynamic set in

More information

Amortized Analysis. Ric Glassey

Amortized Analysis. Ric Glassey Amortized Analysis Ric Glassey glassey@kth.se Overview Amortized Analysis Aim: Develop methods of determining the average cost of operations on a data structure Motivation: We do not always want to think

More information

Graduate Analysis of Algorithms Prof. Karen Daniels

Graduate Analysis of Algorithms Prof. Karen Daniels UMass Lowell Computer Science 9.503 Graduate Analysis of Algorithms Prof. Karen Daniels Fall, 203 Lecture 3 Monday, 9/23/3 Amortized Analysis Stack (computational geometry application) Dynamic Table Binary

More information

Union-Find and Amortization

Union-Find and Amortization Design and Analysis of Algorithms February 20, 2015 Massachusetts Institute of Technology 6.046J/18.410J Profs. Erik Demaine, Srini Devadas and Nancy Lynch Recitation 3 Union-Find and Amortization 1 Introduction

More information

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Algorithms 6.046J/18.401J/SMA5503 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson Welcome to Introduction to Algorithms, Fall 01 Handouts 1. Course Information. Calendar 3. Registration (MIT students

More information

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

Introduction. Introduction: Multi-Pop Stack Example. Multi-Pop Stack Cost (clever) Multi-Pop Stack Cost (naïve) Introduction Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.) Why and when we care: You have an algorithm that

More information

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

Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually. Introduction Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.) Why and when we care: You have an algorithm that

More information

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

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis 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 of each operation in the worst case Three

More information

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

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms CS583 Lecture 12 Jana Kosecka Amortized/Accounting Analysis Disjoint Sets Dynamic Programming Greedy Algorithms Slight digression Amortized analysis Disjoint sets Previously 1 Amortized Analysis Amortized

More information

Graduate Algorithms CS F-12 Amortized Analysis

Graduate Algorithms CS F-12 Amortized Analysis Graduate Algorithms CS673-2016F-12 Amortized Analysis David Galles Department of Computer Science University of San Francisco 12-0: Amortized Analysis Standard Stack Push(S,elem) Pop(S) How much time for

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J LECTURE 1 Analysis of Algorithms Insertion sort Merge sort Prof. Charles E. Leiserson Course information 1. Staff. Prerequisites 3. Lectures 4. Recitations 5.

More information

COMP251: Amortized Analysis

COMP251: Amortized Analysis COMP251: Amortized Analysis Jérôme Waldispühl School of Computer Science McGill University Based on (Cormen et al., 2009) Overview Analyze a sequence of operations on a data structure. We will talk about

More information

Amortization Analysis

Amortization Analysis December 21, 2011 Amortization Analysis The concept of amortized runtime analysis is usually first encountered when studying the efficiency of the resizing operation for an array or vector, such as in

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Amortized Analysis Thanks to the text authors who contributed to these slides What is amortized analysis? Analyze a sequence of operations

More information

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

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Lecture slides by Kevin Wayne. Last updated on Apr 8, :13 AM DATA STRUCTURES amortized analysis binomial heaps Fibonacci heaps union-find Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on Apr 8, 2013 6:13 AM Data structures

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J Lecture 1 Prof. Piotr Indyk Analysis of algorithms The theoretical study of computer-program performance and resource usage. Also important: modularity maintainability

More information

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

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems. 2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper

More information

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

Lecture 11 Unbounded Arrays

Lecture 11 Unbounded Arrays Lecture 11 Unbounded Arrays 15-122: Principles of Imperative Computation (Spring 2018) Rob Simmons, Frank Pfenning Arrays have efficient O(1) access to elements given an index, but their size is set at

More information

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

Amortized Analysis and Union-Find , Inge Li Gørtz Amortized Analysis and Union-Find 02283, Inge Li Gørtz 1 Today Amortized analysis 3 different methods 2 examples Union-Find data structures Worst-case complexity Amortized complexity 2 Amortized Analysis

More information

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

Today s Outline. CS 561, Lecture 18. Potential Method. Pseudocode. Dynamic Tables. Jared Saia University of New Mexico Today s Outline CS 561, Lecture 18 Jared Saia University of New Mexico Dynamic Tables 1 Pseudocode Potential Method Table-Insert(T,x){ if (T.size == 0){allocate T with 1 slot;t.size=1} if (T.num == T.size){

More information

1 Static-to-Dynamic Transformations

1 Static-to-Dynamic Transformations You re older than you ve ever been and now you re even older And now you re even older And now you re even older You re older than you ve ever been and now you re even older And now you re older still

More information

Problem Set 4 Solutions

Problem Set 4 Solutions Design and Analysis of Algorithms March 5, 205 Massachusetts Institute of Technology 6.046J/8.40J Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Problem Set 4 Solutions Problem Set 4 Solutions This

More information

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

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to

More information

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

CSE 373: AVL trees. Michael Lee Friday, Jan 19, 2018 CSE 373: AVL trees Michael Lee Friday, Jan 19, 2018 1 Warmup Warmup: What is an invariant? What are the AVL tree invariants, exactly? Discuss with your neighbor. 2 AVL Trees: Invariants Core idea: add

More information

CPS 231 Exam 2 SOLUTIONS

CPS 231 Exam 2 SOLUTIONS CPS 231 Exam 2 SOLUTIONS Fall 2003 1:00-2:25, Tuesday November 20th Closed book exam NAME: Problem Max Obtained 1 10 2 25 3 (a) 15 3 (b) 15 3 (c) 10 4 (a) 10 4 (b) 15 4 (c) 10 Total 110 1 [10 points ]

More information

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

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch] Amortized Analysis Andreas Klappenecker [partially based on the slides of Prof. Welch] 1 People who analyze algorithms have double happiness. First of all they experience the sheer beauty of elegant mathematical

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 5 Prof. Erik Demaine How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine

More information

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

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term. Amortized Analysis Outline for Today Euler Tour Trees A quick bug fix from last time. Cartesian Trees Revisited Why could we construct them in time O(n)? Amortized Analysis Analyzing data structures over

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 2 Analysis of Algorithms Insertion Sort Loop invariants Asymptotic analysis Sofya Raskhodnikova and Adam Smith The problem of sorting Input: sequence a 1,

More information

Practice Midterm Exam Solutions

Practice Midterm Exam Solutions CSE 332: Data Abstractions Autumn 2015 Practice Midterm Exam Solutions Name: Sample Solutions ID #: 1234567 TA: The Best Section: A9 INSTRUCTIONS: You have 50 minutes to complete the exam. The exam is

More information

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

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1 Connectivity A (simple) undirected graph is connected if there exists a path between every pair of vertices. If a graph is not connected, then G (V,E ) is a connected component of the graph G(V,E) if V

More information

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

Announcements. Problem Set 3 is due this Tuesday! Midterm graded and will be returned on Friday during tutorial (average 60%) CSC263 Week 7 Announcements Problem Set 3 is due this Tuesday! Midterm graded and will be returned on Friday during tutorial (average 60%) Amortized Analysis O"en, we perform sequences of opera.ons on

More information

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

Amortized Analysis. The time required to perform a sequence of (data structure) operations is averaged over all the operations performed. Amortized Analysis The time required to perform a sequence of (data structure) operations is averaged over all the operations performed. No probabilistic assumptions - not an average case analysis! 1 Example:

More information

Theory and Algorithms Introduction: insertion sort, merge sort

Theory and Algorithms Introduction: insertion sort, merge sort Theory and Algorithms Introduction: insertion sort, merge sort Rafael Ramirez rafael@iua.upf.es Analysis of algorithms The theoretical study of computer-program performance and resource usage. What s also

More information

ICS 691: Advanced Data Structures Spring Lecture 3

ICS 691: Advanced Data Structures Spring Lecture 3 ICS 691: Advanced Data Structures Spring 2016 Prof. Nodari Sitchinava Lecture 3 Scribe: Ben Karsin 1 Overview In the last lecture we started looking at self-adjusting data structures, specifically, move-to-front

More information

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.

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. Amortized Analysis Outline for Today Amortized Analysis Analyzing data structures over the long term. Cartesian Trees Revisited Why could we construct them in time O(n)? The Two-Stack Queue A simple and

More information

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

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up CSM B Heaps & Hashing Fall 0 Mentoring : October 3, 0 Min-Heapify This. In general, there are 4 ways to heapify. Which ways actually work? Level order, bubbling up Level order, bubbling down Reverse level

More information

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

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 Lectures Desktop (C) Page 1 Hash Indexing Friday, April 09, 004 11:33 AM Disadvantages of Sequential File Organization Must use an index and/or binary search to locate data File organization based on hashing

More information

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

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 Homework 3 CS 321 - Data Structures Fall 2018 Dec 6, 2018 Name: Collaborators: 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

More information

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

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( ) 17.4 Dynamic tables Let us now study the problem of dynamically expanding and contracting a table We show that the amortized cost of insertion/ deletion is only (1) Though the actual cost of an operation

More information

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

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class 15-122 Assignment 4 Page 1 of 8 15-122 : Principles of Imperative Computation Summer 1 2012 Assignment 4 (Theory Part) Due: Monday, June 11, 2012 in class Name: Andrew ID: Recitation: The written portion

More information

CS:3330 (22c:31) Algorithms

CS:3330 (22c:31) Algorithms What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.

More information

Algorithm Analysis Part 2. Complexity Analysis

Algorithm Analysis Part 2. Complexity Analysis Algorithm Analysis Part 2 Complexity Analysis Introduction Algorithm Analysis measures the efficiency of an algorithm, or its implementation as a program, as the input size becomes large Actually, an estimation

More information

16 Greedy Algorithms

16 Greedy Algorithms 16 Greedy Algorithms Optimization algorithms typically go through a sequence of steps, with a set of choices at each For many optimization problems, using dynamic programming to determine the best choices

More information

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

The resulting array is written as row i + 1 in our matrix: Homework 5 Solutions Fundamental Algorithms, Fall 2004, Professor Yap Due: Thu December 9, in class SOLUTION PREPARED BY Instructor and T.A.s Ariel Cohen and Vikram Sharma INSTRUCTIONS: NOTICE: In the

More information

Lecture 8 13 March, 2012

Lecture 8 13 March, 2012 6.851: Advanced Data Structures Spring 2012 Prof. Erik Demaine Lecture 8 13 March, 2012 1 From Last Lectures... In the previous lecture, we discussed the External Memory and Cache Oblivious memory models.

More information

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

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018 CS8 Integrated Introduction to Computer Science Fisler, Nelson Lecture : Priority Queues, Part : AM, Mar 9, 8 Contents Sorting, Revisited Counting Sort Bucket Sort 4 Radix Sort 6 4. MSD Radix Sort......................................

More information

ECS 153 Discussion Section. April 6, 2015

ECS 153 Discussion Section. April 6, 2015 ECS 153 Discussion Section April 6, 2015 1 What We ll Cover Goal: To discuss buffer overflows in detail Stack- based buffer overflows Smashing the stack : execution from the stack ARC (or return- to- libc)

More information

CS F-12 Amortized Analysis 1

CS F-12 Amortized Analysis 1 CS673-2016F-12 Amortized Analysis 1 12-0: Amortized Analysis Standard Stack Push(S,elem) Pop(S) How much time for each operation? 12-1: Amortized Analysis Standard Stack Push(S,elem)O(1) Pop(S)O(1) Multipop(S,k)

More information

Advanced Database Systems

Advanced Database Systems Lecture IV Query Processing Kyumars Sheykh Esmaili Basic Steps in Query Processing 2 Query Optimization Many equivalent execution plans Choosing the best one Based on Heuristics, Cost Will be discussed

More information

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

We have used both of the last two claims in previous algorithms and therefore their proof is omitted. Homework 3 Question 1 The algorithm will be based on the following logic: If an edge ( ) is deleted from the spanning tree, let and be the trees that were created, rooted at and respectively Adding any

More information

Lecture 5: Sorting Part A

Lecture 5: Sorting Part A Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines

More information

Activity 13: Amortized Analysis

Activity 13: Amortized Analysis Activity 3: Amortized Analysis Model : Incrementing a binary counter 5 4 3 2 9 8 7 6 5 4 3 2 2 3 4 5 Model shows a binary counter, stored as an array of bits with the 2 i place stored at index i, undergoing

More information

Dynamic Storage Allocation

Dynamic Storage Allocation 6.172 Performance Engineering of Software Systems LECTURE 10 Dynamic Storage Allocation Charles E. Leiserson October 12, 2010 2010 Charles E. Leiserson 1 Stack Allocation Array and pointer A un Allocate

More information

Cuckoo Hashing for Undergraduates

Cuckoo Hashing for Undergraduates Cuckoo Hashing for Undergraduates Rasmus Pagh IT University of Copenhagen March 27, 2006 Abstract This lecture note presents and analyses two simple hashing algorithms: Hashing with Chaining, and Cuckoo

More information

Lecture 15 Binary Search Trees

Lecture 15 Binary Search Trees Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to

More information

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

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola CS 3343 Fall 2007 Sorting Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk CS 3343 Analysis of Algorithms 1 How fast can we sort? All the sorting algorithms we have seen

More information

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures Topics for this lecture Containers and genericity

More information

Self Adjusting Data Structures

Self Adjusting Data Structures Self Adjusting Data Structures Pedro Ribeiro DCC/FCUP 2014/2015 Pedro Ribeiro (DCC/FCUP) Self Adjusting Data Structures 2014/2015 1 / 31 What are self adjusting data structures? Data structures that can

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J Menu for Today Show that Θ (n lg n) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in linear time. Hint: maybe the models

More information

Advanced Algorithms and Data Structures

Advanced Algorithms and Data Structures Advanced Algorithms and Data Structures Prof. Tapio Elomaa Course Basics A new 7 credit unit course Replaces OHJ-2156 Analysis of Algorithms We take things a bit further than OHJ-2156 We will assume familiarity

More information

Chapter 8 Sorting in Linear Time

Chapter 8 Sorting in Linear Time Chapter 8 Sorting in Linear Time The slides for this course are based on the course textbook: Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd edition, The MIT Press, McGraw-Hill,

More information

Lower Bound on Comparison-based Sorting

Lower Bound on Comparison-based Sorting Lower Bound on Comparison-based Sorting Different sorting algorithms may have different time complexity, how to know whether the running time of an algorithm is best possible? We know of several sorting

More information

Today: Amortized Analysis (examples) Multithreaded Algs.

Today: Amortized Analysis (examples) Multithreaded Algs. Today: Amortized Analysis (examples) Multithreaded Algs. COSC 581, Algorithms March 11, 2014 Many of these slides are adapted from several online sources Reading Assignments Today s class: Chapter 17 (Amortized

More information

Habanero Extreme Scale Software Research Project

Habanero Extreme Scale Software Research Project Habanero Extreme Scale Software Research Project Comp215: Performance Zoran Budimlić (Rice University) To suffer the penalty of too much haste, which is too little speed. - Plato Never sacrifice correctness

More information

V Advanced Data Structures

V Advanced Data Structures V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 8 Data Structures for Disjoint Sets Thanks to the text authors who contributed to these slides Data Structures for Disjoint Sets Also

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 2 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 1... Algorithm Analysis Course Organization Abstract Data Types and Data Structures Pseudocode Algorithm

More information

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

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

Disjoint set (Union-Find)

Disjoint set (Union-Find) CS124 Lecture 6 Spring 2011 Disjoint set (Union-Find) For Kruskal s algorithm for the minimum spanning tree problem, we found that we needed a data structure for maintaining a collection of disjoint sets.

More information

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

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 12 Dr. Ted Ralphs ISE 172 Lecture 12 1 References for Today s Lecture Required reading Chapter 5 References CLRS Chapter 11 D.E. Knuth, The Art of Computer

More information

Lecture 15 Notes Binary Search Trees

Lecture 15 Notes Binary Search Trees Lecture 15 Notes Binary Search Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, André Platzer, Rob Simmons 1 Introduction In this lecture, we will continue considering ways

More information

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

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016 CITS3001 Algorithms, Agents and Artificial Intelligence Semester 2, 2016 Tim French School of Computer Science & Software Eng. The University of Western Australia 2. Review of algorithmic concepts CLRS,

More information

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

Class Note #02. [Overall Information] [During the Lecture] Class Note #02 Date: 01/11/2006 [Overall Information] In this class, after a few additional announcements, we study the worst-case running time of Insertion Sort. The asymptotic notation (also called,

More information

Problem Set 9 Solutions

Problem Set 9 Solutions Introduction to Algorithms December 8, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 34 Problem Set 9 Solutions Reading: Chapters 32.1

More information

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

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4 Everyone Gets an A! Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 22, 2007 Taking Stock A Canonical

More information

CS2 Algorithms and Data Structures Note 4

CS2 Algorithms and Data Structures Note 4 CS2 Algorithms and Data Structures Note 4 Hash Tables In this lecture, we will introduce a particularly efficient data structure for the Dictionary ADT. 4.1 Dictionaries A Dictionary stores key element

More information

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

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University U Kang 1 In This Lecture Learn how to evaluate algorithm efficiency Learn the concept of average case, best case,

More information

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

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 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 Course Info course webpage: - from the syllabus on http://cs.gmu.edu/

More information

Cilk, Matrix Multiplication, and Sorting

Cilk, Matrix Multiplication, and Sorting 6.895 Theory of Parallel Systems Lecture 2 Lecturer: Charles Leiserson Cilk, Matrix Multiplication, and Sorting Lecture Summary 1. Parallel Processing With Cilk This section provides a brief introduction

More information

CS2 Algorithms and Data Structures Note 1

CS2 Algorithms and Data Structures Note 1 CS2 Algorithms and Data Structures Note 1 Analysing Algorithms This thread of the course is concerned with the design and analysis of good algorithms and data structures. Intuitively speaking, an algorithm

More information

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

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7 CSE341T 09/20/2017 Lecture 7 Scan and Quicksort 1 Scan Scan is a very useful primitive for parallel programming. We will use it all the time in this class. First, lets start by thinking about what other

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/18.401J LECTURE 19 Take-home exam Instructions Academic honesty Strategies for doing well Prof. Charles E. Leiserson Take-home quiz The take-home quiz contains 5 problems

More information

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

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section. CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section. 1. Trees a) For the binary tree shown below, what are the preorder, inorder and post order

More information

V Advanced Data Structures

V Advanced Data Structures V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,

More information

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

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager Hash Tables What is a Dictionary? Container class Stores key-element pairs Allows look-up (find) operation Allows insertion/removal of elements May be unordered or ordered Dictionary Keys Must support

More information

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

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017 University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 207 Midterm Examination Instructor: Dr. Ladan Tahvildari, PEng, SMIEEE Date: Wednesday,

More information

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

Containers and genericity. A standardized naming scheme. Cursor properties. Lists. Adding a cell. A specific implementation: (singly) linked lists Topics for this lecture Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Containers and genericity Container operations Prof. Dr. Bertrand Meyer Lists October

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 2 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 1... Algorithm Analysis Course Organization Abstract Data Types and Data Structures Pseudocode Algorithm

More information

Introduction to Asymptotic Running Time Analysis CMPSC 122

Introduction to Asymptotic Running Time Analysis CMPSC 122 Introduction to Asymptotic Running Time Analysis CMPSC 122 I. Comparing Two Searching Algorithms Let's begin by considering two searching algorithms you know, both of which will have input parameters of

More information

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

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

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

Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Lecture - 18 Database Indexing: Hashing We will start on

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 10 Implementing MST Algorithms Adam Smith Minimum spanning tree (MST) Input: A connected undirected graph G = (V, E) with weight function w : E R. For now, assume

More information

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

Questions. 6. Suppose we were to define a hash code on strings s by: Questions 1. Suppose you are given a list of n elements. A brute force method to find duplicates could use two (nested) loops. The outer loop iterates over position i the list, and the inner loop iterates

More information

CSC236 Week 5. Larry Zhang

CSC236 Week 5. Larry Zhang CSC236 Week 5 Larry Zhang 1 Logistics Test 1 after lecture Location : IB110 (Last names A-S), IB 150 (Last names T-Z) Length of test: 50 minutes If you do really well... 2 Recap We learned two types of

More information

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

Amortized Analysis. Prof. Clarkson Fall Today s music: : Money, Money, Money by ABBA Amortized Analysis Prof. Clarkson Fall 2015 Today s music: : "Money, Money, Money" by ABBA Review Current topic: Reasoning about performance Efficiency Big Oh Today: Alternative notions of efficiency Amortized

More information