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

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

Analysis of Algorithms
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
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.
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
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.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
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.
Amortized Analysis.
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 Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
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.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 5: Advanced Design Techniques.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
CS 361 – Chapter 1 What is this class about? –Data structures, useful algorithm techniques –Programming needs to address both –You already have done some.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
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
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
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.
CMSC 341 Amortized Analysis.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
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.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
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]
CS38 Introduction to Algorithms
Complexity analysis.
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Chapter 17: Amortized Analysis I
Presentation by Marty Krogel
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
CSCE 411 Design and Analysis of Algorithms
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

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

Analyzing Calls to a Data Structure Some algorithms involve repeated calls to one or more data structures Example: Heapsort repeatedly insert keys into a priority queue (heap) repeatedly remove the smallest key from the heap When analyzing the running time of an algorithm, one needs to sum up the time spent in all the calls to the data structure Problem: If different calls take different times, how can we accurately calculate the total time?

Example: Heapsort Each of the n calls to insert into the heap operates on a heap with at most n elements Inserting into a heap with n elements takes O(log n) time So total time spent doing the insertions is O(n log n) time But maybe this is an over-estimate! different insertions take different amounts of time many of the insertions are on significantly smaller heaps

Amortized Analysis Purpose is to accurately compute the total time spent in executing a sequence of operations on a data structure Three different approaches: aggregate method: brute force accounting method: assign costs to each operation so that it is easy to sum them up while still ensuring that the result is accurate potential method: a more sophisticated version of the accounting method (omitted here)

Running Example #1: Augmented Stack S Operations are: Push(S,x) Pop(S) Multipop(S,k) - pop the top k elements Implement with either array or linked list time for Push is O(1) time for Pop is O(1) time for Multipop is O(min(|S|,k))

Running Example #2: k-Bit Counter A Operation: increment(A) - add 1 (initially 0) Implementation: k-element binary array use grade school ripple-carry algorithm

Aggregate Method Show that a sequence of n operations takes T(n) time We can then say that the amortized cost per operation is T(n)/n Makes no distinction between operation types

Augmented Stack: A Simple Worst Case Argument In a sequence of n operations, the stack never holds more than n elements. Thus, the cost of a multipop is O(n) Therefore, the worst-case cost of any sequence of n operations is O(n2). But this is an over-estimate!

Aggregate Method

Aggregate Method for Augmented Stack Key idea: total number of pops (or multipops) in the entire sequence of operations is at most the total number of pushes Suppose that the maximum number of Push operations in the sequence is n. So time for entire sequence is O(n). Amortized cost per operation: O(n)/n = O(1).

Aggregate Method for k-Bit Counter Worst-case time for an increment is O(k). This occurs when all k bits are flipped But in a sequence of n operations, not all of them will cause all k bits to flip: bit 0 flips with every increment bit 1 flips with every 2nd increment bit 2 flips with every 4th increment … bit k flips with every 2k-th increment

Aggregate Method for k-Bit Counter Total number of bit flips in n increment operations is n + n/2 + n/4 + … + n/2k < n(1/(1-1/2))= 2n So total cost of the sequence is O(n). Amortized cost per operation is O(n)/n = O(1).

Accounting Method

Accounting Method Assign a cost, called the "amortized cost", to each operation Assignment must ensure that the sum of all the amortized costs in a sequence is at least the sum of all the actual costs remember, we want an upper bound on the total cost of the sequence How can we ensure this property?

Accounting Method For each operation in the sequence: if amortized cost > actual cost then store extra as a credit with an object in the data structure if amortized cost < actual cost then use the stored credits to make up the difference Never allowed to go into the red! Must have enough credit saved up to pay for any future underestimates.

Accounting Method vs. Aggregate Method first analyze entire sequence then calculate amortized cost per operation Accounting method: first assign amortized cost per operation check that they are valid (never go into the red) then compute cost of entire sequence of operations

Accounting Method for Augmented Stack Assign these amortized costs: Push - 2 Pop - 0 Multipop - 0 For Push, actual cost is 1. Store the extra 1 as a credit, associated with the pushed element. Pay for each popped element (either from Pop or Multipop) using the associated credit

Accounting Method for Augmented Stack There is always enough credit to pay for each operation (never go into red). Each amortized cost is O(1) So cost of entire sequence of n operations is O(n).

Accounting Method for k-Bit Counter Assign amortized cost for increment operation to be 2. Actual cost is the number of bits flipped: a series of 1's are reset to 0 then a 0 is set to 1 Idea: 1 is used to pay for flipping a 0 to 1. The extra 1 is stored with the bit to pay for the next change (when it is flipped back to 0)

Accounting Method for k-Bit Counter 1 1 1 1 1 1 1

Accounting Method for k-Bit Counter All changes from 1 to 0 are paid for with previously stored credit (never go into red) Amortized time per operation is O(1) total cost of sequence is O(n)

Conclusions Amortized Analysis allows one to estimate the cost of a sequence of operations on data structures. The method is typically more accurate than worst case analysis when the data structure is dynamically changing.