0% found this document useful (0 votes)
137 views

Amortized Analysis: - C.ramalingareddy

- Dynamic hash tables start with an initial size and double in size when they overflow to accommodate more items. - A naive worst-case analysis of n insertions into a dynamic table is Θ(n^2) but the actual worst-case is only Θ(n). - Amortized analysis guarantees the average cost of each operation is small even if some operations are expensive, by spreading costs across multiple operations. Common methods are aggregate, accounting, and potential.

Uploaded by

Shyam Sunder
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Amortized Analysis: - C.ramalingareddy

- Dynamic hash tables start with an initial size and double in size when they overflow to accommodate more items. - A naive worst-case analysis of n insertions into a dynamic table is Θ(n^2) but the actual worst-case is only Θ(n). - Amortized analysis guarantees the average cost of each operation is small even if some operations are expensive, by spreading costs across multiple operations. Common methods are aggregate, accounting, and potential.

Uploaded by

Shyam Sunder
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Amortized Analysis

-c.ramalingareddy

Agenda

Dynamic tables.
Aggregate method. Accounting method. Potential method.

How large should a hash table be?


Goal: Make the table as small as possible, but large enough so that it wont overflow (or otherwise become inefficient). Problem: What if we dont know the proper size in advance?

Solution: Dynamic tables.


IDEA: Whenever the table overflows, grow it by allocating (via malloc or new) a new, larger table. Move all items from the old table into the new one, and free the storage for the old table.

Example of a dynamic table


1. INSERT
2. INSERT

Example of a dynamic table


1. INSERT
2. INSERT 3. INSERT 4. INSERT 5. INSERT 6. INSERT

7. INSERT

Worst-case analysis
Consider a sequence of n insertions. The

worst-case time to execute one insertion is


(n). Therefore, the worst-case time for n insertions is n (n) = (n2). WRONG! In fact, the worst-case cost for n insertions is only (n) (n2).

Lets see why.

Tighter Analysis
Let ci = the cost of the i th insertion

if i 1 is an exact power of 2,
1 otherwise.

Tighter Analysis
Let ci = the cost of the i th insertion

if i 1 is an exact power of 2,
1 otherwise.

Tighter Analysis (continued)


Cost of n insertions

Thus, the average cost of each dynamictable operation is (n)/n = (1).

Amortized analysis
An amortized analysis is any strategy for analyzing a sequence of operations to show that the average cost per operation is small, even though a single operation within the sequence might be expensive.

Even though were taking averages, however, probability is not involved!


An amortized analysis guarantees the average performance of each operation in the worst case.

Types of Amortized analysis


Three common amortization arguments: the aggregate method, the accounting method, the potential method. Weve just seen an aggregate analysis. The aggregate method, though simple, lacks the precision of the other two methods. In particular, the accounting and potential methods allow a specific amortized cost to be allocated to each operation.

Accounting Method
Charge i th operation a fictitious amortized cost

, where $1 pays for 1 unit of work (i.e., time). This fee is consumed to perform the operation. Any amount not immediately consumed is stored in the bank for use by subsequent operations. The bank balance must not go negative! We must ensure that for all n. Thus, the total amortized costs provide an upper bound on the total true costs. As a comparison, in aggregate analysis, all operations have same amortized costs.

Accounting analysis of Dynamic Tables


Charge an amortized cost of = $3 for the i th

insertion.
$1 pays for the immediate insertion. $2 is stored for later table doubling. Example: When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item.

Accounting analysis of Dynamic Tables


Charge an amortized cost of = $3 for the i th

insertion.
$1 pays for the immediate insertion. $2 is stored for later table doubling. Example: When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item.

Accounting analysis of Dynamic Tables


Charge an amortized cost of = $3 for the i th

insertion.
$1 pays for the immediate insertion. $2 is stored for later table doubling. Example: When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item.

Accounting Method (continued)


Key invariant: Bank balance never drops below 0.

Thus, the sum of the amortized costs provides an


upper bound on the sum of the true costs.

Potential method
IDEA: View the bank account as the potential energy ( la physics) of the dynamic set. Framework: Start with an initial data structure Operation i transforms to . The cost of operation i is Define a potential function :{ } R, such that ( ) = 0 and ( ) 0 for all i. The amortized cost with respect to is defined to be

Potential method
Same as accounting method: something prepaid
is used later.

Different from accounting method


The prepaid work not as credit, but as potential energy, or potential. The potential is associated with the data structure as a whole rather than with specific objects within the data structure.

Understanding potentials

The Amortized costs bound the true costs


The total amortized cost of n operations is

Summing both sides.

The Amortized costs bound the true costs


The total amortized cost of n operations is

Potential analysis of table doubling


Define the potential of the table after the i th insertion by ) Note: (Assume that

Example:

Calculation of Amortized cost


The amortized cost of the i th insertion is

Calculation
Case 1: is an exact power of 2.

Case 2:

is not an exact power of 2.

Therefore, n insertions cost (n) in the worst case.

Conclusions
Amortized costs can provide a clean abstraction of data-structure performance. Any of the analysis methods can be used when an amortized analysis is called for, but each method has some situations where it is arguably the simplest. Different schemes may work for assigning amortized costs in the accounting method, or

potentials in the potential method, sometimes


yielding radically different bounds.

You might also like