6.1 Find out whether a graph is a tree, a forest or a path
You find out whether a graph has no cycles and at most one parent per node, whether it also has at most one root, and whether every node has at most one child.
Problem
Section titled “Problem”You have data that should be a hierarchy and want to make sure that it is one. It may have a category with two parents, a reference that closes a cycle, or a second root. You want to know this before you treat the graph as a tree, that is, before you index its ancestors, list it in preorder or walk it from the root. A graph with several roots can still be a forest: several trees side by side. The strictest shape is a path: a tree in which every node has at most one child. Some algorithms need that shape. You need this for:
- a hierarchy read from a file or a database, such as categories or folders;
- a graph that a program builds and that should be a tree, such as a syntax tree;
- a sequence of steps that should be a path, such as the versions of a document.
Example
Section titled “Example”The example checks three graphs:
- The first graph
familymodels a family tree that records one parent per person. It has an edge from each parent to each child. - The second graph
linemodels one line of descent in that family, fromAdathroughCleoandFinntoIda. It is built as a graph of its own, with an edge from each of them to the next. - The third graph
dependenciesmodels the packages of a small program. It has an edge from each package to each package it uses.appanduiboth useservice, andmodelandstorageuse each other.
Which of them are forests, trees or paths?
- Package with more than one in-node
- Edge on a cycle
In the picture after, service, model, storage and util are packages with more than one
in-node, and the edges between model and storage lie on a cycle. The line of descent has no
picture. In its graph, Ada, Cleo, Finn and Ida are the nodes 0 to 3. Its edges are 0 -> 1,
1 -> 2 and 2 -> 3.
Solution
Section titled “Solution”// true - one root node, one parent per personboolean familyTree = Graphs.isTree(family);boolean familyPath = Graphs.isPath(family); // false - Ada has two children
// One line of descent, Ada, Cleo, Finn and Ida, as a graph of its own: the nodes// 0 to 3.var builder = new GraphBuilder();builder.addNodes(4);builder.addPath(0, 1, 2, 3);Graph line = builder.build();boolean lineTree = Graphs.isTree(line); // trueboolean linePath = Graphs.isPath(line); // true - one child per person at most
// service has two in-nodes: app and ui. model and storage lie on a cycle.boolean dependenciesForest = Graphs.isForest(dependencies); // false// true - one root node, one parent per personlet family_tree = graphs::is_tree(&family);let family_path = graphs::is_path(&family); // false - Ada has two children
// One line of descent, Ada, Cleo, Finn and Ida, as a graph of its own: the nodes// 0 to 3.let mut builder = GraphBuilder::new();builder.add_nodes(4);builder.add_path(&[0, 1, 2, 3]);let line = builder.build();let line_tree = graphs::is_tree(&line); // truelet line_path = graphs::is_path(&line); // true - one child per person at most
// service has two in-nodes: app and ui. model and storage lie on a cycle.let dependencies_forest = graphs::is_forest(&dependencies); // false// true - one root node, one parent per personconst familyTree = Graphs.isTree(family);const familyPath = Graphs.isPath(family); // false - Ada has two children
// One line of descent, Ada, Cleo, Finn and Ida, as a graph of its own: the nodes// 0 to 3.const builder = new GraphBuilder();builder.addNodes(4);builder.addPath(0, 1, 2, 3);const line = builder.build();const lineTree = Graphs.isTree(line); // trueconst linePath = Graphs.isPath(line); // true - one child per person at most
// service has two in-nodes: app and ui. model and storage lie on a cycle.const dependenciesForest = Graphs.isForest(dependencies); // false// true - one root node, one parent per personbool familyTree = Graphs.IsTree(family);bool familyPath = Graphs.IsPath(family); // false - Ada has two children
// One line of descent, Ada, Cleo, Finn and Ida, as a graph of its own: the nodes// 0 to 3.var builder = new GraphBuilder();builder.AddNodes(4);builder.AddPath(0, 1, 2, 3);Graph line = builder.Build();bool lineTree = Graphs.IsTree(line); // truebool linePath = Graphs.IsPath(line); // true - one child per person at most
// service has two in-nodes: app and ui. model and storage lie on a cycle.bool dependenciesForest = Graphs.IsForest(dependencies); // falseThe family is a tree but not a path, since some people have several children. The line of descent
is both. The packages are not even a forest: service has two in-nodes, and model and storage
lie on a cycle.
Good to know
Section titled “Good to know”- Edges point away from the root. These checks expect edges from parent to child. For a tree
stored with edges from child to parent, check the
transposed graph:
BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedofBiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of. - Before an ancestor index.
Ancestry.ofAncestry::ofAncestry.ofAncestry.Oftakes only a forest and rejects any other graph; check withGraphs.isForestgraphs::is_forestGraphs.isForestGraphs.IsForestfirst if the graph may not be one. See Find the parent and the depth of each node. - Direction does not matter? Then the question is whether the undirected reading is a tree. See Find out whether an undirected graph is a tree or a forest.
- A self-loop is a cycle. A graph with an edge
a -> ais not a forest. The undirected reading differs here: its checks ignore self-loops. - The empty graph counts as a tree, a forest and a path.
API used
Section titled “API used”Graphs.isForest(Graph)graphs::is_forestGraphs.isForestGraphs.IsForestGraphs.isTree(Graph)graphs::is_treeGraphs.isTreeGraphs.IsTreeGraphs.isPath(Graph)graphs::is_pathGraphs.isPathGraphs.IsPathGraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addPath(int...)GraphBuilder::add_pathGraphBuilder.addPath(...nodes: number[])GraphBuilder.AddPathGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.BuildBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.Transposed