====== Lab 09 - Amortized Analysis====== **1.** Stacks **2.** Binary Counter **3.** [[https://ocw.cs.pub.ro/ppcarte/doku.php?id=aa:ammortized-analysis|FIFO]] **4.** Heaps **4.1** Aggregate analysis: Let S be a sequence of n insertions into an initially empty heap. At the end of the sequence, we will have an almost-complete binary tree. To make the analysis simpler, suppose it is complete. On the level 1 (base level) of the tree, we have n/2 elements which have been either swapped-down, or remained there upon insertion On level 2, n/4 elements must have climbed-up exactly once, after insertion On the last level, log(n), the root must have climbed-up (via swapping) ceil(log(n)) times. The aggregate climbing-up costs (the down-climbing is not included as no element can go down without one coming-up), per tree level, are given below: level 1 n/2 elems, 0 swaps level 2 n/4 elems, n/4 swaps level 3 n/8 elems, 2*n/8 swaps level log(n) 1 elem log(n) * 1 swaps cost(S) = ins_cost(S) + swap_cost(S) = n + complicated_sum log(n) complicated_sum = sum n/2^i (elems per level) * (i-2) (swaps) i = 1 log(n) <= sum n/2^i * i = n sum ... i = 1 The sum i/2^i can be solved via derivation. Finally, n * sum ... ~= n/2 - 1 - log(n) Thus, cost(S) <= 2n hence is done in linear time. Banking? Potential? 5. ArrayList with removal Further reading: https://ocw.cs.pub.ro/ppcarte/doku.php?id=aa:ammortized-analysis