pub struct Ancestry { /* private fields */ }Expand description
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_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 node_count) 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 is_ancestor_of(a, a) is true and
lowest_common_ancestor_of(a, a) is Some(a). Two nodes in different trees of the forest have no
common ancestor. Uses heavy-light decomposition.
use graphity_core::{Ancestry, GraphBuilder, PreorderAncestry};
// 0
// 1 2
// 3 4 5
let mut b = GraphBuilder::new();
b.add_nodes(6);
b.add_edge_pairs(&[0, 1, 0, 2, 1, 3, 1, 4, 2, 5]);
let ancestry = Ancestry::of(&b.build()).unwrap();
assert_eq!(ancestry.lowest_common_ancestor_of(3, 4), Some(1));
assert_eq!(ancestry.lowest_common_ancestor_of(4, 5), Some(0));
assert!(ancestry.is_ancestor_of(1, 4));
assert_eq!(ancestry.depth_of(5), 2);
let preorder = PreorderAncestry::of(&ancestry);
assert_eq!(preorder.subtree_of(1), [1, 3, 4]);Where Java’s PreorderAncestry extends Ancestry and overrides isAncestorOf, a
PreorderAncestry dereferences to its Ancestry, and an Ancestry that belongs to a
PreorderAncestry answers is_ancestor_of in O(1) as well.
Instances are immutable and safe to share between threads (Send and Sync).
Memory: 12 x node_count bytes, of which an index built by Ancestry::of_dominators shares
4 x node_count bytes with the dominators.
Implementations§
Source§impl Ancestry
impl Ancestry
Sourcepub fn of(forest: &Graph) -> Result<Ancestry, GraphError>
pub fn of(forest: &Graph) -> Result<Ancestry, GraphError>
Returns the ancestor index of forest, whose edges point from parent to child - Java’s overload
of(Graph).
In such a forest, no node has more than one in-node, and there is no cycle, not even a self-loop;
graphs::is_forest tests this without an error. The roots of the
trees are the root nodes of the graph.
Time: O(node_count + edge_count). Memory: 12 x node_count bytes for the result; while it runs, 12 x node_count bytes more.
§Errors
An InvalidArgument error if a node of forest has more
than one in-node, or forest has a cycle.
Sourcepub fn of_dominators(dominators: &Dominators) -> Ancestry
pub fn of_dominators(dominators: &Dominators) -> Ancestry
Returns the ancestor index of the dominator tree of dominators, in which the ancestors of a node
are its dominators - Java’s overload of(Dominators).
is_ancestor_of(a, b) returns whether a dominates b. lowest_common_ancestor_of(a, b) returns
the closest node that dominates both, or None 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::is_reachable returns false for such a node.
The result is the same as that of Ancestry::of(&graphs::dominator_tree(dominators)), but shares
the immediate dominators instead of building the tree as a graph.
Time: O(node_count). Memory: 8 x node_count bytes for the result, which shares the immediate dominators; while it runs, 20 x node_count bytes more.
Sourcepub fn lowest_common_ancestor_of(&self, a: u32, b: u32) -> Option<u32>
pub fn lowest_common_ancestor_of(&self, a: u32, b: u32) -> Option<u32>
Returns the lowest common ancestor of a and b, or None if they are in different trees -
Java’s -1.
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 node_count).
§Panics
With an index out of bounds message if a or b is not in 0..node_count.
Sourcepub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
pub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
Returns whether node is an ancestor of descendant: whether it lies on the path from the root of
its tree to descendant. Equivalent to
lowest_common_ancestor_of(node, descendant) == Some(node).
A node is its own ancestor. PreorderAncestry answers in O(1), and so does an Ancestry that
belongs to one.
Time: O(log node_count), or O(1) for the Ancestry of a PreorderAncestry.
§Panics
With an index out of bounds message if node or descendant is not in 0..node_count.