Skip to content

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.

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.

The example checks three graphs, each read as undirected:

  • The first graph calls models the calls between the methods of a small program. It has an edge from each method to each method it calls.
  • The second graph rest models the same calls without those made by main and run. See Find the components of a graph read as undirected for another use of this graph.
  • The third graph family models 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?

The calls without those of main and run
All the calls
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Context
  • 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.

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 trees
boolean restTree = Graphs.isUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.
boolean callsForest = Graphs.isUndirectedForest(calls); // false
Full example
let 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 trees
let 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); // false
Full example
const 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 trees
const restTree = Graphs.isUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.
const callsForest = Graphs.isUndirectedForest(calls); // false
Full example
bool 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 trees
bool restTree = Graphs.IsUndirectedTree(rest); // false - not connected
// Read as undirected, run, save, format and show lie on a cycle.
bool callsForest = Graphs.IsUndirectedForest(calls); // false
Full example

Without 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.