Skip to content

2.4 Find the starting points of a graph

You get the root nodes, which no edge leads to.

Where does a graph start? The nodes that no edge leads to are called root nodes. A schedule starts there, and so does a search of the whole graph. You need this to find:

  • the tasks that need no other task and can start right away;
  • the packages that no other package uses, which are often the entry points of a program;
  • the people at the top of a hierarchy.

The example graph steps models the steps that build and publish a small program. It has an edge a -> b if step b may start only after step a. Which steps can start right away, and is compile one of them? The picture after shows the root nodes in green.

The steps of the build
The root nodes of the build
0: generate1: compile2: lint3: compileTests4: docs5: test6: package7: sign8: publish
0: generate1: compile2: lint3: compileTests4: docs5: test6: package7: sign8: publish
Input
  • Step asked about
Output
  • Root node

A root node has no in-nodes, so the graph has to know the in-nodes of every node. BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of builds a BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph, which stores them:

// Root nodes have no in-nodes, so the graph needs its in-nodes.
BiAdjacentGraph steps = BiAdjacentGraph.of(SmallProgram.buildSteps());
SortedIntView roots = Graphs.rootNodes(steps); // [0, 2] - generate and lint
boolean compileIsRoot = steps.isRootNode(compile); // false - it waits for generate
Full example
// Root nodes have no in-nodes, so the graph needs its in-nodes.
let steps = BiAdjacentGraph::of(&small_program::build_steps());
let roots = graphs::root_nodes(&steps); // [0, 2] - generate and lint
let compile_is_root = steps.is_root_node(compile); // false - it waits for generate
Full example
// Root nodes have no in-nodes, so the graph needs its in-nodes.
const steps = BiAdjacentGraph.of(SmallProgram.buildSteps());
const roots = Graphs.rootNodes(steps); // [0, 2] - generate and lint
const compileIsRoot = steps.isRootNode(compile); // false - it waits for generate
Full example
// Root nodes have no in-nodes, so the graph needs its in-nodes.
BiAdjacentGraph steps = BiAdjacentGraph.Of(SmallProgram.BuildSteps());
SortedIntView roots = Graphs.RootNodes(steps); // [0, 2] - generate and lint
// false - it waits for generate
bool compileIsRoot = steps.IsRootNode(compile);
Full example

The root nodes are generate and lint, which are green in the picture after. compile is not one, since it waits for generate.