Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Similar documents
Data Structures and Algorithm. Xiaoqing Zheng

Graduate Analysis of Algorithms Dr. Haim Levkowitz

Lecture 7: Amortized analysis

Amortized analysis. Amortized analysis

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

Tutorial 4. Dynamic Set: Amortized Analysis

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

Lecture Notes for Chapter 17: Amortized Analysis

Today: Amortized Analysis

Amortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person

Amortized Analysis (chap. 17)

CS361 Homework #3 Solutions

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CSE 548: Analysis of Algorithms. Lecture 8 ( Amortized Analysis )

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

DATA STRUCTURES I, II, III, AND IV

CHAPTER 17 Amortized Analysis

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

25. Minimum Spanning Trees

25. Minimum Spanning Trees

N/4 + N/2 + N = 2N 2.

Algorithms and Data Structures

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

CPSC 320 Sample Final Examination December 2013

Review Of Topics. Review: Induction

Analysis of Algorithms

Lecture VI AMORTIZATION

CS Data Structures and Algorithm Analysis

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

Linear Time Selection

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CS 170 Algorithms Fall 2014 David Wagner MT2

Algorithm Theory - Exercise Class

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

CS 161 Summer 2009 Homework #2 Sample Solutions

Fixed-Universe Successor : Van Emde Boas. Lecture 18

Average Case Analysis of Marking Algorithms

CS 161: Design and Analysis of Algorithms

L feb abhi shelat

Introduction to Algorithms

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Introduction to Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS6901: review of Theory of Computation and Algorithms

An Optimal Algorithm for l 1 -Heavy Hitters in Insertion Streams and Related Problems

On Adaptive Integer Sorting

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

BFS Dijkstra. Oct abhi shelat

Data Structures in Java

Weak Heaps and Friends: Recent Developments

2. ALGORITHM ANALYSIS

Mon Tue Wed Thurs Fri

MIDTERM I CMPS Winter 2013 Warmuth

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

CS3719 Theory of Computation and Algorithms

Problem Set 4 Solutions

Introduction to Algorithms

Algorithm Design and Analysis

Algorithm Design and Analysis

CS Analysis of Recursive Algorithms and Brute Force

Design and Analysis of Algorithms

P, NP, NP-Complete, and NPhard

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Splay Trees. CMSC 420: Lecture 8

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Divide-and-Conquer Algorithms Part Two

Sorting algorithms. Sorting algorithms

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

IS 709/809: Computational Methods in IS Research Fall Exam Review

Average Case Analysis. October 11, 2011

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

5. DIVIDE AND CONQUER I

data structures and algorithms lecture 2

7 Dictionary. Ernst Mayr, Harald Räcke 126

Week 5: Quicksort, Lower bound, Greedy

Advanced Counting Techniques. Chapter 8

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

1 Basic Combinatorics

Allocate-On-Use Space Complexity of Shared-Memory Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Problem 1: (Chernoff Bounds via Negative Dependence - from MU Ex 5.15)

Outline for Today. Static Optimality. Splay Trees. Properties of Splay Trees. Dynamic Optimality (ITA) Balanced BSTs aren't necessarily optimal!

Data Structures and Algorithms Running time and growth functions January 18, 2018

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

5. DIVIDE AND CONQUER I

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Greedy Algorithms My T. UF

Math 391: Midterm 1.0 Spring 2016

Amortized Complexity Main Idea

Transcription:

Amortized Analysis: CLRS Chapter 17 Last revised: August 30, 2006 1

In amortized analysis we try to analyze the time required by a sequence of operations. There are many situations in which, even though it is possible for an individual operation to be very expensive, the average cost of an operation (taken over all operations performed) can be shown to be small. Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case. Note that this is very different from what is normally meant by average-case analysis; there is no probability at work here. We will introduce the main ideas behind amortized analysis through the study of two different problems: stack operations with multipop and incrementing a binary counter. 2

Stack-operations We allow three operations: 1. PUSH(S, x): Push x onto stack S. 2. POP(S): Pop top of S and returns popped item 3. MULTIPOP(S, k): while S is not empty and k 0 POP(S) k := k 1. Note that PUSH(S, x) and POP(S) each take only O(1) worst case time but MULTIPOP(S, k) can take up to n time in the worst case, where n is the total number of items in the stack. Even though a MULTIPOP can take Θ(n) time it is not hard to show that any sequence of n operations, can use at most O(n) time. 3

Incrementing a Binary Counter The following code increments a counter: INCREMENT(A) 1 i 0 2 while i < length[a] and A[i] = 1 3 do A[i] 0 4 i i + 1 5 if i < length[a] 6 then A[i] 1 Counter value A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] Total cost 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 2 0 0 0 0 0 0 1 0 3 3 0 0 0 0 0 0 1 1 4 4 0 0 0 0 0 1 0 0 7 5 0 0 0 0 0 1 0 1 8 6 0 0 0 0 0 1 1 0 10 7 0 0 0 0 0 1 1 1 11 8 0 0 0 0 1 0 0 0 15 9 0 0 0 0 1 0 0 1 16 10 0 0 0 0 1 0 1 0 18 11 0 0 0 0 1 0 1 1 19 12 0 0 0 0 1 1 0 0 22 13 0 0 0 0 1 1 0 1 23 14 0 0 0 0 1 1 1 0 25 15 0 0 0 0 1 1 1 1 26 16 0 0 0 1 0 0 0 0 31 An Increment operation can require as many as k = length[a] bit flips. We claim, though, that any sequence of n Increments requires only O(n) bit flips. 4

3 different approaches to Amortized Analysis. Aggregate Analysis: Let T(n) be the total cost of some sequence of n operations. In the worst case the average cost, or amortized cost per operation, is T(n)/n. In this type of analysis, every operation will have the same amortized cost. The Accounting Method: Each operation is charged an amortized cost, which can be different than actual cost. Difference between actual cost and amortized cost is expressed in credits. Extra credits are placed in specific parts of data structure. Lack of credits are made up by using credits previously placed in data structure. Different operations can have different amortized costs. Sum of the amortized costs of a sequence of operations is total cost of the sequence The Potential Method: The data structure is assigned a potential (energy) 0 based on its current configuration. Potential of original configuration is usually 0. The amortized cost of an operation will be the sum of its actual cost plus the difference between the potential before the operation and the potential after the operation. Different operations can have different amortized costs. Sum of the amortized costs of a sequence of operations is total cost of the sequence 5

Aggregate Analysis: Stack-Operations 1. PUSH(S, x): Push x onto stack S. 2. POP(S): Pop top of S and returns popped item 3. MULTIPOP(S, k): while S is not empty and k 0 POP(S) k := k 1. It is easy to see that any sequence of n PUSH, POP, and MULTIPOP operations takes T(n) = O(n) time so the average time per operation is T(n)/n = O(1). We therefore can assign each operation in the sequence an amortized cost of O(1). Note that there is no probability involved. As stated amortized analysis guarantees the average case performance of each operation in the worst case. 6

Aggregate Analysis: Incrementing a counter Counter value A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] Total cost 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 2 0 0 0 0 0 0 1 0 3 3 0 0 0 0 0 0 1 1 4 4 0 0 0 0 0 1 0 0 7 5 0 0 0 0 0 1 0 1 8 6 0 0 0 0 0 1 1 0 10 7 0 0 0 0 0 1 1 1 11 8 0 0 0 0 1 0 0 0 15 9 0 0 0 0 1 0 0 1 16 10 0 0 0 0 1 0 1 0 18 11 0 0 0 0 1 0 1 1 19 12 0 0 0 0 1 1 0 0 22 13 0 0 0 0 1 1 0 1 23 14 0 0 0 0 1 1 1 0 25 15 0 0 0 0 1 1 1 1 26 16 0 0 0 1 0 0 0 0 31 Every A[0] bit gets flipped. Every 2 nd A[1] bit gets flipped. Every 4 th A[2] bit gets flipped. Every 8 th A[3] bit gets flipped. Every 2 ith A[i] bit gets flipped. So the total number of bits flipped after n increment operations will be k i=0 n 2 i n k i=0 1 2 i < 2n 7

Every A[0] bit gets flipped. Every 2 nd A[1] bit gets flipped. Every 4 th A[2] bit gets flipped. Every 8 th A[3] bit gets flipped. Every 2 ith A[i] bit gets flipped. So the total number of bits flipped after n increment operations will be k i=0 n 2 i n k i=0 1 2 i < 2n This means that every operation requires at most 2n/n bit flips on average, i.e., has an amortized cost of O(1). 8

The Accounting method: Stack-Operations In the Accounting method we assign each operation an amortized cost ĉ, which can be different for the different operations. Let c be the actual cost of some operation. If ĉ > c then the algorithm places ĉ c credits on items in the data structure. If ĉ < c then the algorithm must be able to take c ĉ credits off of the data structure at that time to pay for the rest of the operation. Essentially, earlier actual cheap (real cost) operations are prepaying the expense of later more expensive (real cost) operations. Let ĉ i be the given amortized cost of the i th operation and c i its actual cost. Then ĉ i c i. 9

In the stack problem we use the following amortized costs: Operation Real Cost c Amortized Cost ĉ PUSH(S, x) 1 2 POP(S) 1 0 MULTIPOP(S, k) min(k, S ) 0 When doing a PUSH(S, x), 1 unit of amortized cost cost pays for the actual PUSH(S, x) and the 1 remaining unit of credit gets placed on the item in the stack that was just pushed. Whenever an item in the stack is (MULTI)POPed, it is paid for by the unit of credit sitting on it. It is easy to see that there are always credits available to pay for a POP or MULTIPOP so the amortized costs are well defined. 10

The Accounting method: Incrementing a Counter Counter value A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0] Total cost 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 2 0 0 0 0 0 0 1 0 3 3 0 0 0 0 0 0 1 1 4 4 0 0 0 0 0 1 0 0 7 5 0 0 0 0 0 1 0 1 8 6 0 0 0 0 0 1 1 0 10 7 0 0 0 0 0 1 1 1 11 8 0 0 0 0 1 0 0 0 15 9 0 0 0 0 1 0 0 1 16 10 0 0 0 0 1 0 1 0 18 11 0 0 0 0 1 0 1 1 19 12 0 0 0 0 1 1 0 0 22 13 0 0 0 0 1 1 0 1 23 14 0 0 0 0 1 1 1 0 25 15 0 0 0 0 1 1 1 1 26 16 0 0 0 1 0 0 0 0 31 Note that every increment flips exactly one 0 to be a 1; every 1 that is flipped to be a 0 was originally made into a 1 in a previous operation. In this case we will charge every increment an amortized cost of 2 units of which 1 unit will be charged for flipping the one 0 to be a 1 and the other unit wil lbe left as a credit on the 1. All of the flips of 1s into 0s will be paid for using the credits already sitting on those bits. 11

The Potential Method In the The Potential Method, we assign a Potential Φ to the data structure. The potential can be thought of as stored up energy, or credit, that can be used to pay for expensive operations. The difference between this and the accounting method is that in the accounting method the credits were assigned to objects in the data structure while, in the potential method, the potential is a function of the total data structure, 12

The Potential Method (cont) Starting with an initial data structure D 0. Let D i be the data structure after applying operation i in a sequence to D i 1. Φ maps every D to a real number Φ(D). Let c i be the real cost of operation i. The amortized cost ĉ i of operation i is ĉ i = c i + Φ(D i ) Φ(D i 1 ). The total amortized cost of n operations is then ĉ i = = ( ci + Φ(D i ) Φ(D i 1 ) ) c i + Φ(D n ) Φ(D 0 ) If we define Φ so that n,φ(d n ) Φ(D 0 ) we get c i ĉ i so the amortized cost of a sequence of operations upper-bounds the real cost. 13

The Potential Method: Stack Operations Let Φ(D) be the number of items on stack D. Starting with empty stack D 0 gives us that D,Φ(D) 0 = Φ(D 0 ) so the amortized cost of a sequence of operations upper-bounds the real cost. What are the amortized costs of the operations? Assume that D i 1 currently has s items on the stack. We split into the cases that the i th operation is: PUSH(S, x): Φ(D i ) Φ(D i 1 ) = (s + 1) s = 1. Actual cost is c i = 1. Amortized cost is ĉ i = c i +Φ(D i ) Φ(D i 1 ) = 1+(s+1) s = 2. Multipop(S, k): Set k = min(k, s), no. items popped off stack. Φ(D i ) Φ(D i 1 ) = (s k ) s = k. Actual cost is c i = k. Amortized cost is ĉ i = c i + Φ(D i ) Φ(D i 1 ) = k k = 0. POP(S): 0, same as Multipop(S, 1). 14

Combining, we get the same amortized costs as with the accounting method: Operation Real Cost c Amortized Cost ĉ PUSH(S, x) 1 2 POP(S) 1 0 MULTIPOP(S, k) min(k, S ) 0 15

The Potential Method: Incrementing a counter In this case we let Φ(D) = the number of 1s in the counter. Suppose that the i th increment operation flips t i 1 bits to 0; let b i be the number of 1s in the counter after the operation. Actual cost is c i t i + 1. If b i = 0 then increment totally resets the counter and b i 1 = t i = k. If b i > 0 then b i = b i 1 t i + 1. In both cases b i b i 1 t i + 1 so Φ(D i ) Φ(D i 1 ) b i 1 t i +1 b i 1 = 1 t i. Amortized cost is then, ĉ i = c i + Φ(D i ) Φ(D i 1 ) (t i + 1) + (1 t i ) = 2 16

We just saw that with the given potential function Φ the amortized cost per Increment is 2. This means that c i ĉ i (Φ(D n ) Φ(D 0 )), Starting with an empty counter, Φ(D 0 ) = 0 so c i ĉ i Even if we don t start with an empty counter we can still analyze the run time by noting that c i ĉ i + Φ(D 0 ) 2n + Φ(D 0 ) So if n > k (the counter size), the total cost of the sequence of operations will always be O(n). 17

Binary heap Binomial heap Fibonacci heap Procedure (worst-case) (worst-case) (amortized) Make-Heap Θ(1) Θ(1) Θ(1) Insert Θ(lg n) O(lg n) Θ(1) Minimum Θ(1) O(lg n) Θ(1) Extract-Min Θ(lg n) Θ(lg n) O(lg n) Union Θ(n) O(lg n) Θ(1) Decrease-Key Θ(lg n) Θ(lg n) Θ(1) Delete Θ(lg n) Θ(lg n) O(lg n) We started this section with the table above. We now can understand the last column. In particular, Dijkstra s algorithm for solving the single-source shortest path problem and Prim s similar algorithm for constructing a minimum spanning-tree use V Inserts; V Extract-Mins; E Decrease-Keys. The amortized time bounds given for Fibonacci heaps tells us that this sequence of operations can actually be performed in Θ( V log V + E ) instead of the Θ(( E + V )log V ) that an analysis using worstcase operation time yields. In the next part of this section we will see how to build Fibonacci heaps. 18