1 Chapter 17: Amortized Analysis I. 2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time.

Slides:



Advertisements
Similar presentations
Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed.
Advertisements

Growth-rate Functions
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
MS 101: Algorithms Instructor Neelima Gupta
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
Advanced Data Structures Sartaj Sahni
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
Tirgul 9 Amortized analysis Graph representation.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Lecture 3 Tuesday, 2/10/09 Amortized Analysis.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Amortized Analysis (chap. 17) Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Sorting Chapter 10.
Andreas Klappenecker [based on the slides of Prof. Welch]
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
Sorting in Linear Time Lower bound for comparison-based sorting
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Ajinkya Nene, Lynbrook CS Club. 04/21/2014. Definition Allows you to figure the worst-case bound for the performance of an algorithm (most useful for.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 5: Advanced Design Techniques.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
CS 361 – Chapter 1 What is this class about? –Data structures, useful algorithm techniques –Programming needs to address both –You already have done some.
CSC 211 Data Structures Lecture 13
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
CMSC 341 Amortized Analysis.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential.
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
Introduction to Algorithms Amortized Analysis My T. UF.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
1 Chapter 17: Amortized Analysis II. 2 About this lecture Previous lecture shows Aggregate Method This lecture shows two more methods: (2)Accounting Method.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Chapter 17: Amortized Analysis I
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Chapter 17 Amortized Analysis Lee, Hsiu-Hui
Sorting We have actually seen already two efficient ways to sort:
§3 Worst-Case vs. Amortized
Linear-Time Sorting Algorithms
CSCE 411 Design and Analysis of Algorithms
Amortized Analysis and Heaps Intro
Sorting We have actually seen already two efficient ways to sort:
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

1 Chapter 17: Amortized Analysis I

2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time to perform an operation Introduce amortized cost of an operation Three Methods for the Same Purpose (1) Aggregate Method (2)Accounting Method (3) Potential Method This Lecture

3 Super Stack Your friend has created a super stack, which, apart from PUSH/POP, supports: SUPER-POP(k): pop top k items Suppose SUPER-POP never pops more items than current stack size The time for SUPER-POP is O(k) The time for PUSH/POP is O(1)

4 Super Stack Suppose we start with an empty stack, and we have performed n operations But we don’t know the order Questions: Worst-case time of a SUPER-POP ? Ans. O(n) time [why?] Total time of n operations in worst case ? Ans. O(n 2 ) time [correct, but not tight]

5 Super Stack Though we don’t know the order of the operations, we still know that: There are at most n PUSH/POP  Time spent on PUSH/POP = O(n) # items popped by all SUPER-POP cannot exceed total # items ever pushed into stack  Time spent on SUPER-POP = O(n) So, total time of n operations = O(n) !!!

6 Amortized Cost So far, there are no assumptions on n and the order of operations. Thus, we have: For any n and any sequence of n operations, worst-case total time = O(n) We can think of each operation performs in average O(n) / n = O(1) time  We say amortized cost = O(1) per operation (or, each runs in amortized O(1) time)

7 Amortized Cost In general, we can say something like: OP 1 runs in amortized O(x) time OP 2 runs in amortized O(y) time OP 3 runs in amortized O(z) time Meaning: For any sequence of operations with #OP 1 = n 1, #OP 2 = n 2, #OP 3 = n 3, worst-case total time = O(n 1 x + n 2 y + n 3 z)

8 Binary Counter Let us see another example of implementing a k-bit binary counter At the beginning, count is 0, and the counter will be like (assume k=5): which is the binary representation of the count

9 Binary Counter When the counter is incremented, the content will change Example: content of counter when: count = count = 6cost = 2 The cost of the increment is equal to the number of bits flipped

10 Binary Counter Special case: When all bits in the counter are 1, an increment resets all bits to count = MAX count = 0cost = k The cost of the corresponding increment is equal to k, the number of bits flipped

11 Binary Counter Suppose we have performed n increments Questions: Worst-case time of an increment ? Ans. O(k) time Total time of n operations in worst case ? Ans. O(nk) time [correct, but not tight]

12 Binary Counter Let us denote the bits in the counter by b 0, b 1, b 2, …, b k-1, starting from the right b0b0 b1b1 b2b2 b3b3 b4b4 Observation: b i is flipped only once in every 2 i increments Precisely, b i is flipped at x th increment  x is divisible by 2 i

13 Amortized Cost So, for n increments, the total cost is:  i=0 to k  n / 2 i    i=0 to k ( n / 2 i )  2n By dividing total cost with #increments,  amortized cost of increment = O(1)

14 Aggregate Method The computation of amortized cost of an operation in super stack or binary counter follows similar steps: 1.Find total cost (thus, an “aggregation” ) 2.Divide total cost by #operations This method is called Aggregate Method

15 Remarks In amortized analysis, the amortized cost to perform an operation is computed by the average over all performed operations There is a different topic called average- case analysis, which studies average performance over all inputs Both are useful, but they just study different things

16 Example: Average-Case Analysis Consider building a binary search tree for n numbers with random insertion order Final height varies on insertion order Suppose each of the n! possible insertion orders is equally likely to be chosen Then, we may be able to compute the average height of the tree average is over all insertion orders

17 In fact, we can show that average height =  (log n) and very likely, height =  (log n) So, we can say average search time =  (log n) However, we cannot say amortized search time =  (log n) … why? Example: Average-Case Analysis