Table of Contents

Class GraphBuilder

Namespace
Graphity
Assembly
Graphity.Core.dll

A builder for a Graph, from nodes and edges added in any order.

public sealed class GraphBuilder
Inheritance
GraphBuilder
Inherited Members

Examples

Here, the calls between the methods of a program are built from its call sites, one edge per call site; names holds the names of the methods:

var builder = new GraphBuilder();
builder.AddNodes(names.Length);    // one node per method, numbered as in names
foreach (CallSite site in callSites)
{
    builder.AddEdge(site.Caller, site.Callee);
}
Graph calls = builder.Build();
// 14 call sites, 12 edges: run -> show and parse -> parse were added twice

Remarks

The nodes added so far have the node IDs 0..NodeCount-1, in the order they were added; AddNode() and AddNodes(int) 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. 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 (AddOutEdges(int, params ReadOnlySpan<int>), AddInEdges(int, params ReadOnlySpan<int>)), a path (AddPath(params ReadOnlySpan<int>)), pairs of node IDs (AddEdgePairs(params ReadOnlySpan<int>)), the edges of an algorithm result (AddEdges(Edges)) or a whole graph (AddGraph(Graph)). AddChild(int), AddParent(int) and their plural forms add new nodes together with their edges. The out-edges, in-edges and paths take their node IDs as a params ReadOnlySpan<int>: loose arguments, an array, a span or the SortedIntView that OutNodes(int) returns. So a result of this library goes in without being copied into an array first. A span is read during the call and not kept.

A method that takes the node ID of an existing node throws ArgumentOutOfRangeException if it is not in 0..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 suffix AndNodes, such as AddEdgeAndNodes(int, int), 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 Remapped(Graph, NodeMapping) and ForSubgraph(BitArray).

Instances are mutable and not safe to share between threads.

Memory: 8 bytes per added edge, duplicates included, plus room to grow.

A builder holds at most Array.MaxLength - 1 nodes (2,147,483,590), 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 Array.MaxLength added edges (2,147,483,591), duplicates included. This number is the edge limit. A batch method that would add more throws ArgumentException and adds nothing. The single-edge methods AddEdge(int, int), AddEdgeAndNodes(int, int), AddChild(int) and AddParent(int) do not check the edge limit: beyond it, Build() throws ArgumentException, and the single-edge methods throw OutOfMemoryException once the edge storage cannot grow any further. In .NET, that happens at the edge limit itself.

Constructors

GraphBuilder()

Creates an empty builder, without nodes or edges.

public GraphBuilder()

Methods

AddChild(int)

Adds a node with an edge from parent to it, and returns its node ID.

public int AddChild(int parent)

Parameters

parent int

Node ID in 0..NodeCount-1.

Returns

int

The node ID of the new node.

Remarks

Time: O(1) amortized.

Exceptions

InvalidOperationException

The node count has reached the node limit.

ArgumentOutOfRangeException

parent is out of range.

AddChildren(int, int)

Adds count nodes, each with an edge from parent to it, and returns the node ID of the first; the others follow in order.

public int AddChildren(int parent, int count)

Parameters

parent int

Node ID in 0..NodeCount-1.

count int

The number of nodes to add.

Returns

int

The node ID of the first new node.

Remarks

Time: O(count) amortized.

Exceptions

ArgumentException

count is negative, or the nodes or edges would exceed their limit.

ArgumentOutOfRangeException

parent is out of range.

AddEdge(int, int)

Adds the edge source -> target.

public void AddEdge(int source, int target)

Parameters

source int

Node ID in 0..NodeCount-1.

target int

Node ID in 0..NodeCount-1.

Remarks

An edge added more than once is still one edge in the graph: Build() merges duplicates.

Time: O(1) amortized.

Exceptions

ArgumentOutOfRangeException

source or target is out of range.

AddEdgeAndNodes(int, int)

Adds the edge source -> target, and the missing nodes up to the higher of the two node IDs.

public void AddEdgeAndNodes(int source, int target)

Parameters

source int

Node ID, 0 or more.

target int

Node ID, 0 or more.

Remarks

Time: O(1) amortized.

Exceptions

ArgumentOutOfRangeException

source or target is negative or not below the node limit.

AddEdgePairs(params ReadOnlySpan<int>)

Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1], endpoints[2] -> endpoints[3], and so on.

public void AddEdgePairs(params ReadOnlySpan<int> endpoints)

Parameters

endpoints ReadOnlySpan<int>

Node IDs in 0..NodeCount-1, source and target of each edge in turn.

Remarks

Time: O(endpoints.Length) amortized.

Exceptions

ArgumentException

endpoints has an odd length, or the edges would exceed the edge limit.

ArgumentOutOfRangeException

A node of endpoints is out of range.

AddEdgePairsAndNodes(params ReadOnlySpan<int>)

Adds an edge for each pair of node IDs in endpoints, as AddEdgePairs(params ReadOnlySpan<int>) does, and the missing nodes up to the highest of these node IDs.

public void AddEdgePairsAndNodes(params ReadOnlySpan<int> endpoints)

Parameters

endpoints ReadOnlySpan<int>

Node IDs, 0 or more, source and target of each edge in turn.

Remarks

Time: O(endpoints.Length) amortized.

Exceptions

ArgumentException

endpoints has an odd length, or the edges would exceed the edge limit.

ArgumentOutOfRangeException

A node ID is negative or not below the node limit.

AddEdges(Edges)

Adds the edges of an algorithm result, such as Of(Graph) or Bridges.

public void AddEdges(Edges edges)

Parameters

edges Edges

The edges to add, between nodes in 0..NodeCount-1.

Remarks

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.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A source or target is out of range.

AddEdgesAndNodes(Edges)

Adds the edges of an algorithm result, as AddEdges(Edges) does, and the missing nodes up to the highest of their node IDs.

public void AddEdgesAndNodes(Edges edges)

Parameters

edges Edges

The edges to add.

Remarks

Time: O(edges.Count) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A source or target is not below the node limit.

AddGraph(Graph)

Adds the nodes and edges of graph after the nodes already added, and returns the offset of its node IDs.

public int AddGraph(Graph graph)

Parameters

graph Graph

The graph to add.

Returns

int

The offset: node ID of node 0 of graph in this builder.

Examples

Here, the dependencies between the packages of a program are extended to a next version, in which ui also uses storage:

var builder = new GraphBuilder();
// into an empty builder: the node IDs stay the same
builder.AddGraph(dependencies);
builder.AddEdge(ui, storage);
Graph nextVersion = builder.Build();

Remarks

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.

Time: O(graph.NodeCount + graph.EdgeCount) amortized.

Exceptions

ArgumentException

The nodes or edges of graph would exceed their limit.

AddInEdges(int, params ReadOnlySpan<int>)

Adds an edge from each node of sources to target.

public void AddInEdges(int target, params ReadOnlySpan<int> sources)

Parameters

target int

Node ID in 0..NodeCount-1.

sources ReadOnlySpan<int>

Node IDs in 0..NodeCount-1.

Remarks

Time: O(sources.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

target or a node of sources is out of range.

AddInEdgesAndNodes(int, params ReadOnlySpan<int>)

Adds an edge from each node of sources to target, and the missing nodes up to the highest of these node IDs.

public void AddInEdgesAndNodes(int target, params ReadOnlySpan<int> sources)

Parameters

target int

Node ID, 0 or more.

sources ReadOnlySpan<int>

Node IDs, 0 or more.

Remarks

Time: O(sources.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A node ID is negative or not below the node limit.

AddNode()

Adds a node and returns its node ID, which is the next in order.

public int AddNode()

Returns

int

The node ID of the new node.

Remarks

Time: O(1).

Exceptions

InvalidOperationException

The node count has reached the node limit.

AddNodes(int)

Adds count nodes and returns the node ID of the first; the others follow in order.

public int AddNodes(int count)

Parameters

count int

The number of nodes to add.

Returns

int

The node ID of the first new node.

Remarks

Time: O(1).

Exceptions

ArgumentException

count is negative or the nodes would exceed the node limit.

AddOutEdges(int, params ReadOnlySpan<int>)

Adds an edge from source to each node of targets.

public void AddOutEdges(int source, params ReadOnlySpan<int> targets)

Parameters

source int

Node ID in 0..NodeCount-1.

targets ReadOnlySpan<int>

Node IDs in 0..NodeCount-1.

Remarks

Time: O(targets.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

source or a node of targets is out of range.

AddOutEdgesAndNodes(int, params ReadOnlySpan<int>)

Adds an edge from source to each node of targets, and the missing nodes up to the highest of these node IDs.

public void AddOutEdgesAndNodes(int source, params ReadOnlySpan<int> targets)

Parameters

source int

Node ID, 0 or more.

targets ReadOnlySpan<int>

Node IDs, 0 or more.

Remarks

Time: O(targets.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A node ID is negative or not below the node limit.

AddParent(int)

Adds a node with an edge from it to child, and returns its node ID.

public int AddParent(int child)

Parameters

child int

Node ID in 0..NodeCount-1.

Returns

int

The node ID of the new node.

Remarks

Time: O(1) amortized.

Exceptions

InvalidOperationException

The node count has reached the node limit.

ArgumentOutOfRangeException

child is out of range.

AddParents(int, int)

Adds count nodes, each with an edge from it to child, and returns the node ID of the first; the others follow in order.

public int AddParents(int child, int count)

Parameters

child int

Node ID in 0..NodeCount-1.

count int

The number of nodes to add.

Returns

int

The node ID of the first new node.

Remarks

Time: O(count) amortized.

Exceptions

ArgumentException

count is negative, or the nodes or edges would exceed their limit.

ArgumentOutOfRangeException

child is out of range.

AddPath(params ReadOnlySpan<int>)

Adds a path through nodes: an edge from each node of the list to the next.

public void AddPath(params ReadOnlySpan<int> nodes)

Parameters

nodes ReadOnlySpan<int>

Node IDs in 0..NodeCount-1, in the order of the path.

Remarks

Fewer than two nodes add no edge.

Time: O(nodes.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A node of nodes is out of range.

AddPathAndNodes(params ReadOnlySpan<int>)

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.

public void AddPathAndNodes(params ReadOnlySpan<int> nodes)

Parameters

nodes ReadOnlySpan<int>

Node IDs, 0 or more, in the order of the path.

Remarks

Fewer than two nodes add no edge, but still add the nodes.

Time: O(nodes.Length) amortized.

Exceptions

ArgumentException

The edges would exceed the edge limit.

ArgumentOutOfRangeException

A node ID is negative or not below the node limit.

Build()

Returns a graph with the nodes and edges added so far.

public Graph Build()

Returns

Graph

The graph.

Remarks

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; EdgeIndexOf(int, int) 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.

Exceptions

ArgumentException

The edges added exceed the edge limit.

EnsureEdgeCapacity(int)

Makes room for totalEdges edges in total, so that adding up to that many allocates nothing more.

public void EnsureEdgeCapacity(int totalEdges)

Parameters

totalEdges int

The number of edges to make room for, duplicates included.

Remarks

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 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.

Exceptions

ArgumentException

totalEdges is negative or above the edge limit.