39
10 -1 Chapter 10 Amortized Analysis

10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -1

Chapter 10

Amortized Analysis

Page 2: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -2

A sequence of operations: OP1, OP2, … OPm

OPi : several pops (from the stack) and

one push (into the stack) ti : time spent by OPi

the average time per operation:

An Example – Push and Pop

m

iitm 1

ave

1t

Page 3: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -3

Example: a sequence of push and pop

p: pop , u: push i 1 2 3 4 5 6 7 8

OPi 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

Page 4: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -4

Another example: a sequence of push and 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

Page 5: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -5

Amortized Time and Potential Function

ai = ti + i i 1 ai : amortized time of OPi

i: potential function of the stack after OPi i i 1: change of the potential

a tii

m

ii

m

ii

m

i

1 1 1

1( )

ti

i

m

m1

0

If m 0 0, then ai

i

m

1

represents an upper

bound of tii

m

1

Page 6: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -6

Suppose that before we execute Opi , there are k elements in the stack and Opi consists of n pops and 1 push.

Amortized Analysis of the Push-and-Pop Sequence

0 have We

stack thein elements of # :

0

m

i

2

)1()1(

1 Then,

1

1

1

-kk-nn

ta

nk

nt

k

iiii

i

i

i

Page 7: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -7

By observation, at most m pops and m pushes are executed in m operations. Thus,

2. tThen,

2/)( have We

ave

1

m

ii ma

2.tave

Page 8: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -8

Skew Heaps

Let wt(x) of node x denote the number of descendants of node x, including x. Let a heavy node x be a non-root node with wt(x) > wt(p(x)) / 2 where p(x) is the parent node of x. A node x is a light node if it is not a heavy node.

For example, 5 and 10 are heavy nodes.

Page 9: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -9

Skew Heaps

Two skew heaps

Step 1: Merge the right paths.

5 right heavy nodes

Meld: Merge + Swapping

Page 10: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -10

Step 2: Swap the children along the right path.

No right heavy node

Page 11: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -11

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 implemented by melding.

Page 12: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -12

Potential Function of Skew Heaps

wt(x): # of descendants of node x, including x.

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.

Page 13: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -13

light nodes log2n

# heavy=k3 log2npossible heavy nodes

# of nodes: n

Any path in an n-node tree contains at

most log2n light nodes.

The number of right heavy nodes attached to the left path is at most

log2n .

Page 14: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -14

Amortized Time

heap: h1

# of nodes: n1

heap: h2

# of nodes: n2

# light log2n1# heavy = k1

# light log2n2# heavy = k2

Page 15: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -15

ai = ti + i i-1 ti : time spent by OPi ti 2+ log2n1

+k1+ log2n2+k2

(“2” counts the roots of h1 and h2) 2+2 log2n+k1+k2

where n=n1+n2

i i-1 = k3-(k1+k2) log2n k1 k2

ai = ti + i i-1 2+2 log2n+k1+k2+ log2n k1

k2 =2+3 log2n

ai = O(log2n)

Page 16: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -16

AVL-Trees

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

Height balance of node v:hb(v)=height of right subtree - height of left subtree

Page 17: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -17

Add a new node A.

Before insertion, hb(B)=hb(C)=hb(E)=0

hb(I)0 the first nonzero from leaves.

M

I O

E K QN

G LC J P R

B D F

-2

-2

-1

0 0

0

0

0

0

00

0

+1

A

0

-1

-1

-1

Criticalnode

Criticalpath

Page 18: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -18

Amortized Analysis of AVL-Trees

Consider a sequence of m insertions on an empty AVL-tree. T0: an empty AVL-tree.

Ti: the tree after the i-th insertion.

Li: the length of the critical path involved in the ith insertion.

X1: total # of balance factor changing from 0 to +1 or -1 during these m insertions (rebalancing cost)X1=Lii

m

1 , we want to find X1.

Val(T): # of unbalanced node in T (height balance 0)

Page 19: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -19

Case 1 Case 1 : Absorption

newlyadded

Ti-1 Ti

1

0

0

0

0

-1

-1

-1

Li

Val(Ti)=Val(Ti-1)+(Li1)

The tree height is not increased, we need not rebalance it.

Page 20: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -20

Case 2.1Case 2.1 Single Rotation

Page 21: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -21

Case 2 Case 2 : Rebalancing the Tree

Page 22: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -22

Case 2.1Case 2.1 Single Rotation

After a right rotation on the subtree rooted at A:

Val(Ti)=Val(Ti-1)+(Li-2)

Page 23: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -23

Case 2.2Case 2.2 Double Rotation

A

B

F(n-1)E(n-1)

0

-1

D(n)

G(n)C

0

newlyadded

Page 24: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -24

Case 2.2Case 2.2 Double Rotation

C

B

E(n-1)

0

1

D(n) G(n)

A0

F(n-1)

Val(Ti)=Val(Ti-1)+(Li-2)

After a left rotation on the subtree rooted at B and a right rotation on the subtree rooted at A:

Page 25: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -25

Case 3 Case 3 : Height Increase

Val(Ti)=Val(Ti-1)+Li

Li is the height of the root.

Li

C(n) 0

0

newlyadded

0root

-1

-1

-1

root

Page 26: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -26

Amortized Analysis of X1

X2: # of absorptions in case 1 X3: # of single rotations in case 2 X4: # of double rotations in case 2 X5: # of height increases in case 3

Val(Tm) = Val(T0)+ Lii

m

1

X22(X3+X4)

=0+X1X2

2(X3+X4) Val(Tm) 0.618m (proved by Knuth) X1 = Val(Tm)+2(X2+X3+X4)X2 0.618m+2m = 2.618m

Page 27: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -27

A Self-Organizing Sequential Search

Heuristics 3 methods for enhancing the

performance of sequential 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

Page 28: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -28

(2) Move-to-the-Front Heuristics: Query Sequence

B B D D B A A D B D D A B D D A B C C D A B A A C D B

Page 29: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -29

(3) Count Heuristics: (decreasing order by the count) Query Sequence

B B

D B D

A B D A D D B A

D D B A

A D A B

C D A B C

A D A B C

Page 30: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -30

Analysis of 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, # of

interword comparisons of P and Q is exactly # of comparisons made for the subsequence of S consisting of only P’s and Q’s.

(See an example on the next page.)

Page 31: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -31

e.g.

Query Sequence (A, B) comparison C C A A C C C A B B C A C C B A A A C B

# of comparisons made between A and B: 2

Pairwise Independent Property in Move-to-the-

Front

Page 32: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -32

Consider the subsequence consisting of A and B: Query Sequence (A, B) comparison

A A B B A A A B

# of comparisons made between A and B: 2

Page 33: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -33

Query Sequence C A C B C A (A, B) 0 1 1 (A, C) 0 1 1 0 1 (B, C) 0 0 1 1

0 1 1 2 1 2

There are 3 distinct interword comparisons:

(A, B), (A, C) and (B, C)

We can consider them separately and then add them up.

The total number of interword comparisons is0 + 1 + 1+ 2 + 1 + 2 = 7.

 

Page 34: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -34

Theorem for Move-to-the- Front Heuristics

CM(S): # of comparisons of the move-to-the-front heuristics

CO(S): # of comparisons of the optimal static ordering

CM (S) 2CO(S)

Page 35: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -35

Proof

Proof: InterM(S): # of interword comparisons of the

move- to-the-front heuristics InterO(S): # of interword comparisons of the

optimal static ordering Let S consist of a A’s and b B’s, a b. The optimal static ordering: BA

InterO(S) = a

InterM(S) 2a

InterM(S) 2InterO(S)

Page 36: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -36

Consider any sequence consisting of more than two items. Because of the pairwise independent property, we have InterM(S) 2InterO(S)

  IntraM(S): # of intraword comparisons of the move-to-

the-front heuristics IntraO(S): # of intraword comparisons of the optimal

static ordering IntraM(S) = IntraO(S)  InterM(S) + IntraM(S) 2InterO(S) + IntraO(S)

CM(S) 2CO(S)

Proof (cont.)

Page 37: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -37

The Count Heuristics

The count heuristics has a similar result:

CC(S) 2CO(S), where CC(S) is the cost of the count heuristics.

Page 38: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -38

The Transposition Heuristics

The transposition heuristics does not possess the pairwise independent property.

We cannot have a similar upper bound for the

cost of the transposition heuristics. e.g.

Consider pairs of distinct items independently. Query Sequence C A C B C A (A, B) 0 1 1 (A, C) 0 1 1 0 1 (B, C) 0 0 1 1

0 1 1 2 1 2

# of interword comparisons: 7 (not correct)

Page 39: 10 -1 Chapter 10 Amortized Analysis. 10 -2 A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)

10 -39

The correct interword comparisons: Query Sequence C A C B C A Data Ordering C AC CA CBA CBA CAB Number of Interword Comparisons

0 1 1 2 0 2

6