Graphity for TypeScript 3.0.0
    Preparing search index...

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

    Memory: 12 x nodeCount bytes, of which an index built from Dominators shares 4 x nodeCount bytes with the dominators.

    Hierarchy (View Summary)

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

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      depth

      RangeError if node is out of range

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

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      • descendant: number

        node ID in 0..nodeCount-1

      Returns boolean

      true if node is an ancestor of descendant

      RangeError if node or descendant is out of range

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

      Parameters

      • a: number

        node ID in 0..nodeCount-1

      • b: number

        node ID in 0..nodeCount-1

      Returns number

      lowest common ancestor, or -1

      RangeError if a or b is out of range

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

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      parent, or -1

      RangeError if node is out of range

    • Returns 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.

      Parameters

      • forest: Graph

        a forest whose edges point from parent to child

      Returns Ancestry

      the ancestor index

      Error if a node of forest has more than one in-node, or forest has a cycle

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

      Parameters

      • dominators: Dominators

        the dominators whose tree to index

      Returns Ancestry

      the ancestor index of the dominator tree