12 – analisis amortized
TRANSCRIPT
-
7/31/2019 12 Analisis Amortized
1/30
12 Analisis Amortized
Mata Kuliah: Perancangan dan Analisis Algoritma
Dosen: Anny Yuniarti, S.Kom, M.Comp.Sc
-
7/31/2019 12 Analisis Amortized
2/30
Introduction
5/28/2012Jurusan Teknik Informatika FTIF ITS2
Amortized analysis is a tool for analyzing algorithms that perform a
sequence of similar operations.
Instead of bounding the cost of the sequence of operations bybounding the actual cost of each operation separately, an amortizedanalysis can be used to provide a bound on the actual cost of theentire sequence.
One reason this idea can be effective is that it may be impossible ina sequence of operations for all of the individual operations to run intheir known worst-case time bounds. While some operations areexpensive, many others might be cheap.
Amortized analysis is not just an analysis tool, however; it is also away of thinking about the design of algorithms, since the designof an algorithm and the analysis of its running time are often closelyintertwined.
-
7/31/2019 12 Analisis Amortized
3/30
Amortized analysis
5/28/2012Jurusan Teknik Informatika FTIF ITS3
Analyze a sequenceof operations on a datastructure.
Goal:Show that although some individualoperations may be expensive, on average the cost
per operation is small.
Averagein this context does not mean that wereaveraging over a distribution of inputs.
No probability is involved.
Were talking about average cost in the worst case.
-
7/31/2019 12 Analisis Amortized
4/30
Organization
5/28/2012Jurusan Teknik Informatika FTIF ITS4
Well look at 3 methods: aggregate analysis
accounting method
potential method
The aggregate method, though simple, lacks theprecision of the other two methods. In particular, theaccounting and potential methods allow a specific
amortized cost to be allocated to each operation.
-
7/31/2019 12 Analisis Amortized
5/30
Aggregate analysis
5/28/2012Jurusan Teknik Informatika FTIF ITS5
Show that for all n, a sequence of n operations takeworst-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, evenwhen there are several types of operations in the
sequence.
-
7/31/2019 12 Analisis Amortized
6/30
Stack Example
3 ops:
Push(S,x) Pop(S)Multi-
pop(S,k)
Worst-casecost:
O(1) O(1)O(min(|S|,k)
= O(n)
Amortized cost: O(1) per op
PUSH(S, x)
1 top[S] top[S] + 1
2 S[top[S]] x
-
7/31/2019 12 Analisis Amortized
7/30
Stack Example
3 ops:
Push(S,x) Pop(S)Multi-
pop(S,k)
Worst-casecost:
O(1) O(1)O(min(|S|,k)
= O(n)
Amortized cost: O(1) per op
STACK-EMPTY(S)
1 iftop[S] = 0
2 then return TRUE
3 else return FALSE
POP(S)
1 if STACK-EMPTY(S)
2 then error "underflow"
3 elsetop[S] top[S] - 1
4 returnS[top[S] + 1]
-
7/31/2019 12 Analisis Amortized
8/30
Stack Example
3 ops:
Push(S,x) Pop(S)Multi-
pop(S,k)
Worst-casecost:
O(1) O(1)O(min(|S|,k)
= O(n)
Amortized cost: O(1) per op
MULTIPOP(S, k)
1 while not STACK-EMPTY(S) and k 0
2 do POP(S)
3 k k - 1
-
7/31/2019 12 Analisis Amortized
9/30
Stack Example : Aggregate Analysis
5/28/2012Jurusan Teknik Informatika FTIF ITS9
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 its
pushed
Have
-
7/31/2019 12 Analisis Amortized
10/30
Incrementing a Binary Counter
k-bit Binary Counter: A[0..k1]
INCREMENT(A)1. i0
2. while i < length[A] andA[i] = 1
3. do A[i] 0 reset a bit
4. ii + 1
5. if i < length[A]
6.then
A[i]
1set a bit
10 2][
k
i
iiAx
-
7/31/2019 12 Analisis Amortized
11/30
k-bit Binary Counter
Ctr A[4] A[3] A[2] A[1] A[0]
0 0 0 0 0 0
1 0 0 0 0 1
2 0 0 0 1 0
3 0 0 0 1 1
4 0 0 1 0 0
5 0 0 1 0 1
6 0 0 1 1 0
7 0 0 1 1 1
8 0 1 0 0 0
9 0 1 0 0 1
10 0 1 0 1 0
11 0 1 0 1 1
INCREMENT(A)1. i02. while i < length[A]
andA[i] = 1
3. do A[i] 0
4. ii + 1
5.if i < length[A]
6. then A[i]1
-
7/31/2019 12 Analisis Amortized
12/30
k-bit Binary Counter
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
INCREMENT(A)1. i02. while i < length[A]
andA[i] = 1
3. do A[i] 0
4. ii + 1
5.if i < length[A]
6. then A[i]1
The running cost forflipping bits
-
7/31/2019 12 Analisis Amortized
13/30
INCREMENT(A)1. i0
2. while i < length[A] andA[i] = 1
3. do A[i] 0
4. ii + 15. if i < length[A]
6. then A[i]1
-
7/31/2019 12 Analisis Amortized
14/30
Worst-case analysis
Consider a sequence of ninsertions.The worst-case time to execute oneinsertion is Q(k). Therefore, the worst-
case time for ninsertions is n Q(k) =Q(nk).WRONG! In fact, the worst-case costfor ninsertions is only Q(n) Q(nk).
Lets see why. Note: Youll be correctIf youd said O(nk).But, its an overestimate.
-
7/31/2019 12 Analisis Amortized
15/30
Tighter 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 opsn/2
A[2] flipped every 4 opsn/22
A[3] flipped every 8 opsn/23
A[i] flipped every 2iopsn/2i
Total cost of noperations
i k never 0
-
7/31/2019 12 Analisis Amortized
16/30
Tighter analysis (continued)
)(
22/11
1
2
1
2
0
1
0
nO
nnn
n
ii
k
i
i
Cost of nincrements
Thus, the average cost of each incrementoperation is O(n)/n= O(1).
-
7/31/2019 12 Analisis Amortized
17/30
Accounting method
5/28/2012Jurusan Teknik Informatika FTIF ITS17
Assign different charges to different operations. Some are charged more than actual cost.
Some are charged less.
Amortized cost= amount we charge.
When amortized cost > actual cost, store thedifference on specific objectsin the data structure ascredit.
Use credit later to pay for operations whose actualcost > amortized cost.
Differs from aggregate analysis: In the accounting method, different operations can have
different costs.
In aggregate analysis, all operations have same cost.
-
7/31/2019 12 Analisis Amortized
18/30
Accounting method
5/28/2012Jurusan Teknik Informatika FTIF ITS18
Need credit to never go negative. Otherwise, have a sequence of operations for which the
amortized cost is not an upper bound on actual cost.
Amortized cost would tell us nothing.
Let ci=actual cost of ith operation ,=amortized cost of ith operation .
ic
n
ii
n
ii
cc
11
-
7/31/2019 12 Analisis Amortized
19/30
A Simple Example: Accounting
3 ops:
Push(S,x) Pop(S) Multi-pop(S,k)
Assignedcost: 2 0 0
Actual cost: 1 1 min(|S|,k)
Push(S,x) pays for possible later pop of x.
1
1
1
1
1
1
1
1
1
0
1
1
0
0
0
-
7/31/2019 12 Analisis Amortized
20/30
Stack Example: Accounting Methods
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, thecredit can never go negative Therefore, total amortized cost = O(n), is an
upper bound on total actual cost
-
7/31/2019 12 Analisis Amortized
21/30
Example:
Accounting analysis of INCREMENT
Charge an amortized cost of $2 every time a bitis 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 onit that pays for resetting it. (reset is free)
0 0 0 1$1 0 1$1 00 0 0 1$1 0 1$11$1
0 0 0 1$11$10 0
Cost = $2
Cost = $2
-
7/31/2019 12 Analisis Amortized
22/30
Incrementing a Binary Counter
When Incrementing,
Amortized cost for line 3 = $0
Amortized cost for line 6 = $2 Amortized cost for INCREMENT(A) = $2
Amortized cost for nINCREMENT(A) = $2n=O(n)
INCREMENT(A)1. i02. while i < length[A] andA[i] = 13. do A[i] 0 reset a bit4. ii +15. if i< length[A]6. then A[i]1 set a bit
-
7/31/2019 12 Analisis Amortized
23/30
The potential method
5/28/2012Jurusan Teknik Informatika FTIF ITS23
The potential method, or physicist's view, defines a function that
maps a data structure onto a real valued, nonnegative potential.The potential stored in the data structure may be released to pay forfuture operations.
The potential is associated with the data structure as a whole ratherthan with specific objects within the data structure.
Let us refer to the data structure at time i(or alternatively, just afteroperation i), as Di.
The potential function (Di) maps Dionto a real value. Again, denote
the actual cost of operation ias ci.
In the potential method, the amortized cost of operation iis equal tothe actual cost plus the increase in potential due to the operation:
-
7/31/2019 12 Analisis Amortized
24/30
The potential method
5/28/2012Jurusan Teknik Informatika FTIF ITS24
Ensure that the total amortized cost of a sequence isalways an upper bound on the total actual cost
-
7/31/2019 12 Analisis Amortized
25/30
Example: Stack operations
5/28/2012Jurusan Teknik Informatika FTIF ITS25
A potential function on a stack is (Di) = the numberof items in the stack after operationi.
For the empty stack D0 with which we start, we have(D0) = 0.
If the ith operation on a stack containing sobjects isa PUSH operation, then the potential difference is
(Di) - (Di-1) = (s+ 1)s = 1
The amortized cost of this PUSH operation is
-
7/31/2019 12 Analisis Amortized
26/30
Example: Stack operations(contd)
5/28/2012Jurusan Teknik Informatika FTIF ITS26
The amortized cost of each of the three operations isO(1), and thus the total amortized cost of asequence of noperations is O(n).
-
7/31/2019 12 Analisis Amortized
27/30
Example: binary counter
5/28/2012Jurusan Teknik Informatika FTIF ITS27
The potential of the counter after the ith INCREMENT
operation to be bi, the number of 1s in the counterafter theith operation.
Suppose that the ith INCREMENT operation resets tibits. Theactual cost of the operation is therefore at most ti+ 1, since in
addition to resetting tibits, it sets at most one bit to 1.
If bi= 0, then the ith operation resets all kbits, and so bi-1 = ti=k.
If bi> 0, then bi= bi-1 - ti+ 1.
In either case, bi bi-1 - ti+ 1, and the potential difference is
-
7/31/2019 12 Analisis Amortized
28/30
Example: binary counter (contd)
5/28/2012Jurusan Teknik Informatika FTIF ITS28
The amortized cost is therefore
If the counter starts at zero, then (D0) = 0. Since
(Di) 0 for all i, the total amortized cost of asequence of nINCREMENT operations is an upperbound on the total actual cost, and so the worst-casecost of nINCREMENT operations is O(n).
-
7/31/2019 12 Analisis Amortized
29/30
Comparing the accounting and potential
methods
5/28/2012Jurusan Teknik Informatika FTIF ITS29
The accounting method charges each operation acertain amount according to its type (e.g., insert ordelete), putting the focus on each operation'sprepayment to offset future expensive operations.
The potential method instead puts the focus on theeffect of a particular operation at a particular point intime, based on the effects of the operation on the
data structure and the corresponding change inpossibility for future operations to incur expense.
-
7/31/2019 12 Analisis Amortized
30/30
Comparing the accounting and potential
methods
5/28/2012Jurusan Teknik Informatika FTIF ITS30
The accounting method stores credit in particular
areas of a data structure, such as nodes of a tree oritems in a stack, whereas the potential methodgenerally considers properties of the entire datastructure (such as how many nodes are in a tree, orhow many items are in the stack) to compute thepotential.