Skip to content

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.

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.

The example checks three graphs:

  • The first graph family models a family tree that records one parent per person. It has an edge from each parent to each child.
  • The second graph line models one line of descent in that family, from Ada through Cleo and Finn to Ida. It is built as a graph of its own, with an edge from each of them to the next.
  • The third graph dependencies models the packages of a small program. It has an edge from each package to each package it uses. app and ui both use service, and model and storage use each other.

Which of them are forests, trees or paths?

The family tree
The packages, not a forest
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
0: app1: cli2: ui3: service4: model5: storage6: json7: util
Context
  • 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.

// true - one root node, one parent per person
boolean 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); // true
boolean 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
Full example
// true - one root node, one parent per person
let 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); // true
let 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
Full example
// true - one root node, one parent per person
const 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); // true
const 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
Full example
// true - one root node, one parent per person
bool 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); // true
bool 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); // false
Full example

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