Skip to content

Commit c01c153

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

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

README.md

+57-3
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,66 @@
725725
<details>
726726
<summary>Amortized Analysis</summary>
727727

728+
- **Amortized cost**:
729+
- Given a sequence of n operations,
730+
- The amortized cost is: **Cost(n operations) / n**
728731
- Methods to calculate amortized cost:
729732
- The **Aggregate method**:
730733
- It calculates amortized cost based on amortized cost definition
731-
- E.g. Dynamic Array:
732-
- The **Banker's Method**:
733-
- The **Physicist's Method**:
734+
- E.g. Dynamic Array: n calls to PushBack
735+
- Let ci = cost of i-th insertion
736+
- _ i - 1 if i - 1 is a power of 2
737+
/
738+
ci = 1 + |
739+
\ _ 0 otherwise
740+
n ⌊log 2 (n−1)⌋
741+
Amortized Cost = ∑ ci / n = (n + ∑ 2^j ) / n
742+
= O(n)/n = O(1)
743+
- The **Banker's Method**: it consists of:
744+
- Charging extra for each cheap operation
745+
- Saving the extra charge as **tokens** in our data structure (conceptually)
746+
- Using the tokens to pay for expensive operations
747+
- It is like an amortizing loan
748+
- E.g. Dynamic Array: n calls to PushBack:
749+
- 1. Charge 3 for each insertion:
750+
- Use 1 token to pay the cost for insertion;
751+
- Place 1 token on the newly-inserted element
752+
- Plase 1 token on the capacity / 2 elements prior
753+
- 2. When Resize is needed:
754+
- Use 1 token To pay for moving the existing elements (all token in the array will dispear)
755+
- When all old array elements are moved to the new array, insert new element (go to 1)
756+
- The **Physicist's Method**: it consists of:
757+
- Defining a **potential function*, **Φ** which maps states of the data structure to integers:
758+
- `Φ(h0 ) = 0`
759+
- `Φ(ht ) ≥ 0`
760+
- Amortized cost for operation t: `ct + Φ(ht) − Φ(ht−1)`
761+
- Choose Φ so that:
762+
- if ct is small, the potential increases
763+
- if ct is large, the potential decreases by the same scale
764+
- The sum of the amortized costs is:
765+
n n
766+
Φ(hn) − Φ(h0) + ∑ ci ≥ ∑ ci
767+
i=0 i=0
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)
772+
- Calculating Amortized cost for operation i (adding element i): `ci + Φ(hi) − Φ(hi−1)`:
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
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
783+
- Related Problems:
784+
- [Dynamic Array with a Popback Operation I](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/24)
785+
- [Dynamic Array with a Popback Operation II](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/25)
786+
- [Dynamic Array with a Popback Operation III](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/26)
787+
- [Dynamic Array with a PopMany Operation](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/27)
734788
- More details:
735789
- [Amortized Analysis](https://youtu.be/U5XKyIVy2Vc)
736790
- [UC San Diego](https://github.com/hamidgasmi/algorithms-datastructures/blob/master/2-data-sructures-fundamentals/2_dynamic_arrays_and_amortized_complexity/02_1_dynamic_arrays_and_amortized_analysis.pdf)

0 commit comments

Comments
 (0)