Chapter 17: Amortized Analysis I

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

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
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.
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.
Andreas Klappenecker [based on the slides of Prof. Welch]
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.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
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.
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.
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.
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.
+ 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,
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.
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.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
1 i206: Lecture 17: Exam 2 Prep ; Intro to Regular Expressions Marti Hearst Spring 2012.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
CSE373: Data Structures & Algorithms Priority Queues
Chapter 4 Stacks
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
COSC160: Data Structures Binary Trees
Complexity analysis.
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of 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.
Chapter 17 Amortized Analysis Lee, Hsiu-Hui
Distribution of the Sample Means
Introduction to Algorithms
COSC160: Data Structures Linked Lists
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Chapter Trees and B-Trees
Chapter Trees and B-Trees
Computation.
Sorting We have actually seen already two efficient ways to sort:
Quicksort analysis Bubble sort
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
§3 Worst-Case vs. Amortized
Haim Kaplan, Uri Zwick March 2018
Linear-Time Sorting Algorithms
CSCE 411 Design and Analysis of Algorithms
Chapter 8-2: Sorting in Linear Time
Chapter 7 Quicksort.
CSE 326: Data Structures Lecture #12 Hashing II
Amortized Analysis and Heaps Intro
CSE 326: Data Structures Lecture #9 AVL II
Quicksort and Randomized Algs
326 Lecture 9 Henry Kautz Winter Quarter 2002
Sorting We have actually seen already two efficient ways to sort:
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Chapter 17: Amortized Analysis I

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

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)

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(n2) time [correct, but not tight]

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) !!!

worst-case total time = O(n) 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)

worst-case total time = O(n1x + n2y + n3z) Amortized Cost In general, we can say something like: OP1 runs in amortized O(x) time OP2 runs in amortized O(y) time OP3 runs in amortized O(z) time Meaning: For any sequence of operations with #OP1 = n1, #OP2 = n2, #OP3 = n3, worst-case total time = O(n1x + n2y + n3z)

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

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

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

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]

bi is flipped only once in every 2i increments Binary Counter Let us denote the bits in the counter by b0, b1, b2, …, bk-1, starting from the right b0 b1 b2 b3 b4 Observation: bi is flipped only once in every 2i increments Precisely, bi is flipped at xth increment  x is divisible by 2i

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

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

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

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

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