6.4 Test many times whether one node is an ancestor of another
You test whether a node is an ancestor of another, in logarithmic time with the ancestor index and in constant time with the index with the preorder.
Problem
Section titled “Problem”You want to know whether one node lies above another in a hierarchy, and you ask that very often. In a tree, a node that lies above another is called its ancestor. An analysis may ask this for every pair of nodes it looks at. That can be millions of times, so each answer should be cheap. You need this to find out:
- whether an expression lies inside a given loop or block of a program;
- whether an employee belongs to a department, directly or through one of its sub-departments;
- whether a file lies in a folder or in one of its subfolders;
- whether a person descends from another.
Example
Section titled “Example”The example graph family models a family tree that records one
parent per person. It has an edge from each parent to each
child. Is Ben an ancestor of Hugo? Is Cleo? Is Hugo himself?
- Person asked about
- Other ancestor of Hugo
The ancestors of Hugo are the nodes on the path from the root to him, including Hugo himself.
In the picture after, Ben and Hugo are two of them and Cleo is not. Ada and Dora are the
other ancestors, whom the example does not ask about.
Solution
Section titled “Solution”Ancestry ancestry = Ancestry.of(family);// true - his grandfatherboolean benIsAncestor = ancestry.isAncestorOf(ben, hugo);// false - his great-auntboolean cleoIsAncestor = ancestry.isAncestorOf(cleo, hugo);// true - his own ancestorboolean hugoIsAncestor = ancestry.isAncestorOf(hugo, hugo);
// For many tests, the index with the preorder answers each one in constant time.PreorderAncestry index = PreorderAncestry.of(ancestry);boolean fastBenIsAncestor = index.isAncestorOf(ben, hugo); // truelet ancestry = Ancestry::of(&family)?;let ben_is_ancestor = ancestry.is_ancestor_of(ben, hugo); // true - his grandfatherlet cleo_is_ancestor = ancestry.is_ancestor_of(cleo, hugo); // false - his great-auntlet hugo_is_ancestor = ancestry.is_ancestor_of(hugo, hugo); // true - his own ancestor
// For many tests, the index with the preorder answers each one in constant time.let index = PreorderAncestry::of(&ancestry);let fast_ben_is_ancestor = index.is_ancestor_of(ben, hugo); // trueconst ancestry = Ancestry.of(family);const benIsAncestor = ancestry.isAncestorOf(ben, hugo); // true - his grandfatherconst cleoIsAncestor = ancestry.isAncestorOf(cleo, hugo); // false - his great-auntconst hugoIsAncestor = ancestry.isAncestorOf(hugo, hugo); // true - his own ancestor
// For many tests, the index with the preorder answers each one in constant time.const index = PreorderAncestry.of(ancestry);const fastBenIsAncestor = index.isAncestorOf(ben, hugo); // trueAncestry ancestry = Ancestry.Of(family);// true - his grandfatherbool benIsAncestor = ancestry.IsAncestorOf(ben, hugo);// false - his great-auntbool cleoIsAncestor = ancestry.IsAncestorOf(cleo, hugo);// true - his own ancestorbool hugoIsAncestor = ancestry.IsAncestorOf(hugo, hugo);
// For many tests, the index with the preorder answers each one in constant time.PreorderAncestry index = PreorderAncestry.Of(ancestry);bool fastBenIsAncestor = index.IsAncestorOf(ben, hugo); // trueBen is the grandfather of Hugo and so an ancestor of him. Cleo is his great-aunt and no
ancestor of him. Hugo is his own ancestor. The index with the
preorder gives the same answers, each in constant time
instead of logarithmic time.
Good to know
Section titled “Good to know”- Which index.
AncestryAncestryAncestryAncestryanswers in logarithmic time at most, however deep the tree.PreorderAncestryPreorderAncestryPreorderAncestryPreorderAncestryanswers in constant time and shares the arrays of the index it is built from. Building it takes time and memory of its own, so for a few tests the ancestor index is enough. - How the index with the preorder answers. It lists the nodes in preorder, in which every
subtree takes a range of consecutive positions. So a node
ais an ancestor of a nodebexactly if the position ofblies in the range of the subtree ofa. See List a node and all its descendants. - A node is its own ancestor in both indexes.
- The lowest common ancestor of two nodes comes from the same index. See Find the lowest common ancestor of two nodes.
- Not a tree? In a DAG (directed acyclic graph), a node is an ancestor of another exactly if it reaches it. A family with both parents recorded is such a graph. For many such tests, see Answer many reachability questions at once.
API used
Section titled “API used”Ancestry.of(Graph)Ancestry::ofAncestry.of(forest: Graph)Ancestry.Of(Graph)Ancestry.isAncestorOf(int, int)Ancestry::is_ancestor_ofAncestry.isAncestorOfAncestry.IsAncestorOfPreorderAncestry.of(Ancestry)PreorderAncestry::ofPreorderAncestry.ofPreorderAncestry.OfPreorderAncestry.isAncestorOf(int, int)PreorderAncestry::is_ancestor_ofPreorderAncestry.isAncestorOfPreorderAncestry.IsAncestorOf