Amortized analysis is a technique used to analyze the average time complexity of a sequence of operations, where some of the operations may be expensive and others may be inexpensive. In amortized analysis, the cost of a sequence of operations is averaged over all the operations, so the overall cost per operation is more accurate and informative than just analyzing the cost of a single operation.
Amortized analysis is particularly useful when analyzing data structures that support operations with varying costs. For example, a dynamic array supports the operations of adding and deleting elements, which can be expensive if the array needs to be resized. However, the cost of resizing can be amortized over multiple operations to get a more accurate estimate of the overall cost per operation.
There are two main methods for performing amortized analysis: aggregate analysis and accounting method.
In aggregate analysis, the total cost of a sequence of operations is calculated and then divided by the number of operations to obtain an average cost per operation. This approach assumes that each operation has the same cost, which is not always the case.
In the accounting method, each operation is assigned a credit or debit based on its actual cost compared to its amortized cost. The credit or debit is stored in a data structure called a potential function, which represents the difference between the current state of the data structure and the ideal state. The potential function is used to pay for the extra cost of expensive operations, so that the overall cost per operation is averaged out.
For example, consider a dynamic array that supports the operations of adding and deleting elements. When the array is full and a new element is added, the array needs to be resized, which can be an expensive operation. However, the cost of resizing can be amortized over multiple operations by using the accounting method.
Suppose we assign a potential of 2 dollars to each empty slot in the array. When a new element is added, the cost of resizing is charged to the potential, which increases by the cost of the resize operation (say 4 dollars) minus the potential saved by filling in the empty slot (2 dollars). The total cost of the operation is then the actual cost of the resize operation (4 dollars) plus the potential increase (2 dollars), which gives a total cost of 6 dollars. The potential is then updated to reflect the new state of the array.
When an element is deleted, the potential saved by freeing up the slot is added to the potential, which can be used to pay for the cost of future expensive operations. In this way, the cost of resizing is averaged over multiple operations, resulting in a more accurate estimate of the overall cost per operation.
In conclusion, amortized analysis is a powerful technique for analyzing the average time complexity of a sequence of operations, especially for data structures that support operations with varying costs. It allows us to accurately estimate the cost per operation and make informed decisions about the efficiency and scalability of algorithms and data structures.