Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
aa:ammortized-analysis [2017/01/11 16:14] pdmatei |
aa:ammortized-analysis [2019/11/26 13:25] (current) pdmatei |
||
---|---|---|---|
Line 58: | Line 58: | ||
For the FIFO, we estimate: | For the FIFO, we estimate: | ||
* $math[\hat{c}_{enq} = 3] since each inserted element in $math[l] **may** be subsequently removed from $math[l] and inserted in $math[r]. We charge extra to **amortise** for this potential cost. | * $math[\hat{c}_{enq} = 3] since each inserted element in $math[l] **may** be subsequently removed from $math[l] and inserted in $math[r]. We charge extra to **amortise** for this potential cost. | ||
- | * $mat[\hat{c}_{deq} = 1] which represents the deletion from $math[r]. | + | * $math[\hat{c}_{deq} = 1] which represents the deletion from $math[r]. |
To validate this estimation, we verify the **golden rule**. | To validate this estimation, we verify the **golden rule**. | ||
Line 194: | Line 194: | ||
===== The potential method ===== | ===== The potential method ===== | ||
+ | |||
+ | We fix $math[\Phi(L_0) = 0] and $math[\Phi(L_i) = 2 * elems(L_i) - size(L_i)]. | ||
+ | |||
+ | We observe that $math[elems(L_i) \geq size(L_i)/2] since we can never have fewer elements that half the capacity of the array. Thus, the golden rule of the potential method: | ||
+ | |||
+ | $math[\forall S: \Phi(F_n) - \Phi(F_0) \geq 0] | ||
+ | |||
+ | is immediately verified. | ||
+ | |||
+ | To compute the ammortised cost, we observe two cases: | ||
+ | * the operation does not trigger doubling: $math[size(L_i) = size(L_{i-1})]. Hence $math[\hat{c_i} = 1 + 2*elems(L_i) - size(L_i) -2*elems(L_{i-1}) + size(L_{i-1}) = 1 + 2 = 3] | ||
+ | * the operation triggers doubling: $math[size(L_i) = 2*size(L_{i-1})]. Hence $math[\hat{c_i} = 1 + size(L_{i-1}) + 2*elems(L_i) - size(L_i) -2*elems(L_{i-1}) + size(L_{i-1}) = 1 + 2 = 3] | ||
+ | |||
+ | Incidentally, we have identified precisely the same ammortised cost as in the previous method. | ||