6.2 Find the parent and the depth of each node
You build an ancestor index that gives the parent and the depth of every node of a forest.
Problem
Section titled “Problem”You have a hierarchy and want to know where each node sits in it: which node is directly above it, and how far below the top it lies. The node directly above a node is called its parent. The number of edges from the root down to a node is called its depth. You want both for every node, in one index that also answers questions about ancestors. You need this to find:
- the enclosing element of a node in a syntax tree, and how deeply it is nested;
- the manager of an employee, and the level of management;
- the folder of a file, and how deeply it is nested.
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. Who is the parent of Greta, and in which generation is she? Who is
the parent of Ada? And in which generation is each of the others?
- Person asked about
- Generation
- Parent
In the picture after, the frames show the generation of every person. The generation is the depth
that the index returns. Dora is the parent of Greta. Ada is the root and has no parent.
Solution
Section titled “Solution”Ancestry ancestry = Ancestry.of(family);int parent = ancestry.parentOf(greta); // 3 - Doraint generation = ancestry.depthOf(greta); // 3 - three generations below Adaint parentOfAda = ancestry.parentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.int[] generations = new int[family.nodeCount()];for (int person = 0; person < generations.length; person++) { generations[person] = ancestry.depthOf(person);}// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and// Jonas in 3let ancestry = Ancestry::of(&family)?;let parent = ancestry.parent_of(greta); // Some(3) - Doralet generation = ancestry.depth_of(greta); // 3 - three generations below Adalet parent_of_ada = ancestry.parent_of(ada); // None - Ada is the root
// The generation of every person, by node ID.let generations: Vec<u32> = family .nodes() .map(|person| ancestry.depth_of(person)) .collect();// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and// Jonas in 3const ancestry = Ancestry.of(family);const parent = ancestry.parentOf(greta); // 3 - Doraconst generation = ancestry.depthOf(greta); // 3 - three generations below Adaconst parentOfAda = ancestry.parentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.const generations = new Int32Array(family.nodeCount);for (let person = 0; person < generations.length; person++) { generations[person] = ancestry.depthOf(person);}// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and// Jonas in 3Ancestry ancestry = Ancestry.Of(family);int parent = ancestry.ParentOf(greta); // 3 - Doraint generation = ancestry.DepthOf(greta); // 3 - three generations below Adaint parentOfAda = ancestry.ParentOf(ada); // -1 - Ada is the root
// The generation of every person, by node ID.int[] generations = new int[family.NodeCount];for (int person = 0; person < generations.Length; person++){ generations[person] = ancestry.DepthOf(person);}// generations: [0, 1, 1, 2, 2, 2, 3, 3, 3, 3] - Ada in 0, Greta, Hugo, Ida and// Jonas in 3Dora is the parent of Greta, who is three generations below Ada. Ada is the root and has no
parent, so Ancestry.parentOfAncestry::parent_ofAncestry.parentOfAncestry.ParentOf returns
-1None-1-1 for her. The depths of all the people are the
generations of the picture after.
Good to know
Section titled “Good to know”- Only for forests.
Ancestry.ofAncestry::ofAncestry.ofAncestry.Ofrejects a graph in which a node has more than one in-node, or which has a cycle (IllegalArgumentExceptionErrorKind::InvalidArgumentErrorArgumentException). If the graph may not be a forest, check it withGraphs.isForestgraphs::is_forestGraphs.isForestGraphs.IsForestfirst. See Find out whether a graph is a tree, a forest or a path. - One index, many questions. The same
AncestryAncestryAncestryAncestryfinds the lowest common ancestor of two nodes and tells whether one node is an ancestor of another. See Find the lowest common ancestor of two nodes and Test many times whether one node is an ancestor of another. - Edges from child to parent. Build the index of the
transposed graph:
BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedofBiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of. - The parent alone is also the single in-node of a node, which
BiAdjacentGraph.inNodesBiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodesreturns. The depths and the questions about ancestors need the ancestor index.
API used
Section titled “API used”Ancestry.of(Graph)Ancestry::ofAncestry.of(forest: Graph)Ancestry.Of(Graph)Ancestry.parentOf(int)Ancestry::parent_ofAncestry.parentOfAncestry.ParentOfAncestry.depthOf(int)Ancestry::depth_ofAncestry.depthOfAncestry.DepthOfGraphs.isForest(Graph)graphs::is_forestGraphs.isForestGraphs.IsForestBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedBiAdjacentGraph.inNodes(int)BiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodes