Skip to content

Commit 8afb1bd

Browse files
author
Hamid Gasmi
committed
Amortized Analysis section is updated
1 parent c01c153 commit 8afb1bd

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -762,24 +762,24 @@
762762
- if ct is small, the potential increases
763763
- if ct is large, the potential decreases by the same scale
764764
- The sum of the amortized costs is:
765-
n n
765+
- n n
766766
Φ(hn) − Φ(h0) + ∑ ci ≥ ∑ ci
767767
i=0 i=0
768768
- 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)
772772
- Calculating Amortized cost for operation i (adding element i): `ci + Φ(hi) − Φ(hi−1)`:
773773
-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
778778
- 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
783783
- Related Problems:
784784
- [Dynamic Array with a Popback Operation I](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/24)
785785
- [Dynamic Array with a Popback Operation II](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/25)

0 commit comments

Comments
 (0)