59
Mergeable Trees 1 Mergeable Trees Mergeable Trees Robert E. Tarjan Princeton University and HP Labs work with Loukas Georgiadis, Haim Kaplan, Nira Shafrir and Renato W Partial results: Georgiadis, Tarjan, and Werneck, Design of data structures for mergeable trees, SODA 2006

Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Embed Size (px)

DESCRIPTION

Mergeable Trees Robert E. Tarjan Princeton University and HP Labs. Joint work with Loukas Georgiadis, Haim Kaplan, Nira Shafrir and Renato Werneck Partial results: Georgiadis, Tarjan, and Werneck, Design of data structures for mergeable trees, SODA 2006. TexPoint fonts used in EMF. - PowerPoint PPT Presentation

Citation preview

Page 1: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 1

Mergeable TreesMergeable Trees

Robert E. TarjanPrinceton University and HP Labs

Joint work with Loukas Georgiadis, Haim Kaplan, Nira Shafrir and Renato Werneck

Partial results: Georgiadis, Tarjan, and Werneck, Design of data structures for mergeable trees, SODA 2006

Page 2: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 2

Outline

1. The Problem

2. The Motivating Application

3. Dynamic Trees

4. Mergeable Trees via Dynamic Trees

5. Implicit Mergeable Trees

6. Mergeable Trees via Partition by Rank

7. Open Problems

Page 3: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 3

Mergeable Trees

• Goal: Maintain a forest of rooted trees.

• Queries: parent(v)

nca(v,w)

root(v)

Page 4: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 4

Mergeable Trees

• Goal: Maintain a forest of rooted trees.

• Queries: parent(v)

nca(v,w)

root(v)

• Trees are heap-ordered (root=smallest)

• Updates:

insert(v)merge(v,w) : merge the v-to-root and w-to-root paths,

preserving the heap order.

Page 5: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 5

Mergeable Trees

• Goal: Maintain a forest of rooted trees.

• Queries: parent(v)

nca(v,w)

root(v)

• Trees are heap-ordered (root=smallest)

• Updates:

insert(v)merge(v,w) : merge the v-to-root and w-to-root paths,

preserving the heap order.

• Other updates:

link(v,w) : make root v a child of node w in another tree = merge(v,w). cut(v) : make v a root by disconnecting from parent. delete(v) : delete leaf v = cut(v), discard v.

Page 6: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 6

Example

3 2

6 7 4

9

11

10

1

5

8

Page 7: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 7

Example

merge(5,2)

3 2

6 7 4

9

11

10

1

5

8

3 2

6 7 4

9

1

8 11

10

5

Page 8: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 8

Example

merge(6,11)

3

2

6

7

4

9

1

8

11

10

5

3 2

6 7 4

9

1

8 11

10

5

Page 9: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 9

Example

merge(6,11)

3

2

6

7

4

9

1

8

11

10

5

3 2

6 7 4

9

1

8 11

10

5

3

2

6 7

4

9

1

811

10

5

merge(7,8)

Page 10: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 10

Mergeable Trees: Motivation

Used in [Agarwal, Edelsbrunner, Harer and Wang ’04];

sub-problem in their algorithm for computing the structure of 2-manifolds embedded in RR3.

nodes = critical points (minima, maxima, saddle points), heap-ordered by height.

merging is used for pairing critical points;

no cut operation;

link only attaches leaves to the tree;

the arguments of merge are always leaves.

Page 11: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 11

Turbo Exhaust Manifold

Page 12: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 12

12

3

4

6

8

9

5

7

10

Reeb Graph encodes skeleton of manifold

sink (0 out-degree)

source (0 in-degree)

up-fork (1 in, 2 out)

down-fork (2 in, 1 out)

Nodes in Reeb Graph

Page 13: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 13

Pairing Algorithm on Reeb Graph

x

x

x

v

v w

v

merge(x,v)

root(v) ≠ root(w): pair x with max{root(v),root(w)}root(v) = root(w): pair x with nca(v, w)

merge(x,v); merge(x,w)

merge(x,v) while v is paired, replace by parent(v)

pair x with v

sink

Page 14: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 14

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12

3

4

6

8

9

5

7

10

Example

Page 15: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 15

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12 (3)

3 (2)

4

6

8

9

5

7

10

pair(3,2)merge(3,1)merge(3,2)

Example

Page 16: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 16

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12 (3)

3 (2)

4

6

8

9

5

7

10

merge(4,3)merge(5,4)merge(6,4)

Example

Page 17: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 17

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12 (3)

3 (2)

4

6

8

9

5 (7)

7 (5)

10

pair(7,5)merge(7,5)

Example

Page 18: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 18

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12 (3)

3 (2)

4 (8)

6

8 (4)

9

5 (7)

7 (5)

10

pair(8,4)merge(8,7)merge(8,6)

Example

Page 19: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 19

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

12 (3)

3 (2)

4 (8)

6 (9)

8 (4)

9 (6)

5 (7)

7 (5)

10

merge(9,8)pair(9,6)(6 is the closestunpaired ancestor of 9)

Example

Page 20: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 20

12

3

4

6

8

9

5

7

10

Reeb Graph Mergeable Trees

2 (3)

3 (2)

4 (8)

6 (9)

8 (4)

9 (6)

5 (7)

7 (5)

10

merge(10,6)pair(10,1)(1 is the closestunpaired ancestor of 10)

Example

Page 21: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 21

Two-Pass Algorithm

To avoid parent operation

Pair max with min

Run algorithm forward and backward

Do no pairing in “sink” case

Page 22: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 22

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

2

3

4

6

8

9

5

7

10 (1)

1 (10)

10 (max) is paired with 1 (min)

Reverse Pass of Two-Pass Algorithm

Page 23: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 23

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

2

3

4

6

8

9

5

7

10 (1)

merge(8,9)merge(7,8)

1 (10)

Reverse Pass of Two-Pass Algorithm

Page 24: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 24

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

2

3

4

6 (9)

8

9 (6)

5

7

10 (1)

pair(9,6)* merge(6,8)merge(6,10)

*pair 6 with root of min label;this pair is missing from forward pass.

1 (10)

Reverse Pass of Two-Pass Algorithm

Page 25: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 25

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

2

3

4

6 (9)

8

9 (6)

5 (7)

7 (5)

10 (1)

pair(5,7) merge(5,7)

1 (10)

Reverse Pass of Two-Pass Algorithm

Page 26: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 26

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

2

3

4 (8)

6 (9)

8 (4)

9 (6)

5 (7)

7 (5)

10 (1)

pair(4,8) merge(4,5)merge(4,6)

1 (10)

Reverse Pass of Two-Pass Algorithm

Page 27: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 27

12

3

4

6

8

9

5

7

10

Reverse Reeb Graph Mergeable Trees

1 (10)2

3

4 (8)

6 (9)

8 (4)

9 (6)

5 (7)

7 (5)

10 (1)

merge(3,4)merge(1,3)merge(2,3)

Reverse Pass of Two-Pass Algorithm

Page 28: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 28

Mergeable Trees: Results

n = number of nodes in mergesm = number of merges

O(log2n) amortized time per operation

O(logn) amortized time without cuts

O(logn) worst-case without cuts, parent queries

Page 29: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 29

Dynamic Trees

Goal: maintain a forest of trees with values on vertices and/or edges.

Operations:

link(v,w) : add an edge between v and w. (no cycles allowed)

cut(v,w) : delete edge (v,w).

various operations (e.g. find a vertex of minimum value on a path or in a tree).

Trees can be free (unrooted), rooted, or ordered.

Lots of applications: network flows, static and dynamic graph algorithms, computational geometry, …

Several data structures with optimal O(logn) time per operation (worst case, amortized or randomized)

Page 30: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 30

Dynamic Trees

Optimal Data Structures:

• Sleator and Tarjan (‘83, ‘85): Link-Cut Trees – Worst Case and Amortized.

• Frederickson (‘85 , ‘97): Topology Trees – Worst Case.

• Alstrup, Holm, de Lichtenberg and Thorup (‘97, ’03): Top Trees – Worst Case.

• Acar, Blelloch, Harper, Vittes and Woo (’03): RC-Trees – Randomized.

• Tarjan and Werneck (‘05): Self-Adjusting Top Trees – Amortized.

Page 31: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 31

merge(w,p) :

Suppose we have computed nca(w,p) and isolated the two paths to be merged. It remains to do the actual merge by changing parents.

Mergeable Trees via Dynamic Trees

m

j

g

r

dc

a

i

f

l

b

u

t

v

s

w

e

h

k

onqp

Page 32: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 32

Mergeable Trees via Dynamic Trees

First idea: Iterated Insertions Insert the nodes of the shorter path into the longer. (Suggested in Agarwal et al.)

Cost is bounded below by the sum of the lengths of the shorter paths.

Unfortunately, this is

m

j

g

r

dc

a

i

f

l

b

u

t

v

s

w

e

h

k

onqp

£(n3=2)

merge(w,p) :

Suppose we have computed nca(w,p) and isolated the two paths to be merged. It remains to do the actual merge by changing parents.

Page 33: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 33

Mergeable Trees via Dynamic Trees

merge(w,p):Suppose we have computed nca(w,p) and isolated the two paths;

it remains to do the actual merge by changing the parent pointers.

m

j

g

r

dc

a

i

f

l

b

u

t

v

s

w

e

h

k

onqp

Better idea: Interleaved Merges

Cut edges where parents change and link the pieces together.

Time is O(#cuts × logn).

Page 34: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 34

Mergeable Trees via Dynamic Trees

merge(w,p):Suppose we have computed nca(w,p) and isolated the two paths;

it remains to do the actual merge by changing the parent pointers.

m

j

g

r

dc

a

i

f

l

b

u

t

v

s

w

e

h

k

onqpTo enable fast merging:

Query:

topmost(v,w) : return the smallest (topmost) ancestor of v strictly greater than w. (assume v>w)

r

Better idea: Interleaved Merges

Cut edges where parents change and link the pieces together.

Time is O(#cuts × logn).

Page 35: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 35

Mergeable Trees via Dynamic Trees

1

2

31

2

3

6

7

8

9

4

5

101112

1314

Page 36: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 36

Mergeable Trees via Dynamic Trees

1

2

3

4

5

1

2

3

6

7

8

9

4

5

101112

1314

Page 37: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 37

Mergeable Trees via Dynamic Trees

1

2

3

4

5

6

7

8

9

1

2

3

6

7

8

9

4

5

101112

1314

Page 38: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 38

Mergeable Trees via Dynamic Trees

1

2

3

4

5

6

7

8

9

101112

1

2

3

6

7

8

9

4

5

101112

1314

1314

Page 39: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 39

Analysis of Merge

#parent changes = O(m logn).

w

v

Each node has parent potential (on its parent) has child potential (on its children).

Á(v;w) = 2log(v ¡ w) + 1

©=P

(v;w) Á(v;w)

1

log(v-w)

log(v-w)

Page 40: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 40

Analysis of Merge

#parent changes = O(m logn).

w

v

cut (or delete) : one parent change (to null) Φ 1.

Á(v;w) = 2log(v ¡ w) + 1

©=P

(v;w) Á(v;w)

1

log(v-w)

log(v-w)

Page 41: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 41

Analysis of Merge

#parent changes = O(m logn).

w

v

cut (or delete) : one parent change (to null) Φ 1.

Á(v;w) = 2log(v ¡ w) + 1

©=P

(v;w) Á(v;w)

1

log(v-w)

log(v-w)

merge(v,w) : Φ O(logn) if v,w in different trees (initial link).

Page 42: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 42

Analysis of Merge

#parent changes = O(m logn).

w

v

All other Φ changes are non-positive.

Á(v;w) = 2log(v ¡ w) + 1

©=P

(v;w) Á(v;w)

1

log(v-w)

log(v-w)

z

w

z

y

x

ww-x ≤ (w-z)/2 parent potential of w 1y-z ≤ (w-z)/2 child potent of z 1

Pays for parent change of w.

Page 43: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 43

Implicit Mergeable Trees

3 2

8

12

75

14

11

6 10

9

Parents are computable but not efficiently.

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Page 44: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 44

Implicit Mergeable Trees

merge(v,w): Case (a) v,w in different trees: link(v,w)

3 2

8

12

75

14

11

6 10

9

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Page 45: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 45

Implicit Mergeable Trees

3 2

8

12

75

14

11

6 10

9

3 2

8

12

75

14

11

6 10

9

Real forestEquivalent forest

merge(v,w): Case (a) v,w in different trees: link(v,w)

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Page 46: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 46

Implicit Mergeable Trees

3 2

8

12

75

14

11

6 10

9

3 2

8

12

75

14

11

6 10

9

Real forest

merge(10,11)

merge(v,w): Case (a) v,w in different trees: link(v,w)

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Equivalent forest

Page 47: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 47

Implicit Mergeable Trees

3 2

8

12

75

14

11

6 10

9

3 2

8

11

75

1

4

12

6

10

9

merge(10,11)

Real forest

merge(v,w): Case (a) v,w in different trees: link(v,w)

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Equivalent forest

Page 48: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 48

Implicit Mergeable Trees

merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where

u = pathmin(v,w) , q = successor of u in path from u to v

3 2

8

12

75

14

11

6 10

9

3 2

8

11

75

1

4

12

6

10

9

Real forest

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Equivalent forest

Page 49: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 49

Implicit Mergeable Trees

merge(6,12)

3 2

8

12

75

14

11

6 10

9

3 2

8

11

75

1

4

12

6

10

9

Real forest

merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where

u = pathmin(v,w) , q = successor of u in path from u to v

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Equivalent forest

Page 50: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 50

Implicit Mergeable Trees

3 2

8

12

75

14

11

6 10

9

3 2

8

11

7

5

1

4

12

6

10

9

Real forest

merge(6,12)

merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where

u = pathmin(v,w) , q = successor of u in path from u to v

Each mergeable tree represented by an equivalent unrooted dynamic tree:

root = treemin, nca = pathmin

Equivalent forest

Page 51: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 51

• Main idea: partition the vertices in a tree into disjoint solid paths connected by dashed edges, represent solid paths by search trees.

Sleator and Tarjan’s Link-Cut Trees

Page 52: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 52

Sleator and Tarjan’s Link-Cut Trees

• Main idea: partition the vertices in a tree into disjoint solid paths connected by dashed edges, represent solid paths by search trees.

Page 53: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 53

Mergeable Trees via Partition by Rank

24(4)

16(4)

7(2)

6(2)

4(2)

1(0) 1(0

)

1(0)

1(0)

1(0)

1(0)

1(0)

1(0)

1(0)

2(1)

2(1)

3(1)

4(2)

5(2)

6(2)

7(2)

5(2)

6(2)

2(1)

We can do better when arbitrary cuts are disallowed.

(x,y) is solid if rank(x)=rank(y); dashed otherwise.

rank(x) = blgsize(x)c

Page 54: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 54

In every leaf to root path there are at most logn dashed arcs nca(·,·) can be implemented in O(logn)-time worst case.

We can do better when arbitrary cuts are disallowed.

(x,y) is solid if rank(x)=rank(y); dashed otherwise.

Mergeable Trees via Partition by Rank

24(4)

16(4)

7(2)

6(2)

4(2)

1(0) 1(0

)

1(0)

1(0)

rank(x) = blgsize(x)c

1(0)

1(0)

1(0)

1(0)

1(0)

2(1)

2(1)

3(1)

4(2)

5(2)

6(2)

7(2)

5(2)

6(2)

2(1)

Each node points to (a node that points to)The top node on its path.

Page 55: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 55

Mergeable Trees via Partition by Rank

We can do better when arbitrary cuts are disallowed.

(x,y) is solid if rank(x)=rank(y); dashed otherwise.

Merging idea: Insert nodes of lower rank into solid paths of higher rank.

rank(p) ≥ rank(q)

p

q

The rank of q increases by at least 1 O(nlogn) such insertions.

rank(x) = blgsize(x)c

Page 56: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 56

Mergeable Trees via Partition by Rank

We can do better when arbitrary cuts are disallowed.

(x,y) is solid if rank(x)=rank(y); dashed otherwise.

Merging idea: Insert nodes of lower rank into solid paths of higher rank.

rank(p) ≥ rank(q)

q

The rank of q increases by at least 1 O(nlogn) such insertions.

p

A prefix of the resulting path may change rank O(nlogn) such events.

rank(x) = blgsize(x)c

Page 57: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 57

Mergeable Trees via Partition by Rank

Operations: (1) insert a node in an arbitrary position on a solid path; (2) remove top node of a solid path.

Solid paths represented by finger search trees.

x

zy

P

x = most-recently-accessed node of Pz is inserted immediately below y

cost ~ log(d+2)d = length of path from y to x

d

Page 58: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 58

Mergeable Trees via Partition by Rank

x

zy

x = most-recently-accessed node of Pz is inserted immediately below y

cost ~ log(d+2)d = length of path from y to x

d

P

di = length of the (solid) insertion path for the i-th insertion (1≤i≤nlogn).

Claim:P

i logdi = O(n logn)

Page 59: Mergeable Trees Robert E. Tarjan Princeton University and HP Labs

Mergeable Trees 59

Open Problems

Get an O(logn)-time algorithm when arbitrary cuts are allowed.

Conjecture:

The interleaved merging algorithm implemented with self-adjusting dynamic trees takes O(logn) amortized time per operation.

Other applications?