the algorithm of garsia and wachs
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 PresentationTRANSCRIPT
The algorithm of Garsia and Wachs
Presentation by a more recent proof of Karpinski et al.
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
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
4
So we will focus on how to find the depths
Observation 1 (Cont)
2 3 4 4 1
3
2
1
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
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
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 ?
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
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.
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
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
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
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
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)
15
But why does this algorithm produce the optimal tree ??
16
Notation
jj ii
ii
1 andbetween inserted be tois 1 and
combiningby obtain weitem new The
LMPan is )1,(
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
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:
19
We will prove more
lexi trees over π
opt
lexi trees over π such that i, i+1, are at the same depth
opt
20
lexi trees over π
opt
A
lexi trees over π such that i, i+1, are at the same depth
opt
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
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
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
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
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
38
lexi trees over π
opt
A
lexi trees over π such that i, i+1, are at the same depth
opt
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.
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
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
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
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
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
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)
46
Movability Lemma
1
2 5 9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
47
Movability Lemma
1
2 5 9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
48
Movability Lemma
1
2 5 9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
49
Movability Lemma
1
2 5 9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
50
Movability Lemma
1
2 5 9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
51
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
1
52
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
1
53
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6 6 7
8 10 11
1
3 4
54
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
1
55
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6
3 4
6 7
8 10 11
1
56
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6 6 7
8 10 11
1
3 4
57
Movability Lemma
1
2 5
9
12
0
1
2
3
h = 4
5
6 6 7
8 10 11
1
3 4
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)
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
60
1
2 5
9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
61
1
2 5
9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
62
1
2 5
9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
63
1
2
5 9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
64
1
2
5 9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
65
1
2 5 9
0
1
2
3
h = 4
5
6
3 4
6 7
8
10 111
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
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
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
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
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
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