CMSC 341 Amortized Analysis.

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

Chapter 13. Red-Black Trees
Amortized Analysis Some of the slides are from Prof. Leong Hon Wais resources at National University of Singapore Prof. Muhammad Saeed.
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
CPSC 320: Intermediate Algorithm Design & Analysis Splay Trees (for Amortized Analysis) Steve Wolfman 1.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
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.
CS305/503, Spring 2009 Amortized Analysis Michael Barnathan.
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]
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
Splay Trees Splay trees are binary search trees (BSTs) that:
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
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.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
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.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
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.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
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.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Jim Anderson Comp 750, Fall 2009 Splay Trees - 1 Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
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.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
11/20/2016IT 2791 Is this an AVL tree?
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
Lecture 25 Splay Tree Chapter 10 of textbook
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.
CMSC 341 Splay Trees.
CSCE 411 Design and Analysis of Algorithms
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
Lecture 21 Amortized Analysis
CSCE 411 Design and Analysis of Algorithms
Presentation transcript:

CMSC 341 Amortized Analysis

What is amortized analysis? Consider a sequence of operations on a dynamic data structure Insert or delete in any (valid) order Worst case analysis asks: What is the most expensive any single operation can be? Amortized analysis asks: What is an upper bound on the average per-operation cost over the entire sequence? 10/6/2008 UMBC CMSC 341 Amortized Analysis

Nature of the amortized bound Amortized bounds are hard bounds They do not mean “on average (or most of the time) the bound holds” They do mean “for any sequence of n operations, the bound holds over that sequence” 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Three methods Aggregate method T(n) = upper bound on total cost of n operations Amortized cost is T(n)/n Some operations may cost more, a lot more, than T(n)/n If so, some operations must cost less But the average cost over the sequence will never exceed T(n)/n 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Three methods Accounting method Each operation pays a “fee” (cost of operation) Overcharge some operations and store extra as pre-payment for later operations Amortized cost is (total of fees paid)/n Must ensure bank account never negative, otherwise fee was not high enough and bound does not hold Overpayment stored with specific objects in data structure (e.g., nodes in a BST) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Three methods (cont.) Potential method Like accounting method Overpayment stored as “potential energy” of entire data structure (not specific objects) Must ensure that potential energy never falls below zero 10/6/2008 UMBC CMSC 341 Amortized Analysis

Increment a binary counter A(k-1) A(k-2) A(2) A(1) A(0) K-bit value stored in array To increment value, flip bits right-to-left until you turn a 0 into a 1 Each bit flip costs O(1) What is the amortized cost of counting from 0 to n? 10/6/2008 UMBC CMSC 341 Amortized Analysis

Increment a binary counter A(k-1) A(k-2) A(2) A(1) A(0) Worst case Flip k bits per increment Do that n times to count to n O(kn) But, most of the time we don’t flip many bits 10/6/2008 UMBC CMSC 341 Amortized Analysis

Increment a binary counter Cost A(k-1) A(k-2) A(2) A(1) A(0) 1 1 2 1 3 1 2 1 4 Total: 15 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Aggregate method A(0) flips every time, or n times A(1) flips every 2nd time, or n/2 times A(2) flips every 4th time, or n/4 times A(i) flips n/2i times Total cost is Σi=0,k-1n/2i ≤ Σi=0,∞n/2i = 2n = O(n) So amortized cost is O(n)/n = O(1) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method Flipping a bit costs $1 (one unit of computational work) Pay $2 to change a 0 to a 1 Use $1 to pay for flipping the bit to 1 Leave $1 there to pay when/if the bit gets flipped back to 0 Since only one bit gets flipped to 1 per increment, total cost is $2n = O(n) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method A(k-1) A(k-2) A(2) A(1) A(0) $0 Flip A(0) to 1 and pay $1 for flip and leave $1 with that bit (total fee of $2) A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 Flip A(0) to 0 and pay with the $1 that was there already Flip A(1) to 1 and pay $1 for flip and leave $1 with that bit (total fee of $2) A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 Flip A(0) to 1 and pay $1 for flip and leave $1 with that bit (total fee of $2) A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 Flip A(0) and A(1) to 0 and pay with the $$ that were there already Flip A(2) to 1 and pay $1 for flip and leave $1 with that bit (total fee of $2) A(k-1) A(k-2) A(2) A(1) A(0) 1 $0 $1 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Accounting method … and so on We “overpay” by $1 for flipping each 0 to 1 Use the extra $1 to pay for the cost of flipping it back to a zero Because a $2 fee for each increment ensures that we have enough money stored to complete that increment, amortized cost is $2 = O(1) per operation 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Potential method Record overpayments as “potential energy” (or just “potential”) of entire data structure Contrast with accounting method where overpayments stored with specific parts of data structure (e.g., array cells) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Potential method Initial data structure is D0 Perform operations i = 1, 2, 3, …, n The actual cost of operation i is ci The ith operation yields data structure Di Φ(Di) = potential of Di, or stored overpayment Amortized cost of the ith operation is xi = ci + Φ(Di) - Φ(Di-1) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Potential method If Φ(Di) - Φ(Di-1) > 0 then xi is an overcharge to ith operation We paid more than the actual cost of the operation If Φ(Di) - Φ(Di-1) < 0 then xi is an undercharge to the ith operation We paid less than the actual cost of the operation, but covered the difference by spending potential 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Potential method Total amortized cost: Σxi = Σ(ci + Φ(Di) - Φ(Di-1)) = Σci + Φ(Dn) - Φ(D0) Sum of actual costs plus whatever potential we added but didn’t use Require that Φ(Di) ≥ 0 so we always “pay in advance” 10/6/2008 UMBC CMSC 341 Amortized Analysis

Potential method: Binary counter Need to choose potential function Φ(Di) Want to make xi = ci + Φ(Di) - Φ(Di-1) small Usually have to be lucky or clever! Let Φ(Di) = bi, the number of ones in the counter after the ith operation Note that Φ(Di) ≥ 0 so we’re OK Recall $1 stored with each 1 in the array when using the accounting method 10/6/2008 UMBC CMSC 341 Amortized Analysis

Potential method: Binary counter Operation i resets (zeroes) ti bits True cost of operation i is ti + 1 The +1 is for setting a single bit to 1 Number of ones in counter after ith operation is therefore bi = bi-1 – ti + 1 10/6/2008 UMBC CMSC 341 Amortized Analysis

Potential method: Binary counter Number of ones in counter after ith operation is bi = bi-1 – ti + 1 Potential difference is Φ(Di) - Φ(Di-1) = bi – bi-1 = (bi-1–ti+1) – bi-1 = 1 – ti Amortized cost is xi = ci + Φ(Di) - Φ(Di-1) = (ti + 1) + (1 – ti) = 2 If we pay just $2 per operation, we always have enough potential to cover our actual costs per operation 10/6/2008 UMBC CMSC 341 Amortized Analysis

Amortized analysis of splay trees Use the accounting method Store $$ with nodes in tree First, some definitions Let nx be the number of nodes in the subtree rooted by node x Let rx = floor(log(nx)) Called the rank of x 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis What we’ll show If every node x always has rx credits then splay operations require amortized O(lgn) time This is called the “credit invariant” For each operation (find, insert, delete) we’ll have to show that we can maintain the credit invariant and pay for the true cost of the operation with O(lgn) $$ per operation 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis First things first Consider a single splay step Single rotation (no grandparent), zig-zig, or zig-zag Nodes x, y = parent(x), z = parent(y) rx, ry, and rz are ranks before splay step r’x, r’y,and r’z are ranks after splay step 10/6/2008 UMBC CMSC 341 Amortized Analysis

For example (zig-zig case) rz r’x z x ry r’y D y A y r’z rx x C B z A B C D 10/6/2008 UMBC CMSC 341 Amortized Analysis

What does a single splay step cost? To pay for rotations (true cost of step) and maintain credit invariant 3(r’x – rx) + 1 credits suffice for single rotation 3(r’x – rx) credits suffice for zig-zig and zig-zag 10/6/2008 UMBC CMSC 341 Amortized Analysis

What does a sequence of splay steps cost? As node x moves up the tree, sum costs of individual steps r’x for one step becomes rx for next step Summing over all steps to the root telescopes to become 3(rv – rx) + 1 where v is the root node 3(r’x – rx) + 3(r’’x – r’x) + 3(r’’’x – r’’x) … + 1 Note +1 only required (sometimes) for last step 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis The punch line! 3(rv – rx) + 1 = O(logn) v is root node of tree with n nodes rv = floor(logn) We can splay any node to the root in O(logn) time 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Single rotation rx ry r’x r’y y x x C A y A B B C To maintain credit invariant at all nodes need to add $Δ Only rx and ry can change Δ = (r’x – rx) + (r’y – ry) Note that r’x = ry so … Δ = r’y – rx Note that r’x ≥ r’y so … Δ = r’y – rx ≤ r’x - rx 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Single rotation rx ry r’x r’y y x x C A y A B B C To maintain credit invariant at all nodes it suffices to pay $(r’x – rx) Still need to pay O(1) for the rotation We allocated 3(r’x – rx) + 1 credits If r’x > rx we’ve still got 2(r’x – rx) > 1 credits to pay for the rotation The +1 is there in case r’x = rx When can that happen? 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Zig-zig rz r’x z x ry r’y D y A y r’z rx x C B z A B C D To maintain credit invariant at all nodes need to add $Δ Δ = (r’x – rx) + (r’y – ry) + (r’z – rz) Note that r’x = rz so … Δ = r’y + r’z – rx – ry Note that r’x ≥ r’y and r’x ≥ r’z and rx ≤ ry so … Δ = r’y + r’z – rx – ry ≤ r’x + r’x – rx – rx = 2(r’x – rx) 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Zig-zig rz r’x z x ry r’y D y A y r’z rx x C B z A B C D To maintain credit invariant at all nodes it suffices to pay $2(r’x – rx) Still need to pay O(1) for the rotations If r’x > rx we can use r’x – rx ≥ 1 credits to pay for the two rotations for a total of $3(r’x – rx) Otherwise, r’x = rx so r’x = rx = ry = rz Why? In this case, we can show that maintaining the invariant frees one or more credits that can be used to pay for the rotations 10/6/2008 UMBC CMSC 341 Amortized Analysis

UMBC CMSC 341 Amortized Analysis Zig-zag z x A y z y x D A B C D B C Analysis analogous to zig-zig step At most $3(r’x – rx) required to maintain invariant and pay for rotations 10/6/2008 UMBC CMSC 341 Amortized Analysis