Returns whether node is an ancestor of descendant: whether it lies on the path from the
root of its tree to descendant. Equivalent to
lowestCommonAncestorOf(node, descendant) === node.
A node is its own ancestor. PreorderAncestry answers in O(1).
Time: O(log nodeCount).
node ID in 0..nodeCount-1
node ID in 0..nodeCount-1
true if node is an ancestor of descendant
Returns the lowest common ancestor of a and b, or -1 if they are in different trees.
The lowest common ancestor is the deepest node that is an ancestor of both. As a node is its
own ancestor, the result is a if a is an ancestor of b.
Time: O(log nodeCount).
node ID in 0..nodeCount-1
node ID in 0..nodeCount-1
lowest common ancestor, or -1
StaticofReturns the ancestor index of forest, whose edges point from parent to child.
In such a forest, no node has more than one in-node, and there is no cycle, not even a self-loop; Graphs.isForest tests this without an error. The roots of the trees are the root nodes of the graph.
Time: O(nodeCount + edgeCount). Memory: 12 x nodeCount bytes for the result; while it runs, 12 x nodeCount bytes more.
a forest whose edges point from parent to child
the ancestor index
Returns the ancestor index of the dominator tree of dominators, in which the ancestors of a
node are its dominators.
isAncestorOf(a, b) returns whether a dominates b. lowestCommonAncestorOf(a, b) returns
the closest node that dominates both, or -1 if no node does. With several entries, only a
virtual root lies above them. A node that no entry reaches is a tree of its own and dominates
only itself here; Dominators.isReachable returns false for such a node.
The result is the same as that of Ancestry.of(Graphs.dominatorTree(dominators)), but shares
the immediate dominators instead of building the tree as a graph.
Time: O(nodeCount). Memory: 8 x nodeCount bytes for the result, which shares the immediate dominators; while it runs, 20 x nodeCount bytes more.
the dominators whose tree to index
the ancestor index of the dominator tree
An index of a forest for ancestor queries: the parent and depth of a node, whether one node is an ancestor of another, and the lowest common ancestor of two nodes.
Ancestry.of builds the index of a forest whose edges point from parent to child, such as a syntax tree. For a forest whose edges point from child to parent, pass the transposed graph:
Ancestry.of(BiAdjacentGraph.of(forest).transposed()). Ancestry.of with Dominators builds the index of a dominator tree, in which the ancestors of a node are its dominators. The index is built once and answers each query in O(log nodeCount) at most, however deep the forest; PreorderAncestry.of adds an ancestor test in O(1) and the nodes of each subtree.A node is its own ancestor, so
isAncestorOf(a, a)istrueandlowestCommonAncestorOf(a, a)isa. Two nodes in different trees of the forest have no common ancestor. Uses heavy-light decomposition.Instances are immutable.
Memory: 12 x nodeCount bytes, of which an index built from Dominators shares 4 x nodeCount bytes with the dominators.