Top tree

From HandWiki

A top tree is a data structure based on a binary tree for unrooted dynamic trees that is used mainly for various path-related operations. It allows simple divide-and-conquer algorithms. It has since been augmented to maintain dynamically various properties of a tree such as diameter, center and median. A top tree [math]\displaystyle{ \Re }[/math] is defined for an underlying tree [math]\displaystyle{ \mathcal{T} }[/math] and a set [math]\displaystyle{ \partial{T} }[/math] of at most two vertices called as External Boundary Vertices

An image depicting a top tree built on an underlying tree (black nodes). A tree divided into edge clusters and the complete top-tree for it. Filled nodes in the top-tree are path-clusters, while small circle nodes are leaf-clusters. The big circle node is the root. Capital letters denote clusters, non-capital letters are nodes.

Glossary

Boundary Node

See Boundary Vertex

Boundary Vertex

A vertex in a connected subtree is a Boundary Vertex if it is connected to a vertex outside the subtree by an edge.

External Boundary Vertices

Up to a pair of vertices in the top tree [math]\displaystyle{ \Re }[/math] can be called as External Boundary Vertices, they can be thought of as Boundary Vertices of the cluster which represents the entire top tree.

Cluster

A cluster is a connected subtree with at most two Boundary Vertices. The set of Boundary Vertices of a given cluster [math]\displaystyle{ \mathcal{C} }[/math] is denoted as [math]\displaystyle{ \partial{C}. }[/math] With each cluster [math]\displaystyle{ \mathcal{C} }[/math] the user may associate some meta information [math]\displaystyle{ I(\mathcal{C}), }[/math] and give methods to maintain it under the various internal operations.

Path Cluster

If [math]\displaystyle{ \pi(\mathcal{C}) }[/math] contains at least one edge then [math]\displaystyle{ \mathcal{C} }[/math] is called a Path Cluster.

Point Cluster

See Leaf Cluster

Leaf Cluster

If [math]\displaystyle{ \pi(\mathcal{C}) }[/math] does not contain any edge i.e. [math]\displaystyle{ \mathcal{C} }[/math] has only one Boundary Vertex then [math]\displaystyle{ \mathcal{C} }[/math] is called a Leaf Cluster.

Edge Cluster

A Cluster containing a single edge is called an Edge Cluster.

Leaf Edge Cluster

A Leaf in the original Cluster is represented by a Cluster with just a single Boundary Vertex and is called a Leaf Edge Cluster.

Path Edge Cluster

Edge Clusters with two Boundary Nodes are called Path Edge Cluster.

Internal Node

A node in [math]\displaystyle{ \mathcal{C} }[/math] \ [math]\displaystyle{ \partial{C} }[/math] is called an Internal Node of [math]\displaystyle{ \mathcal{C}. }[/math]

Cluster Path

The path between the Boundary Vertices of [math]\displaystyle{ \mathcal{C} }[/math] is called the cluster path of [math]\displaystyle{ \mathcal{C} }[/math] and it is denoted by [math]\displaystyle{ \pi(\mathcal{C}). }[/math]

Mergeable Clusters

Two Clusters [math]\displaystyle{ \mathcal{A} }[/math] and [math]\displaystyle{ \mathcal{B} }[/math] are Mergeable if [math]\displaystyle{ \mathcal{A}\cap\mathcal{B} }[/math] is a singleton set (they have exactly one node in common) and [math]\displaystyle{ \mathcal{A}\cup\mathcal{B} }[/math] is a Cluster.

Introduction

Top trees are used for maintaining a Dynamic forest (set of trees) under link and cut operations.

The basic idea is to maintain a balanced Binary tree [math]\displaystyle{ \Re }[/math] of logarithmic height in the number of nodes in the original tree [math]\displaystyle{ \mathcal{T} }[/math]( i.e. in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time) ; the top tree essentially represents the recursive subdivision of the original tree [math]\displaystyle{ \mathcal{T} }[/math] into clusters.

In general the tree [math]\displaystyle{ \mathcal{T} }[/math] may have weight on its edges.

There is a one-to-one correspondence with the edges of the original tree [math]\displaystyle{ \mathcal{T} }[/math] and the leaf nodes of the top tree [math]\displaystyle{ \Re }[/math] and each internal node of [math]\displaystyle{ \Re }[/math] represents a cluster that is formed due to the union of the clusters that are its children.

The top tree data structure can be initialized in [math]\displaystyle{ \mathcal{O}(n) }[/math] time.

Therefore the top tree [math]\displaystyle{ \Re }[/math] over ([math]\displaystyle{ \mathcal{T}, }[/math] [math]\displaystyle{ \partial{T} }[/math]) is a binary tree such that

  • The nodes of [math]\displaystyle{ \Re }[/math] are clusters of ([math]\displaystyle{ \mathcal{T}, }[/math] [math]\displaystyle{ \partial{T} }[/math] );
  • The leaves of [math]\displaystyle{ \Re }[/math] are the edges of [math]\displaystyle{ \mathcal{T}; }[/math]
  • Sibling clusters are neighbours in the sense that they intersect in a single vertex, and then their parent cluster is their union.
  • Root of [math]\displaystyle{ \Re }[/math] is the tree [math]\displaystyle{ \mathcal{T} }[/math] itself, with a set of at most two External Boundary Vertices.

A tree with a single vertex has an empty top tree, and one with just an edge is just a single node.

These trees are freely augmentable allowing the user a wide variety of flexibility and productivity without going into the details of the internal workings of the data structure, something which is also referred to as the Black Box.

Dynamic Operations

The following three are the user allowable Forest Updates.

  • Link(v, w): Where [math]\displaystyle{ v }[/math] and [math]\displaystyle{ w }[/math] are vertices in different trees [math]\displaystyle{ \mathcal{T} }[/math]1 and [math]\displaystyle{ \mathcal{T} }[/math]2. It returns a single top tree representing [math]\displaystyle{ \Re }[/math]v[math]\displaystyle{ \cup }[/math][math]\displaystyle{ \Re }[/math]w[math]\displaystyle{ \cup{(v,w)} }[/math]
  • Cut(v, w): Removes the edge [math]\displaystyle{ {(v,w)} }[/math] from a tree [math]\displaystyle{ \mathcal{T} }[/math] with top tree [math]\displaystyle{ \Re, }[/math] thereby turning it into two trees [math]\displaystyle{ \mathcal{T} }[/math]v and [math]\displaystyle{ \mathcal{T} }[/math]w and returning two top trees [math]\displaystyle{ \Re }[/math]v and [math]\displaystyle{ \Re }[/math]w.
  • Expose(S): Is called as a subroutine for implementing most of the queries on a top tree. [math]\displaystyle{ S }[/math] contains at most 2 vertices. It makes original external vertices to be normal vertices and makes vertices from [math]\displaystyle{ S }[/math] the new External Boundary Vertices of the top tree. If [math]\displaystyle{ S }[/math] is nonempty it returns the new Root cluster [math]\displaystyle{ \mathcal{C} }[/math] with [math]\displaystyle{ \partial{C} = S. }[/math] Expose({v,w}) fails if the vertices are from different trees.

Internal Operations

The Forest updates are all carried out by a sequence of at most [math]\displaystyle{ \mathcal{O}(\log n) }[/math] Internal Operations, the sequence of which is computed in further [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time. It may happen that during a tree update, a leaf cluster may change to a path cluster and the converse. Updates to top tree are done exclusively by these internal operations.

The [math]\displaystyle{ I(\mathcal{C}) }[/math] is updated by calling a user defined function associated with each internal operation.

  • Merge[math]\displaystyle{ (\mathcal{A},\mathcal{B}){:} }[/math] Here [math]\displaystyle{ \mathcal{A} }[/math] and [math]\displaystyle{ \mathcal{B} }[/math] are Mergeable Clusters, it returns [math]\displaystyle{ \mathcal{C} }[/math] as the parent cluster of [math]\displaystyle{ \mathcal{A} }[/math] and [math]\displaystyle{ \mathcal{B} }[/math] and with boundary vertices as the boundary vertices of [math]\displaystyle{ \mathcal{A}\cup\mathcal{B}. }[/math] Computes [math]\displaystyle{ I(\mathcal{C}) }[/math] using [math]\displaystyle{ I(\mathcal{A}) }[/math] and [math]\displaystyle{ I(\mathcal{B}). }[/math]
  • Split[math]\displaystyle{ (\mathcal{C}){:} }[/math] Here [math]\displaystyle{ \mathcal{C} }[/math] is the root cluster [math]\displaystyle{ \mathcal{A}\cup\mathcal{B}. }[/math] It updates [math]\displaystyle{ I(\mathcal{A}) }[/math] and [math]\displaystyle{ I(\mathcal{B}) }[/math] using [math]\displaystyle{ I(\mathcal{C}) }[/math] and than it deletes the cluster [math]\displaystyle{ \mathcal{C} }[/math] from [math]\displaystyle{ \Re }[/math].

Split is usually implemented using Clean[math]\displaystyle{ (\mathcal{C}) }[/math] method which calls user method for updates of [math]\displaystyle{ I(\mathcal{A}) }[/math] and [math]\displaystyle{ I(\mathcal{B}) }[/math] using [math]\displaystyle{ I(\mathcal{C}) }[/math] and updates [math]\displaystyle{ I(\mathcal{C}) }[/math] such that it's known there is no pending update needed in its children. Than the [math]\displaystyle{ \mathcal{C} }[/math] is discarded without calling user defined functions. Clean is often required for queries without need to Split. If Split does not use Clean subroutine, and Clean is required, its effect could be achieved with overhead by combining Merge and Split.

The next two functions are analogous to the above two and are used for base clusters.

  • Create[math]\displaystyle{ (v,w){:} }[/math] Creates a cluster [math]\displaystyle{ \mathcal{C} }[/math] for the edge [math]\displaystyle{ (v,w). }[/math] Sets [math]\displaystyle{ \partial{C} = \partial }[/math][math]\displaystyle{ (v,w). }[/math] [math]\displaystyle{ I(\mathcal{C}) }[/math] is computed from scratch.
  • Eradicate[math]\displaystyle{ (\mathcal{C}){:} }[/math] [math]\displaystyle{ \mathcal{C} }[/math] is the edge cluster [math]\displaystyle{ (v,w). }[/math] User defined function is called to process [math]\displaystyle{ I(\mathcal{C}) }[/math] and than the cluster [math]\displaystyle{ \mathcal{C} }[/math] is deleted from the top tree.

Non local search

User can define Choose[math]\displaystyle{ (\mathcal{C}){:} }[/math] operation which for a root (nonleaf) cluster selects one of its child clusters. The top tree blackbox provides Search[math]\displaystyle{ (\mathcal{C}){:} }[/math] routine, which organizes Choose queries and reorganization of the top tree (using the Internal operations) such that it locates the only edge in intersection of all selected clusters. Sometimes the search should be limited to a path. There is a variant of nonlocal search for such purposes. If there are two external boundary vertices in the root cluster [math]\displaystyle{ \mathcal{C} }[/math], the edge is searched only on the path [math]\displaystyle{ \pi(\mathcal{C}) }[/math]. It is sufficient to do following modification: If only one of root cluster children is path cluster, it is selected by default without calling the Choose operation.

Examples of non local search

Finding i-th edge on longer path from [math]\displaystyle{ v }[/math] to [math]\displaystyle{ w }[/math] could be done by [math]\displaystyle{ \mathcal{C} }[/math]=Expose({v,w}) followed by Search([math]\displaystyle{ \mathcal{C} }[/math]) with appropriate Choose. To implement the Choose we use global variable representing [math]\displaystyle{ v }[/math] and global variable representing [math]\displaystyle{ i. }[/math] Choose selects the cluster [math]\displaystyle{ \mathcal{A} }[/math] with [math]\displaystyle{ v\in\partial{A} }[/math] iff length of [math]\displaystyle{ \pi(\mathcal{A}) }[/math] is at least [math]\displaystyle{ i }[/math]. To support the operation the length must be maintained in the [math]\displaystyle{ I }[/math].

Similar task could be formulated for graph with edges with nonunit lengths. In that case the distance could address an edge or a vertex between two edges. We could define Choose such that the edge leading to the vertex is returned in the latter case. There could be defined update increasing all edge lengths along a path by a constant. In such scenario these updates are done in constant time just in root cluster. Clean is required to distribute the delayed update to the children. The Clean should be called before the Choose is invoked. To maintain length in [math]\displaystyle{ I }[/math] would in that case require to maintain unitlength in [math]\displaystyle{ I }[/math] as well.

Finding center of tree containing vertex [math]\displaystyle{ v }[/math] could be done by finding either bicenter edge or edge with center as one endpoint. The edge could be found by [math]\displaystyle{ \mathcal{C} }[/math]=Expose({v}) followed by Search([math]\displaystyle{ \mathcal{C} }[/math]) with appropriate Choose. The choose selects between children [math]\displaystyle{ \mathcal{A}, }[/math] [math]\displaystyle{ \mathcal{B} }[/math] with [math]\displaystyle{ a\in \partial{A}\cap \partial{B} }[/math] the child with higher maxdistance[math]\displaystyle{ (a) }[/math]. To support the operation the maximal distance in the cluster subtree from a boundary vertex should be maintained in the [math]\displaystyle{ I }[/math]. That requires maintenance of the cluster path length as well.

Interesting Results and Applications

A number of interesting applications originally implemented by other methods have been easily implemented using the top tree's interface. Some of them include

  • ([SLEATOR AND TARJAN 1983]). We can maintain a dynamic collection of weighted trees in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time per link and cut, supporting queries about the maximum edge weight between any two vertices in [math]\displaystyle{ O (\log n) }[/math] time.
    • Proof outline: It involves maintaining at each node the maximum weight (max_wt) on its cluster path, if it is a point cluster then max_wt([math]\displaystyle{ \mathcal{C} }[/math]) is initialised as [math]\displaystyle{ -\infty. }[/math] When a cluster is a union of two clusters then it is the maximum value of the two merged clusters. If we have to find the max wt between [math]\displaystyle{ v }[/math] and [math]\displaystyle{ w }[/math] then we do [math]\displaystyle{ \mathcal{C}= }[/math] Expose[math]\displaystyle{ (v,w), }[/math] and report max_wt[math]\displaystyle{ (\mathcal{C}). }[/math]
  • ([SLEATOR AND TARJAN 1983]). In the scenario of the above application we can also add a common weight [math]\displaystyle{ x }[/math] to all edges on a given path [math]\displaystyle{ v }[/math] · · ·[math]\displaystyle{ w }[/math] in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time.
    • Proof outline: We introduce a weight called extra([math]\displaystyle{ \mathcal{C} }[/math]) to be added to all the edges in [math]\displaystyle{ \pi(\mathcal{C}). }[/math] Which is maintained appropriately ; split([math]\displaystyle{ \mathcal{C} }[/math]) requires that, for each path child [math]\displaystyle{ \mathcal{A} }[/math] of [math]\displaystyle{ \mathcal{C}, }[/math] we set max_wt(A) := max_wt([math]\displaystyle{ \mathcal{A} }[/math]) + extra([math]\displaystyle{ \mathcal{C} }[/math]) and extra([math]\displaystyle{ \mathcal{A} }[/math]) := extra([math]\displaystyle{ \mathcal{A} }[/math]) + extra([math]\displaystyle{ \mathcal{C} }[/math]). For [math]\displaystyle{ \mathcal{C} }[/math] := join([math]\displaystyle{ \mathcal{A}, }[/math] [math]\displaystyle{ \mathcal{B} }[/math]), we set max_wt([math]\displaystyle{ \mathcal{C} }[/math]) := max {max_wt([math]\displaystyle{ \mathcal{A} }[/math]), max_wt([math]\displaystyle{ \mathcal{B} }[/math])} and extra([math]\displaystyle{ \mathcal{C} }[/math]) := 0. Finally, to find the maximum weight on the path [math]\displaystyle{ v }[/math] · · ·[math]\displaystyle{ w, }[/math] we set [math]\displaystyle{ \mathcal{C} }[/math] := Expose[math]\displaystyle{ (v,w) }[/math] and return max_wt([math]\displaystyle{ \mathcal{C} }[/math]).
  • ([GOLDBERG ET AL. 1991]). We can ask for the maximum weight in the underlying tree containing a given vertex [math]\displaystyle{ v }[/math] in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time.
    • Proof outline: This requires maintaining additional information about the maximum weight non cluster path edge in a cluster under the Merge and Split operations.
  • The distance between two vertices [math]\displaystyle{ v }[/math] and [math]\displaystyle{ w }[/math] can be found in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time as length(Expose[math]\displaystyle{ (v,w) }[/math]).
    • Proof outline:We will maintain the length length([math]\displaystyle{ \mathcal{C} }[/math]) of the cluster path. The length is maintained as the maximum weight except that, if [math]\displaystyle{ \mathcal{C} }[/math] is created by a join(Merge), length([math]\displaystyle{ \mathcal{C} }[/math]) is the sum of lengths stored with its path children.
  • Queries regarding diameter of a tree and its subsequent maintenance takes [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time.
  • The Center and Median can me maintained under Link(Merge) and Cut(Split) operations and queried by non local search in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time.
  • Top trees are used in state-of-the-art algorithms for dynamic two-edge connectivity. In this problem, similarly to dynamic connectivity, the graph is subject to edge deletions and insertions, as well as queries asking whether a pair of vertices are two-edge connected, or there is a bridge separating them. Holm, de Lichtenberg, and Thorup[1] give a deterministic algorithm with amortized update time [math]\displaystyle{ O(\log^4 n) }[/math], and [math]\displaystyle{ O(\log n / \log \log n) }[/math] query time. Subsequent work by Holm, Rotenberg, and Thorup improves this to an amortized update time of [math]\displaystyle{ O(\log^2 n \log^2 \log n) }[/math], also using top trees[2][3]
  • The graph could be maintained allowing to update the edge set and ask queries on vertex 2-connectivity. Amortized complexity of updates is [math]\displaystyle{ O(\log^5 n) }[/math]. Queries could be implemented even faster. The algorithm is not trivial, [math]\displaystyle{ I(\mathcal{C}) }[/math] uses [math]\displaystyle{ \Theta(\log^2 n) }[/math] space.[4]
  • Top trees can be used to compress trees in a way that is never much worse than DAG compression, but may be exponentially better.[5]

Implementation

Top trees have been implemented in a variety of ways, some of them include implementation using a Multilevel Partition (Top-trees and dynamic graph algorithms Jacob Holm and Kristian de Lichtenberg. Technical Report), and even by using Sleator-Tarjan s-t trees (typically with amortized time bounds), Frederickson's Topology Trees (with worst case time bounds) (Alstrup et al. Maintaining Information in Fully Dynamic Trees with Top Trees).

Amortized implementations are more simple, and with small multiplicative factors in time complexity. On the contrary the worst case implementations allow speeding up queries by switching off unneeded info updates during the query (implemented by persistence techniques). After the query is answered the original state of the top tree is used and the query version is discarded.

Using Multilevel Partitioning

Any partitioning of clusters of a tree [math]\displaystyle{ \mathcal{T} }[/math] can be represented by a Cluster Partition Tree CPT[math]\displaystyle{ (\mathcal{T}), }[/math] by replacing each cluster in the tree [math]\displaystyle{ \mathcal{T} }[/math] by an edge. If we use a strategy P for partitioning [math]\displaystyle{ \mathcal{T} }[/math] then the CPT would be CPTP[math]\displaystyle{ \mathcal{T}. }[/math] This is done recursively till only one edge remains.

We would notice that all the nodes of the corresponding top tree [math]\displaystyle{ \Re }[/math] are uniquely mapped into the edges of this multilevel partition. There may be some edges in the multilevel partition that do not correspond to any node in the top tree, these are the edges which represent only a single child in the level below it, i.e. a simple cluster. Only the edges that correspond to composite clusters correspond to nodes in the top tree [math]\displaystyle{ \Re. }[/math]

A partitioning strategy is important while we partition the Tree [math]\displaystyle{ \mathcal{T} }[/math] into clusters. Only a careful strategy ensures that we end up in an [math]\displaystyle{ \mathcal{O}(\log n) }[/math] height Multilevel Partition ( and therefore the top tree).

  • The number of edges in subsequent levels should decrease by a constant factor.
  • If a lower level is changed by an update then we should be able to update the one immediately above it using at most a constant number of insertions and deletions.

The above partitioning strategy ensures the maintenance of the top tree in [math]\displaystyle{ \mathcal{O}(\log n) }[/math] time.

See also

References

  1. Holm, J.; De Lichtenberg, K.; Thorup, M. (2001). "Poly-logarithmic deterministic fully-dynamic algorithms for connectivity, minimum spanning tree, 2-edge, and biconnectivity". Journal of the ACM 48 (4): 723. doi:10.1145/502090.502095. 
  2. Thorup, Mikkel (2000), "Near-optimal fully-dynamic graph connectivity", Proceedings of the Thirty-Second Annual {ACM} Symposium on Theory of Computing 
  3. Holm, Jacob; Rotenberg, Eva; Thorup, Mikkel (2018), "Dynamic Bridge-Finding in [math]\displaystyle{ \tilde{O}(\log ^2 n) }[/math] Amortized Time", Proceedings of the Twenty-Ninth Annual {ACM-SIAM} Symposium on Discrete Algorithms, {SODA} 2018, doi:10.1137/1.9781611975031.3 
  4. Holm, J.; De Lichtenberg, K.; Thorup, M. (2001). "Poly-logarithmic deterministic fully-dynamic algorithms for connectivity, minimum spanning tree, 2-edge, and biconnectivity". Journal of the ACM 48 (4): 723. doi:10.1145/502090.502095. 
  5. Bille, Philip; Gørtz, Inge Li; Landau, Gad M.; Weimann, Oren (2015). "Tree Compression with Top Trees". Inf. Comput. 243: 166–177. doi:10.1016/j.ic.2014.12.012. 

External links