2.4 Find the starting points of a graph
You get the root nodes, which no edge leads to.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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.
- Step asked about
- Root node
Solution
Section titled “Solution”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 lintboolean compileIsRoot = steps.isRootNode(compile); // false - it waits for generate// 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 lintlet compile_is_root = steps.is_root_node(compile); // false - it waits for generate// 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 lintconst compileIsRoot = steps.isRootNode(compile); // false - it waits for generate// 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 generatebool compileIsRoot = steps.IsRootNode(compile);The root nodes are generate and lint, which are green in the picture after. compile is not
one, since it waits for generate.
Good to know
Section titled “Good to know”- A DAG (directed acyclic graph) with at least one node has a root node, and a search from all its root nodes reaches every node. A graph with cycles may have no root node, since every node on a cycle has an in-node. Use the root nodes of its condensed graph instead. These are the SCCs (strongly connected components) that no edge from another SCC leads to. A search from one node of each such SCC reaches every node. See Condense each group into one node to get a DAG for how to build this graph.
- Leaves are the nodes without
out-nodes. You can find them without a
BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph: they are the nodes whoseGraph.outDegreeGraph::out_degreeGraph.outDegreeGraph.OutDegreeis 0. - Levels. The root nodes are the nodes of level 0. See Find which nodes can be processed in parallel.
API used
Section titled “API used”BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfGraphs.rootNodes(BiAdjacentGraph)graphs::root_nodesGraphs.rootNodesGraphs.RootNodesBiAdjacentGraph.isRootNode(int)BiAdjacentGraph::is_root_nodeBiAdjacentGraph.isRootNodeBiAdjacentGraph.IsRootNodeGraph.outDegree(int)Graph::out_degreeGraph.outDegreeGraph.OutDegree