the algorithm of garsia and wachs

59
The algorithm of Garsia and Wachs Presentation by a more recent proof of Karpinski et al.

Upload: mason

Post on 17-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

The algorithm of Garsia and Wachs. Presentation by a more recent proof of Karpinski et al. . w i. The problem. We will identify the items with their weights w 1 , w 2 . . . . w n , w i ≥ 0 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The algorithm of Garsia and Wachs

The algorithm of Garsia and Wachs

Presentation by a more recent proof of Karpinski et al.

Page 2: The algorithm of Garsia and Wachs

2

We will identify the items with their weights w1, w2 . . . . wn , wi ≥ 0

We are after a binary tree with w1, w2 . . . . wn at the leaves from left to right such that

wi depth(i) is minimized

The problem

Page 3: The algorithm of Garsia and Wachs

3

If we new the di’s (depths) then we could build the tree easily in O(n) time

Observation 1

2

3

4 4 13

2

1

Page 4: The algorithm of Garsia and Wachs

4

So we will focus on how to find the depths

Observation 1 (Cont)

2 3 4 4 1

3

2

1

Page 5: The algorithm of Garsia and Wachs

5

Definitions

1)TwoSum( ii wwi

2 if )1TwoSum()TwoSum(

and

1 if )TwoSum()1TwoSum(

:if (LMP) a is )1,(

n-iii

iii-

imal pairlocaly minii

Page 6: The algorithm of Garsia and Wachs

6

The algorithm

13 8 5 7 6 9 14

12

13 8

5 7

6 9 1412

14

13

85 7 6

9 1412 14

21 13 12 13 15 23

Page 7: The algorithm of Garsia and Wachs

7

The algorithm (in words)

Combine an LMP put the resulting node right before

the first node to the right which is larger or equal

Claim: The depths defined by the resulting tree are the depths of an optimal alphabetic tree.

Ok, great, suppose we believe it, how do we implement this efficiently ?

Page 8: The algorithm of Garsia and Wachs

8

Implementation (Bob Tarjan)

We will always combine the rightmost LMP.

Maintain the current list of weights broken into sublists and singletons. The following invariant should hold

• In a list of length at least 3: aj ≤ aj+2

• In a list of length at least 2 the next to last element is smaller than the element following the list.

kaaa ,....,, 21

2 jj aa

'21 ,......,, kbbb

Page 9: The algorithm of Garsia and Wachs

9

Where can we have LMP

All pairs in a list except the leftmost are not LMPs.

If the list is of length at least 2 then the

Rightmost element and the following on are not LMP.

Page 10: The algorithm of Garsia and Wachs

10

Implementation (Cont)

We pick the last two lists A and B and check whether b1

and b2 are LMP. If not we simply catenate A and B and repeat.

Otherwise, we delete b1 and b2 from B and combine them to create b’.

We search for the first item in B’ which is no smaller than b’

We split B’ just before that item that item to B1 and B2 and add b’ to the end of B1

Page 11: The algorithm of Garsia and Wachs

11

Implementation (Cont)

We represent sublists as search tree

1 and of maximum theis, ofkey The jjj aaa

kaaa ,....,, 21

2 jj aa

'21 ,......,, kbbb

Page 12: The algorithm of Garsia and Wachs

12

Implementation (Cont)

13 8 5 7 6 9 14

7 6 9 145

5 7 7 9 14

We can find the first item from the left that is greater than or equal to some value in logarithmic time

813

Page 13: The algorithm of Garsia and Wachs

13

Implementation (Cont)

7 6 9 145

5 7 7 9 14

813

6 9 14

6 9 14

813

75

13

7

6 9 14

5

6 9 14

813 13

Page 14: The algorithm of Garsia and Wachs

14

Implementation

Each combination of LMP triggers a constant number of search tree operations. (2 deletion, search, split, insertion)

Number of sublists is O(n), since there are n items and n-1 combinations of LMPs. Therefore the number of catenations is O(n)

Total running time is therefore O(nlogn)

Page 15: The algorithm of Garsia and Wachs

15

But why does this algorithm produce the optimal tree ??

Page 16: The algorithm of Garsia and Wachs

16

Notation

jj ii

ii

1 andbetween inserted be tois 1 and

combiningby obtain weitem new The

LMPan is )1,(

Page 17: The algorithm of Garsia and Wachs

17

Definitions (Cont)

njiijii

jii

,...,1,1,,,...,2,1,...,1'

ofright the to1 and position in items e th

movingby from created sequence theis '

21

iff )( are and 2121

TT dd

TTTT

equivalent level

Page 18: The algorithm of Garsia and Wachs

18

Theorem 1 (correctness of GW)

TT,...,nπ

T

i,iπ

T

such that 1 sequence

original over the treealphabetic optimalan is Then there

siblings.

are 1in which ' sequence over the

treealphabetic optimalan be Let

By induction on the length of the sequence, correctness reduces to:

Page 19: The algorithm of Garsia and Wachs

19

We will prove more

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

Page 20: The algorithm of Garsia and Wachs

20

lexi trees over π

opt

A

lexi trees over π such that i, i+1, are at the same depth

opt

Page 21: The algorithm of Garsia and Wachs

21

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are siblings

Page 22: The algorithm of Garsia and Wachs

22

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

lexi trees over π ‘ such that i, i+1, are at the same depth

opt

lexi trees over π ‘ such that i, i+1, are siblings

B

Page 23: The algorithm of Garsia and Wachs

23

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

lexi trees over π ‘ such that i, i+1, are at the same depth

opt

C

lexi trees over π ‘ such that i, i+1, are siblings

Page 24: The algorithm of Garsia and Wachs

24

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are siblings

C

Page 25: The algorithm of Garsia and Wachs

25

Theorem 1 (correctness of GW)

TT,...,nπ

T

i,iπ

T

such that 1 sequence

original over the treealphabetic optimalan is Then there

siblings.

are 1in which ' sequence over the

treealphabetic optimalan be Let

Page 26: The algorithm of Garsia and Wachs

38

lexi trees over π

opt

A

lexi trees over π such that i, i+1, are at the same depth

opt

Page 27: The algorithm of Garsia and Wachs

39

Proof of A

).1()( suppose

done. are then welevel same on the are 1i and i If

tree.lexi optimalan is T Assume

T

idepthidepth T

There is an optimal lexi tree in which i, and i+1 are at the same level.

Page 28: The algorithm of Garsia and Wachs

40

i

i+1

i+2

i i+1 i+2

LeftShift(i,i+1)

T T`

.1 perform 1e If

ioncontradict a

Since 2

)(i,iRightShift)(idepth(i)pthd

)Tcost(cost(T)

pp

TT

ii

Page 29: The algorithm of Garsia and Wachs

41

lexi trees over π

opt

lexi trees over π such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are at the same depth

opt

lexi trees over π’ such that i, i+1, are siblings

B

Page 30: The algorithm of Garsia and Wachs

42

Proof of B

i i+1

i i+1

LeftShift(i,i+1)

T T`

j+1

j+1

( ) ( )cost T cost T

Among optimal trees over π’ in which i, and i+1 are on the same level there is one in which they are siblings.

So T’ is also optimal

Page 31: The algorithm of Garsia and Wachs

43

Definition of Well Shaped Segments

- Lowest Common Ancestor of( , nd ) aT uLCA u v v

1

2

3 4

5

6 7

8

9

10 11

12

0

1

2

3

h = 4

5

6

A set S of leaves of T is h-isolated iff:

1. For any uS, depthT(u) ≥ h

2. For any uS, wS, depthT(LCA(u,w)) ≤ h

Page 32: The algorithm of Garsia and Wachs

44

Definition of Well Shaped Segments

1

2

3 4

5

6 7

8

9

10 11

12

0

1

2

3

h = 4

5

6

Active Window

S=[i,…j] is left well shaped iff it is h-isolated and depthT(i) =depthT(i+1) = h+1

S=[i,…j] is right well shaped iff it is h-isolated and depthT(j) =depthT(j+1) = h+1

Page 33: The algorithm of Garsia and Wachs

45

Movability Lemma

If the segment [i,…,j] is left well shaped, then the active pair(i,i+1) can be moved to the other side of the segment by locally rearranging sub-trees in the active window without changing the relative order of the other items and without changing the depthfunction of the tree.

(similar lemma holds for segments which are right well shaped)

Page 34: The algorithm of Garsia and Wachs

46

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 35: The algorithm of Garsia and Wachs

47

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 36: The algorithm of Garsia and Wachs

48

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 37: The algorithm of Garsia and Wachs

49

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 38: The algorithm of Garsia and Wachs

50

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 39: The algorithm of Garsia and Wachs

51

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

1

Page 40: The algorithm of Garsia and Wachs

52

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

1

Page 41: The algorithm of Garsia and Wachs

53

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6 6 7

8 10 11

1

3 4

Page 42: The algorithm of Garsia and Wachs

54

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

1

Page 43: The algorithm of Garsia and Wachs

55

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

1

Page 44: The algorithm of Garsia and Wachs

56

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6 6 7

8 10 11

1

3 4

Page 45: The algorithm of Garsia and Wachs

57

Movability Lemma

1

2 5

9

12

0

1

2

3

h = 4

5

6 6 7

8 10 11

1

3 4

Page 46: The algorithm of Garsia and Wachs

58

The main theorem to establish C

THM:

(a) if T is optimal over π such that i, i+1 are at the same depth then the segment [i,….,j] is left well shaped in T

(b) if T’ is optimal over π’ such that i, i+1 are at the same depth then the segment [i+2,….,j,i,i+1] is right well shaped in T

Focus on (a)

Page 47: The algorithm of Garsia and Wachs

59

Proof

First we have to show that every leaf u[i,…,j], depth(u) ≥ h

where h = depth(i)-1= depth(i+1)-1

Page 48: The algorithm of Garsia and Wachs

60

1

2 5

9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 49: The algorithm of Garsia and Wachs

61

1

2 5

9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 50: The algorithm of Garsia and Wachs

62

1

2 5

9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 51: The algorithm of Garsia and Wachs

63

1

2

5 9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 52: The algorithm of Garsia and Wachs

64

1

2

5 9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 53: The algorithm of Garsia and Wachs

65

1

2 5 9

0

1

2

3

h = 4

5

6

3 4

6 7

8

10 111

Page 54: The algorithm of Garsia and Wachs

66

Homework

To finish we have to consider one more case where i, and i+1 are siblings.

We also have to prove that depthT(LCA(j,j+1)) ≤ h

Case (b) of the THM is similar

Page 55: The algorithm of Garsia and Wachs

67

Hu-Tucker Algorithm

Transparent items and opaque items

Compatible pair – No opaque items in the middle

Minimal compatible pair (mcp) – compatible pair (i,i+1) whereWeight(i) + weight(i+1) is minimal

2018 14 12 1617 26 13 19

Tie Breaking Rule

Page 56: The algorithm of Garsia and Wachs

68

Hu-Tucker Algorithm

( );

0;

find any of

create a new item v whose weight is ;

replace by the new item and delete ;

);

);

u w

GW

π v

level[v]

(u,w)

p p

u v w

Move(v,π

GW(π

le

procedure

if then

else begin

lmp

1;

;

vel[u] : level[w] : level[v] end

Page 57: The algorithm of Garsia and Wachs

69

Hu-Tucker Algorithm GWprocedure

( );

0;

find any of

create a new item v whose weight is ;

replace by the new item and delete ;

);

u w

π v

level[v]

(u,w)

p p

u v w

Move(v,π

T

GW

H

if then

else begin

lmp

);

1;

;

level[u] : level[w] : level

H

[v]

T

end

Page 58: The algorithm of Garsia and Wachs

70

Hu-Tucker Algorithm GWprocedure

( );

0;

fin an d y

π v

level[v

H

]

T

if then

else begin

lmp of

create a new item v whose weight is ;

replace by the new item and delete ;

);

u w

(u,w)

p p

u v w

Move

mcp

(v,π

GW

);

1;

;

level[u] : level[w] : level

H

[v]

T

end

Page 59: The algorithm of Garsia and Wachs

71

Hu-Tucker Algorithm GWprocedure

( );

0;

fin an d y

π v

level[v

H

]

T

if then

else begin

lmp of

create a new item v whose weight is ;

replace by the new item and delete ;

)

u w

(u,w)

p

Mo

mcp

ve(

p

u v w

v,π

make transp ;

a

re

nt

v

GW );

1;

;

level[u] : level[w] : level

H

[v]

T

end