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.

Slides:



Advertisements
Similar presentations
October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Advertisements

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.
Data Structures Using C++ 2E
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.
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
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.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
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.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
Interval Trees.
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 Complexity Aggregate method. Accounting method. Potential function method.
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.
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.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Amortized Analysis of Rehashing
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Data Structures Using C++ 2E
Data Structure Interview Question and Answers
Complexity analysis.
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Chapter 17: Amortized Analysis I
Table Amortized cost: $3 Insert 5 Actual cost: $1.
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
Hashing CENG 351.
Data Structures Using C++ 2E
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
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

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 over all the operations performed. Average performance of each operation in the worst case.

Computer Theory Lab. Chapter 17P.3 For all n, a sequence of n operations takes worst time T(n) in total. The amortize cost of each operation is.

Computer Theory Lab. Chapter 17P.4 Three common techniques aggregate analysis accounting method potential method

Computer Theory Lab. Chapter 17P The aggregate analysis Stack operation - PUSH(S, x) - POP(S) - M ULTIPOP (S, k) M ULTIPOP (S, k) 1 while not S TACK- E MPTY (S) and k  0 2 do POP(S) 3 k  k – 1

Computer Theory Lab. Chapter 17P.6 Action of M ULTIPOP on a stack S top   initial top   M ULTIPOP (S,4)  M ULTIPOP (S,7)

Computer Theory Lab. Chapter 17P.7 Analysis a sequence of n PUSH, POP, and MULTIPOP operation on an initially empty stack. O(n 2 ) O(n) (better bound) The amortize cost of an operation is.

Computer Theory Lab. Chapter 17P.8 Incremental of a binary counter

Computer Theory Lab. Chapter 17P.9 I NCREMENT I NCREMENT (A) 1i  0 2while i < length[A] and A[i] = 1 3do A[i]  0 4i  i + 1 5if i < length[A] 6then A[i]  1

Computer Theory Lab. Chapter 17P.10 Analysis: O(n k) (k is the word length) Amortize Analysis:

Computer Theory Lab. Chapter 17P The accounting method We assign different charges to different operations, with some operations charged more or less than the actually cost. The amount we charge an operation is called its amortized cost.

Computer Theory Lab. Chapter 17P.12 When an operation ’ s amortized cost exceeds its actually cost, the difference is assign to specific object in the data structure as credit. Credit can be used later on to help pay for operations whose amortized cost is less than their actual cost.

Computer Theory Lab. Chapter 17P.13 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.

Computer Theory Lab. Chapter 17P.14 If we denote the actual cost of the i th operation by c i and the amortized cost of the i th operation by, we require for all sequence of n operations. The total credit stored in the data structure is the difference between the total actual cost, or.

Computer Theory Lab. Chapter 17P.15 Stack operation Amortize cost: O(1) PUSH1 2 POP1 0 M ULTIPOP min{k,s}M ULTIPOP 0

Computer Theory Lab. Chapter 17P.16 Incrementing a binary counter 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). 0101 1 0101 2 1010 1 1010 0

Computer Theory Lab. Chapter 17P The potential method

Computer Theory Lab. Chapter 17P.18

Computer Theory Lab. Chapter 17P.19 If then. If then the potential increases.

Computer Theory Lab. Chapter 17P.20 Stack operations the number of objects in the stack of the i th operation.

Computer Theory Lab. Chapter 17P.21 PUSH

Computer Theory Lab. Chapter 17P.22 MULTIPOP

Computer Theory Lab. Chapter 17P.23 POP The amortized cost of each of these operations is O(1).

Computer Theory Lab. Chapter 17P.24 Incrementing a binary counter the number of 1 ’ s in the counter after the ith operations =. = the ith INCREMENT operation resets bits. Amortized cost = O(1)

Computer Theory Lab. Chapter 17P.25 Even if the counter does not start at zero:

17.4 Dynamic tables

Computer Theory Lab. Chapter 17P Table expansion TABLE-INSERT TABLE-DELETE (discuss later) load-factor (load factor)

Computer Theory Lab. Chapter 17P.28 TABLE_INSERT T ABLE _I NSERT (T, x) 1if size[T] = 0 2 then allocate table[T] with 1 slot 3 size[T]  1 4if 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

Computer Theory Lab. Chapter 17P.29 Aggregate method: amortized cost = 3

Computer Theory Lab. Chapter 17P.30 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.

Computer Theory Lab. Chapter 17P.31 Potential method: (not expansion)

Computer Theory Lab. Chapter 17P.32 Example:

Computer Theory Lab. Chapter 17P.33 (expansion)

Computer Theory Lab. Chapter 17P.34 Example: Amortized cost = 3

Computer Theory Lab. Chapter 17P Table expansion and contraction To implement a T ABLE -D ELETE 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.

Computer Theory Lab. Chapter 17P.36 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

Computer Theory Lab. Chapter 17P.37 The first operations are inserted. The second 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 T ABLE _D ELETE ) (after the contraction, the load factor become )

Computer Theory Lab. Chapter 17P.38

Computer Theory Lab. Chapter 17P.39 Initial

Computer Theory Lab. Chapter 17P.40 TABLE-INSERT if, same as before. if

Computer Theory Lab. Chapter 17P.41 Example:

Computer Theory Lab. Chapter 17P.42 If

Computer Theory Lab. Chapter 17P.43 Example: Amortized cost of Table insert is O(1).

Computer Theory Lab. Chapter 17P.44 T ABLE -D ELETE If does not cause a contraction (i.e.,

Computer Theory Lab. Chapter 17P.45 Example:

Computer Theory Lab. Chapter 17P.46 causes a contraction

Computer Theory Lab. Chapter 17P.47

Computer Theory Lab. Chapter 17P.48 Example:

Computer Theory Lab. Chapter 17P.49 if (Exercise ) Amortized cost O(1).