Amortized Analysis
This note is part of my learning notes on Data Structures & Algorithms.
Amortized Analysis
Amortized analysis is a method used in computer science to analyze the average time complexity of an algorithm over a sequence of operations, rather than evaluating the time complexity of a single operation. In the case where an algorithm has an occasional operation that is very slow, but most of the other operations are faster, this approach helps to spread the cost of expensive operations over multiple cheap operations.
Common Techniques Used In Amortized Analysis
- Aggregate Analysis
- Accounting / Banker’s / Charge Mehod
- Potential Method
Aggregate Analysis
Aggregate analysis is a technique that involves calculating the total cost of a sequence of operations and then dividing this total by the number of operations to obtain the average cost per operation. This method works well when the total cost is easy to compute and when operations do not vary drastically in cost.
Steps
- Determine the sequence of operations that we need to analyze.
- Determine an upper bound on the total cost of a sequence of operations.
- Divide the total cost by the number of operations to get the average cost per operation .
Accounting / Banker’s / Charge Method
The accounting method is an amortized analysis technique where we assign a “charge” to each operation, and this charge might be more than the actual cost of the operation. The excess charge is stored as “credit” and can be used to pay for future operations that are more expensive. This method ensures that the total assigned charges cover the actual total cost of all operations.
Steps
- Determine a suitable charge for each operation.
- Track the credits accumulated and used for each operation.
- Make sure that the total charge is always greater than or equal to the actual cost, ensuring credits cover future expensive operations.
Potential Method
The potential method is a technique in amortized analysis that uses a potential function to measure the “stored energy” or potential of a data structure. The potential function helps account for the cost of operations by considering both the actual cost and the change in potential.
Steps
- Choose an appropriate potential function that reflects the state of the data structure.
- Calculate the change in potential function that changes with each operation.
- Compute the amortized cost
Example 1: Multi-pop Operations In A Stack
Using aggregate analysis
- There is a sequence of
PUSH
with cost ,POP
with cost andMULTIPOP
with cost operations on an initially empty stack. - The worst-case cost of
MULTIPOP
operation in the sequence is . Therefore, the worst-case time of any sequence of stack operations is . - The average cost of an operation is then .
Example 2: Incrementing A Binary Counter
Using aggregate analysis
- There is a sequence of
INCREMENT
operations with cost that depends on the number of bits flipped, to add 1 to the value of binary counter. - A single execution of
INCREMENT
takes in the worst case, in which array contains all s. Thus, a sequence ofINCREMENT
operations on an initially zero counter takes time in the worst case. - Tighten the bounds: For , bit does not exist. The total number of flips in the sequence is thus
- The worst case time for a sequence of
INCREMENT
operations on an initially zero counter is therefore . - The average or amortized cost of each operation is .
Using potential method
- There is a sequence of
INCREMENT
operations with cost that depends on the number of bits flipped, to add 1 to the value of binary counter. - Let be the number of 1s in the binary counter.
- Change in potential .
- actual cost: (flipping k bits)
- potential change: each 1 flipped to 0 decreases potential by 1, and the last 0 flipped to 1 increases potential by 1.
- Amortized costs: which is .
Example 3: Dynamic Arrays
We have a dynamic array that doubles in size when full. We want to analyze the cost of inserting elements into this array.
Using banker’s method
- There is a sequence of
INSERTION
operations with cost and operations with cost. - Assign 3 units of charge for
INSERTION
(1 for the operation itself, another 2 as credit). - Insert 1 element: Charge 3 units = actual cost 1 unit + credit 2 units.
- Insert another element (causes resizing): Charge 3 units = actual insert cost 1 unit + credit 2 units. Resize cost = 2 units (covered by credits from previous insertion).
- Total charges
INSERTION
:RESIZING
: The credits saved are used for resizing.- The total actual cost:
- Amortized cost = which is .