correctness of constructing optimal alphabetic trees revisited

77
Correctness of Constructing Optimal Alphabetic Trees Revisited Marek Karpinski, Lawrence L. Larmore, Wojciech Rytter Theoretical computer science 180 (1997) 309-324

Upload: morwen

Post on 07-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Correctness of Constructing Optimal Alphabetic Trees Revisited. Theoretical computer science 180 (1997) 309-324. Marek Karpinski, Lawrence L. Larmore, Wojciech Rytter. Outline. Definitions General version of Garsia-Wachs (GW) algorithm Proof of GW Hu-Tacker (HT) algorithm - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Correctness of Constructing Optimal Alphabetic Trees Revisited

Correctness of Constructing Optimal Alphabetic Trees Revisited

Marek Karpinski,

Lawrence L. Larmore,

Wojciech Rytter

Theoretical computer science 180 (1997) 309-324

Page 2: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 2

Outline

Definitions General version of Garsia-Wachs (GW)

algorithm Proof of GW Hu-Tacker (HT) algorithm Proof of HT by similarity to GW

Page 3: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 3

Definitions

nipi 1 0 weights

npp ...n Permutatio 1

Binary tree: Every internal node has exactly two sons

n

iTi ipT

1

)(level)cost(

T in tree leafth - theofdepth theis )(level iiT

Page 4: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 4

Definitions

1)TwoSum( ii ppi

2 if )1TwoSum()TwoSum(

and

1 if )TwoSum()1TwoSum(

:if (lmp) a is )1,(

n-iii

iii-

imal pairlocaly minii

Page 5: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 5

The Move Operator

w

w

w

ofneighbor larger right

nearest theofr predecesso theis )(RightPos

: thenlist ain itemany is If

RPw

wwwwwww 7654321

)149675813(

Page 6: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 6

The Move Operator

wRP

wwwwwww 7625431

)149867513(

),RightPos(w of

right the to wmovingby Changes ),(

wmove

Page 7: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 7

The Move Operator

weight(v) t(u) weigh

leastat is weight whoseitemright

nearest theofr predecesso theis ),(RightPos

: thenlist ain itemsadjacent twoare v, If

vu

u

RPvu

wwwwwww 7654321

)149675813(

Page 8: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 8

The Move Operator

vuRP

wwwwwww 7326541

)145896713(

),RightPos(w of

right the to vandu movingby Changes ),,(

vumove

Page 9: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 9

Definitions

njiijii

jii

ji

ji

,...,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 levellevel

TTTT

equivalent level

Page 10: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 10

Theorem 1 (correctness of GW)

TT,...,nπ

T

i,iπT

j),iRightPos(i)(i,i

i,j

such that 1 sequence

original over the treealphabetic optimalan is Then there

siblings.

are 1in which sequence over the treea be let and

1 and a be 1Let pair minimallocaly

Page 11: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 11

Garsia-Wachs Algorithm

;

;1

);

);

; delete and item new by the replace

; is weight whose vitem new a create

of any find

;0

);GW(

end

begin else

thenif

procedure

level[v]level[w]:level[u]:

GW(π

Move(v,π

wvu

pp

(u,w)

level[v]

wu

lmp

Page 12: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 12

Definitions

level same at the are 1ii, and 1,...n

over treesalphabetic optimal all ofset - )(

iOPT

),iRightPos(ij

i,i

(i)OPT

ji

moved

1 ere wh

level same at the are 1 and

over treesalphabetic optimal all ofset -

,

versa viceand such that

is thereeach for iff

TT

TOPTOPTTTOPOPT

Page 13: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 13

Theorem 2

siblings. as 1 with treea contains (3)

. treealphabetic optimalan contains (2)

. (1)

Then .an be 1Let

i,iT(i)OPT

TOPT(i)

(i)OPTOPT(i)

)(i,i

moved

moved

lmp

Page 14: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 14

Shift Operations

Tin leaves ofsegment econsecutiv acover trees thoseand

2k ,...,T subtreesdisjoint of roots are ,...,v

iffcut a called is Tin nodes of ,...,v Sequence

11

1

kk

k

Tv

v

Page 15: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 15

Shift Operations

kk

k

i-i

k

vv

v

vvki

vu,v

vvuv

u,v

vvLeftShift

offather to of sibling of subtree move

delete

to of subtree move 2for

under put

to of subtree and , to of subtree move

nodes new Two

),...,(

1

1

21

1

waysame in the Defined

),...,( 1 kvvRightShift

Page 16: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 16

LeftShift Example

201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10 11 12

13 14 15 16

Page 17: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 17

LeftShift Example

43 201 2 17 18 19

1v2v

3v4v 5v

43 5 6 7 8

9 10 11 12

13 14 15 16

u

Page 18: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 18

LeftShift Example

5 6 7 8

9 10 201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10

11 12

13 14 15 16

u v

Page 19: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 19

LeftShift Example

201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10

11 12

13 14 15 16

u v

11 12

Page 20: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 20

LeftShift Example

201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10

13 14

15 16

u v

11 12

13 14

Page 21: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 21

LeftShift Example

201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10

13 14

15 16

u v

11 12

15 16

Page 22: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 22

LeftShift Example

201 2 17 18 19

1v2v

3v4v 5v

43

5 6 7 8

9 10

13 14u v

11 12

15 16

Page 23: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 23

LeftShift Example

201 2 17 18 19

1v2v

3v

43

5 6 7 8

9 10

13 14u v

11 12

15 16

15 16

Page 24: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 24

LeftShift Example

201 2 17 18 19

1v2v

3v

43

5 6 7 8

9 10

13 14u v

11 12

15 16

Page 25: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 25

Theorem 2

siblings. as 1 with treea contains (3)

. treealphabetic optimalan contains (2)

. (1)

Then .an be 1Let

i,iT(i)OPT

TOPT(i)

(i)OPTOPT(i)

)(i,i

moved

moved

lmp

Page 26: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 26

Proof of Point 2 in Theorem 2

. treealphabetic optimalan contains (2) TOPT(i)

oneleast at by decreases )1(level

increasenot does )1(level

one.by increases )(level

After this 1).ii,LeftShift( performcan Then we

).1()(level suppose So

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

tree.alphabetic optimalan is T Assume

T

T

T

T

i

i

i

ileveli T

Page 27: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 27

Proof of Point 2 in Theorem 2

i

i+1

i+2

i i+1 i+2

LeftShift(i,i+1)

T T`

QED .1 perform 1 If

siblings are 1 and Thus

Since 2

)(i,iRightShift)(ilevel(i)level

i,iOPT(i)T

)Tcost(cost(T)

pp

TT

ii

Page 28: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 28

Proof of Point 3 Theorem 2

( )movedT OPT iIf and 1 are not siblings then:i i

i i+1

i+2

i i+1

i+2LeftShift(i,i+1)

T T`

( )movedT OPT i

( ) ( )cost T cost T

(3) contains a tree with 1 as siblingsmovedOPT (i) T i,i

Page 29: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 29

Definition of Well Shaped Segments

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

set of leaves of is iff:

1. For any , ( ) .

2. For any and , ( ( , ))T

T

S T

u S level u h

u S w S level LC

h isola

A u w

d

h

te

1

2

3 4

5

6 7

8

9

10 11

12

0

1

2

3

h = 4

5

6

Page 30: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 30

Definition of Well Shaped Segments

[ ,..., ]S i j is iff ( ) ( 1)T TS level i level i left well shaped

is iff ( ) ( 1)T TS level j level j right well shaped

1

2

3 4

5

6 7

8

9

10 11

12

0

1

2

3

h = 4

5

6

Active Window

Page 31: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 31

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 levelfunction of the tree.

Page 32: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 32

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 33: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 33

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 34: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 34

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 35: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 35

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 36: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 36

Movability Lemma

1

2 5 9

12

0

1

2

3

h = 4

5

6

3 4

6 7

8 10 11

Page 37: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 37

Theorem 3

Assume ( , 1) is an , ( , 1),

( ), and ( ).

Then:

the segment [ 2,..., , , 1] is right well shaped in

the segment [ ,..., ] is left well shaped in

moved

i i j RightPos i i

T OPT i T OPT i

i j i i T

i j T

1.

2.

lmp

Page 38: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 38

Point 1 in Theorem 2

( ) ( )movedOPT i OPT i

Page 39: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 39

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 40: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 40

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 41: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 41

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 42: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 42

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 43: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 43

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

Page 44: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 44

GW` Algorithm

gmp – Globaly Minimal Pair

GW` - the same as GW but always choose gmp instead of some other lmp.

Page 45: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 45

Definitions

Special sequence – sequence of weights, each one is either transparent or opaque

Normal sequence – sequence of weights

MoveTransparent operator – converts a special sequence into a normal sequence and moves all transparent items to their RightPos. (first it moves the rightmost item, then the one to its left, etc…)

Page 46: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 46

MoveTransparent

2018 14 12 1617 26 13 19

Page 47: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 47

MoveTransparent

2018 14 12 1617 26

13

19

Page 48: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 48

MoveTransparent

2018 14 12 1617 26 13 19

Page 49: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 49

MoveTransparent

2018 14 12 1617

26

13 19

Page 50: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 50

MoveTransparent

2018 14 12 1617

26

13 19

Page 51: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 51

MoveTransparent

2018 14 12 1617

26

13 19

Page 52: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 52

MoveTransparent

2018 14 12 1617 2613 19

Page 53: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 53

MoveTransparent

2018 14 12 16

17

2613 19

Page 54: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 54

MoveTransparent

2018 14 12 16

17

2613 19

Page 55: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 55

MoveTransparent

2018 14 12 16

17

2613 19

Page 56: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 56

MoveTransparent

2018 14 12 16 17 2613 19

Page 57: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 57

MoveTransparent

20

18 14 12 16 17 2613 19

Page 58: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 58

MoveTransparent

20

18 14 12 16 17 2613 19

Page 59: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 59

MoveTransparent

20

18 14 12 16 17 2613 19

Page 60: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 60

MoveTransparent

2018 14 12 16 17 2613 19

Page 61: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 61

MoveTransparent

18 14 12 16 1913 17 20 26

Page 62: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 62

The Simulation Lemma

iLet be the working sequence of items after the -th iteration

of the algorithm, and let be the working sequence of items

after the -th iteration of the algorithm. For 0 -1 let

( , )

i

i i

i

HT

i GW i n

u w

be the globaly minimal pair in and ( , ) be the mcp

in then:

for each 1 -1

( ) and ( , ) ( , )

i i i

i

u w

i i i i i ii n

MoveTransparent u w u w

Assuming there are no ties

Page 63: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 63

Proof of the Simulation Lemma

Assume that ( ). Then if ( , ) is the globally

minimal pair in , the items , are visible to each other in .

MoveTransparent u w

u w

Claim A

Claim B

Assume that ( ). Then if ( , ) is the minimal

compatible pair in , the items , are adjacent in .

MoveTransparent u w

u w

Page 64: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 64

Proof of Claim A

By contradiction assume that w,u are not visible to each other.This means there is an opaque item q between them.Two cases:

Case 1: in is before w u

is transparent because it "floats over" , ( ) ( )w u weight u weight w

..... ... ... ... ....... .......w uq uw

..... ... ... ... ....... .......uw q uw

Page 65: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 65

Proof of Claim A

let be the predecessor of in q u

( ) ( ) because ( , ) is gmp.weight q weight w u w

If would be transparent it would "float over" and .

Thus it is opaque.

q u w

..... ... ... ... ...... .......w u wq q u

Since cannot "float over" , must be to the left of w q q w

.. .. ... ... ... ...... .......q q u uww q

Contradiction – no place for q

Page 66: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 66

Proof of Claim A

Case 2: in is before u w

is transparent because it "floats over" u q

..... ... ... ... ....... .......u wq uw

..... ... ... ... ....... .......wu q uw

( ) ( ) since u does not float over weight u weight w w

Page 67: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 67

Proof of Claim A

let be the predecessor of in q u

( ) ( ) because ( , ) is gmp.weight q weight w u w

If would be transparent it would "float over" and .

Thus it is opaque.

q u w

Since cannot "float over" , must be to the left of u q q u

Contradiction – no place for q

..... ... ... ... ...... .......u w wq q u

.. .. ... ... ... ...... .......q q w uwu q

Page 68: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 68

Proof of Claim B

Claim BAssume that ( ). Then if ( , ) is the minimal

compatible pair in , the items , are adjacent in .

MoveTransparent u w

u w

ProofAssume there is an item between and in q u w

..... ...... ... ....... .. .. .......u w u q w

Page 69: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 69

Proof of Claim B

The order of movements is from right to left, thus w isProcessed first then the items between u and w, and then u.At this point u and w must be adjacent, thus q must be processedlater, and thus it is to the left of u.

... ... .... ... ....... .. .. .......q u w u q w

( ) ( ) because stopped before .weight q weight w q w

q is not visible from u because (u,w) is mcp

Page 70: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 70

Proof of Claim B

Let q` be the opaque item between q and u visible from u

... .. .. .... ... ....... .. .. .......u w u q wq q

( ) ( ) because floated over .

( ) ( ) because stopped before .

thus is a better pair for then .

weight q weight q q q

weight q weight w q w

q u w

Contradiction

Page 71: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 71

Conclusion

if ( ), ( , ) is in

and ( , ) is in

then

( , ) ( , )

MoveTransparent u v

u v

u v u v

gmp

mcp

Page 72: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 72

Claim C

if ( ) then

( ( )) ( )

MoveTransparent

MoveTransparent HT GW

( )

( ) (

)

)

(

...... .......... ....... ...... ......

...... _ .......... ....

..

..._ ...... .

..

.... .....

mcp gmp

u w

HT GW

u w

u w uw

Page 73: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 73

Proof of claim C

( )

(

( )

) ( )

...... .......... ....... ...... ......

......_ _ .......... .......__ ...... ......

...... _ .......... .......__

...

...... ......

.

....

....u w

mcp gmp

u w

HT GW

u w uw

Page 74: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 74

Proof of Simulation Lemma

The proof of the lemma is by induction

0 0( )MoveTransparent

assume ( )i iMoveTransparent

1

1

( ) ( ( ))

( )i i

i i

MoveTransparent MoveTransparent HT

GW

Page 75: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 75

Tie Braking Rule

Theorem 4The Tie Breaking Rule (TBR) is correct

2weights in arithmetics.

( , ) ( , ) ( , )

( , ) ( , ) (( ) ( ))

zero element (0,0)

a b c d a c b d

a b c d a c or a c and b d

Proof

Page 76: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 76

Proof of TBR

Case 1: All weights are strictly positive

1

1

2 1 2 2 21 2

let ,..., the original sequence

_ ( ) ,...,

, 2 , , 2 ,..., , 2

n

n

n n n nn

p p

de tie p p

p p p

No ties are possible in over

C

_ (

laim A

)

HT de tie

Page 77: Correctness of Constructing Optimal Alphabetic Trees Revisited

18/12/2000 77

Proof of TBR

For each 0 , and ( , ) (

Claim B

, )i i i i i ii n u w u w

Case 2: Some of the original weights are zero

1i iif p 0 set p 0,2i