Amortized Analysis
In your first data structures course, you learned how to analyze data structures using either the worst-case or the average-case per query paradigm. For instance, you may have seen the following results:
Data Structure | search | insertion | deletion |
---|---|---|---|
(Unsorted) Doubly-Linked List | |||
Sorted Array | |||
Hash Table | |||
Balanced Binary Search Tree |
Where the last two rows are examples of data structures that are analyzed using the average-case per query paradigm.
The worst-case analysis for balanced binary search trees is
In this lecture, we will learn how to analyze data structures using the amortized analysis paradigm. In amortized analysis, we analyze the average cost of a sequence of operations, rather than the cost of a single operation. So, while the worst-case per query might be high for one particular query, the amortized cost of a sequence of operations might be low, provided that most queries have low cost.
Remark 1: Amortized analysis is a worst-case analysis, but it is a worst-case analysis of a sequence of operations, rather than a single operation.
Remark 2: data structures with great amortized performance are ideal for internal processes, such as graph algorithms (for instance finding minimum spanning trees). However, these data structures are not so great in the client-server model (such as internet queries), where the user is waiting for a response. In the latter setting, we want to guarantee that the worst-case response time per query is low.
Types of Amortized Analysis
There are three types of amortized analysis:
-
Aggregate Analysis: We analyze the total cost
of a sequence of operations, and then divide by the number of operations. Thus, in this case the amortized cost of a sequence of operations is . -
Accounting Method: We assign a certain charge to each operation (independent of the actual cost of the operation). In the accounting method, we must show that the total charges to a sequence of operations is an upper bound on the total true cost of the sequence of operations. The idea is that if the cost of an operation is cheaper than the charge, then we can use the extra charge to pay for more expensive (later) operations.
-
Potential Method: We come up with a potential function that maps the state of the data structure to a real number. Each operation will then cahnge the state of the data structure, and thus change the potential function. The amortized cost of an operation is then the true cost of the operation plus the change in the potential function.
Example: Binary Counter
We will use the binary counter data structure as an example.
Input: A binary counter
Output: increment this counter up to the integer
The counter
increment(
The question of interest is: how many bit operations does it take to increment the counter from
Note that, per query, the worst-case cost of incrementing the counter from
However, upon closer inspection, we see that the most significant bits of the counter are flipped less frequently than the least significant bits. So, is the above analysis tight?
Aggregate Analysis
With aggregate analysis, we analyze the total cost of incrementing the counter from
Now, let
Since the
Hence,
Since
Accounting Method
In the accounting method, we assign a charge to each operation.
In the accounting method, we will assign a charge
Thus, the total charge is an upper bound on the total true cost of the sequence of operations.
How do we assign charges?
Let us charge the cost of “clearing a bit” (i.e. changing the bit from
Thus, if at the end of the
Note that since we have charged the clearing beforehand, and we have charged
In the end, we have
Potential Method
In the potential method, we define a potential function
Remark 3: a potential function
For the binary counter, we define the potential function
Amortized cost of the
Hence, the total amortized cost is