CSCE 411 Design and Analysis of Algorithms

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

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.
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.
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.
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.
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
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.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Amortized Analysis.
Sorting With Priority Queue In-place Extra O(N) space
Chapter 4 Stacks
School of Computing Clemson University Fall, 2012
Andreas Klappenecker [partially based on the slides of Prof. Welch]
CS38 Introduction to Algorithms
Complexity analysis.
Amortized Analysis (chap. 17)
Priority Queues and Heaps
CSCE 411 Design and Analysis of Algorithms
Analysis Of Binomial Heaps
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
October 30th – Priority QUeues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Heaps and the Heapsort Heaps and priority queues
Ch. 8 Priority Queues And Heaps
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.
Lecture 3 / 4 Algorithm Analysis
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CSCE 411 Design and Analysis of Algorithms
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
ECE 352 Digital System Fundamentals
Amortized Analysis and Heaps Intro
Graph Algorithms: Shortest Path
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Lecture 21 Amortized Analysis
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

CSCE 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Spring 2013 CSCE 411, Spring 2013: Set 6

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 the overall algorithm, need to sum up the time spent in all the calls to the data structure When different calls take different times, how can we accurately calculate the total time? CSCE 411, Spring 2013: Set 6

Heapsort Example 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 CSCE 411, Spring 2013: Set 6

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 result is accurate potential method: a more sophisticated version of the accounting method CSCE 411, Spring 2013: Set 6

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)) CSCE 411, Spring 2013: Set 6

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 CSCE 411, Spring 2013: Set 6

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 CSCE 411, Spring 2013: Set 6

Simple Argument for Augmented Stack In a sequence of n operations, the stack never holds more than n elements. So cost of a multipop is O(n) So worst-case cost of any sequence of n operations is O(n2). But this is an over-estimate! CSCE 411, Spring 2013: Set 6

Aggregate Method for Augmented Stack Key idea: total number of elements popped in the entire sequence is at most the total number of Pushes done. for both Pop and Multipop Maximum number of Pushes is n. So time for entire sequence is O(n). And amortized cost per operation is O(n)/n = O(1). CSCE 411, Spring 2013: Set 6

Aggregate Method for k-Bit Counter Worst-case time for an increment is O(k), 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 CSCE 411, Spring 2013: Set 6

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

Accounting Method Assign a cost, called the "amortized cost", to each operation Each amortized cost should be something simple so it is easy to add up all the amortized costs 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 to ensure this property? CSCE 411, Spring 2013: Set 6

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. CSCE 411, Spring 2013: Set 6

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 CSCE 411, Spring 2013: Set 6

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 CSCE 411, Spring 2013: Set 6

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). CSCE 411, Spring 2013: Set 6

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) CSCE 411, Spring 2013: Set 6

Accounting Method for k-Bit Counter 1 1 1 1 1 1 1 CSCE 411, Spring 2013: Set 6

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) CSCE 411, Spring 2013: Set 6

Potential Method Similar to accounting method Amortized costs are assigned in a more complicated way based on a potential function and the current state of the data structure Must ensure that sum of amortized costs of all operations in the sequence is at least the sum of the actual costs of all operations in the sequence. CSCE 411, Spring 2013: Set 6

Potential Method Define potential function  which maps any state of the data structure to a real number Notation: D0 - initial state of data structure Di - state of data structure after i-th operation ci - actual cost of i-th operation mi - amortized cost of i-th operation CSCE 411, Spring 2013: Set 6

Potential Method Define amortized cost of i-th operation: mi = ci + (Di) – (Di–1) This is the actual cost plus the change in the potential from previous state to current state Sum of all the amortized costs is sum of all the actual costs plus (Dn) — (D0) “telescoping sum” must ensure this last term is nonnegative CSCE 411, Spring 2013: Set 6

Potential Function Usually  is defined so that (D0) = 0 and (Di) ≥ 0 so it is easy to see that (Dn) — (D0) is nonnegative CSCE 411, Spring 2013: Set 6

Potential Method for Augmented Stack Define (Di) to be number of elements in the stack after the i-th operation Check: (D0) = 0, since stack is initially empty (Di) ≥ 0, since can't have a negative number of elements in the stack Next calculate amortized cost of each operation… CSCE 411, Spring 2013: Set 6

Potential Method for Augmented Stack If i-th operation is a Push and stack has s elements: mi = ci + (Di) — (Di-1) = 1 + (s+1) — s = 2 If i-th operation is a pop and stack has s elements: = 1 + (s–1) — s = 0 CSCE 411, Spring 2013: Set 6

Potential Method for Augmented Stack If i-th operation is a Multipop(k) and stack has s elements: Let x = min(s,k) mi = ci + (Di) — (Di-1) = x + (s–x) — s = 0 All operations have O(1) amortized time So cost of entire sequence is O(n) CSCE 411, Spring 2013: Set 6

Potential Method for k-Bit Counter Define (Di) to be number of 1's in the counter after the i-th operation Check: (D0) = 0, since counter is initially all 0's (Di) ≥ 0, since can't have a negative number of 1's in the counter Next calculate amortized cost of the increment operation… CSCE 411, Spring 2013: Set 6

Potential Method for k-Bit Counter Let b = number of 1's just before i-th op Let x = number of 1's that are changed to 0 in i-th op mi = ci + (Di) — (Di-1) = (x+1) + (b–x+1) – b = 2 All ops have O(1) amortized time So total cost of sequence is O(n) x 1's are changed to 0 and one 0 is changed to 1 Variability in actual cost is cancelled out by variability in potential function change, leading to a simple amortized cost. CSCE 411, Spring 2013: Set 6