Chapter 17 Amortized Analysis Lee, Hsiu-Hui

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed.
CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables.
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
1 Chapter 17: Amortized Analysis III. 2 Dynamic Table Sometimes, we may not know in advance the #objects to be stored in a table We may allocate space.
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.
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 Anaysis of Algorihms, A.Yazici, Spring 2007CEng Amortized Analysis of Algorithms Adnan YAZICI Dept. of Computer Engineering Middle East.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
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.
Andreas Klappenecker [based on the slides of Prof. Welch]
Tirgul 11 Solutions for questions from T2, T3 DFS & BFS - reminder Some Hashing Reminder : don’t forget to run ~dast/bin/testEx3.csh on your jar file before.
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.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
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.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
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.
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.
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.
David Luebke 1 12/12/2015 CS 332: Algorithms 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.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Amortized Analysis of Rehashing
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Data Structures Using C++ 2E
Complexity analysis.
Amortized Analysis (chap. 17)
Chapter 18 B-Trees Lee, Hsiu-Hui
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.
Heapsort Chapter 6 Lee, Hsiu-Hui
Data Structures Using C++ 2E
Asymptotic Notations Algorithms Lecture 9.
Hash Tables – 2 Comp 122, Spring 2004.
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.
Haim Kaplan, Uri Zwick March 2018
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
Amortized Analysis and Heaps Intro
Vectors and Array Lists
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Hash Tables – 2 1.
Presentation transcript:

Chapter 17 Amortized Analysis Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.

Amortized Analysis The time required to perform a sequence of data structure operations is averaged over all the operations performed. An amortized analysis guarantees the average performance of each operation in the worst case. 20071228 Hsiu-Hui Lee

Three common techniques aggregate analysis accounting method potential method 20071228 Hsiu-Hui Lee

17.1 Aggregate Analysis For all n, a sequence of n operations takes worst time T(n) in total. The amortize cost of each operation is . 20071228 Hsiu-Hui Lee

EX1-Stack operation PUSH(S, x) / POP(S) / MULTIPOP(S, k) 1 while not STACK-EMPTY(S) and k  0 2 do POP(S) 3 k  k – 1 20071228 Hsiu-Hui Lee

Action of MULTIPOP on a stack S  top  10 47  top  23 17 6 39 10 47  MULTIPOP(S,4) MULTIPOP(S,7) 20071228 Hsiu-Hui Lee

PUSH(S, x) / POP(S) MULTIPOP(S, k) Each runs in O(1) time. Actual running time for a sequence of n PUSH, POP operations is (n). MULTIPOP(S, k) pops the top k objects of stack Total cost : min(s, k) s : stack size 20071228 Hsiu-Hui Lee

We can get a better bound. (see the next) Analyze a sequence of n PUSH, POP, and MULTIPOP operation on an initially empty stack. 問每一個 operation 在 worst-case 之 amortized cost? PUSH O(1) POP O(1) MULTIPOP O(n) (the stack size is at most n) Total cost of n operations: O(n2) We can get a better bound. (see the next) Total cost of any seq of n operations: O(n) 20071228 Hsiu-Hui Lee

Each object can be popped at most once for each time it is pushed. Number of times the POP can be called on a nonempty stack, including calls within MULTIPOP, is at most the number of PUSH operatons, which is at most n. Total cost of any seq of n operations: O(n) better bound ! The amortized cost of an operation is . 20071228 Hsiu-Hui Lee

EX2-Incrementing a binary counter A[i] 每 2i 次改變一次 20071228 Hsiu-Hui Lee

2 while i < length[A] and A[i] = 1 3 do A[i]  0 4 i  i + 1 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 length[A]=k Cost : (k) 20071228 Hsiu-Hui Lee

Analysis: A seq of n increment operations : O(nk) (k is the word length) Amortize Analysis: The total number of flips in the sequence is 20071228 Hsiu-Hui Lee

17.2 Accounting method We assign differing charges to different operations, with some operations charged more or less than the actual cost. The amount we charge an operation is called its amortized cost. Some are charged more than actual cost. Some are charged less. (在aggregate method所有operation有相同的amortized cost) 20071228 Hsiu-Hui Lee

If the amortized cost > actual cost, the difference is treated as credit Credit can be used later on to help pay for operations whose amortized cost is less than their actual cost. 20071228 Hsiu-Hui Lee

If we want analysis with amortized costs to show that in the worst cast the average cost per operation is small, the total amortized cost of a sequence of operations must be an upper bound on the total actual cost of the sequence. Moreover, as in aggregate analysis, this relationship must hold for all sequences of operations. 20071228 Hsiu-Hui Lee

for all sequence of n operations. If we denote the actual cost of the ith operation by ci and the amortized cost of the ith operation by , we require (i.e. total amortized cost  total actual cost) for all sequence of n operations. The total credit stored in the data structure is the difference between the total actual cost, or The total credit must be nonnegative at all times. 20071228 Hsiu-Hui Lee

EX1-Stack operation n 個運作之後, total amortized cost = O(n) PUSH 1 2 POP actual cost n 個運作之後, total amortized cost = O(n) amortized cost PUSH 1 2 POP MULTIPOP min{k,s} 20071228 Hsiu-Hui Lee

EX2-Incrementing a binary counter actual cost amortized cost Each time, there is exactly one 0 that is changed into 1. The number of 1’s in the counter is never negative! Amortized cost is at most 2 = O(1). 執行 n 次 Increment 其 total amortized cost = O(n) 01 1 2 10 20071228 Hsiu-Hui Lee

17.3 Potential method Like the accounting method, but think of the credit as potential stored with the entire data structure. Accounting method stores credit with specific objects. Potential method stores potential in the data structure as a whole. Can release potential to pay for future operations. Most flexible of the amortized analysis methods. 20071228 Hsiu-Hui Lee

20071228 Hsiu-Hui Lee

20071228 Hsiu-Hui Lee

If then the potential increases. If for all i, then the amortized cost is always an upper bound on actual cost . If then the potential increases. 20071228 Hsiu-Hui Lee

EX1-Stack operation the number of objects in the stack of the ith operation. 20071228 Hsiu-Hui Lee

PUSH 20071228 Hsiu-Hui Lee

MULTIPOP 20071228 Hsiu-Hui Lee

POP The amortized cost of each of these three operations is O(1). the total amortized cost of a sequence of n operations is O(n) 20071228 Hsiu-Hui Lee

EX2-Incrementing a binary counter the number of 1’s in the counter after the ith operations The ith INCREMENT operation resets bits. The Counter starts at zero Amortized cost = O(1) Total amortized cost of n operations is O(n) 20071228 Hsiu-Hui Lee

Even if the counter does not start at zero: 20071228 Hsiu-Hui Lee

17.4 Dynamic tables A nice use of amortized analysis. Scenario Have a table—maybe a hash table. Don’t know in advance how many objects will be stored in it. When it fills, must reallocate with a larger size, copying all objects into the new larger table. When it gets sufficiently small, might want to reallocate with a smaller size. Details of table organization not important. 20071228 Hsiu-Hui Lee

Load factor α = num/size, Goals 1. O(1) amortized time per operation. 2. Unused space always ≤ constant fraction of allocated space. Load factor α = num/size, num = # items stored size = allocated size. If size = 0, then num = 0. Call α = 1. Never allow α > 1. Keepα > a constant fraction ⇒ goal (2). 20071228 Hsiu-Hui Lee

Table expansion Consider only TABLE-INSERT TABLE-DELETE (discuss later) Guarantees that load-factor (load factor) Each time we actually insert an item into the table, it’s an elementary insertion. 20071228 Hsiu-Hui Lee

2 then allocate table[T] with 1 slot 3 size[T]  1 TABLE_INSERT(T, x) 1 if size[T] = 0 2 then allocate table[T] with 1 slot 3 size[T]  1 4 if num[T] = size[T] 5 then allocate new-table with 2  size[T] slots 6 insert all items in table[T] in new-table 7 free table[T] 8 table[T]  new-table 9 size[T]  2  size[T] 10 insert x into table[T] 11 num[T]  num[T] + 1 20071228 Hsiu-Hui Lee

Analysis Running time: Charge 1 per elementary insertion. Count only elementary insertions, since all other costs together are constant per call. ci = actual cost of ith operation If not full, ci = 1. If full, have i - 1 items in the table at the start of the ith operation. Have to copy all i - 1 existing items, then insert ith item⇒ci = i . n operations ⇒ci = O(n)⇒ O(n2) time for n operations. (?) 20071228 Hsiu-Hui Lee

Aggregate method: amortized cost = 3 20071228 Hsiu-Hui Lee

Accounting method: Each item pays for 3 elementary insertions; 1. inserting itself in the current table, 2. moving itself when the table is expanded, and 3. moving another item that has already been moved once when the table is expanded. Charge $3 per insertion of x. $1 pays for x’s insertion. $1 pays for x to be moved in the future. $1 pays for some other item to be moved. 20071228 Hsiu-Hui Lee

Potential method: (not expansion) 20071228 Hsiu-Hui Lee

Example: 20071228 Hsiu-Hui Lee

(expansion) 20071228 Hsiu-Hui Lee

Example: Amortized cost = 3 20071228 Hsiu-Hui Lee

Table expansion and contraction To implement a TABLE-DELETE operation, it is desirable to contract the table when the load factor of the table becomes too small, so that the waste space is not exorbitant. 20071228 Hsiu-Hui Lee

Goal: The load factor of the dynamic table is bounded below by a constant. The amortized cost of a table operation is bounded above by a constant. Set load factor 20071228 Hsiu-Hui Lee

The first n/2 operations are inserted: The second n/2 operations, we perform I, D, D, I, I, D, D, … Total cost of these n operations is . Hence the amortized cost is . Set load factor (as TABLE_DELETE) (after the contraction, the load factor become ) 20071228 Hsiu-Hui Lee

20071228 Hsiu-Hui Lee

Initial 20071228 Hsiu-Hui Lee

TABLE-INSERT if , same as before. if 20071228 Hsiu-Hui Lee

Example: 20071228 Hsiu-Hui Lee

If 20071228 Hsiu-Hui Lee

Example: Amortized cost of Table insert is O(1). 20071228 Hsiu-Hui Lee

TABLE-DELETE If does not cause a contraction (i.e., 20071228 Hsiu-Hui Lee

Example: 20071228 Hsiu-Hui Lee

causes a contraction 20071228 Hsiu-Hui Lee

20071228 Hsiu-Hui Lee

Example: 20071228 Hsiu-Hui Lee

if (Exercise 18.4.3) Amortized cost O(1). 20071228 Hsiu-Hui Lee