Renato Werneck

The Dynamic Trees Problem

• Dynamic trees: Self-Adjusting Top Trees ƒ Goal: maintain an n-vertex forest that changes over time. •link(v,w): creates an edge between vertices v and w. •cut(v,w): deletes edge (v,w). ƒ Application-specific data associated with edges and/or vertices.

• Concrete examples: ƒ Find minimum-weight edge in the path between any two vertices. ƒ Add a value to all edges in the path between two vertices. ƒ Find total weight of all vertices in a subtree.

• O(log n) time per operation. Robert Tarjan (Princeton University/HP) ƒ map arbitrary onto balanced tree. Renato Werneck (Princeton University)

Dynamic Trees

Data Structures Data Structures

ST-trees ST-trees Topology RC-trees Top Trees ET-trees ST-trees ST-trees Topology RC-trees Top Trees ET-trees [ST83] [ST85] [Fre85] [ABHW03] [AHdLT97] [HK95] [ST83] [ST85] [Fre85] [ABHW03] [AHdLT97] [HK95]

Arbitrary subtree Arbitrary subtree bounded bounded bounded bounded YES YES bounded bounded bounded bounded YES YES queries? degree degree degree degree queries? degree degree degree degree

Arbitrary path Arbitrary path YES YES bounded bounded YES NO YES YES bounded bounded YES NO queries? degree degree queries? degree degree

Simple to Simple to NO fairly fairly fairly interface YES NO fairly fairly fairly fairly YES implement? only implement?

Generic Generic NO NO NO YES YES NO NO NO NO YES YES NO interface? Interface?

O(log n) worst interface O(log n) worst YES amortized YES randomized YES YES amortized YES randomized amortized YES case? only case?

path path tree tree tree linearization path path tree tree tree contr./ linearization Principle decomp. decomp. contraction contraction contraction (Euler tour) Principle decomp. decomp. contraction contraction path decomp. (Euler tour)

Dynamic Trees Dynamic Trees

Contractions: Rake and Compress Contractions: Rake and Compress

• Proposed by Miller and Reif [1985] (parallel setting). • Contraction:

• Rake: ƒ Series of rakes and compresses; ƒ Eliminates a degree-one vertex. ƒ Reduces a tree to a single cluster (edge). ƒ Collapses edge onto successor. • Top tree embodies a contraction: • Assumes circular order of edges. ƒ Direct access only to root cluster. • Compress: ƒ User defines what information to store in parent. ƒ Eliminates a degree-two vertex. ƒ Any order of rakes and compresses is “right”: ƒ Combines two edges into one. • root will have the correct information. • Balanced: updates in O(log n) time. • Original edges and resulting edge are clusters. • Alstrup et al. [1997] use topology trees: high overhead.

• We show a direct implementation.

Dynamic Trees Dynamic Trees

Dynamic Trees 1 Renato Werneck

Representation Representation

• Consider some unrooted tree: • Pick a degree-one vertex as root, direct all edges towards it.

• We call this a unit tree (rooted tree with degree-one root).

Dynamic Trees Dynamic Trees

Representation Representation

•Pick a root path: • Represent the root path as a : ƒ starts at some leaf; ƒ Leaves: base clusters (original edges). ƒ ends at the root. ƒ Internal nodes: compress clusters.

Nc

f N N ab b e e c d ab bc Nd ef

cd de

Dynamic Trees Dynamic Trees

Representation Representation

• What if the degree of a vertex is not two? • What if the degree of a vertex is not two? ƒ Recursively represent each subtree rooted at the vertex. ƒ Recursively represent each subtree rooted at the vertex. • At most two because of circular order. ƒ Before vertex is compressed, rake subtree onto adjacent cluster.

N N V c c

W X V f N N f W N Y N ab b e ab X b e e W e c c d ab bc Nd ef d V ab bc X Nd Z ef Z Y cd de cd de Z

Y

Dynamic Trees Dynamic Trees

Dynamic Trees 2 Renato Werneck

Representation Representation

• Representation: • How does the recursive representation work? ƒ Up to four children per node (up to two foster children). ƒ Must represent subtrees rooted at the root path. ƒ Meaning: up to two rakes followed by a compress.

N c V

V W X f W N Y N f ab X b e ab W e e c c d V ab bc X Nd Z ef d Z Y cd de Z

ƒ Example: Ne = compress(rake(X, ce), rake(Z, ef)) = cf Y

Dynamic Trees Dynamic Trees

Representation Representation

• How does the recursive representation work? • How does the recursive representation work? ƒ Must represent subtrees rooted at the root path. ƒ Must represent subtrees rooted at the root path. ƒ Each subtree is a sequence of unit trees. ƒ Each subtree is a sequence of unit trees. ƒ Represent each unit tree recursively.

c c

C

A B

Y Y

Dynamic Trees Dynamic Trees

Representation Representation

• How does the recursive representation work? • Interpretations: ƒ Must represent subtrees rooted at the root path. ƒ User interface: . ƒ Each subtree is a sequence of unit trees. • sequence of rakes and compresses; ƒ Represent each unit tree recursively. •a single tree; • similar to topology trees and RC-trees. ƒ Build a binary tree of rakes. ƒ Implementation: path decomposition. • maximal edge-disjoint paths; • hierarchy of binary trees (rake trees/compress trees). c Y = A Y •similar to ST-trees.

C B C

A B (Each circle is rake cluster.) Y

Dynamic Trees Dynamic Trees

Dynamic Trees 3 Renato Werneck

Self-Adjusting Top Trees Self-Adjusting Top Trees

• Topmost compress tree represents the root path. • Splaying: series of rotations within a rake/compress subtree: ƒ Top tree interface allows the user to access the root path only. ƒ keeps subtree “balanced” (in the amortized sense); ƒ expose makes a node v part of the root path (and/or changes root). ƒ brings vertex to the root of the subtree. •Main tools: splay and splice.

Ny Nx rotate right

r r H N G yz F wx E N z x y rotate left

G F wx E xy H xy G yz expose(v) y

H E x

u u F w v v actual tree

Dynamic Trees Dynamic Trees

Self-Adjusting Top Trees Self-Adjusting Top Trees

• Splice: changes the partition of the original tree into paths. •expose(v) in 3 passes: 1. Splay within each binary tree between v and the root;

Nv Nv 2. perform a series of splices;

X Z Y Z 3. splay within the final tree. splice(X) •Main result: O(log n) amortized time. r X r actual tree Y

expose(v) data structure Z splice(X) Z

X v X v u Y Y u

v v

Dynamic Trees Dynamic Trees

Links Hidden Details

•link(v,w): first expose v and w, then rearrange appropriately. • Exposing the vertex is slightly different from changing the root. u • Top tree nodes represent edges; must also associate with vertices. b A • Degree of vertices exposed matters (special cases). v

B D • Left-right relation must be relaxed in compress trees. w a C • Must call user-defined functions in the appropriate order. x

Nw Nw

C xw D bw C xw Nv link(v,w)

Nv D bw A uv vw

A uv B av B av

Dynamic Trees Dynamic Trees

Dynamic Trees 4 Renato Werneck

Practical Considerations Further Work

• Compress node: • Worst-case variant?

ƒ Actually represents up to 3 clusters. • Careful experimental study: ƒ Could be implemented as one cluster = one node. ƒ Top trees tend to be slower than ET-trees and ST-trees, but: • Splaying and splicing get slightly more complicated. •More generic: • Special cases (application-dependent): – bounded/unbounded degrees; – subtree/path operations; ƒ No circular order: – circular order around vertices. • Compress nodes have at most 3 (not 4) children. • Much easier to adapt to different applications; • Simpler splices. • Easier to reason about. ƒ Trivial rakes: essentially ST-Trees. ƒ How does it compare to RC-trees? •No rake trees. • No pointers to “middle children” (dashed edges).

Dynamic Trees Dynamic Trees

Dynamic Trees 5