Amortized analysis. Amortized analysis

Similar documents
Data Structures and Algorithm. Xiaoqing Zheng

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

Lecture 7: Amortized analysis

Amortized Analysis (chap. 17)

Lecture Notes for Chapter 17: Amortized Analysis

Graduate Analysis of Algorithms Dr. Haim Levkowitz

Today: Amortized Analysis

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

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

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

Tutorial 4. Dynamic Set: Amortized Analysis

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

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

Solution suggestions for examination of Logic, Algorithms and Data Structures,

Searching. Constant time access. Hash function. Use an array? Better hash function? Hash function 4/18/2013. Chapter 9

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

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

Hash Tables. Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing. CS 5633 Analysis of Algorithms Chapter 11: Slide 1

Splay Trees. CMSC 420: Lecture 8

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Amortized Complexity Main Idea

CS 240 Data Structures and Data Management. Module 4: Dictionaries

CS361 Homework #3 Solutions

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

Sorting algorithms. Sorting algorithms

Hash tables. Hash tables

Hash tables. Hash tables

Hash tables. Hash tables

Fundamental Algorithms

DATA STRUCTURES I, II, III, AND IV

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

7 Dictionary. Ernst Mayr, Harald Räcke 126

Hashing. Algorithm : Design & Analysis [09]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Heaps and Priority Queues

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Disjoint Sets : ADT. Disjoint-Set : Find-Set

CS6901: review of Theory of Computation and Algorithms

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

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

Week 5: Quicksort, Lower bound, Greedy

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

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

Assignment 5: Solutions

Math 391: Midterm 1.0 Spring 2016

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Central Algorithmic Techniques. Iterative Algorithms

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

Advanced Data Structures

Hashing. Data organization in main memory or disk

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

Data Structures and Algorithm. Xiaoqing Zheng

Collision. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Part IA Algorithms Notes

CS Data Structures and Algorithm Analysis

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Weight-balanced Binary Search Trees

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

6.1 Occupancy Problem

INF2220: algorithms and data structures Series 1

CPSC 320 Sample Final Examination December 2013

Data Structures 1 NTIN066

Binary Search Trees. Motivation

Weight-balanced Binary Search Trees

Lecture VI AMORTIZATION

Lecture 5: Loop Invariants and Insertion-sort

CHAPTER 17 Amortized Analysis

6.854 Advanced Algorithms

Data Structures and Algorithms Winter Semester

Algorithms and Data Structures

16. Binary Search Trees

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

16. Binary Search Trees

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

Priority queues implemented via heaps

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Lecture 6 September 21, 2016

Problem Set 4 Solutions

Dictionary: an abstract data type

Advanced Data Structures

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

Ch 01. Analysis of Algorithms

Amortized Complexity Verified

Hash Tables. Given a set of possible keys U, such that U = u and a table of m entries, a Hash function h is a

Queues. Principles of Computer Science II. Basic features of Queues

CS3719 Theory of Computation and Algorithms

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

Data Structures and Algorithms " Search Trees!!

Introduction to Hashtables

Dictionary: an abstract data type

Ch01. Analysis of Algorithms

Review Of Topics. Review: Induction

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

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

1 Maintaining a Dictionary

Midterm 1 for CS 170

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

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

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

Transcription:

In amortized analysis the goal is to bound the worst case time of a sequence of operations on a data-structure. If n operations take T (n) time (worst case), the amortized cost of an operation is T (n)/n. In the aggregate analysis method, we directly bound T (n).

Stack with Multipop operation A stack with Multipop operation supports the standard stack operations (Push, Pop, IsEmpty) and the a Multipop operation: Multipop(S, k) (1) while not IsEmpty(S) and k > 0 (2) Pop(S) (3) k k 1 Given n operations (starting with an empty stack): There are at most n push operations. Each element is popped from the stack at most once for each time we pushed it into the stack Therefore, there are at most n pop operations, including those performed during Multipop. Total cost is Θ(n). Amortized cost is Θ(n)/n = Θ(1).

Binary counter Suppose we store a counter in an array A[0..k 1] of bits, where A[0] is the least significant bit. We implement operation Increment(A) which increases the value of the counter by 1, as follows. Increment(A) (1) i 0 (2) while i < A.length and A[i] = 1 (3) A[i] 0 (4) i i + 1 (5) if i < A.length (6) A[i] 1 To bound the time complexity of n increment operations (starting with A containing only zeros), it suffices to bound the number of bit flips in these operations.

Binary counter 0 1 2 3 4 5 6 7 8 A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 The total number of flips is k 1 n/2 i < n/2 i = n i=0 i=0 bit flip freq. number of flips 0 1 n 1 1/2 n/2 2 1/4 n/4 3 1/8 n/8. i 1/2 i n/2 i. 1/2 i = 2n. Total cost of n increments is Θ(n). Amortized cost Θ(1). i=0

Accounting method Each operation has a cost which is its actual time complexity. When an operation is performed, we charge a certain amount, which may be different than the cost. Part of a charge can be stored in objects of the data-structures. To pay the cost of an operation, we can use both the charge or stored credit. The total cost of the operation is at most the sum of the charges.

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge Push 1 2 Pop 1 0 Multipop s 0 s is number of elements popped For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. push(s,4) $2 The amortized time complexity of an operation is Θ(1).

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge Push 1 2 push(s,4) Pop 1 0 Multipop s 0 s is number of elements popped 4 $1 For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. The amortized time complexity of an operation is Θ(1).

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge push(s,5) Push 1 2 Pop 1 0 Multipop s 0 5 $1 s is number of elements popped 4 $1 For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. The amortized time complexity of an operation is Θ(1).

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge Push 1 2 Pop 1 0 Multipop s 0 s is number of elements popped For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. push(s,2) 2 $1 5 $1 4 $1 The amortized time complexity of an operation is Θ(1).

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge Push 1 2 Pop 1 0 Multipop s 0 s is number of elements popped For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. multipop(s,2) 2 $1 5 $1 4 $1 The amortized time complexity of an operation is Θ(1).

Stack with Multipop operation For stack with Multipop, the costs and charges of the operations are as follows: operation cost charge Push 1 2 multipop(s,2) Pop 1 0 Multipop s 0 s is number of elements popped 4 $1 For a push operation, we use $1 to pay for the cost, and the remaining $1 is stored in the pushed element. The cost of Pop/Multipop is payed using the dollars stored at the popped elements. The total cost of n operations is at most 2n. The amortized time complexity of an operation is Θ(1).

Binary counter The cost of an Increment(A) operation is the number of bit flips. The charge of an Increment(A) operation is $2. We keep the following invariant: each 1 in A stores $1. To pay the cost of Increment(A), we use the dollars stored at the 1 s that were changed to 0 s, and $1 from the charge (to pay for the 0 that was changed to 1). The remaining $1 of the charge is stored at the 0 that was changed to 1. A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 0 0

Binary counter The cost of an Increment(A) operation is the number of bit flips. The charge of an Increment(A) operation is $2. We keep the following invariant: each 1 in A stores $1. To pay the cost of Increment(A), we use the dollars stored at the 1 s that were changed to 0 s, and $1 from the charge (to pay for the 0 that was changed to 1). The remaining $1 of the charge is stored at the 0 that was changed to 1. A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 0 1 $1

Binary counter The cost of an Increment(A) operation is the number of bit flips. The charge of an Increment(A) operation is $2. We keep the following invariant: each 1 in A stores $1. To pay the cost of Increment(A), we use the dollars stored at the 1 s that were changed to 0 s, and $1 from the charge (to pay for the 0 that was changed to 1). The remaining $1 of the charge is stored at the 0 that was changed to 1. A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 1 0 $1

Binary counter The cost of an Increment(A) operation is the number of bit flips. The charge of an Increment(A) operation is $2. We keep the following invariant: each 1 in A stores $1. To pay the cost of Increment(A), we use the dollars stored at the 1 s that were changed to 0 s, and $1 from the charge (to pay for the 0 that was changed to 1). The remaining $1 of the charge is stored at the 0 that was changed to 1. A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 0 1 1 $1$1

Binary counter The cost of an Increment(A) operation is the number of bit flips. The charge of an Increment(A) operation is $2. We keep the following invariant: each 1 in A stores $1. To pay the cost of Increment(A), we use the dollars stored at the 1 s that were changed to 0 s, and $1 from the charge (to pay for the 0 that was changed to 1). The remaining $1 of the charge is stored at the 0 that was changed to 1. A[5] A[4] A[3] A[2] A[1] A[0] 0 0 0 1 0 0 $1

Hash table Suppose that we use a hash table with chaining. Let α = n/m, where n is the number of elements in the table and m is the size of the table. We want to maintain the invariant 1/4 α 1: α 1 gives Θ(1) expected search time. α 1/4 gives Θ(n) space. Assume we only have insertions (no deletions). We can maintain the invariant by performing rehashing when the m + 1-th element is inserted to the table: We create a new table of size 2m and move all elements to the new table.

Hash table The cost of Insert is 1 for inserting the element to the table, and n for performing rehashing. The charge of Insert is $3. When we insert an element to the table, we use $1 from the charge to pay for the cost, and we store the remaining $2 in the table. When rehashing is performed, the cost is payed using dollars stored in the table. Immediately after rehashing, no dollars are stored in the table. The next rehashing occurs after m/2 insertions, so the table stores m dollars at the time of the next rehashing.

Example T $0 6

Example T $2 3 6 insert(t,3)

Example T $0 6 3 insert(t,4)

Example T $2 6 insert(t,4) 4 3

Example T $4 insert(t,2) 6 2 4 3

Example T $0 insert(t,7) 3 4 6 2

Example T $2 insert(t,7) 3 4 6 7 2

BB[α] tree definition Let size(v) be the number of vertices in the subtree of v. A vertex v is of bounded balance α if size(v.left) α size(v) and size(v.right) α size(v) A BB[α] tree (α < 0.5) is a binary search tree such that every vertex v is of bounded balance α. The height of a BB[α] tree with n vertices is at most log 1/(1 α) n. 9 12 5 3 8 16 α = 1/3 3 1 1 1 4 10 14 20 1 1 2 6

Insertion To insert an element to a BB[α] tree, insert it as a new leaf. Let w be the highest vertex that is not of bounded balance α. If w exists, replace the subtree of w by a balanced tree containing the same elements. The time complexity is O(depth(w) + size(w)), which is Θ(n) in the worst case. 9 12 5 3 8 16 α = 1/3 3 1 1 1 4 10 14 20 1 1 2 6

Insertion To insert an element to a BB[α] tree, insert it as a new leaf. Let w be the highest vertex that is not of bounded balance α. If w exists, replace the subtree of w by a balanced tree containing the same elements. The time complexity is O(depth(w) + size(w)), which is Θ(n) in the worst case. 10 12 6 3 w 8 16 α = 1/3 4 1 1 1 4 10 14 20 2 1 2 6 3

Insertion To insert an element to a BB[α] tree, insert it as a new leaf. Let w be the highest vertex that is not of bounded balance α. If w exists, replace the subtree of w by a balanced tree containing the same elements. The time complexity is O(depth(w) + size(w)), which is Θ(n) in the worst case. 10 12 6 3 6 16 α = 1/3 3 2 1 1 3 8 14 20 1 1 1 2 4 10

Amortized complexity The cost of insert is depth(w) + size(w), where w is the vertex whose subtree was replaced (or the new leaf). The charge of insert is ( 1 + 1 ) log 1 2α 1/(1 α) n + 2 1 2α. We use log 1/(1 α) n dollars from the charge to pay for the depth(w) cost. In each node on the path from the root to the new leaf, we put 1/(1 2α) dollars from the charge. This requires 1 log 1 2α 1/(1 α) n dollars. We use the dollars stored in w and 2/(1 2α) dollars from the charge to pay for the size(w) cost. We need to show that w contains at least size(w) 2 1 2α dollars.

Example After the insertion, we add $3 to the nodes on the path from the root to the new leaf. $6 12 $6 w 8 16 α = 1/3 4 10 14 20 2 6

Example After the insertion, we add $3 to the nodes on the path from the root to the new leaf. $9 12 $9 w 8 16 $3 4 10 14 20 $3 2 6 α = 1/3 3 $3

Example To pay for the balancing of w, we use the dollars stored in w. $9 12 w 6 16 α = 1/3 3 8 14 20 2 4 10

Analysis Consider the tree before the balancing of w. Let M = size(w). Without loss of generality, size(w.left) size(w.right). Since w is not of bounded balance α size(w.right) = αm 1 size(w.left) = M size(w.right) 1 = M αm Consider the insert operations of elements to the subtree of w since the last balancing operation involving w. The least number of such insertions is when all elements were inserted to w.left. In that case, after last balancing operation involving w, size(w.right) = αm 1 and size(w.left) αm. Therefore, the number of insert operations is at least (M αm ) αm M(1 2α) 2.

Analysis Each insert operation of an element to the subtree of w adds 1/(1 2α) dollars to w. Therefore, w contains at least dollars. 1 2 (M(1 2α) 2) = M 1 2α 1 2α