|
762 | 762 | - if ct is small, the potential increases
|
763 | 763 | - if ct is large, the potential decreases by the same scale
|
764 | 764 | - The sum of the amortized costs is:
|
765 |
| - n n |
| 765 | + - n n |
766 | 766 | Φ(hn) − Φ(h0) + ∑ ci ≥ ∑ ci
|
767 | 767 | i=0 i=0
|
768 | 768 | - E.g. Dynamic Array: n calls to PushBack:
|
769 |
| - - Let Φ(h) = 2 × size − capacity |
770 |
| - - Φ(h0) = 2 × 0 − 0 = 0 |
771 |
| - - Φ(hi) = 2 × size − capacity > 0 (since size > capacity/2) |
| 769 | + - Let `Φ(h) = 2 × size − capacity` |
| 770 | + - `Φ(h0) = 2 × 0 − 0 = 0` |
| 771 | + - `Φ(hi) = 2 × size − capacity > 0` (since size > capacity/2) |
772 | 772 | - Calculating Amortized cost for operation i (adding element i): `ci + Φ(hi) − Φ(hi−1)`:
|
773 | 773 | -Without resize:
|
774 |
| - ci = 1; |
775 |
| - Φ(hi) = 2 * (k + 1) - c |
776 |
| - Φ(hi-1) = 2 * k - 2 - c |
777 |
| - ci + Φ(hi) − Φ(hi−1) = 1 + 2 * k - c - 2 * k + 2 + c = +3 |
| 774 | + ci = 1; |
| 775 | + Φ(hi) = 2 * (k + 1) - c |
| 776 | + Φ(hi-1) = 2 * k - 2 - c |
| 777 | + ci + Φ(hi) − Φ(hi−1) = 1 + 2 * k - c - 2 * k + 2 + c = +3 |
778 | 778 | - With resize:
|
779 |
| - ci = k + 1; |
780 |
| - Φ(hi) = 2 * (k + 1) - 2 * k = 2 since there is a resize, the array capacity is doubled |
781 |
| - Φ(hi-1) = 2 * k - k = k since before the resize, the array capacity is equal to the array size |
782 |
| - ci + Φ(hi) − Φ(hi−1) = k + 1 + 2 - k = +3 |
| 779 | + ci = k + 1; |
| 780 | + Φ(hi) = 2 * (k + 1) - 2 * k = 2 since there is a resize, the array capacity is doubled |
| 781 | + Φ(hi-1) = 2 * k - k = k since before the resize, the array capacity is equal to the array size |
| 782 | + ci + Φ(hi) − Φ(hi−1) = k + 1 + 2 - k = +3 |
783 | 783 | - Related Problems:
|
784 | 784 | - [Dynamic Array with a Popback Operation I](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/24)
|
785 | 785 | - [Dynamic Array with a Popback Operation II](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/25)
|
|
0 commit comments