Amortized Analysis problems [00T7]
- November 18, 2026
Amortization practice [00T8]
- November 18, 2026
These questions are courtesy of Jeff Erickson.
Suppose we are maintaining a data structure under a series of n operations. Let f(k) denote the actual running time of the kth operation. For each of the following functions f, determine the resulting amortized cost of a single operation. For amortized costs other than \Theta (1), be sure to argue why your cost is also a lower bound, i.e. why it is not possible to do any better. Hints:
- Start by either making tables of values and looking for patterns, and/or trying to write down a mathematical expression for the total cost f(1) + f(2) + \dots + f(n).
- If you notice a pattern, you could try proving it using the accounting method.
- Alternatively, if you are able to write down a mathematical expression for an upper bound on the total cost and simplify it, divide by n to get the amortized cost of a single operation.
[A1] Ruler function (1 pt)
- November 18, 2026
f(k) is the largest integer i such that k is evenly divisible by 2^i.
[A2] Powers of two (1 pt)
- November 18, 2026
f(k) = k if k is a power of 2, and f(k) = 1 otherwise.
[A3] Fibonacci numbers (1 pt)
- November 18, 2026
f(k) = k if k is a Fibonacci number, and f(k) = 1 otherwise.
[A4] Squares (1 pt)
- November 18, 2026
f(k) = k if k is a perfect square, and f(k) = 1 otherwise.
[A5] Doubly-extensible array (2 pts) [00T9]
- November 18, 2026
This question is also courtesy of Jeff Erickson.
A doubly extensible array is a data structure that stores a sequence of items and supports the following operations:
- \mathsf {AddToFront}(x) adds x to the beginning of the sequence.
- \mathsf {AddToEnd}(x) adds x to the end of the sequence.
- \mathsf {Lookup}(k) returns the kth item in the sequence (0-indexed), or \mathsf {Null} if the current length of the sequence is less than or equal to k.
Describe and analyze a simple data structure that implements a doubly extensible array. Your \mathsf {AddToFront} and \mathsf {AddToBack} algorithms should take O(1) amortized time, and your \mathsf {Lookup} algorithm should take O(1) worst-case time. The data structure should use O(n) space, where n is the current length of the sequence.
Hint.
- November 18, 2026
Use two arrays.
[A6] Queue from Two Stacks (2 pts) [00TA]
- November 19, 2026
Describe how to implement a queue using two stacks and O(1) additional memory, so that the amortized time for any enqueue or dequeue operation is O(1). The only access you have to the stacks is through the standard methods \mathsf {Push}, \mathsf {Pop}, and \mathsf {IsEmpty}; in particular you should not assume anything about how the stacks are implemented. You may assume that \mathsf {Push}, \mathsf {Pop}, and \mathsf {IsEmpty} take O(1) time in the worst case.
[A7] Fits (3 pts) [00TB]
- November 19, 2026
Suppose that instead of powers of two, we represent integers as a sum of distinct Fibonacci numbers. In other words, instead of an array of bits, we keep an array of fits (fibonacci digits), where each fit is either 0 or 1, and the i^{th} least significant fit indicates whether the sum includes the i^{th} Fibonacci number F_{i}. For example, the fit-string 101110_{F} represents the number F_6+F_4+F_3+F_2=8+3+2+1=14. Design and analyze an algorithm to increment a fit-string in \Theta (1) amortized time, assuming that a fit-string counter starts at 0 and is repeatedly incremented. As with the example above, assume that F_{1}=F_{2}=1, that the array of fits is indexed starting from 1, and that the i^{th} index of the array stores the i^{th} fit. Also observe that unlike binary, a given number n does not necessarily have a unique representation as a fit-string; it does not matter which representations are generated by your algorithm.