5.5 Find out whether an undirected graph is a tree or a forest
You find out whether a graph read as undirected has no cycles apart from self-loops, and whether it is connected as well.
Problem
Section titled “Problem”You want to know whether a graph has the shape of a tree, whatever the direction of its edges. Read as undirected, a graph without cycles is a forest. A forest that is connected is a tree. You need this for:
- a network that should have no redundant connections;
- a hierarchy that someone stored as plain links, without a direction from parent to child;
- the result of an algorithm that should be a tree, such as a set of connections that connects all nodes of a network with as few connections as possible.
Example
Section titled “Example”The example checks three graphs, each read as undirected:
- The first graph
callsmodels the calls between the methods of a small program. It has an edge from each method to each method it calls. - The second graph
restmodels the same calls without those made bymainandrun. See Find the components of a graph read as undirected for another use of this graph. - The third graph
familymodels a family tree that records one parent per person. It has an edge from each parent to each child.
Which of the three are forests, and which are trees?
- Call on the cycle
The picture after shows all the calls, and its orange edges lie on a cycle through run, save,
format and show. The family tree has no picture here.
Solution
Section titled “Solution”boolean familyTree = Graphs.isUndirectedTree(family); // true
// The self-loops of parse and format do not count as cycles here.boolean restForest = Graphs.isUndirectedForest(rest); // true - five treesboolean restTree = Graphs.isUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.boolean callsForest = Graphs.isUndirectedForest(calls); // falselet family_tree = graphs::is_undirected_tree(&family); // true
// The self-loops of parse and format do not count as cycles here.let rest_forest = graphs::is_undirected_forest(&rest); // true - five treeslet rest_tree = graphs::is_undirected_tree(&rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.let calls_forest = graphs::is_undirected_forest(&calls); // falseconst familyTree = Graphs.isUndirectedTree(family); // true
// The self-loops of parse and format do not count as cycles here.const restForest = Graphs.isUndirectedForest(rest); // true - five treesconst restTree = Graphs.isUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.const callsForest = Graphs.isUndirectedForest(calls); // falsebool familyTree = Graphs.IsUndirectedTree(family); // true
// The self-loops of parse and format do not count as cycles here.bool restForest = Graphs.IsUndirectedForest(rest); // true - five treesbool restTree = Graphs.IsUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.bool callsForest = Graphs.IsUndirectedForest(calls); // falseWithout the calls of main and run, each of the five
components is a tree once the self-loops of parse
and format are ignored. Together they are a forest. They are not a tree, since they are not
connected. With all the calls, run, save, format and show lie on a cycle, so the calls are
not even a forest. The family tree is a tree in either reading.
Good to know
Section titled “Good to know”- Self-loops are ignored by these checks:
parseandformatcall themselves, and their components are still trees. To count a self-loop as a cycle, also checkGraph.containsSelfLoopGraph::contains_self_loopGraph.containsSelfLoopGraph.ContainsSelfLoopfor every node. A self-loop does make a graph not bipartite. See Find out whether a graph is bipartite. - Edges in both directions count as one undirected edge: a tree may store each of its edges as
a -> band asb -> a. - Edges that point away from the root. Whether a graph is a tree in which every edge points from a parent to a child is a different question. See Find out whether a graph is a tree, a forest or a path.
- The empty graph is a tree and a forest.
API used
Section titled “API used”Graphs.isUndirectedTree(Graph)graphs::is_undirected_treeGraphs.isUndirectedTreeGraphs.IsUndirectedTreeGraphs.isUndirectedForest(Graph)graphs::is_undirected_forestGraphs.isUndirectedForestGraphs.IsUndirectedForestGraph.containsSelfLoop(int)Graph::contains_self_loopGraph.containsSelfLoopGraph.ContainsSelfLoop