Creates an empty builder, without nodes or edges.
Adds a node with an edge from parent to it, and returns its node ID.
Time: O(1) amortized.
node ID in 0..nodeCount-1
node ID of the new node
Adds count nodes, each with an edge from parent to it, and returns the node ID of the
first; the others follow in order.
Time: O(count) amortized.
node ID in 0..nodeCount-1
number of nodes to add
node ID of the first new node
Adds the edge source -> target.
An edge added more than once is still one edge in the graph: GraphBuilder.build merges duplicates.
Time: O(1) amortized.
node ID in 0..nodeCount-1
node ID in 0..nodeCount-1
Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1],
endpoints[2] -> endpoints[3], and so on.
Time: O(number of endpoints) amortized.
node IDs in 0..nodeCount-1, source and target of each edge in turn, as one
array or as loose arguments
Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1],
endpoints[2] -> endpoints[3], and so on.
Time: O(number of endpoints) amortized.
node IDs in 0..nodeCount-1, source and target of each edge in turn, as one
array or as loose arguments
Adds an edge for each pair of node IDs in endpoints, as GraphBuilder.addEdgePairs
does, and the missing nodes up to the highest of these node IDs.
Time: O(number of endpoints) amortized.
node IDs, 0 or more, source and target of each edge in turn, as one array or
as loose arguments
Adds an edge for each pair of node IDs in endpoints, as GraphBuilder.addEdgePairs
does, and the missing nodes up to the highest of these node IDs.
Time: O(number of endpoints) amortized.
node IDs, 0 or more, source and target of each edge in turn, as one array or
as loose arguments
Adds the edges of an algorithm result, such as BackEdges.of or Biconnectivity.bridges.
Only the sources and targets are read: the edge indices of edges belong to the graph the
algorithm ran on.
Time: O(edges.count) amortized.
the edges to add, between nodes in 0..nodeCount-1
Adds the edges of an algorithm result, as GraphBuilder.addEdges does, and the missing nodes up to the highest of their node IDs.
Time: O(edges.count) amortized.
the edges to add
Adds the nodes and edges of graph after the nodes already added, and returns the offset of
its node IDs.
Node v of graph becomes node offset + v of this builder, where the offset is the node
count before the call. Into an empty builder, the node IDs stay the same, which makes this the
way to extend a graph: a Graph is immutable, but a builder that starts with it can add
to it and build a new one. Here, the dependencies between the packages of a program are
extended to a next version, in which ui also uses storage:
const builder = new GraphBuilder();
// into an empty builder: the node IDs stay the same
builder.addGraph(dependencies);
builder.addEdge(ui, storage);
const nextVersion = builder.build();
Time: O(graph.nodeCount + graph.edgeCount) amortized.
the graph to add
the offset: node ID of node 0 of graph in this builder
Adds an edge from each node of sources to target.
Time: O(number of sources) amortized.
node ID in 0..nodeCount-1
node IDs in 0..nodeCount-1
Adds an edge from each node of sources to target.
Time: O(sources.length) amortized.
node ID in 0..nodeCount-1
node IDs in 0..nodeCount-1
Adds an edge from each node of sources to target, and the missing nodes up to the highest
of these node IDs.
Time: O(number of sources) amortized.
node ID, 0 or more
node IDs, 0 or more
Adds an edge from each node of sources to target, and the missing nodes up to the highest
of these node IDs.
Time: O(sources.length) amortized.
node ID, 0 or more
node IDs, 0 or more
Adds an edge from source to each node of targets.
Time: O(number of targets) amortized.
node ID in 0..nodeCount-1
node IDs in 0..nodeCount-1
Adds an edge from source to each node of targets.
Time: O(targets.length) amortized.
node ID in 0..nodeCount-1
node IDs in 0..nodeCount-1
Adds an edge from source to each node of targets, and the missing nodes up to the highest
of these node IDs.
Time: O(number of targets) amortized.
node ID, 0 or more
node IDs, 0 or more
Adds an edge from source to each node of targets, and the missing nodes up to the highest
of these node IDs.
Time: O(targets.length) amortized.
node ID, 0 or more
node IDs, 0 or more
Adds a node with an edge from it to child, and returns its node ID.
Time: O(1) amortized.
node ID in 0..nodeCount-1
node ID of the new node
Adds count nodes, each with an edge from it to child, and returns the node ID of the first;
the others follow in order.
Time: O(count) amortized.
node ID in 0..nodeCount-1
number of nodes to add
node ID of the first new node
Adds a path through nodes: an edge from each node of the list to the next.
Fewer than two nodes add no edge.
Time: O(number of nodes) amortized.
node IDs in 0..nodeCount-1, in the order of the path
Adds a path through nodes: an edge from each node of the array to the next.
Fewer than two nodes add no edge.
Time: O(nodes.length) amortized.
node IDs in 0..nodeCount-1, in the order of the path
Adds a path through nodes, and the missing nodes up to the highest of these node IDs. The
path has an edge from each node of the list to the next.
Fewer than two nodes add no edge, but still add the nodes.
Time: O(number of nodes) amortized.
node IDs, 0 or more, in the order of the path
Adds a path through nodes, and the missing nodes up to the highest of these node IDs. The
path has an edge from each node of the array to the next.
Fewer than two nodes add no edge, but still add the nodes.
Time: O(nodes.length) amortized.
node IDs, 0 or more, in the order of the path
Returns a graph with the nodes and edges added so far.
The edges are sorted into edge index order (by source and then by target), and duplicates are merged, so an edge does not keep the position at which it was added; Graph.edgeIndexOf finds where it went. The builder does not change: more nodes and edges can be added and built into another graph.
Time: O(nodeCount + k log d), where k is the number of added edges, duplicates included, and d the most of them with the same source. Memory: 4 x (nodeCount + edgeCount) bytes for the result; while it runs, briefly up to 4 x (nodeCount + k) bytes more.
the graph
Makes room for totalEdges edges in total, so that adding up to that many allocates nothing
more.
The argument counts all edges the builder will hold, duplicates included, not the ones still to come. It can be called at any time and does not change the graph that GraphBuilder.build returns.
Time: O(totalEdges) if the room does not suffice yet, otherwise O(1). Memory: 8 x totalEdges bytes if the room does not suffice yet, otherwise none.
the number of edges to make room for, duplicates included
A builder for a Graph, from nodes and edges added in any order.
The nodes added so far have the node IDs
0..nodeCount-1, in the order they were added; GraphBuilder.addNode and GraphBuilder.addNodes return the IDs of new ones. Edges name their source and target by node ID, in any order and as often as the input has them. Here, the calls between the methods of a program are built from its call sites, one edge per call site;namesholds the names of the methods:GraphBuilder.build sorts the edges into edge index order and merges duplicates. The builder stays usable afterwards: more nodes and edges can be added, and
build()called again.Besides single edges, the builder takes edges in batches: the out-edges or in-edges of one node (GraphBuilder.addOutEdges, GraphBuilder.addInEdges), a path (GraphBuilder.addPath), pairs of node IDs (GraphBuilder.addEdgePairs), the edges of an algorithm result (GraphBuilder.addEdges) or a whole graph (GraphBuilder.addGraph). GraphBuilder.addChild, GraphBuilder.addParent and their plural forms add new nodes together with their edges. The out-edges, in-edges and paths take their node IDs as loose arguments or as an IntList, such as the SortedIntView that Graph.outNodes returns. So a result of this library goes in without being copied into an array first. A list is read during the call and not kept. Here, part of the dependencies between the packages of a program is built, with the node IDs of
dependencies:A method that takes the node ID of an existing node throws a
RangeErrorif it is not in0..nodeCount-1, so that an edge to a missing node fails at the call. A batch method that throws adds nothing. The methods that add edges between given nodes each have a twin with the suffixAndNodes, such as GraphBuilder.addEdgeAndNodes, which adds missing nodes instead. A twin raises the node count to one more than the highest node ID it is given, and the nodes in between are added too, without edges. So a graph can be built from input numbered elsewhere without counting its nodes first. However, a node ID that is too high adds nodes instead of failing.Nodes and edges cannot be removed. To leave nodes out, build the graph and take a subgraph with Graphs.remapped and NodeMapping.forSubgraph.
Instances are mutable.
Memory: 8 bytes per added edge, duplicates included, plus room to grow.
A builder holds at most 2^31 - 10 nodes, as many as a graph can have. This number is the node limit. A method that would add more throws before it changes anything. A builder builds a graph from at most 2^31 - 9 added edges, duplicates included. This number is the edge limit. A batch method that would add more throws an
Errorand adds nothing. The single-edge methods GraphBuilder.addEdge, GraphBuilder.addEdgeAndNodes, GraphBuilder.addChild and GraphBuilder.addParent do not check the edge limit: beyond it, GraphBuilder.build throws anError, and the single-edge methods throw once the engine cannot grow the edge storage any further (Java:OutOfMemoryError).