590 likes | 683 Views
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.
E N D
Mergeable TreesRobert 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 Mergeable Trees TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAAAAA
Outline • The Problem • The Motivating Application • Dynamic Trees • Mergeable Trees via Dynamic Trees • Implicit Mergeable Trees • Mergeable Trees via Partition by Rank • Open Problems Mergeable Trees
Goal: Maintain a forest of rooted trees. Queries: Mergeable Trees parent(v) root(v) nca(v,w) Mergeable Trees
Goal: Maintain a forest of rooted trees. Queries: Mergeable Trees parent(v) root(v) nca(v,w) • 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. Mergeable Trees
Goal: Maintain a forest of rooted trees. Queries: Mergeable Trees parent(v) root(v) nca(v,w) • 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. Mergeable Trees
Example 1 3 2 6 7 4 8 9 5 10 11 Mergeable Trees
Example 1 1 3 2 3 2 6 7 4 merge(5,2) 6 7 4 5 8 9 8 9 10 11 5 10 11 Mergeable Trees
Example 1 3 2 6 7 4 merge(6,11) 1 5 2 8 9 10 11 4 3 5 7 8 9 6 10 11 Mergeable Trees
Example 1 3 2 6 7 4 merge(6,11) 1 5 2 8 9 10 11 4 3 5 7 1 8 9 6 merge(7,8) 2 10 11 3 5 4 6 7 9 10 11 8 Mergeable Trees
Used in [Agarwal, Edelsbrunner, Harer and Wang ’04]; sub-problem in their algorithm for computing the structure of 2-manifolds embedded in R3. 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. Mergeable Trees: Motivation Mergeable Trees
Turbo Exhaust Manifold Mergeable Trees
Reeb Graph encodes skeleton of manifold 10 Nodes in Reeb Graph 9 8 sink (0 out-degree) 7 6 source (0 in-degree) 5 4 up-fork (1 in, 2 out) 3 down-fork (2 in, 1 out) 2 1 Mergeable Trees
Pairing Algorithm on Reeb Graph x merge(x,v) v root(v) ≠ root(w): pair x with max{root(v),root(w)} x root(v) = root(w): pair x with nca(v, w) v w merge(x,v); merge(x,w) merge(x,v) sink x while v is paired, replace by parent(v) v pair x with v Mergeable Trees
Example 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 8 8 7 7 6 6 5 5 4 pair(3,2) merge(3,1) merge(3,2) 4 3 3 (2) 2 2 (3) 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 8 8 7 7 6 6 5 5 4 merge(4,3) merge(5,4) merge(6,4) 4 3 3 (2) 2 2 (3) 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 8 8 7 7 (5) 6 6 5 5 (7) 4 pair(7,5) merge(7,5) 4 3 3 (2) 2 2 (3) 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 8 8 (4) 7 7 (5) 6 6 5 5 (7) 4 pair(8,4) merge(8,7) merge(8,6) 4 (8) 3 3 (2) 2 2 (3) 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 (6) 8 8 (4) 7 7 (5) 6 6 (9) 5 5 (7) 4 merge(9,8) pair(9,6) (6 is the closest unpaired ancestor of 9) 4 (8) 3 3 (2) 2 2 (3) 1 1 Reeb Graph Mergeable Trees Mergeable Trees
Example 10 10 9 9 (6) 8 8 (4) 7 7 (5) 6 6 (9) 5 5 (7) 4 merge(10,6) pair(10,1) (1 is the closest unpaired ancestor of 10) 4 (8) 3 3 (2) 2 2 (3) 1 Reeb Graph Mergeable Trees Mergeable Trees
Two-Pass Algorithm To avoid parent operation Pair max with min Run algorithm forward and backward Do no pairing in “sink” case Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 8 8 7 7 6 6 5 5 10 (max) is paired with 1 (min) 4 4 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 8 8 7 7 6 6 5 5 merge(8,9) merge(7,8) 4 4 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 (6) 8 8 7 7 6 6 (9) pair(9,6)* merge(6,8) merge(6,10) *pair 6 with root of min label; this pair is missing from forward pass. 5 5 4 4 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 (6) 8 8 7 7 (5) 6 6 (9) 5 5 (7) pair(5,7) merge(5,7) 4 4 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 (6) 8 8 (4) 7 7 (5) 6 6 (9) 5 5 (7) pair(4,8) merge(4,5) merge(4,6) 4 4 (8) 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Reverse Pass of Two-Pass Algorithm 10 10 (1) 9 9 (6) 8 8 (4) 7 7 (5) 6 6 (9) 5 5 (7) merge(3,4) merge(1,3) merge(2,3) 4 4 (8) 3 3 2 2 1 (10) 1 Reverse Reeb Graph Mergeable Trees Mergeable Trees
Mergeable Trees: Results n = number of nodes in merges m = number of merges O(log2n) amortized time per operation O(logn) amortized time without cuts O(logn) worst-case without cuts, parent queries Mergeable 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) Dynamic Trees Mergeable 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. Dynamic Trees Mergeable Trees
merge(w,p) : Suppose we have computednca(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 a b c d f e g i h j l m k p q r s n o t u v w Mergeable Trees
merge(w,p) : Suppose we have computednca(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 a b 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 c d f e g i h j l m k p q r s n o t u v w Mergeable Trees
Better idea: Interleaved Merges Cut edges where parents change and link the pieces together. Time is O(#cuts × logn). Mergeable Trees via Dynamic Trees merge(w,p): Suppose we have computednca(w,p) and isolated the two paths; it remains to do the actual merge by changing the parent pointers. a b c d f e g i h j l m k p q r s n o t u v w Mergeable Trees
Better idea: Interleaved Merges Cut edges where parents change and link the pieces together. Time is O(#cuts × logn). Mergeable Trees via Dynamic Trees merge(w,p): Suppose we have computednca(w,p) and isolated the two paths; it remains to do the actual merge by changing the parent pointers. a b c d f e g i h j l m k To enable fast merging: Query: topmost(v,w) : return the smallest (topmost) ancestor of v strictly greater than w. (assume v>w) p q r r s n o t u v w Mergeable Trees
Mergeable Trees via Dynamic Trees 1 2 4 1 3 5 2 3 10 6 11 7 12 8 9 13 14 Mergeable Trees
Mergeable Trees via Dynamic Trees 1 2 4 1 3 5 2 4 3 10 5 6 11 7 12 8 9 13 14 Mergeable Trees
Mergeable Trees via Dynamic Trees 1 2 4 1 3 5 2 4 3 10 5 6 11 6 7 12 7 8 8 9 9 13 14 Mergeable Trees
Mergeable Trees via Dynamic Trees 1 2 4 1 3 5 2 4 3 10 5 6 11 6 7 12 7 8 8 9 9 13 10 14 11 12 13 14 Mergeable Trees
Analysis of Merge #parent changes = O(m logn). w log(v-w) 1 v log(v-w) Each node has parent potential (on its parent) has child potential (on its children). Mergeable Trees
Analysis of Merge #parent changes = O(m logn). w log(v-w) 1 v log(v-w) cut (or delete) : one parent change (to null) Φ 1. Mergeable Trees
Analysis of Merge #parent changes = O(m logn). w log(v-w) 1 v log(v-w) cut (or delete) : one parent change (to null) Φ 1. merge(v,w) : Φ O(logn) if v,w in different trees (initial link). Mergeable Trees
Analysis of Merge #parent changes = O(m logn). w log(v-w) 1 v log(v-w) z z y All other Φ changes are non-positive. w x w-x ≤ (w-z)/2 parent potential of w1 y-z ≤ (w-z)/2 child potent of z 1 w Pays for parent change of w. Mergeable Trees
Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin Parents are computable but not efficiently. 1 4 3 2 5 7 8 6 9 10 11 12 Mergeable Trees
merge(v,w): Case (a) v,w in different trees: link(v,w) Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin 1 4 3 2 5 7 8 6 9 10 11 12 Mergeable Trees
Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin merge(v,w): Case (a) v,w in different trees: link(v,w) 1 1 4 4 3 2 5 3 2 5 7 7 8 8 6 9 10 6 9 10 11 12 11 12 Real forest Equivalent forest Mergeable Trees
Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin merge(v,w): Case (a) v,w in different trees: link(v,w) merge(10,11) 1 1 4 4 3 2 5 3 2 5 7 7 8 8 6 9 10 6 9 10 11 12 11 12 Real forest Equivalent forest Mergeable Trees
Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin merge(v,w): Case (a) v,w in different trees: link(v,w) merge(10,11) 1 3 2 1 4 3 2 5 4 7 5 7 8 6 9 10 6 9 8 11 12 12 10 11 Real forest Equivalent forest Mergeable Trees
merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin u = pathmin(v,w), q = successor of u in path from u to v 1 3 2 1 4 3 2 5 4 7 5 7 8 6 9 10 6 9 8 11 12 12 10 11 Real forest Equivalent forest Mergeable Trees
merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin u = pathmin(v,w), q = successor of u in path from u to v merge(6,12) 1 3 2 1 4 3 2 5 4 7 5 7 8 6 9 10 6 9 8 11 12 12 10 11 Real forest Equivalent forest Mergeable Trees
merge(v,w): Case (b) v,w in same tree: cut (u,q); link (v,w) where Implicit Mergeable Trees Each mergeable tree represented by an equivalent unrooted dynamic tree: root = treemin, nca = pathmin u = pathmin(v,w), q = successor of u in path from u to v merge(6,12) 1 3 2 1 4 4 5 3 2 5 7 6 8 6 9 10 7 11 12 9 8 12 10 11 Real forest Equivalent forest Mergeable Trees