Open In App

Accounting Method | Amortized Analysis

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Amortized analysis is a method used to analyze the performance of algorithms that perform a sequence of operations, where each individual operation may be fast, but the sequence of operations may be slow as a whole. It is used to determine the average cost per operation, allowing for a more accurate comparison of algorithms that perform different numbers of operations.

For example, suppose you have an algorithm that performs a series of insertions into a dynamic array. Each insertion is fast, but if the array becomes full, the algorithm must perform a slower operation to resize the array and make room for the new insertion. The amortized cost of each insertion is the average cost per insertion, taking into account the cost of the occasional slower operation to resize the array.

Amortized analysis using the Accounting method:

The accounting method of amortized analysis can be useful for understanding the performance of algorithms that perform a sequence of operations with varying costs. It can be applied to a wide range of data structures and algorithms.

To solve a problem using amortized analysis using the accounting method, you can follow these steps:

  • Identify the sequence of operations that the algorithm will perform, and determine which operations are “cheap” and which are “expensive.” Cheap operations are those that require less time or resources than the average operation, while expensive operations are those that require more time or resources.
  • Define a credit or potential function that will be used to track the credit that has been accumulated by the algorithm. This function should assign a credit value to the state of the data structure at each point in time.
  • Initialize the credit to 0.
  • For each operation in the sequence, do the following:
    • If the operation is cheap, increment the credit by the cost of the operation.
    • If the operation is expensive, subtract the credit from the cost of the operation to determine the actual cost. The actual cost is the difference between the cost of the operation and the credit.
    • If the credit becomes negative, reset it to 0.
  • At the end of the sequence, divide the total cost by the number of operations to determine the average cost per operation.

Here is an example of solving a problem using amortized analysis using the accounting method:

Suppose we have an algorithm that performs a series of insertions into a dynamic array. Each insertion is fast, but if the array becomes full, the algorithm must perform a slower operation to resize the array and make room for the new insertion. We want to use amortized analysis to determine the average cost per insertion.

  • Identify the sequence of operations: Each insertion is a cheap operation, and the resize operation is an expensive operation.
  • Define a credit function: We can define the credit function as follows:
    • If the array is at least half empty, the credit is 0.
    • If the array is less than half empty, the credit is the number of empty slots in the array.
  • Initialize the credit to 0.
  • Perform the operations:

Insertion 1:
Cost = 1
Credit = 1

Insertion 2:
Cost = 1
Credit = 2

Insertion 3:
Array is full, must resize
Cost = 2 (insertion + resize)
Credit = 0

Insertion 4:
Cost = 1
Credit = 2

Insertion 5:
Cost = 1
Credit = 3


 

Calculate the average cost per operation: To calculate the average cost per operation, we sum the total cost and divide by the number of operations. In this example, the total cost is 8 and the number of operations is 5, so the average cost per operation is 8/5 = 1.6.

This average cost per operation reflects the fact that some insertions have a lower cost due to the credit accumulated through previous cheap operations, while other insertions have a higher cost due to the expensive resize operation.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads