Professional Documents
Culture Documents
L07-Amortized Analysis-Printfriendly
L07-Amortized Analysis-Printfriendly
Algorithms
Week 7
Amortized Analysis
CS3230 Diptarka Chakraborty
Why do we need “Amortized Analysis”?
• There is a sequence of n operations 𝑜𝑜1 , 𝑜𝑜2 , … , 𝑜𝑜𝑛𝑛
• Let t(i) be the time complexity of i-th operation 𝑜𝑜𝑖𝑖
• Let T(n) be the time complexity of all n operations
𝑇𝑇 𝑛𝑛 = ∑𝑛𝑛𝑖𝑖=1 𝑡𝑡(𝑖𝑖) f(n) = worst case
time complexity
= 𝑛𝑛 𝑓𝑓(𝑛𝑛)
of any of the n
operations
Could be
grossly wrong!
Binary counter
A motivating example for amortized analysis
k-bit Binary Counter: How do we increment?
Ctr A[4] A[3] A[2] A[1] A[0] Objective: Count total Bit flips (0 1, 1 0)
0 0 0 0 0 0 during n increments
1 0 0 0 0 1
𝑻𝑻 𝒏𝒏 = total no. of bit flips during 𝒏𝒏 increments
2 0 0 0 1 0
3 0 0 0 1 1
Aim: To get a tight bound on 𝑻𝑻(𝒏𝒏)
4 0 0 1 0 0
5 0 0 1 0 1 INCREMENT(A)
6 0 0 1 1 0 1. i ← 0
7 0 0 1 1 1 2. while i < length[A] and A[i] = 1 do
8 0 1 0 0 0 3. A[i] ← 0 ⊳ flip 10
9 0 1 0 0 1 4. i←i+1
10 0 1 0 1 0 5. if i < length[A]
11 0 1 0 1 1 6. then A[i] ← 1 ⊳ flip 01
k-bit Binary Counter: No. of bit flips
Ctr A[4] A[3] A[2] A[1] A[0] Total
Cost Objective: Count total Bit flips (0 1, 1 0)
0 0 0 0 0 0 0
during n increments
1 0 0 0 0 1 1 𝑻𝑻 𝒏𝒏 = total no. of bit flips during 𝒏𝒏 increments
2 0 0 0 1 0 3 Aim: To get a tight bound on 𝑻𝑻(𝒏𝒏)
3 0 0 0 1 1 4
Attempt 1:
4 0 0 1 0 0 7
5 0 0 1 0 1 8
Let 𝒕𝒕 𝒊𝒊 = no. of bit flips during 𝒊𝒊th increment
𝒏𝒏
6 0 0 1 1 0 10
𝑻𝑻 𝒏𝒏 = � 𝒕𝒕(𝒊𝒊)
7 0 0 1 1 1 11
𝒊𝒊=𝟏𝟏
8 0 1 0 0 0 15
9 0 1 0 0 1 16 Is this a tight
10 0 1 0 1 0 18
In the worst case, 𝒕𝒕(𝒊𝒊) = ?𝑘𝑘
bound?
11 0 1 0 1 1 19 𝑻𝑻 𝒏𝒏 = 𝑶𝑶(𝒏𝒏 𝒌𝒌)
k-bit Binary Counter: No. of bit flips
Ctr A[4] A[3] A[2] A[1] A[0] Cost Total Objective: Count total Bit flips (0 1, 1 0) during n
increments
Cost
𝑻𝑻 𝒏𝒏 = total no. of bit flips during 𝒏𝒏 increments
0 0 0 0 0 0 0 0
Aim: To get a tight bound on 𝑻𝑻(𝒏𝒏)
1 0 0 0 0 1 1 1
Attempt 2:
2 0 0 0 1 0 2 3
Let 𝒇𝒇 𝒊𝒊 = no. of times 𝒊𝒊th bit flips
3 0 0 0 1 1 1 4 𝒌𝒌−𝟏𝟏
4 0 0 1 0 0 3 7 𝑻𝑻 𝒏𝒏 = � 𝒇𝒇(𝒊𝒊)
𝒊𝒊=𝟎𝟎
5 0 0 1 0 1 1 8
𝒇𝒇(𝟎𝟎) = 𝒏𝒏?
6 0 0 1 1 0 2 10
𝒇𝒇(𝟏𝟏) = 𝒏𝒏/𝟐𝟐
?
Much better
7 0 0 1 1 1 1 11 𝒇𝒇(𝟐𝟐) = 𝒏𝒏/𝟒𝟒
? than 𝑂𝑂(𝑛𝑛𝑛𝑛)
8 0 1 0 0 0 4 15 𝒇𝒇(𝒊𝒊) = 𝒏𝒏/𝟐𝟐𝒊𝒊 since 𝑘𝑘 ≈ log 𝑛𝑛
9 0 1 0 0 1 1 16
10 0 1 0 1 0 2 18 𝑻𝑻 𝒏𝒏 = 𝒏𝒏 ∑𝒌𝒌−𝟏𝟏
𝒊𝒊=𝟎𝟎 𝟐𝟐 −𝒊𝒊
< 𝟐𝟐𝟐𝟐
11 0 1 0 1 1 1 19
Amortized Analysis
• Amortized analysis is a strategy for analyzing a sequence of
operations to show that the average cost per operation is small, even
though a single operation within the sequence might be expensive.
𝑇𝑇(𝑛𝑛)
• In our last example, average cost per increment = < 2 = 𝑂𝑂(1)
𝑛𝑛
Amortized cost of Θ(1) means that the total cost divided by the number
of operations is Θ(1). Hence the total cost is Θ(n).
Amortized analysis is done in the worst case.
Amortized Analysis
• Amortized analysis is a strategy for analyzing a sequence of
operations to show that the average cost per operation is small, even
though a single operation within the sequence might be expensive.
0 0 1 1 0 1 1 0 0 0 0 0
0 0 1 1 0 1 1 0 0 0 0 0
Can we do 1 0
“free of cost” ?
Accounting analysis of Binary Increment Why?
At some point
• Charge $2 for each 0 1 of time, this
$1 pays for the actual bit setting. bit must have
$1 is stored in the bank. been set
• Observation: At any point, every 1 bit in the counter has $1 on its
bank.
• Use that $1 to pay for resetting 1 0. (reset is “free”)
0 0 0 1$1 0 1$1 0
Example: 0 0 0 1$1 0 1$1 1$1 Amortized Cost = $2
0 0 0 1$1 1$1 0 0 Amortized Cost = $2
Accounting (Banker’s) method
• Charge i th operation a fictitious amortized cost c(i), assuming $1 pays
for 1 unit of work (time) e.g., bit-flips in the previous example.
• This fee is consumed to perform the operation.
• Any amount not immediately consumed is stored in the bank for use
by subsequent operations.
• The idea is to impose an extra charge on inexpensive operations and
use it to pay for expensive operations later on.
• The bank balance must not go negative!
• We must ensure that ∑𝑛𝑛𝑖𝑖=1 𝑡𝑡(𝑖𝑖) ≤ ∑𝑛𝑛𝑖𝑖=1 𝑐𝑐(𝑖𝑖) for all n.
• Thus, the total amortized costs provides an upper bound on the
total true costs.
Accounting analysis of Binary Increment
• Invariant we need to keep: Bank balance
never drops below 0. Thus, the sum of the
amortized costs provides an upper bound on
the sum of the true costs.
Observation: At any point, every 1 bit in the counter has $1
on its bank.
• Conclusion:
• Amortized cost for each increment = 2 = 𝑂𝑂(1)
• Amortized cost for n increments = 2n = 𝑂𝑂(𝑛𝑛)
• Actual cost for n increments = 𝑂𝑂(𝑛𝑛)
Robert Tarjan
Try to view carefully the costly operation and see if there is some
quantity that is “decreasing” during the operation.
Potential method on Binary Increment
• Take a close look on increment operation
Is there 0 0 1 1 0 1 0 1 1 1 1 1 Cost = 6
anything that is Compare no. of 1’s
decreasing? 0 0 1 1 0 1 1 0 0 0 0 0
𝑙𝑙𝑖𝑖
• Actual cost of 𝒊𝒊th increment = 1 + Length of the longest suffix with all 1’s
Actual cost of ith increment ∆𝝓𝝓𝑖𝑖 Amortized cost of ith increment
𝒍𝒍𝒊𝒊 + 𝟏𝟏 − 𝒍𝒍𝒊𝒊 + 𝟏𝟏 𝟐𝟐
𝝓𝝓(𝒊𝒊): No. of 1’s in the counter after 𝒊𝒊th increment
Amortized cost of n increments = 2n = 𝑂𝑂(𝑛𝑛)
Actual cost of n increments = 𝑂𝑂(𝑛𝑛)
Dynamic table
For Insertion only
How large should a table be?
• Goal: Make the table as small as possible, but large enough so that it
won’t overflow (otherwise becomes inefficient).
What happens
if more space is
Free space
needed by T ?
Table T
Memory
32
How large should a table be?
• Goal: Make the table as small as possible, but large enough so that it won’t
overflow (or otherwise become inefficient).
• Problem: What if we don’t know the proper size in advance?
• free(𝑻𝑻): free the space (return the space to OS) occupied by table 𝑻𝑻.
A trivial way to perform Insert(𝒙𝒙)
If (𝒏𝒏 = 𝟎𝟎)
𝑻𝑻 createTable(𝟏𝟏);
Else // Table is full
If(𝒏𝒏 = size(𝑻𝑻))
{ 𝑻𝑻𝑻 createTable( 𝒏𝒏 + 𝟏𝟏);
copy(𝑻𝑻,𝑻𝑻𝑻);
free(𝑻𝑻);
𝑻𝑻 𝑻𝑻𝑻
}
Insert 𝒙𝒙 into 𝑻𝑻;
𝒏𝒏 𝒏𝒏 + 𝟏𝟏;
35
Example of a Dynamic Table
1. INSERT 1
2. INSERT overflow
Example of a Dynamic Table
1. INSERT 1
2. INSERT overflow
Example of a Dynamic Table
1. INSERT 1
2. INSERT 2
Example of a Dynamic Table
1. INSERT 1
2. INSERT 2
3. INSERT overflow
Example of a Dynamic Table
1. INSERT 1
2. INSERT 2
3. INSERT overflow
Example of a Dynamic Table
1. INSERT 1
2. INSERT 2
3. INSERT
Example of a Dynamic Table
1. INSERT 1
2. INSERT 2
3. INSERT 3
A trivial way to perform Insert(𝒙𝒙)
If (𝒏𝒏 = 𝟎𝟎)
𝑻𝑻 createTable(𝟏𝟏);
Else
If(𝒏𝒏 = size(𝑻𝑻)) // Table is full
{ 𝑻𝑻𝑻 createTable( 𝒏𝒏 + 𝟏𝟏);
copy(𝑻𝑻,𝑻𝑻𝑻); Idea: Every time
free(𝑻𝑻); table is full, create
𝑻𝑻 𝑻𝑻𝑻 a new table of
} double the size
Insert 𝒙𝒙 into 𝑻𝑻;
𝒏𝒏 𝒏𝒏 + 𝟏𝟏;
Is this tight?
Amortized Analysis
• Observe, once the table is full, we create a table of double
the size.
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
1 1 1 1 1 1 1 1 1 1 Cost for i th insert
t(i)
1 2 4 8 Cost for copying
due to overflow
Aggregate Method = 2log 𝑛𝑛−1 +1 −1
𝑛𝑛 ≤ 2(𝑛𝑛 − 1)
Cost of n insertions = 𝑖𝑖=1 𝑡𝑡(𝑖𝑖)
∑
(Geometric series)
log(𝑛𝑛−1) 𝑗𝑗
≤n+ ∑𝑗𝑗=0 2
≤ 3𝑛𝑛
• 1) Yes
• 2) No
Potential Method (Recall)
𝝓𝝓: Potential function associated with the algorithm/data-structure
𝝓𝝓(𝒊𝒊): Potential at the end of 𝒊𝒊th operation
Important conditions to be fulfilled by 𝝓𝝓
𝝓𝝓(𝟎𝟎) = 𝟎𝟎
𝝓𝝓 𝒊𝒊 ≥ 𝟎𝟎 for all 𝒊𝒊 ∆𝝓𝝓𝑖𝑖 = Potential difference
𝝓𝝓 cannot be negative.
Potential Method for Insert(𝒙𝒙)
𝒊𝒊 − 𝟏𝟏
Before Insert(𝒙𝒙)
𝟐𝟐(𝒊𝒊 − 𝟏𝟏)
After Insert(𝒙𝒙)
𝑥𝑥
𝒊𝒊
Operation Insert(𝒙𝒙) Actual Cost ∆𝝓𝝓𝑖𝑖 Amortized Cost
𝒍𝒍
After Insert(𝒙𝒙)
𝑥𝑥 𝝓𝝓(𝒊𝒊) = 2𝑖𝑖 − 𝑙𝑙
𝒍𝒍
After Insert(𝒙𝒙)
𝑥𝑥 𝝓𝝓(𝒊𝒊) = 2𝑖𝑖 − 𝑙𝑙