Tirgul 9 Amortized analysis Graph representation.

Slides:



Advertisements
Similar presentations
Transformations We want to be able to make changes to the image larger/smaller rotate move This can be efficiently achieved through mathematical operations.
Advertisements

Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hash-Based Indexes The slides for this text are organized into chapters. This lecture covers Chapter 10. Chapter 1: Introduction to Database Systems Chapter.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Data Structures Haim Kaplan and Uri Zwick October 2013 Lecture 2 Amortized Analysis “amortized analysis bounds the worst case running time of a sequence.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
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.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
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.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
1 Hash-Based Indexes Yanlei Diao UMass Amherst Feb 22, 2006 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Lecture 3 Tuesday, 2/10/09 Amortized Analysis.
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.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Andreas Klappenecker [based on the slides of Prof. Welch]
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
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.
Hashing General idea: Get a large array
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
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.
WEEK 8 SYSTEMS OF EQUATIONS DETERMINANTS AND CRAMER’S RULE.
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.
Targil 6 Notes This week: –Linear time Sort – continue: Radix Sort Some Cormen Questions –Sparse Matrix representation & usage. Bucket sort Counting sort.
An Optimal Cache-Oblivious Priority Queue and Its Applications in Graph Algorithms By Arge, Bender, Demaine, Holland-Minkley, Munro Presented by Adam Sheffer.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
1.1 CS220 Database Systems Indexing: Hashing Slides courtesy G. Kollios Boston University via UC Berkeley.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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.
2 2.1 © 2012 Pearson Education, Inc. Matrix Algebra MATRIX OPERATIONS.
© Love Ekenberg Hashing Love Ekenberg. © Love Ekenberg In General These slides provide an overview of different hashing techniques that are used to store.
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.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
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.
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
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
Hash-Based Indexes Chapter 11
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

Tirgul 9 Amortized analysis Graph representation

Amortized Analysis Many times we have data structures where some operations take time O(n) in the worst-case. But the total time to perform n operations is less than O(n 2 ). Amortized analysis is used for exactly such cases. We want to analyze the average time per operation, for the worst sequence of n operations. So, this is a combination of a worst case analysis with an average case analysis, but without assuming any distribution on the input.

Example - Stack with multi-pop As a first example we analyze the performance of a stack with one additional operation - multipop(k) - that pops the top k elements of the stack. Since multipop can take O(n) time in the worst-case, we might conclude that n stack operations can take O(n 2 ) in the worst case. We will analyze this simple D.S. using three methods: –The aggregate method –The accounting method –The potential method and see that the average time per operation is still O(1).

The aggregate method In this method we find T(n), the total time to perform n operations in the worst-case. The amortized cost of each operation is then defined as T(n)/n In our stack example, T(n) = O(n) : If we performed a total of k push operations, then the total time for the pop and multipop operations is also at most k (the number of elements we can pop is at most the number of elements we pushed). Since k is at most n, then T(n)=O(n). Thus the amortized cost for all operations is O(1).

The accounting method In this method, we receive “money” for each operation. –With it we pay for the actual cost of the operation (we denote it by c i ). –With what’s left we may pay for other operations. –The total cost for n operations is the total “money” we got, since with it we covered the entire actual cost. –The amortized cost of each operation is the money we got for this operation. In our stack example, we define the following: –The amortized cost (the “money” we get) for a push operation is 2. The amortized cost of the pop and multipop operation is 0. –How do we pay for all operations? For a push, the new element receives 2 dollars, pay 1 for the cost of the push and has 1 dollar left to his credit. With this it we pays for its pop or multipop.

The accounting method (continued) So we see that the average cost of each operation is constant (in contrast to the actual cost). In other words, since the total payment is at most 2n, the total time is at most O(n), and the average time is per operation is O(1). OperationActual costAverage (amortized) cost push pop multipop 1 1 k 2 0 0

The potential method Notation: D 0 denotes the initial d.s., and D i is the d.s. after i operation. In this method, we define a potential function. The amortized cost of the i’th operation is: and so the total amortized cost is: If we have then the actual cost is less than the amortized cost, so we can just look at the amortized cost. For example, let’s look again at our multipop stack...

The potential method (continued) Define the potential function to have a value equal to the number of elements in D i. Sine we start with an empty stack, for all i. So what is the amortized cost of each operation: –For a push, the actual cost is 1, and the potential change is +1, therefore the amortized cost is 2. –For a pop and multipop, suppose we popped k elements. Then the actual cost is k, and the potential change is -k, therefore the amortized cost is 0. Therefore the average cost of each operation is O(1).

Dynamic Tables Dynamic table is an array that expands and shrinks dynamically when it becomes over/under-loaded, to fit itself to a variable demand. What is it good for? –Hash tables. –Heaps. –Java’s Vector (if no free cells are left in the middle). In a dynamic table, besides the regular insert and delete, there are also: – Expansion: when the table is overloaded and we want to insert a new element. –Contraction: when a delete causes the table to become under- loaded. (the exact meaning of over/under-loaded will be discussed in the next slides).

Expansion Our goal: to find the amortized cost of the following expansion operation: –Start with a size of 1. –Upon an insert, if the table is full, create a table twice the old size and copy the old table to beginning of the new one. Actual cost: 1 for regular insertion, size(T) for expansion. First intuition to amortized cost, the accounting method: –Suppose we pay 3 for every regular insertion: 1 for the actual cost, and 2 remains as credit for this element. –How do we pay for an expansion? Suppose the table is doubled from size x to 2x. Then we have x/2 elements that didn’t pay for an expansion yet (have credit of 2). Thus each one of them pays for itself and for one other element out of the x/2 with credit 0.

Expansion - the potential method Consider first only insertions and expansions. Denote by num(T) the number of elements in T, and by size(T) the total size of T. Define the potential function: Since the table is always at least half full, the potential is not less than zero. Thus the total actual cost is at most the total amortized cost. What is the amortized cost of the i’th insertion? –Insertion to a non-full table: The size didn’t change, so the potential difference is 2[(num(T) + 1) - num(T)] = 2. The actual cost is one so the amortized cost is 1+2=3. –Insertion + expansion: Now size(T) = num(T), and the potential difference is: [2(num(T)+1)-2num(T)] - [2num(T)-num(T)] = 2-num(T) The actual cost is 1+num(T), so the amortized cost is again 3. Interestingly, right after an expansion, the potential is zero, and it increases to exactly num(T) before the next expansion, as to “pay for it”.

Contraction Let be the load factor of the table. When it becomes too low, we want to decrease the table size, to save space. First try: if there are only insertions we are guaranteed that. So, if the load factor decreases below 1/2, decrease the table size by half. Problem: the amortized cost may be high. For example, suppose a full table with size x, and after one insertion the table expands. Now, after two deletes the table contracts, then after two insertions the table expands again, and so. If we do this many times the average cost per operation will get close to x. Conclusion: We must ensure that before a contraction, we perform enough operations to pay for it.

Contraction - alternative method A Solution: Perform a contraction when the load factor drops below 1/4 (I.e. after a delete, num(T) < size(T)/4). Let us analyze the amortized cost, by using the potential: Since the potential function never decreases below 0 here as well, the amortized cost is no less than the actual cost. So all we have to do is to calculate the amortized cost. For the analysis of the amortized cost of the i’th operation, we denote by: the relevant values after the i’th operation.

Contraction - the amortized cost of insertion If then the potential function before and after the insertion is the same as we’ve done before, so the amortized cost is 3. If then both potential functions are size(t)/2-num(t). The table size has not changed, thus the potential difference is -1, and the amortized cost is 0. So the amortized cost of insertion is at most 3.

Contraction - the amortized cost of deletion If and there was no contraction, then the size has not changed, so the potential difference is 1, the actual cost is 1, thus the amortized cost is 2. If and there was a contraction, then before the delete, num(T)=size(T)/4 so the potential difference is: [(size(T)/2)/2 - (num(T)-1)] - [size(T)/2 - num(T)] = 1-size(T)/4 = 1-num(T). The actual cost is (num(T)-1) + 1, so the amortized cost is 1. If then the potential function is in the other form before and after the delete. The size didn’t change so the potential difference is -2. The actual cost is 1 so the amortized cost is -1. If then after the delete and so the potential function changed its form. The size didn’t change, and before the delete, num(T) = size(T)/2 so the potential change is [size(T)/2-(num(T)-1)] - [2num(T) - size(T)] = 1, the actual cost is 1 so the amortized cost is 2.

Graph representations Adjacency list: each node holds a list of all its neighbors: Advantage: –Dynamic structure - easy addition/deletion of nodes. Also enables various connections among objects (nodes can point on edges instead of neighbor nodes, etc.) Disadvantage: –If a node has many neighbors (when the graph is dense), it takes a lot of time to check if two nodes are neighbors. nodes......

Graph representations (2) Adjacency matrix: If we have N nodes, we use an NxN matrix, where the cell (i,j) is 1 if i and j are neighbors, 0 if not: If the graph is weighted we can put real numbers in the cells. Advantage: Takes O(1) times to find out if i and j are neighbors. Disadvantages: –Requires more memory –Cannot add/delete nodes to the graph (this can be fixed by using a hash function to determine the index of the node)

Graph representations (3) In summary, the considerations for choosing a graph d.s. are: –The needs of the algorithm we intend to use. –Space vs. Time (as usual...) –The need to have a dynamic structure.

Sparse matrices The choice between matrix and list representation is related to sparse matrices: –when we represent a large matrix, if most of its values are 0, we can represent it by linked lists, similarly to the way described below. –Each row and each column are represented by a linked list. We keep only the non zero entries. Each node in the linked list contains a value and an index, thus representing a non-zero entry. Besides the space reduction, a linked list representation also improves the performance of the matrix operations (add, multiply): We don’t have to go over all the 0 cells, just over the full ones. For example, how do we multiply matrices when using a linked list representation? The disadvantage, as for graphs, is that we can’t determine the value of a specific entry in O(1).