Table of Contents

Class Ancestry

Namespace
Graphity
Assembly
Graphity.Core.dll

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.

public class Ancestry
Inheritance
Ancestry
Derived
Inherited Members

Remarks

Of(Graph) 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()). Of(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; Of(Ancestry) adds an ancestor test in O(1) and the nodes of each subtree.

A node is its own ancestor, so IsAncestorOf(a, a) is true and LowestCommonAncestorOf(a, a) is a. Two nodes in different trees of the forest have no common ancestor. Uses heavy-light decomposition.

Instances are immutable and safe to share between threads. No other assembly can derive from this class; PreorderAncestry is its only subclass.

Memory: 12 x NodeCount bytes, of which an index built by Of(Dominators) shares 4 x NodeCount bytes with the dominators.

Methods

DepthOf(int)

Returns the depth of node: the number of edges from the root of its tree to it. A root has depth 0.

public int DepthOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The depth.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

IsAncestorOf(int, int)

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.

public virtual bool IsAncestorOf(int node, int descendant)

Parameters

node int

Node ID in 0..NodeCount-1.

descendant int

Node ID in 0..NodeCount-1.

Returns

bool

true if node is an ancestor of descendant.

Remarks

A node is its own ancestor. PreorderAncestry answers in O(1).

Time: O(log NodeCount).

Exceptions

ArgumentOutOfRangeException

node or descendant is out of range.

LowestCommonAncestorOf(int, int)

Returns the lowest common ancestor of a and b, or -1 if they are in different trees.

public int LowestCommonAncestorOf(int a, int b)

Parameters

a int

Node ID in 0..NodeCount-1.

b int

Node ID in 0..NodeCount-1.

Returns

int

The lowest common ancestor, or -1.

Remarks

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).

Exceptions

ArgumentOutOfRangeException

a or b is out of range.

Of(Dominators)

Returns the ancestor index of the dominator tree of dominators, in which the ancestors of a node are its dominators.

public static Ancestry Of(Dominators dominators)

Parameters

dominators Dominators

The dominators whose tree to index.

Returns

Ancestry

The ancestor index of the dominator tree.

Remarks

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; IsReachable(int) 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.

Of(Graph)

Returns the ancestor index of forest, whose edges point from parent to child.

public static Ancestry Of(Graph forest)

Parameters

forest Graph

A forest whose edges point from parent to child.

Returns

Ancestry

The ancestor index.

Remarks

In such a forest, no node has more than one in-node, and there is no cycle, not even a self-loop; IsForest(Graph) tests this without an exception. 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.

Exceptions

ArgumentException

A node of forest has more than one in-node, or forest has a cycle.

ParentOf(int)

Returns the parent of node, or -1 if it is the root of its tree.

public int ParentOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The parent, or -1.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.