The Wayback Machine - https://web.archive.org/web/20230312022613/https://www.geeksforgeeks.org/accounting-method-amortized-analysis/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Accounting Method | Amortized Analysis

Improve Article
Save Article
  • Last Updated : 21 Dec, 2022
Improve Article
Save Article

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 potential method:

One way to perform the amortized analysis is to use the “potential method“. In this method, we define a potential function that assigns a potential value to the state of the data structure at each point in time. The potential function represents the “potential” cost of the next operation. The actual cost of an operation is the difference between the potential before and after the operation. The amortized cost of an operation is the sum of the actual cost and the potential before the operation.

Example: Suppose we have a dynamic array with a potential function defined as follows:

  • If the array is at least half empty, the potential is 0.
  • If the array is less than half empty, the potential is the number of empty slots in the array.
  • The potential function reflects the “potential” cost of the next insertion operation. If the array is at least half empty, there is no potential cost, because the next insertion can be performed without the need for a resize operation. If the array is less than half empty, the potential cost is the number of empty slots, because a resize operation will be needed to make room for the insertion.

Insertion 1:
Before: potential = 0
After: potential = 0
Actual cost = 1
Amortized cost = 1 + 0 = 1

Insertion 2:
Before: potential = 0
After: potential = 0
Actual cost = 1
Amortized cost = 1 + 0 = 1

Insertion 3:
Before: potential = 0
After: potential = 1 (array is less than half empty)
Actual cost = 2 (insertion + resize)
Amortized cost = 2 + 0 = 2

Insertion 4:
Before: potential = 1
After: potential = 0 (array is at least half empty)
Actual cost = 1
Amortized cost = 1 + 1 = 2

Insertion 5:
Before: potential = 0
After: potential = 0
Actual cost = 1
Amortized cost = 1 + 0 = 1

…

In this example, the credit is accumulated through a series of cheap insertions and is used to offset the cost of the expensive resize operation. As a result, the average cost per insertion is less than the actual cost of the resize operation.

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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!