SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Amortized Analysis
Algorithm and Complexity
Salabat Khan | 3820160108
1
Amortized Analysis
What is Amortized Analysis ?
In amortized analysis, the time required to perform
a sequence of operations is averaged over all the
operations performed.
 No involvement of probability
Average performance on a sequence of operations,
even some operation is expensive.
Guarantee average performance of each operation
among the sequence in worst case.
Amortized analysis is not just an analysis tool, it is also a way of
thinking about designing algorithms.
2017/05/15 2Amortized Analysis
Amortized Analysis
Methods of Amortized Analysis
 Accounting Method: we overcharge
some operations early and use them
to as prepaid charge later.
 Aggregate Method: we determine an
upper bound T(n) on the total sequence
of n operations. The cost of each will
then be T(n)/n.
 Potential Method: we maintain credit
as potential energy associated with
the structure as a whole.
2017/05/15 3Amortized Analysis
Amortized Analysis
1. Aggregate Method
Show that for all n, a sequence of n operations take
worst-case time T(n) in total
In the worst case, the average cost, or amortized cost ,
per operation is T(n)/n.
The amortized cost applies to each operation, even
when there are several types of operations in the
sequence.
2017/05/15 4Amortized Analysis
Amortized Analysis
3 ops:
Push(S,x) Pop(S)
Multi-
pop(S,k)
Worst-case
cost:
O(1) O(1)
O(min(|S|,k)
= O(n)
Amortized cost: O(1) per operation
Aggregate Analysis: Stack Example
2017/05/15 5Amortized Analysis
Amortized Analysis
Sequence of n push, pop, Multipop operations
Worst-case cost of Multipop is O(n)
Have n operations
Therefore, worst-case cost of sequence is O(n2
)
Observations
Each object can be popped only once per time that it’s
pushed
Have <= n pushes => <= n pops, including those in
Multipop
Therefore total cost = O(n)
Average over n operations => O(1) per operation on
average
Notice that no probability involved
……. Aggregate Analysis: Stack Example
2017/05/15 6Amortized Analysis
Amortized Analysis
2. Accounting Method
Charge i th operation a fictitious amortized cost ĉi,
where $1 pays for 1 unit of work (i.e., time).
Assign different charges (amortized cost ) to different
operations
 Some are charged more than actual cost
 Some are charged less
This fee is consumed to perform the operation.
Any amount not immediately consumed is stored in the
bank for use by subsequent operations.
The bank balance (the credit) must not go negative!
We must ensure that
for all n.
∑∑
==
≤
n
i
i
n
i
i cc
11
ˆ
Thus, the total amortized costs provide an upper
bound on the total true costs.
2017/05/15 7Amortized Analysis
Amortized Analysis
3 ops:
Push(S,x) Pop(S) Multi-pop(S,k)
•Assigned
cost:
2 0 0
•Actual cost: 1 1 min(|S|,k)
Push(S,x) pays for possible later pop of x.
….. Accounting Method: Stack Example
2017/05/15 8Amortized Analysis
Amortized Analysis
….. Accounting Method: Stack Example
When pushing an object, pay $2
$1 pays for the push
$1 is prepayment for it being popped by either pop
or Multipop
Since each object has $1, which is credit, the
credit can never go negative
Therefore, total amortized cost = O(n), is an upper
bound on total actual cost
2017/05/15 9Amortized Analysis
Amortized Analysis
….. Accounting Method: Binary Counter
k-bit Binary Counter: A[0..k−1]
INCREMENT(A)
1. i ← 0
2. while i < length[A] and A[i] = 1
3. do A[i] ← 0 ⊳ reset a bit
4. i ← i + 1
5. if i < length[A]
6. then A[i] ← 1 ⊳ set a bit
∑ ⋅= −
=
1
0 2][k
i
i
iAx
Introduction
2017/05/15 10Amortized Analysis
Amortized Analysis
Consider a sequence of n increments. The
worst-case time to execute one increment is
Θ(k). Therefore, the worst-case time for n
increments is n · Θ(k) = Θ(n⋅ k).
WRONG! In fact, the worst-case cost for n
increments is only Θ(n) ≪ Θ(n⋅ k).
Let’s see why. Note: You’d be correct
if you’d said O(n⋅ k).
But, it’s an overestimate.
….. Accounting Method: Binary Counter
2017/05/15 11Amortized Analysis
Amortized Analysis
Ctr A[4] A[3] A[2] A[1] A[0] Cost
0 0 0 0 0 0 0
1 0 0 0 0 1 1
2 0 0 0 1 0 3
3 0 0 0 1 1 4
4 0 0 1 0 0 7
5 0 0 1 0 1 8
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
10 0 1 0 1 0 18
11 0 1 0 1 1 19
A[0] flipped every op n
A[1] flipped every 2 ops n/2
A[2] flipped every 4 ops n/22
A[3] flipped every 8 ops n/23
… … … … …
A[i] flipped every 2i
ops n/2i
Total cost of n operations
….. Accounting Method: Binary Counter
2017/05/15 12Amortized Analysis
Amortized Analysis
 
)(
2
2
1
2
1
lg
1
n
nn
n
i
i
n
i
i
Θ=
=<




=
∑
∑
∞
=
=
Cost of n increments
Thus, the average cost of each increment operation is
Θ(n)/n = Θ(1).
….. Accounting Method: Binary Counter
2017/05/15 13Amortized Analysis
Amortized Analysis
Example:
Charge an amortized cost of $2 every time a bit is set from 0 to 1
• $1 pays for the actual bit setting.
• $1 is stored for later re-setting (from 1 to 0).
At any point, every 1 bit in the counter has $1 on it… that pays for
resetting it. (reset is “free”)
0 0 0 1$1
0 1$1
0
0 0 0 1$1
0 1$1
1$1
0 0 0 1$1
1$1
0 0
Cost = $2
Cost = $2
….. Accounting Method: Binary Counter
2017/05/15 14Amortized Analysis
Amortized Analysis
When Incrementing,
Amortized cost for line 3 = $0
Amortized cost for line 6 = $2
Amortized cost for INCREMENT(A) = $2
Amortized cost for n INCREMENT(A) = $2n =O(n)
INCREMENT(A)
1. i ← 0
2. while i < length[A] and A[i] = 1
3. do A[i] ← 0 ⊳ reset a bit
4. i ← i + 1
5. if i < length[A]
6. then A[i] ← 1 ⊳ set a bit
….. Accounting Method: Binary Counter
2017/05/15 15Amortized Analysis
Amortized Analysis
Accounting Method vs. Aggregate Method
Aggregate Method :
First analyze entire sequence
Then calculate amortized cost per operation
2017/05/15 16Amortized Analysis
Accounting method:
First assign amortized cost per operation
Check that they are valid .
Then compute cost of entire sequence of
operations
Amortized Analysis
3. Potential Method
IDEA: View the bank account as the
potential energy (as in physics) of the
dynamic set.
FRAMEWORK:
Start with an initial data structure D0.
Operation i transforms Di–1 to Di.
The cost of operation i is ci.
Define a potential function Φ : {Di} → R,
such that Φ(D0 ) = 0 and Φ(Di ) ≥ 0 for all i.
The amortized cost ĉi with respect to Φ is
defined to be ĉi = ci + Φ(Di) – Φ(Di–1).
2017/05/15 17Amortized Analysis
Amortized Analysis
Like the accounting method, but think of the
credit as potential stored with the entire data
structure.
 Accounting method stores credit with
specific objects while potential method
stores potential in the data structure as a
whole.
 Can release potential to pay for future
operations
Most flexible of the amortized analysis
methods .
….. Potential Method
2017/05/15 18Amortized Analysis
Amortized Analysis
ĉi = ci + Φ(Di) – Φ(Di–1)
potential difference ∆Φi
 If ∆Φi > 0, then ĉi > ci. Operation i
stores work in the data structure for later
use.
 If ∆Φi < 0, then ĉi < ci. The data
structure delivers up stored work to help
pay for operation i.
….. Potential Method
2017/05/15 19Amortized Analysis
Amortized Analysis
The total amortized cost of n operations is
Summing both sides telescopically.
….. Potential Method
)()( 0
1
DDc n
n
i
i Φ−Φ+= ∑=
( )∑∑ =
−
=
Φ−Φ+=
n
i
iii
n
i
i DDcc
1
1
1
)()(ˆ
∑=
≥
n
i
ic
1
since Φ(Dn) ≥ 0 and Φ(D0 ) = 0.
2017/05/15 20Amortized Analysis
Amortized Analysis
….. Potential Method: Stack Example
Define: φ(Di) = #items in stack Thus, φ(D0)=0.
Plug in for operations:
Push: ĉi = ci + φ(Di) - φ(Di-1)
= 1 + j - (j-1)
= 2
Pop: ĉi = ci + φ(Di) - φ(Di-1)
= 1 + (j-1) - j
= 0
Multi-pop: ĉi = ci + φ(Di) - φ(Di-1)
= k’ + (j-k’) - j k’=min(|S|,k)
= 0
2017/05/15 21Amortized Analysis
Amortized Analysis
….. Potential Method: Binary Counter
Define the potential of the counter after the ith
operation
by Φ(Di) = bi, the number of 1’s in the counter after the ith
operation.
Note:
• Φ(D0 ) = 0,
• Φ(Di) ≥ 0 for all i.
Example:
0 0 0 1 0 1 0
0 0 0 1$1
0 1$1
0 Accounting method)(
2017/05/15 22Amortized Analysis
Amortized Analysis
….. Potential Method
The amortized cost of the i th INCREMENT is
ĉi = ci + Φ(Di) – Φ(Di–1)
= (ti + 1) + (1 − ti)
= 2
Assume ith INCREMENT resets ti bits (in line 3).
Actual cost ci = (ti + 1)
Number of 1’s after ith operation: bi = bi–1 – ti + 1
Therefore, n INCREMENTs cost Θ(n) in the worst case.
2017/05/15 23Amortized Analysis
Amortized Analysis
….. Dynamic Tables
Problem: if too many items inserted, table may be too
small
Implementing a table (e.g., hash table) for dynamic
data, want to make it small as possible
Idea: allocate more memory as needed
2017/05/15 24Amortized Analysis
Amortized Analysis
In some applications:
We don't know how many objects will be stored in a table.
Similarly, if many objects are deleted from the table:
It may be worthwhile to reallocate the table with a smaller size .
This problem is called
Dynamically Expanding and Contracting a table.
….. Dynamic Tables: Why Dynamic Tables?
2017/05/15 25Amortized Analysis
Amortized Analysis
Using amortized analysis we will show that :
The amortized cost of insertion and deletion is O(1).
Even though the actual cost of an operation is large when it triggers an
expansion or a contraction.
We will also show how to guarantee that:
The unused space in a dynamic table never exceeds a constant fraction of
the total space.
Operations on Dynamic Table
TABLE-INSERT: Inserts into the table an item that occupies a single slot.
TABLE-DELETE: Removes an item from the table & frees its slot.
Load Factor: For empty its 1 and for non empty its given below
….. Dynamic Tables:
2017/05/15 26Amortized Analysis
tabletheof)(
tablein thestoreditemsofNumber
)(
slotsofnumbersize
T =α
Amortized Analysis
Assumption:
Table is allocated as an array of slots.
A table fills up when:
All slots have been used.
Equivalently, when its load factor becomes 1.
Table-Expansion occurs when:
An item is to be inserted into a full table.
A Common Heuristic:
Allocate a new table that has twice as many slots as the old one.
Hence, insertions are performed if only :
….. Insertion-Only Dynamic Tables:
2017/05/15 27Amortized Analysis
1 / 2 ≤ α(T) ≤ 1
Amortized Analysis
Table Insert:
….. Insertion-Only Dynamic Tables:
2017/05/15 28Amortized Analysis
1 / 2 ≤ α(T) ≤ 1
TABLE-INSERT (T, x)
1. if size[T] = 0 then
2. allocate table[T] with 1 slot
3. size[T] ← 1
4. if num[T] = size[T] then
5. allocate new-table with 2.size[T] slots
6. copy all items in table[T] into new-table
7. free table[T]
8. table[T] ← new-table[T]
9. size[T] ← 2.size[T]
10. insert x into table[T]
11. num[T] ← num[T] + 1
12. end
table[T] : pointer to block
of table storage
num[T] : number of
items in the table
size[T] : total number of
slots in the table
Initially, table is empty,
so num[T] = size[T] = 0
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Amortized Analysis
2017/05/15 27Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Amortized Analysis
2017/05/15 28Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Amortized Analysis
2017/05/15 29Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Insert(4) 4 1 4
Amortized Analysis
2017/05/15 30Amortized Analysis
Let ci = cost of ith insert
ci = i if i-1 is exact power of 2, 1 otherwise
Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Insert(4) 4 1 4
Insert(5) 8 1 + 4 5
Amortized Analysis
2017/05/15 31Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Insert(4) 4 1 4
Insert(5) 8 1 + 4 5
Insert(6) 8 1 6
Amortized Analysis
2017/05/15 32Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Insert(4) 4 1 4
Insert(5) 8 1 + 4 5
Insert(6) 8 1 6
Insert(7) 8 1 7
Amortized Analysis
2017/05/15 33Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2 3
Insert(4) 4 1 4
Insert(5) 8 1 + 4 5
Insert(6) 8 1 6
Insert(7) 8 1 7
Insert(8) 8 1 8
Amortized Analysis
2017/05/15 34Amortized Analysis
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2
Insert(4) 4 1
Insert(5) 8 1 + 4
Insert(6) 8 1
Insert(7) 8 1
Insert(8) 8 1
Insert(9) 16 1 + 8
1
2
3
4
5
6
7
8
9
Amortized Analysis
2017/05/15 35Amortized Analysis
Amortized Analysis
1. Aggregate Method
Table is initially empty.
Observe:
o i-th operation causes an expansion only when i-1
is an exact power of 2.
2017/05/15 38Amortized Analysis



=
otherwise1
2ofpowerexactanisif ii
ci
Amortized Analysis
…Aggregate Method
Therefore the total cost of n TABLE-INSERT
operations
 The amortized cost of a single operation is 3n/n=3 = O(1)
2017/05/15 39Amortized Analysis
 
nnnnnc
n
j
j
n
j
j
n
i
i 3222
lg
0
lg
01
=+=+<+= ∑∑∑ ===
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
ci
1 2 3 1 5 1 1 1 9 1 1 1 1 1 1 1 17 1 1 1 ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
Expansion
cost
1 2 4 8 16
Amortized Analysis
Assign the following amortized costs
– Table-Expansion : 0
– Insertion of a new item : 3
Insertion of a new item
a) 1 (as an actual cost) for inserting itself into the table
b) 1 (as a credit) for moving itself in the next expansion
c) 1 (as a credit) for moving another item (in the next
expansion) that has already moved in the last expansion
2017/05/15 40Amortized Analysis
… Accounting Method: Dynamic Tables
Amortized Analysis
Size of the table: M
Immediately after an expansion (just before the insertion)
num[T] = M/2 and size[T] = M where M is a power of 2.
Table contains no credits
2017/05/15 41Amortized Analysis
…Accounting Method : Dynamic Tables
Amortized Analysis
1st insertion
2nd insertion
2017/05/15 42Amortized Analysis
…Accounting Method : Dynamic Tables
(a) $1 for
insertion(b)
(c)
Amortized Analysis
M/2th Insertion
Thus, by the time the table contains M items and
is full
– each item in the table has $1 of credit to pay for its
move during the next expansion
2017/05/15 43Amortized Analysis
…Accounting Method : Dynamic Tables
Amortized Analysis
Define a potential function Φ that is
– 0 immediately after an expansion
– builds to the table size by the time table becomes full
Next expansion can be paid for by the potential.
One possible Φ is:
Φ(T) = 2*num[T] –size[T]
Immediately after an expansion
size[T] = 2*num[T] ⇒ Φ(T) = 0
Immediately before an expansion
size[T] = num[T] ⇒ Φ(T) = num[T]
The initial value for the potential is 0
2017/05/15 44Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Definition of Φ:
Since the table is at least half full
(i.e. num[T] ≥ size[T] / 2)
Φ(T) is always nonnegative.
Thus, the sum of the amortized cost of n TABLE-INSERT
operations is an upper bound on the sum of the actual
costs.
2017/05/15 45Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Analysis of i-th Table Insert:
numi : num[T] after the i-th operation
sizei : size[T] after the i-th operation
Φi : Potential after the i-th operation
Initially we have numi = sizei = Φi = 0
Note that, numi = numi-1 +1 always hold.
2017/05/15 46Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Insertion without Expansion:
sizei = sizei – 1 ; ci = 1
Insertion with Expansion:
sizei = 2.sizei – 1 and sizei-1 =numi – 1 =numi - 1
2017/05/15 47Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Adding Delete Operation:
TABLE-DELETE: Remove the specified item from the
table. It is often desirable to contract the table.
In table contraction, we would like to preserve two
properties
 Load factor of dynamic table is bounded below by a
constant
 Amortized cost of a table operation is bounded above
by a constant
We assume that the cost can be measured in terms of
elementary insertions and deletions
2017/05/15 48Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
A natural strategy for expansion and contraction
• Double the table size when an item is inserted into a
full table
• Halve the size when a deletion would cause
< 1 / 2
This strategy guarantees
Unfortunately, it can cause the amortized cost of an
operation to be quite large
2017/05/15 49Amortized Analysis
……. Potential Method: Dynamic Tables
)(Tα
1)(
2
1
≤≤ Tα
Amortized Analysis
Worst-Case for α(T) ≥ ½
Consider the following worst case scenario
– We perform n operations on an empty table
where n is a power of 2
– First n/2 operations are all insertions , cost a
total of Θ(n)
at the end: we have num[T] = size[T] = n/2
– Second n/2 operations repeat the sequence
I D D I
that is I D D I I D D I I D D I ...
2017/05/15 50Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Worst-Case for α(T) ≥ ½
2017/05/15 51Amortized Analysis
……. Potential Method: Dynamic Tables
Example: n=16
In the second n/2 operations
– The first INSERT cause an expansion
– Two further DELETEs cause contraction
– Two further INSERTs cause expansion ... and so on
Hence there are n/8 expansions and n/8 contractions
The cost of each expansion and contraction is ≈ n/2
Amortized Analysis
Worst-Case for α(T) ≥ ½
Thus the total cost of n operations is Θ(n2
) since
– First n/2 operations : 3n
– Second n/2 operations : (n/4)*(n/2)=n2
/8
The amortized cost of an operation is Θ(n)
The difficulty with this strategy is
– After an expansion, we do not perform enough
deletions to pay for a contraction
– After a contraction, we do not perform enough
insertions to pay for an expansion
2017/05/15 52Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Improving Expansion – Contraction
We can improve upon this strategy by allowing α(T)
to drop below ½
We continue to double the table size when an item
is inserted into a full table
But, we halve the table size (perform contraction)
when a deletion causes α(T) < ¼ rather than α(T)
< ½ ,
Therefore, ¼ ≤ α(T) ≤ 1
2017/05/15 53Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Improving Expansion – Contraction
Hence after an expansion, α(T) = ½ , thus at least
half of the items in the table must be deleted
before a contraction can occur.
Similarly, after a contraction α(T) = ½ , thus the
number of items in the table must be doubled by
insertions before an expansion can occur.
2017/05/15 54Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Define the potential function as follows
• Φ(T) = 0 immediately after an expansion or
contraction
• Recall that, α(T) = ½ immediately after and
expansion or contraction,
therefore the potential should build up to num[T]
as α(T) increases to 1 or decreases to ¼
• So that the next expansion or contraction can be
paid by the potential.
2017/05/15 55Amortized Analysis
……. Potential Method: Dynamic Tables
Amortized Analysis
Description of New Φ: One such Φ is
• Φ(T) = 0 when α = ½
• Φ(T) = num[T] when α = ¼
• Φ (T)= 0 for an empty table (num[T] = size[T]=0)
• Φ is always nonnegative
2017/05/15 56Amortized Analysis
……. Potential Method: Dynamic Tables





<−
≥−
=Φ
2
1
(T)if][
2
size[T]
2
1
(T)if][][2
)(
α
α
Tnum
TsizeTnum
T
Amortized Analysis
2017/05/15 57Amortized Analysis
……. Potential Method: Dynamic Tables
Operations are:
– TABLE-INSERT
– TABLE-DELETE
Amortized Analysis
2017/05/15 58Amortized Analysis
……. Potential Method: Dynamic Tables
Table Insert:
 αi-1 ≥ ½
 Analysis is identical to that for table expansion so ĉi = 3.
 αi-1 < ½ and αi < ½
Expansion may not occur (ĉi = 1 , sizei = sizei-1 )
Amortized Analysis
2017/05/15 59Amortized Analysis
……. Potential Method: Dynamic Tables
Table Insert:
 αi-1 < ½ but αi ≥ ½
Thus, amortized cost of a TABLE-INSERT operation is at most 3.
Amortized Analysis
2017/05/15 60Amortized Analysis
……. Potential Method: Dynamic Tables
Table Delete:
 αi-1 < ½ (No contraction) :sizei = sizei-1
 αi-1 < ½ (Contraction) ĉi =numi + 1 , sizei /2= sizei-1 /4 = numi-
1= numi + 1
 Thus, the amortized cost of a TABLE-DELETE operation is at
most 2
 Since the amortized cost of each operation is bounded above by
a constant
 The actual time for any sequence of n operations on a Dynamic
Table is O(n)
Amortized Analysis
….. Dynamic Tables
2017/05/15 25Amortized Analysis
Amortized Analysis
2017/05/15 62Amortized Analysis
Amortized Analysis
2017/15/05 63Amortized Analysis
Amortized Analysis
4/15/2012 64Amortized Analysis

Weitere ähnliche Inhalte

Was ist angesagt?

Unit3:Informed and Uninformed search
Unit3:Informed and Uninformed searchUnit3:Informed and Uninformed search
Unit3:Informed and Uninformed searchTekendra Nath Yogi
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...Abhishek Singh
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAsst.prof M.Gokilavani
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Game playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphGame playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphSyed Zaid Irshad
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problemharsh kothari
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.mohanrathod18
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13Kumar
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmHema Kashyap
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 

Was ist angesagt? (20)

Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Unit3:Informed and Uninformed search
Unit3:Informed and Uninformed searchUnit3:Informed and Uninformed search
Unit3:Informed and Uninformed search
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptx
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Game playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphGame playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graph
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Gradient descent method
Gradient descent methodGradient descent method
Gradient descent method
 
Informed search
Informed searchInformed search
Informed search
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 

Ähnlich wie Aoa amortized analysis

Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms sathish sak
 
DOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptxDOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptx03Shrishti
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis sathish sak
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdfpasinduneshan
 
lecture 23
lecture 23lecture 23
lecture 23sajinsc
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 

Ähnlich wie Aoa amortized analysis (20)

Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
DOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptxDOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptx
 
09. amortized analysis
09. amortized analysis09. amortized analysis
09. amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 
lecture 23
lecture 23lecture 23
lecture 23
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
13 Amortized Analysis
13 Amortized Analysis13 Amortized Analysis
13 Amortized Analysis
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 

Kürzlich hochgeladen

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Kürzlich hochgeladen (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Aoa amortized analysis

  • 1. Amortized Analysis Algorithm and Complexity Salabat Khan | 3820160108 1
  • 2. Amortized Analysis What is Amortized Analysis ? In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed.  No involvement of probability Average performance on a sequence of operations, even some operation is expensive. Guarantee average performance of each operation among the sequence in worst case. Amortized analysis is not just an analysis tool, it is also a way of thinking about designing algorithms. 2017/05/15 2Amortized Analysis
  • 3. Amortized Analysis Methods of Amortized Analysis  Accounting Method: we overcharge some operations early and use them to as prepaid charge later.  Aggregate Method: we determine an upper bound T(n) on the total sequence of n operations. The cost of each will then be T(n)/n.  Potential Method: we maintain credit as potential energy associated with the structure as a whole. 2017/05/15 3Amortized Analysis
  • 4. Amortized Analysis 1. Aggregate Method Show that for all n, a sequence of n operations take worst-case time T(n) in total In the worst case, the average cost, or amortized cost , per operation is T(n)/n. The amortized cost applies to each operation, even when there are several types of operations in the sequence. 2017/05/15 4Amortized Analysis
  • 5. Amortized Analysis 3 ops: Push(S,x) Pop(S) Multi- pop(S,k) Worst-case cost: O(1) O(1) O(min(|S|,k) = O(n) Amortized cost: O(1) per operation Aggregate Analysis: Stack Example 2017/05/15 5Amortized Analysis
  • 6. Amortized Analysis Sequence of n push, pop, Multipop operations Worst-case cost of Multipop is O(n) Have n operations Therefore, worst-case cost of sequence is O(n2 ) Observations Each object can be popped only once per time that it’s pushed Have <= n pushes => <= n pops, including those in Multipop Therefore total cost = O(n) Average over n operations => O(1) per operation on average Notice that no probability involved ……. Aggregate Analysis: Stack Example 2017/05/15 6Amortized Analysis
  • 7. Amortized Analysis 2. Accounting Method Charge i th operation a fictitious amortized cost ĉi, where $1 pays for 1 unit of work (i.e., time). Assign different charges (amortized cost ) to different operations  Some are charged more than actual cost  Some are charged less This fee is consumed to perform the operation. Any amount not immediately consumed is stored in the bank for use by subsequent operations. The bank balance (the credit) must not go negative! We must ensure that for all n. ∑∑ == ≤ n i i n i i cc 11 ˆ Thus, the total amortized costs provide an upper bound on the total true costs. 2017/05/15 7Amortized Analysis
  • 8. Amortized Analysis 3 ops: Push(S,x) Pop(S) Multi-pop(S,k) •Assigned cost: 2 0 0 •Actual cost: 1 1 min(|S|,k) Push(S,x) pays for possible later pop of x. ….. Accounting Method: Stack Example 2017/05/15 8Amortized Analysis
  • 9. Amortized Analysis ….. Accounting Method: Stack Example When pushing an object, pay $2 $1 pays for the push $1 is prepayment for it being popped by either pop or Multipop Since each object has $1, which is credit, the credit can never go negative Therefore, total amortized cost = O(n), is an upper bound on total actual cost 2017/05/15 9Amortized Analysis
  • 10. Amortized Analysis ….. Accounting Method: Binary Counter k-bit Binary Counter: A[0..k−1] INCREMENT(A) 1. i ← 0 2. while i < length[A] and A[i] = 1 3. do A[i] ← 0 ⊳ reset a bit 4. i ← i + 1 5. if i < length[A] 6. then A[i] ← 1 ⊳ set a bit ∑ ⋅= − = 1 0 2][k i i iAx Introduction 2017/05/15 10Amortized Analysis
  • 11. Amortized Analysis Consider a sequence of n increments. The worst-case time to execute one increment is Θ(k). Therefore, the worst-case time for n increments is n · Θ(k) = Θ(n⋅ k). WRONG! In fact, the worst-case cost for n increments is only Θ(n) ≪ Θ(n⋅ k). Let’s see why. Note: You’d be correct if you’d said O(n⋅ k). But, it’s an overestimate. ….. Accounting Method: Binary Counter 2017/05/15 11Amortized Analysis
  • 12. Amortized Analysis Ctr A[4] A[3] A[2] A[1] A[0] Cost 0 0 0 0 0 0 0 1 0 0 0 0 1 1 2 0 0 0 1 0 3 3 0 0 0 1 1 4 4 0 0 1 0 0 7 5 0 0 1 0 1 8 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 10 0 1 0 1 0 18 11 0 1 0 1 1 19 A[0] flipped every op n A[1] flipped every 2 ops n/2 A[2] flipped every 4 ops n/22 A[3] flipped every 8 ops n/23 … … … … … A[i] flipped every 2i ops n/2i Total cost of n operations ….. Accounting Method: Binary Counter 2017/05/15 12Amortized Analysis
  • 13. Amortized Analysis   )( 2 2 1 2 1 lg 1 n nn n i i n i i Θ= =<     = ∑ ∑ ∞ = = Cost of n increments Thus, the average cost of each increment operation is Θ(n)/n = Θ(1). ….. Accounting Method: Binary Counter 2017/05/15 13Amortized Analysis
  • 14. Amortized Analysis Example: Charge an amortized cost of $2 every time a bit is set from 0 to 1 • $1 pays for the actual bit setting. • $1 is stored for later re-setting (from 1 to 0). At any point, every 1 bit in the counter has $1 on it… that pays for resetting it. (reset is “free”) 0 0 0 1$1 0 1$1 0 0 0 0 1$1 0 1$1 1$1 0 0 0 1$1 1$1 0 0 Cost = $2 Cost = $2 ….. Accounting Method: Binary Counter 2017/05/15 14Amortized Analysis
  • 15. Amortized Analysis When Incrementing, Amortized cost for line 3 = $0 Amortized cost for line 6 = $2 Amortized cost for INCREMENT(A) = $2 Amortized cost for n INCREMENT(A) = $2n =O(n) INCREMENT(A) 1. i ← 0 2. while i < length[A] and A[i] = 1 3. do A[i] ← 0 ⊳ reset a bit 4. i ← i + 1 5. if i < length[A] 6. then A[i] ← 1 ⊳ set a bit ….. Accounting Method: Binary Counter 2017/05/15 15Amortized Analysis
  • 16. Amortized Analysis Accounting Method vs. Aggregate Method Aggregate Method : First analyze entire sequence Then calculate amortized cost per operation 2017/05/15 16Amortized Analysis Accounting method: First assign amortized cost per operation Check that they are valid . Then compute cost of entire sequence of operations
  • 17. Amortized Analysis 3. Potential Method IDEA: View the bank account as the potential energy (as in physics) of the dynamic set. FRAMEWORK: Start with an initial data structure D0. Operation i transforms Di–1 to Di. The cost of operation i is ci. Define a potential function Φ : {Di} → R, such that Φ(D0 ) = 0 and Φ(Di ) ≥ 0 for all i. The amortized cost ĉi with respect to Φ is defined to be ĉi = ci + Φ(Di) – Φ(Di–1). 2017/05/15 17Amortized Analysis
  • 18. Amortized Analysis Like the accounting method, but think of the credit as potential stored with the entire data structure.  Accounting method stores credit with specific objects while potential method stores potential in the data structure as a whole.  Can release potential to pay for future operations Most flexible of the amortized analysis methods . ….. Potential Method 2017/05/15 18Amortized Analysis
  • 19. Amortized Analysis ĉi = ci + Φ(Di) – Φ(Di–1) potential difference ∆Φi  If ∆Φi > 0, then ĉi > ci. Operation i stores work in the data structure for later use.  If ∆Φi < 0, then ĉi < ci. The data structure delivers up stored work to help pay for operation i. ….. Potential Method 2017/05/15 19Amortized Analysis
  • 20. Amortized Analysis The total amortized cost of n operations is Summing both sides telescopically. ….. Potential Method )()( 0 1 DDc n n i i Φ−Φ+= ∑= ( )∑∑ = − = Φ−Φ+= n i iii n i i DDcc 1 1 1 )()(ˆ ∑= ≥ n i ic 1 since Φ(Dn) ≥ 0 and Φ(D0 ) = 0. 2017/05/15 20Amortized Analysis
  • 21. Amortized Analysis ….. Potential Method: Stack Example Define: φ(Di) = #items in stack Thus, φ(D0)=0. Plug in for operations: Push: ĉi = ci + φ(Di) - φ(Di-1) = 1 + j - (j-1) = 2 Pop: ĉi = ci + φ(Di) - φ(Di-1) = 1 + (j-1) - j = 0 Multi-pop: ĉi = ci + φ(Di) - φ(Di-1) = k’ + (j-k’) - j k’=min(|S|,k) = 0 2017/05/15 21Amortized Analysis
  • 22. Amortized Analysis ….. Potential Method: Binary Counter Define the potential of the counter after the ith operation by Φ(Di) = bi, the number of 1’s in the counter after the ith operation. Note: • Φ(D0 ) = 0, • Φ(Di) ≥ 0 for all i. Example: 0 0 0 1 0 1 0 0 0 0 1$1 0 1$1 0 Accounting method)( 2017/05/15 22Amortized Analysis
  • 23. Amortized Analysis ….. Potential Method The amortized cost of the i th INCREMENT is ĉi = ci + Φ(Di) – Φ(Di–1) = (ti + 1) + (1 − ti) = 2 Assume ith INCREMENT resets ti bits (in line 3). Actual cost ci = (ti + 1) Number of 1’s after ith operation: bi = bi–1 – ti + 1 Therefore, n INCREMENTs cost Θ(n) in the worst case. 2017/05/15 23Amortized Analysis
  • 24. Amortized Analysis ….. Dynamic Tables Problem: if too many items inserted, table may be too small Implementing a table (e.g., hash table) for dynamic data, want to make it small as possible Idea: allocate more memory as needed 2017/05/15 24Amortized Analysis
  • 25. Amortized Analysis In some applications: We don't know how many objects will be stored in a table. Similarly, if many objects are deleted from the table: It may be worthwhile to reallocate the table with a smaller size . This problem is called Dynamically Expanding and Contracting a table. ….. Dynamic Tables: Why Dynamic Tables? 2017/05/15 25Amortized Analysis
  • 26. Amortized Analysis Using amortized analysis we will show that : The amortized cost of insertion and deletion is O(1). Even though the actual cost of an operation is large when it triggers an expansion or a contraction. We will also show how to guarantee that: The unused space in a dynamic table never exceeds a constant fraction of the total space. Operations on Dynamic Table TABLE-INSERT: Inserts into the table an item that occupies a single slot. TABLE-DELETE: Removes an item from the table & frees its slot. Load Factor: For empty its 1 and for non empty its given below ….. Dynamic Tables: 2017/05/15 26Amortized Analysis tabletheof)( tablein thestoreditemsofNumber )( slotsofnumbersize T =α
  • 27. Amortized Analysis Assumption: Table is allocated as an array of slots. A table fills up when: All slots have been used. Equivalently, when its load factor becomes 1. Table-Expansion occurs when: An item is to be inserted into a full table. A Common Heuristic: Allocate a new table that has twice as many slots as the old one. Hence, insertions are performed if only : ….. Insertion-Only Dynamic Tables: 2017/05/15 27Amortized Analysis 1 / 2 ≤ α(T) ≤ 1
  • 28. Amortized Analysis Table Insert: ….. Insertion-Only Dynamic Tables: 2017/05/15 28Amortized Analysis 1 / 2 ≤ α(T) ≤ 1 TABLE-INSERT (T, x) 1. if size[T] = 0 then 2. allocate table[T] with 1 slot 3. size[T] ← 1 4. if num[T] = size[T] then 5. allocate new-table with 2.size[T] slots 6. copy all items in table[T] into new-table 7. free table[T] 8. table[T] ← new-table[T] 9. size[T] ← 2.size[T] 10. insert x into table[T] 11. num[T] ← num[T] + 1 12. end table[T] : pointer to block of table storage num[T] : number of items in the table size[T] : total number of slots in the table Initially, table is empty, so num[T] = size[T] = 0
  • 29. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Amortized Analysis 2017/05/15 27Amortized Analysis
  • 30. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Amortized Analysis 2017/05/15 28Amortized Analysis
  • 31. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Amortized Analysis 2017/05/15 29Amortized Analysis
  • 32. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Amortized Analysis 2017/05/15 30Amortized Analysis
  • 33. Let ci = cost of ith insert ci = i if i-1 is exact power of 2, 1 otherwise Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Amortized Analysis 2017/05/15 31Amortized Analysis
  • 34. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Insert(6) 8 1 6 Amortized Analysis 2017/05/15 32Amortized Analysis
  • 35. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Insert(6) 8 1 6 Insert(7) 8 1 7 Amortized Analysis 2017/05/15 33Amortized Analysis
  • 36. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Insert(6) 8 1 6 Insert(7) 8 1 7 Insert(8) 8 1 8 Amortized Analysis 2017/05/15 34Amortized Analysis
  • 37. ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 Insert(4) 4 1 Insert(5) 8 1 + 4 Insert(6) 8 1 Insert(7) 8 1 Insert(8) 8 1 Insert(9) 16 1 + 8 1 2 3 4 5 6 7 8 9 Amortized Analysis 2017/05/15 35Amortized Analysis
  • 38. Amortized Analysis 1. Aggregate Method Table is initially empty. Observe: o i-th operation causes an expansion only when i-1 is an exact power of 2. 2017/05/15 38Amortized Analysis    = otherwise1 2ofpowerexactanisif ii ci
  • 39. Amortized Analysis …Aggregate Method Therefore the total cost of n TABLE-INSERT operations  The amortized cost of a single operation is 3n/n=3 = O(1) 2017/05/15 39Amortized Analysis   nnnnnc n j j n j j n i i 3222 lg 0 lg 01 =+=+<+= ∑∑∑ === i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... ci 1 2 3 1 5 1 1 1 9 1 1 1 1 1 1 1 17 1 1 1 ... 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... Expansion cost 1 2 4 8 16
  • 40. Amortized Analysis Assign the following amortized costs – Table-Expansion : 0 – Insertion of a new item : 3 Insertion of a new item a) 1 (as an actual cost) for inserting itself into the table b) 1 (as a credit) for moving itself in the next expansion c) 1 (as a credit) for moving another item (in the next expansion) that has already moved in the last expansion 2017/05/15 40Amortized Analysis … Accounting Method: Dynamic Tables
  • 41. Amortized Analysis Size of the table: M Immediately after an expansion (just before the insertion) num[T] = M/2 and size[T] = M where M is a power of 2. Table contains no credits 2017/05/15 41Amortized Analysis …Accounting Method : Dynamic Tables
  • 42. Amortized Analysis 1st insertion 2nd insertion 2017/05/15 42Amortized Analysis …Accounting Method : Dynamic Tables (a) $1 for insertion(b) (c)
  • 43. Amortized Analysis M/2th Insertion Thus, by the time the table contains M items and is full – each item in the table has $1 of credit to pay for its move during the next expansion 2017/05/15 43Amortized Analysis …Accounting Method : Dynamic Tables
  • 44. Amortized Analysis Define a potential function Φ that is – 0 immediately after an expansion – builds to the table size by the time table becomes full Next expansion can be paid for by the potential. One possible Φ is: Φ(T) = 2*num[T] –size[T] Immediately after an expansion size[T] = 2*num[T] ⇒ Φ(T) = 0 Immediately before an expansion size[T] = num[T] ⇒ Φ(T) = num[T] The initial value for the potential is 0 2017/05/15 44Amortized Analysis ……. Potential Method: Dynamic Tables
  • 45. Amortized Analysis Definition of Φ: Since the table is at least half full (i.e. num[T] ≥ size[T] / 2) Φ(T) is always nonnegative. Thus, the sum of the amortized cost of n TABLE-INSERT operations is an upper bound on the sum of the actual costs. 2017/05/15 45Amortized Analysis ……. Potential Method: Dynamic Tables
  • 46. Amortized Analysis Analysis of i-th Table Insert: numi : num[T] after the i-th operation sizei : size[T] after the i-th operation Φi : Potential after the i-th operation Initially we have numi = sizei = Φi = 0 Note that, numi = numi-1 +1 always hold. 2017/05/15 46Amortized Analysis ……. Potential Method: Dynamic Tables
  • 47. Amortized Analysis Insertion without Expansion: sizei = sizei – 1 ; ci = 1 Insertion with Expansion: sizei = 2.sizei – 1 and sizei-1 =numi – 1 =numi - 1 2017/05/15 47Amortized Analysis ……. Potential Method: Dynamic Tables
  • 48. Amortized Analysis Adding Delete Operation: TABLE-DELETE: Remove the specified item from the table. It is often desirable to contract the table. In table contraction, we would like to preserve two properties  Load factor of dynamic table is bounded below by a constant  Amortized cost of a table operation is bounded above by a constant We assume that the cost can be measured in terms of elementary insertions and deletions 2017/05/15 48Amortized Analysis ……. Potential Method: Dynamic Tables
  • 49. Amortized Analysis A natural strategy for expansion and contraction • Double the table size when an item is inserted into a full table • Halve the size when a deletion would cause < 1 / 2 This strategy guarantees Unfortunately, it can cause the amortized cost of an operation to be quite large 2017/05/15 49Amortized Analysis ……. Potential Method: Dynamic Tables )(Tα 1)( 2 1 ≤≤ Tα
  • 50. Amortized Analysis Worst-Case for α(T) ≥ ½ Consider the following worst case scenario – We perform n operations on an empty table where n is a power of 2 – First n/2 operations are all insertions , cost a total of Θ(n) at the end: we have num[T] = size[T] = n/2 – Second n/2 operations repeat the sequence I D D I that is I D D I I D D I I D D I ... 2017/05/15 50Amortized Analysis ……. Potential Method: Dynamic Tables
  • 51. Amortized Analysis Worst-Case for α(T) ≥ ½ 2017/05/15 51Amortized Analysis ……. Potential Method: Dynamic Tables Example: n=16 In the second n/2 operations – The first INSERT cause an expansion – Two further DELETEs cause contraction – Two further INSERTs cause expansion ... and so on Hence there are n/8 expansions and n/8 contractions The cost of each expansion and contraction is ≈ n/2
  • 52. Amortized Analysis Worst-Case for α(T) ≥ ½ Thus the total cost of n operations is Θ(n2 ) since – First n/2 operations : 3n – Second n/2 operations : (n/4)*(n/2)=n2 /8 The amortized cost of an operation is Θ(n) The difficulty with this strategy is – After an expansion, we do not perform enough deletions to pay for a contraction – After a contraction, we do not perform enough insertions to pay for an expansion 2017/05/15 52Amortized Analysis ……. Potential Method: Dynamic Tables
  • 53. Amortized Analysis Improving Expansion – Contraction We can improve upon this strategy by allowing α(T) to drop below ½ We continue to double the table size when an item is inserted into a full table But, we halve the table size (perform contraction) when a deletion causes α(T) < ¼ rather than α(T) < ½ , Therefore, ¼ ≤ α(T) ≤ 1 2017/05/15 53Amortized Analysis ……. Potential Method: Dynamic Tables
  • 54. Amortized Analysis Improving Expansion – Contraction Hence after an expansion, α(T) = ½ , thus at least half of the items in the table must be deleted before a contraction can occur. Similarly, after a contraction α(T) = ½ , thus the number of items in the table must be doubled by insertions before an expansion can occur. 2017/05/15 54Amortized Analysis ……. Potential Method: Dynamic Tables
  • 55. Amortized Analysis Define the potential function as follows • Φ(T) = 0 immediately after an expansion or contraction • Recall that, α(T) = ½ immediately after and expansion or contraction, therefore the potential should build up to num[T] as α(T) increases to 1 or decreases to ¼ • So that the next expansion or contraction can be paid by the potential. 2017/05/15 55Amortized Analysis ……. Potential Method: Dynamic Tables
  • 56. Amortized Analysis Description of New Φ: One such Φ is • Φ(T) = 0 when α = ½ • Φ(T) = num[T] when α = ¼ • Φ (T)= 0 for an empty table (num[T] = size[T]=0) • Φ is always nonnegative 2017/05/15 56Amortized Analysis ……. Potential Method: Dynamic Tables      <− ≥− =Φ 2 1 (T)if][ 2 size[T] 2 1 (T)if][][2 )( α α Tnum TsizeTnum T
  • 57. Amortized Analysis 2017/05/15 57Amortized Analysis ……. Potential Method: Dynamic Tables Operations are: – TABLE-INSERT – TABLE-DELETE
  • 58. Amortized Analysis 2017/05/15 58Amortized Analysis ……. Potential Method: Dynamic Tables Table Insert:  αi-1 ≥ ½  Analysis is identical to that for table expansion so ĉi = 3.  αi-1 < ½ and αi < ½ Expansion may not occur (ĉi = 1 , sizei = sizei-1 )
  • 59. Amortized Analysis 2017/05/15 59Amortized Analysis ……. Potential Method: Dynamic Tables Table Insert:  αi-1 < ½ but αi ≥ ½ Thus, amortized cost of a TABLE-INSERT operation is at most 3.
  • 60. Amortized Analysis 2017/05/15 60Amortized Analysis ……. Potential Method: Dynamic Tables Table Delete:  αi-1 < ½ (No contraction) :sizei = sizei-1  αi-1 < ½ (Contraction) ĉi =numi + 1 , sizei /2= sizei-1 /4 = numi- 1= numi + 1
  • 61.  Thus, the amortized cost of a TABLE-DELETE operation is at most 2  Since the amortized cost of each operation is bounded above by a constant  The actual time for any sequence of n operations on a Dynamic Table is O(n) Amortized Analysis ….. Dynamic Tables 2017/05/15 25Amortized Analysis