Skip to content

1.3 Build trees and paths in a few calls

You add children and parents with their edges, paths through several nodes, and the edges between one node and several others.

You want to build a graph of a common shape in a few calls, because adding it edge by edge is tedious. In a tree, every new node is the child of a node that already exists. In a path, each node has an edge to the next. A third shape is a node with edges to many others. You need this for:

  • hierarchies, such as a family tree, the folders of a file system or an organization chart;
  • sequences, such as the stages of a pipeline;
  • plans in which one step waits for several others, or several steps wait for one.

The first example graph models a family tree of four generations that records one parent for each person. It has an edge from each parent to each child: Ada has the children Ben and Cleo; Ben has Dora and Emil; and so on. The graph is a tree whose root node is Ada:

0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas

The second example graph 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. The steps partly run one after the other and partly wait for several others: compile runs after generate; compileTests, docs and package run after compile; package also waits for lint and test; and so on.

0: generate1: compile2: lint3: compileTests4: docs5: test6: package7: sign8: publish

How can both be written without listing every edge?

GraphBuilder.addChildGraphBuilder::add_childGraphBuilder.addChildGraphBuilder.AddChild adds a node with the edge from its parent and returns the new node ID, so the tree is written from the top, child by child. GraphBuilder.addChildrenGraphBuilder::add_childrenGraphBuilder.addChildrenGraphBuilder.AddChildren adds several children of one parent at once. It returns the node ID of the first child, and the others follow without gaps:

// A family tree: an edge from each parent to each child. addChild adds a new node and
// the edge to it, and returns the new node ID.
var builder = new GraphBuilder();
int ada = builder.addNode(); // 0
int ben = builder.addChild(ada); // 1
int cleo = builder.addChild(ada); // 2
int dora = builder.addChild(ben); // 3
builder.addChild(ben); // 4 - Emil
int finn = builder.addChild(cleo); // 5
builder.addChildren(dora, 2); // returns 6 - Greta, then Hugo as 7
builder.addChildren(finn, 2); // returns 8 - Ida, then Jonas as 9
Graph family = builder.build();
Full example
// A family tree: an edge from each parent to each child. add_child adds a new node
// and the edge to it, and returns the new node ID.
let mut builder = GraphBuilder::new();
let ada = builder.add_node(); // 0
let ben = builder.add_child(ada); // 1
let cleo = builder.add_child(ada); // 2
let dora = builder.add_child(ben); // 3
builder.add_child(ben); // 4 - Emil
let finn = builder.add_child(cleo); // 5
builder.add_children(dora, 2); // returns 6 - Greta, then Hugo as 7
builder.add_children(finn, 2); // returns 8 - Ida, then Jonas as 9
let family = builder.build();
Full example
// A family tree: an edge from each parent to each child. addChild adds a new node and
// the edge to it, and returns the new node ID.
const builder = new GraphBuilder();
const ada = builder.addNode(); // 0
const ben = builder.addChild(ada); // 1
const cleo = builder.addChild(ada); // 2
const dora = builder.addChild(ben); // 3
builder.addChild(ben); // 4 - Emil
const finn = builder.addChild(cleo); // 5
builder.addChildren(dora, 2); // returns 6 - Greta, then Hugo as 7
builder.addChildren(finn, 2); // returns 8 - Ida, then Jonas as 9
const family = builder.build();
Full example
// A family tree: an edge from each parent to each child. AddChild adds a new node
// and the edge to it, and returns the new node ID.
var builder = new GraphBuilder();
int ada = builder.AddNode(); // 0
int ben = builder.AddChild(ada); // 1
int cleo = builder.AddChild(ada); // 2
int dora = builder.AddChild(ben); // 3
builder.AddChild(ben); // 4 - Emil
int finn = builder.AddChild(cleo); // 5
builder.AddChildren(dora, 2); // returns 6 - Greta, then Hugo as 7
builder.AddChildren(finn, 2); // returns 8 - Ida, then Jonas as 9
Graph family = builder.Build();
Full example

The result is the family tree of the first picture. Its node IDs follow the order in which the people were added.

The build steps are made of paths and of nodes with several edges. GraphBuilder.addPathGraphBuilder::add_pathGraphBuilder.addPathGraphBuilder.AddPath adds an edge from each node of a path to the next. GraphBuilder.addOutEdgesGraphBuilder::add_out_edgesGraphBuilder.addOutEdgesGraphBuilder.AddOutEdges adds edges from one node to several others. GraphBuilder.addInEdgesGraphBuilder::add_in_edgesGraphBuilder.addInEdgesGraphBuilder.AddInEdges adds edges from several nodes to one. Both take the one node first and then the others, so addInEdges(6, 2, 5)add_in_edges(6, &[2, 5])addInEdges(6, 2, 5)AddInEdges(6, 2, 5) adds 2 -> 6 and 5 -> 6:

// The steps that build and publish a program, as the nodes 0 to 8: generate, compile,
// lint, compileTests, docs, test, package, sign and publish. An edge a -> b means
// that step b may start only after step a.
var builder = new GraphBuilder();
builder.addNodes(9);
builder.addPath(0, 1, 3, 5); // generate -> compile -> compileTests -> test
builder.addOutEdges(1, 4, 6); // compile -> docs, compile -> package
// lint -> package, test -> package: the target comes first
builder.addInEdges(6, 2, 5);
builder.addPath(6, 7, 8); // package -> sign -> publish
builder.addEdge(4, 8); // docs -> publish
Graph steps = builder.build();
// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],
// 7 -> [8], 8 -> []
Full example
// The steps that build and publish a program, as the nodes 0 to 8: generate, compile,
// lint, compileTests, docs, test, package, sign and publish. An edge a -> b means
// that step b may start only after step a.
let mut builder = GraphBuilder::new();
builder.add_nodes(9);
builder.add_path(&[0, 1, 3, 5]); // generate -> compile -> compileTests -> test
builder.add_out_edges(1, &[4, 6]); // compile -> docs, compile -> package
// lint -> package, test -> package: the target comes first
builder.add_in_edges(6, &[2, 5]);
builder.add_path(&[6, 7, 8]); // package -> sign -> publish
builder.add_edge(4, 8); // docs -> publish
let steps = builder.build();
// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],
// 7 -> [8], 8 -> []
Full example
// The steps that build and publish a program, as the nodes 0 to 8: generate, compile,
// lint, compileTests, docs, test, package, sign and publish. An edge a -> b means
// that step b may start only after step a.
const builder = new GraphBuilder();
builder.addNodes(9);
builder.addPath(0, 1, 3, 5); // generate -> compile -> compileTests -> test
builder.addOutEdges(1, 4, 6); // compile -> docs, compile -> package
// lint -> package, test -> package: the target comes first
builder.addInEdges(6, 2, 5);
builder.addPath(6, 7, 8); // package -> sign -> publish
builder.addEdge(4, 8); // docs -> publish
const steps = builder.build();
// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],
// 7 -> [8], 8 -> []
Full example
// The steps that build and publish a program, as the nodes 0 to 8: generate,
// compile, lint, compileTests, docs, test, package, sign and publish. An edge
// a -> b means that step b may start only after step a.
var builder = new GraphBuilder();
builder.AddNodes(9);
builder.AddPath(0, 1, 3, 5); // generate -> compile -> compileTests -> test
builder.AddOutEdges(1, 4, 6); // compile -> docs, compile -> package
// lint -> package, test -> package: the target comes first
builder.AddInEdges(6, 2, 5);
builder.AddPath(6, 7, 8); // package -> sign -> publish
builder.AddEdge(4, 8); // docs -> publish
Graph steps = builder.Build();
// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6],
// 6 -> [7], 7 -> [8], 8 -> []
Full example

The result is the graph of the second picture, with its ten edges.