AMORTIZED
ANALYSIS
Algorithm Design
TA Sessions
By S.Shayan Daneshvar
Amortized Analysis
Amortized analysis is a method for analyzing a given algorithm's complexity, or how
much of a resource, especially time or memory, it takes to execute.The motivation
for amortized analysis is that looking at the worst-case run time per operation,
rather than per algorithm.
Amortization (Amortized Analysis) is a useful tool for understanding the running
times of algorithms that have steps with widely varying performance.
CommonTechniques:
• Aggregate Analysis
• Accounting Method
• Potential Functions
Amortization – Aggregate Analysis
Aggregate analysis determines the upper bound T(n) on the total cost of a sequence
of n operations, then calculates the amortized cost to be T(n) / n.
Example:
A = {0, 0, …, 0} // size of k
Function increment(k):
i = 1
while i <= k and A[i] == 1 :
A[i] = 0
i = i + 1
if i < k:
A[i] = 1
Amortization – Aggregate Analysis
Aggregate analysis determines the upper bound T(n) on the total cost of a sequence
of n operations, then calculates the amortized cost to be T(n) / n.
Example:
A = {0, 0, …, 0} // size of k
Function increment(k):
i = 1
while i <= k and A[i] == 1 :
A[i] = 0
i = i + 1
if i < k:
A[i] = 1
FunctionT(n):
for i <- 1 until n:
increment(k)
Amortization Cost ?
Amortization – Accounting Method
The accounting method is a form of aggregate analysis which assigns to each
operation an amortized cost which may differ from its actual cost. Early operations
have an amortized cost higher than their actual cost, which accumulates a saved
"credit" that pays for later operations having an amortized cost lower than their
actual cost. Because the credit begins at zero, the actual cost of a sequence of
operations equals the amortized cost minus the accumulated credit.
Example?
Amortization – Accounting Method
Example:
A = {0, 0, …, 0} // size of k
Function increment(k):
i = 1
while i <= k and A[i] == 1 :
A[i] = 0
i = i + 1
if i < k:
A[i] = 1
Amortization - Analyzing an Extendable Array (DynamicArray)
Let us provide a means to grow the array A:
Amortization - Analyzing an Extendable Array (Dynamic
Array)
Theorem: Let S be a list implemented by means of an extendable array A, as
described above.The total time to perform a series of n add operations in S, starting
from S being empty and A having size N = 1, is O(n).
Proof?
Amortization - Analyzing an Extendable Array (Dynamic
Array)
Proof?

Amortized analysis

  • 1.
  • 2.
    Amortized Analysis Amortized analysisis a method for analyzing a given algorithm's complexity, or how much of a resource, especially time or memory, it takes to execute.The motivation for amortized analysis is that looking at the worst-case run time per operation, rather than per algorithm. Amortization (Amortized Analysis) is a useful tool for understanding the running times of algorithms that have steps with widely varying performance. CommonTechniques: • Aggregate Analysis • Accounting Method • Potential Functions
  • 3.
    Amortization – AggregateAnalysis Aggregate analysis determines the upper bound T(n) on the total cost of a sequence of n operations, then calculates the amortized cost to be T(n) / n. Example: A = {0, 0, …, 0} // size of k Function increment(k): i = 1 while i <= k and A[i] == 1 : A[i] = 0 i = i + 1 if i < k: A[i] = 1
  • 4.
    Amortization – AggregateAnalysis Aggregate analysis determines the upper bound T(n) on the total cost of a sequence of n operations, then calculates the amortized cost to be T(n) / n. Example: A = {0, 0, …, 0} // size of k Function increment(k): i = 1 while i <= k and A[i] == 1 : A[i] = 0 i = i + 1 if i < k: A[i] = 1 FunctionT(n): for i <- 1 until n: increment(k) Amortization Cost ?
  • 5.
    Amortization – AccountingMethod The accounting method is a form of aggregate analysis which assigns to each operation an amortized cost which may differ from its actual cost. Early operations have an amortized cost higher than their actual cost, which accumulates a saved "credit" that pays for later operations having an amortized cost lower than their actual cost. Because the credit begins at zero, the actual cost of a sequence of operations equals the amortized cost minus the accumulated credit. Example?
  • 6.
    Amortization – AccountingMethod Example: A = {0, 0, …, 0} // size of k Function increment(k): i = 1 while i <= k and A[i] == 1 : A[i] = 0 i = i + 1 if i < k: A[i] = 1
  • 7.
    Amortization - Analyzingan Extendable Array (DynamicArray) Let us provide a means to grow the array A:
  • 8.
    Amortization - Analyzingan Extendable Array (Dynamic Array) Theorem: Let S be a list implemented by means of an extendable array A, as described above.The total time to perform a series of n add operations in S, starting from S being empty and A having size N = 1, is O(n). Proof?
  • 9.
    Amortization - Analyzingan Extendable Array (Dynamic Array) Proof?

Editor's Notes

  • #2 Are your classroom colors different than what you see in this template? That’s OK! Click on Design -> Variants (the down arrow) -> Pick the color scheme that works for you! Feel free to change any “You will…” and “I will…” statements to ensure they align with your classroom procedures and rules!