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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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:
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.
How can both be written without listing every edge?
Solution
Section titled “Solution”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(); // 0int ben = builder.addChild(ada); // 1int cleo = builder.addChild(ada); // 2int dora = builder.addChild(ben); // 3builder.addChild(ben); // 4 - Emilint finn = builder.addChild(cleo); // 5builder.addChildren(dora, 2); // returns 6 - Greta, then Hugo as 7builder.addChildren(finn, 2); // returns 8 - Ida, then Jonas as 9Graph family = builder.build();// 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(); // 0let ben = builder.add_child(ada); // 1let cleo = builder.add_child(ada); // 2let dora = builder.add_child(ben); // 3builder.add_child(ben); // 4 - Emillet finn = builder.add_child(cleo); // 5builder.add_children(dora, 2); // returns 6 - Greta, then Hugo as 7builder.add_children(finn, 2); // returns 8 - Ida, then Jonas as 9let family = builder.build();// 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(); // 0const ben = builder.addChild(ada); // 1const cleo = builder.addChild(ada); // 2const dora = builder.addChild(ben); // 3builder.addChild(ben); // 4 - Emilconst finn = builder.addChild(cleo); // 5builder.addChildren(dora, 2); // returns 6 - Greta, then Hugo as 7builder.addChildren(finn, 2); // returns 8 - Ida, then Jonas as 9const family = builder.build();// 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(); // 0int ben = builder.AddChild(ada); // 1int cleo = builder.AddChild(ada); // 2int dora = builder.AddChild(ben); // 3builder.AddChild(ben); // 4 - Emilint finn = builder.AddChild(cleo); // 5builder.AddChildren(dora, 2); // returns 6 - Greta, then Hugo as 7builder.AddChildren(finn, 2); // returns 8 - Ida, then Jonas as 9Graph family = builder.Build();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 -> testbuilder.addOutEdges(1, 4, 6); // compile -> docs, compile -> package// lint -> package, test -> package: the target comes firstbuilder.addInEdges(6, 2, 5);builder.addPath(6, 7, 8); // package -> sign -> publishbuilder.addEdge(4, 8); // docs -> publishGraph steps = builder.build();// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],// 7 -> [8], 8 -> []// 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 -> testbuilder.add_out_edges(1, &[4, 6]); // compile -> docs, compile -> package// lint -> package, test -> package: the target comes firstbuilder.add_in_edges(6, &[2, 5]);builder.add_path(&[6, 7, 8]); // package -> sign -> publishbuilder.add_edge(4, 8); // docs -> publishlet steps = builder.build();// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],// 7 -> [8], 8 -> []// 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 -> testbuilder.addOutEdges(1, 4, 6); // compile -> docs, compile -> package// lint -> package, test -> package: the target comes firstbuilder.addInEdges(6, 2, 5);builder.addPath(6, 7, 8); // package -> sign -> publishbuilder.addEdge(4, 8); // docs -> publishconst steps = builder.build();// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6], 6 -> [7],// 7 -> [8], 8 -> []// 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 -> testbuilder.AddOutEdges(1, 4, 6); // compile -> docs, compile -> package// lint -> package, test -> package: the target comes firstbuilder.AddInEdges(6, 2, 5);builder.AddPath(6, 7, 8); // package -> sign -> publishbuilder.AddEdge(4, 8); // docs -> publishGraph steps = builder.Build();// steps: 0 -> [1], 1 -> [3, 4, 6], 2 -> [6], 3 -> [5], 4 -> [8], 5 -> [6],// 6 -> [7], 7 -> [8], 8 -> []The result is the graph of the second picture, with its ten edges.
Good to know
Section titled “Good to know”- New parents.
GraphBuilder.addParentGraphBuilder::add_parentGraphBuilder.addParentGraphBuilder.AddParentandGraphBuilder.addParentsGraphBuilder::add_parentsGraphBuilder.addParentsGraphBuilder.AddParentsadd new nodes, each with an edge from it to a node that exists. The new nodes become the in-nodes of that node. This fits the ancestors of a person, with two new parents at a time. It also fits several new steps that one existing step waits for. - Lists from Graphity.
GraphBuilder.addPathGraphBuilder::add_pathGraphBuilder.addPathGraphBuilder.AddPath,GraphBuilder.addOutEdgesGraphBuilder::add_out_edgesGraphBuilder.addOutEdgesGraphBuilder.AddOutEdgesandGraphBuilder.addInEdgesGraphBuilder::add_in_edgesGraphBuilder.addInEdgesGraphBuilder.AddInEdgestake any list of nodes that Graphity returns, such as the out-nodes of another graph, without copying it into an array first. - Missing nodes. As for single edges, the path methods and the methods for the edges of one
node each have a twin ending in
AndNodes_and_nodesAndNodesAndNodes, such asGraphBuilder.addPathAndNodesGraphBuilder::add_path_and_nodesGraphBuilder.addPathAndNodesGraphBuilder.AddPathAndNodes. The twin adds the missing nodes up to the highest node ID it is given, those in between included. The methods for children and parents need no twin: they add new nodes anyway. - Which way the edges point is your choice. This guide lets them point from parent to child and from the step that runs first to the step that runs after it. Whatever you choose, say it once, next to the graph: every result depends on it. The functions of Graphity for trees, such as the tree check and the ancestor index, expect edges from parent to child. For a tree stored with edges from child to parent, pass them the transposed graph. See Find out whether a graph is a tree, a forest or a path.
API used
Section titled “API used”GraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNode()GraphBuilder::add_nodeGraphBuilder.addNodeGraphBuilder.AddNodeGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addChild(int)GraphBuilder::add_childGraphBuilder.addChildGraphBuilder.AddChildGraphBuilder.addChildren(int, int)GraphBuilder::add_childrenGraphBuilder.addChildrenGraphBuilder.AddChildrenGraphBuilder.addParent(int)GraphBuilder::add_parentGraphBuilder.addParentGraphBuilder.AddParentGraphBuilder.addParents(int, int)GraphBuilder::add_parentsGraphBuilder.addParentsGraphBuilder.AddParentsGraphBuilder.addPath(int...)GraphBuilder::add_pathGraphBuilder.addPath(...nodes: number[])GraphBuilder.AddPathGraphBuilder.addPath(IntList)GraphBuilder::add_pathGraphBuilder.addPath(nodes: IntList)GraphBuilder.AddPathGraphBuilder.addPathAndNodes(int...)GraphBuilder::add_path_and_nodesGraphBuilder.addPathAndNodes(...nodes: number[])GraphBuilder.AddPathAndNodesGraphBuilder.addOutEdges(int, int...)GraphBuilder::add_out_edgesGraphBuilder.addOutEdges(source: number, ...targets: number[])GraphBuilder.AddOutEdgesGraphBuilder.addOutEdges(int, IntList)GraphBuilder::add_out_edgesGraphBuilder.addOutEdges(source: number, targets: IntList)GraphBuilder.AddOutEdgesGraphBuilder.addOutEdgesAndNodes(int, int...)GraphBuilder::add_out_edges_and_nodesGraphBuilder.addOutEdgesAndNodes(source: number, ...targets: number[])GraphBuilder.AddOutEdgesAndNodesGraphBuilder.addInEdges(int, int...)GraphBuilder::add_in_edgesGraphBuilder.addInEdges(target: number, ...sources: number[])GraphBuilder.AddInEdgesGraphBuilder.addInEdges(int, IntList)GraphBuilder::add_in_edgesGraphBuilder.addInEdges(target: number, sources: IntList)GraphBuilder.AddInEdgesGraphBuilder.addInEdgesAndNodes(int, int...)GraphBuilder::add_in_edges_and_nodesGraphBuilder.addInEdgesAndNodes(target: number, ...sources: number[])GraphBuilder.AddInEdgesAndNodesGraphBuilder.addEdge(int, int)GraphBuilder::add_edgeGraphBuilder.addEdgeGraphBuilder.AddEdgeGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.Build