8/6/2019 algo10
1/38
10 -1
Chapter 10
Amortized Analysis
8/6/2019 algo10
2/38
10 -2
A sequence of operations: OP1, OP2, OPm
OPi : several pops (from the stack) andone push (into the stack)
ti : time spent by OPithe average time per operation:
An example push and pop
!
!m
i
it
m 1ave
1t
8/6/2019 algo10
3/38
10 -3
Example: a sequence of push and pop
p: pop , u: push
i 1 2 3 4 5 6 7 8OPi 1u 1u 2p 1u 1u 1u 2p 1p
1u 1u 1u
ti 1 1 3 1 1 1 3 2
tave = (1+1+3+1+1+1+3+2)/8
= 13/8
= 1.625
8/6/2019 algo10
4/38
10 -4
Another example: a sequence of pushand pop
p: pop , u: push
tave = (1+2+1+1+1+1+6+1)/8
= 14/8
= 1.75
i 1 2 3 4 5 6 7 8
OPi 1u 1p 1u 1u 1u 1u 5p 1u
1u 1u
ti 1 2 1 1 1 1 6 1
8/6/2019 algo10
5/38
10 -5
Amortized time and potential function
ai = ti + * *i i 1ai : amortized timeofO i
*i :potentialf nctionofthe stackafterO i
* *i i 1: changeofthe potential
a tii
m
ii
m
ii
m
i! ! !
! 1 1 1
1( )* *
! !
t ii
m
m1
0* *
If * *m 0 u 0, then a ii
m
!
1
represents an upper
boundof t ii
m
!
1
8/6/2019 algo10
6/38
10 -6
Suppose that before we execute Opi , there are kelements in the stack and Opi consists of n pops and1 push.
Amortized analysis of thepush-and-pop sequence
0haveWe
stactheine e entsof#:
0u**
*
m
i
2
)1()1(
1Then,
1
1
1
!
!
**!
!*
!
!*
-kk-nn
ta
nk
nt
k
iiii
i
i
i
8/6/2019 algo10
7/38
10 -7
By observation, at most m pops and mpushes are executed in m operations. Thus,
2.tThen,
2/)(haveWe
ave
1
e
!!
m
i
ima
2.tave e
8/6/2019 algo10
8/38
10 -8
Skew heaps
1
50
13 20
10
16 25
5
12
30
19
40 14
Two skew heaps
Step 1: Merge the right paths.
5 right heavy nodes: yellow
meld: merge + swapping
8/6/2019 algo10
9/38
10 -9
1
5
13
20
10
16
25
50
12
30
19
40
14
Step 2: Swap the children along the right path.
No right heavy node
8/6/2019 algo10
10/38
10 -10
Amortized analysis of skew heaps
meld: merge + swapping
operations on a skew heap:
find-min(h): find the min of a skew heap h.
insert(x, h): insert x into a skew heap h.
delete-min(h): delete the min from a skew heap h.
meld(h1, h2): meld two skew heaps h1 and h2.
The first three operations can be implementedby melding.
8/6/2019 algo10
11/38
10 -11
Potential function of skew heaps
wt(x): # of descendants of node x, includingx.
heavy node x: wt(x) " wt(p(x))/2, where
p(x) is the parent node of x.
light node : not a heavy node
potential function *i: # of right heavy nodes
of the skew heap.
8/6/2019 algo10
12/38
10 -12
light nodese log2n
# heavy= 3e log2n
possible heavy nodes# of nodes: n
Any path in an n-node tree contains atmost
log2n light nodes.
The number ofright heavy nodes attached to
the left path is atmost log2n .
8/6/2019 algo10
13/38
10 -13
Amortized time
heap: h1
# of nodes: n1
heap: h2
# of nodes: n2
# lighte log2n1# heavy= k1
# lighte log2n2# heavy= k2
8/6/2019 algo10
14/38
10 -14
ai=ti+*i*i-1
ti : timespent by OPitie 2+ log2n1+k1+ log2n2+k2
(2 countsthe roots of h1and h2)
e 2+2 log2n+k
1+k2where n=n1+n2
*i*i-1= k3-(k1+k2) e log2nk1k2ai=ti+*i*i-1
e2+2 log2n+k1+k2+ log2nk1k2=2+3 log2n
ai= O(log2n)
8/6/2019 algo10
15/38
10 -15
AVL-treesheight balance of node v:hb(v)= (height of right subtree) (height of left subtree)
The hb(v) of every node never differ by more than 1.
M
I O
E K QN
G LC J P R
B D F
-1
-1
-1
0
0
0 0 0
0
0
0
0
00
0
+1
Fig. An AVL-Tree with Height Balance Labeled
8/6/2019 algo10
16/38
10 -16
Add a new node A.
Before insertion, hb(B)=hb(C)=hb(E)=0
hb(I){0 the first nonzero from leaves.
8/6/2019 algo10
17/38
10 -17
Amortized analysis of AVL-trees
Consider a sequence ofm insertions on anempty AVL-tree.
T0: an empty AVL-tree.
Ti: the tree after the ith insertion.
Li: the length of the critical path involved in the ithinsertion.
X1: total # of balance factor changing from 0 to +1or -1 during these m insertions (the total cost for
rebalancing)
X1= Lii
m
!
1
, we wantto find X1.
Val(T): # ofunbalanced node in T
(height balance{0)
8/6/2019 algo10
18/38
10 -18
Case 1Case 1 : Absorption
Val(Ti)=Val(Ti-1)+(Li1)
The tree height is not increased, we need notrebalance it.
8/6/2019 algo10
19/38
10 -19
Case 2.1Case 2.1 single rotation
8/6/2019 algo10
20/38
10 -20
Case 2Case 2 : Rebalancing the tree
D
B F
E GA C
D
B
F
E
G
A C
D
B
F
E G
A
C
left
rotation
right
rotation
8/6/2019 algo10
21/38
10 -21
Case 2.1Case 2.1 single rotation
After a right rotation on the subtree rooted atD:
Val(Ti)=Val(Ti-1)+(Li-2)
8/6/2019 algo10
22/38
10 -22
Case 2.2Case 2.2 double rotation
8/6/2019 algo10
23/38
10 -23
Case 2.2Case 2.2 double rotation
Val(Ti)=Val(Ti-1)+(Li-2)
After a left rotation on the subtree rooted atB and a right rotation on the subtree rootedat F:
8/6/2019 algo10
24/38
10 -24
Case 3Case 3 : Height increase
Val(Ti)=Val(Ti-1)+Li
Li is the height of the root. 0
-1
-1
-1
root
8/6/2019 algo10
25/38
10 -25
Amortized analysis of X1X2: # ofabsorptions in case1
X3: # ofsingle rotations in case 2
X4: # of double rotations in case 2
X5: # of height increases in case 3Val(Tm) = Val(T0)+ Li
i
m
!
1
X22(X3+X4)
=0+X1X22(X3+X4)
Val(Tm) e0.618m (proved by Knuth)X1= Val(Tm)+2(X2+X3+X4)X2e0.618m+2m= 2.618m
8/6/2019 algo10
26/38
10 -26
A self-organizing sequentialsearch heuristics
3 methods for enhancing the performance ofsequential search
(1) Transpose Heuristics:Query Sequence
B B
D D B
A D A B
D D A B
D D A B
C D A C B
A A D C B
8/6/2019 algo10
27/38
10 -27
(2) Move-to-the- ront euristics:uery equence
8/6/2019 algo10
28/38
10 -28
(3) ount euristics:( ecreasing order bythecount)uery equence
8/6/2019 algo10
29/38
10 -29
Analysis of the move-to-the-
front heuristics interword comparison: unsuccessful
comparison
intraword comparison: successful comparison pairwise independent property:
For any sequence S and all pairs P and Q, # ofinterword comparisons of P and Q is exactly # of
interword comparisons made for the subsequenceof S consisting of only Ps and Qs.
(See the example on the next page.)
8/6/2019 algo10
30/38
10 -30
e.g.Query Sequence (A, B) comparison
C CA A C
C C A
B B C A
C C B AA A C B
# ofcomparisons made between A and B: 2
Pairwise independent
property in move-to-the-front
8/6/2019 algo10
31/38
10 -31
Considerthesubsequenceconsisting of A and B:
Query Sequence (A, B) comparisonA A
B B A
A A B
# ofcomparisons made between A and B: 2
8/6/2019 algo10
32/38
10 -32
Query equence
(A, ) 0 1 1
(A, ) 0 1 1 0 1
(B, ) 0 0 1 1
0 1 1 2 1 2
hereare 3 distinctinter ord co parisons:
(A, B), (A, ) and (B, )
We can consider them separately and thenadd them up.
the total number of interword comparisons:
0+1+1+2+1+2 = 7
8/6/2019 algo10
33/38
10 -33
Theorem for the move-to-the-
front heuristicsCM(S): # of comparisons of the move-to-the-
front heuristics
CO(S): # of comparisons of the optimal staticordering
CM
(S)e 2CO(S)
8/6/2019 algo10
34/38
10 -34
Proof
Proof:
InterM(S): # of interword comparisons of the move tothe front heuristics
InterO(S): # of interword com
parisons of the optim
alstatic ordering
Let S consist of a As and b Bs, a b.
The optimal static ordering: BA
InterO(S) = aInterM(S) e 2a
InterM(S) e 2InterO(S)
8/6/2019 algo10
35/38
10 -35
Consider any sequence consisting ofmore than twoitems. Because of the pairwise independent property,we have InterM(S) e 2InterO(S)
IntraM(S): # of intraword comparisons of the move-to-the-front heuristics
IntraO(S): # of intraword comparisons of the optimalstatic ordering
IntraM(S) = Intra
O(S)
InterM(S) + IntraM(S) e 2InterO(S) + IntraO(S)
CM(S) e 2CO(S)
Proof(cont.)
8/6/2019 algo10
36/38
10 -36
The count heuristics
The count heuristics has a similar result:
CC(S) e 2CO(S), where CC(S) is the cost
of the count heuristics
8/6/2019 algo10
37/38
10 -37
The transposition heuristics
The transposition heuristics does not possess thepairwise independent property.
We can not have a similar upper bound for the cost
of the transposition heuristics.
e.g.onsider pairs ofdistinct items independently.Query equence A B A
(A, B) 0 1 1
(A, ) 0 1 1 0 1
(B, ) 0 0 1 1
0 1 1 2 1 2
# ofinter ord comparisons: 7 (notcorrect)
8/6/2019 algo10
38/38
10 -38
thecorrect interword comparisons:Query Sequence C A C B C A
Data Ordering C AC CA CBA CBA CAB
Number ofInterword
Comparisons0 1 1 2 0 2
6