Class Components
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
The algorithms for the components of a graph, and for the edges and nodes whose removal splits them.
public static class Components
- Inheritance
-
Components
- Inherited Members
Remarks
SccsOf(Graph) returns the SCCs (strongly connected components), and WccsOf(Graph) the weakly connected components. CcsOf(Graph) returns the weakly connected components under another name: the connected components of the graph read as undirected. BiconnectivityOf(BiAdjacentGraph) returns the bridges, articulation points and biconnected components. Bridges and articulation points are the edges and nodes whose removal splits a connected component. StrongConnectivityOf(BiAdjacentGraph) returns the strong bridges and strong articulation points, which split an SCC.
A graph is always directed, and each method says how it reads the edges. SccsOf and
StrongConnectivityOf follow them in their direction. WccsOf, CcsOf and
BiconnectivityOf read the graph as undirected: an edge connects its source and target
whether or not the reverse edge exists, and the edges a -> b and b -> a count as
one undirected edge, not two. An undirected graph can therefore be stored with one edge per pair
of neighbors or with both directions, and these methods read it the same way. For the bridges
this matters: read as two edges, the two directions would form a cycle, and no edge stored in
both directions could be a bridge.
Methods
BiconnectivityOf(BiAdjacentGraph)
Returns the bridges, articulation points and biconnected components of
graph, read as undirected.
public static Biconnectivity BiconnectivityOf(BiAdjacentGraph graph)
Parameters
graphBiAdjacentGraphThe graph.
Returns
- Biconnectivity
The bridges, articulation points and biconnected components.
Remarks
All three come from one depth-first search, so there is no cheaper method for one of them
alone; Biconnectivity describes them. The search follows the edges of every
node in both directions, so it takes a BiAdjacentGraph. For a plain
Graph, call Components.BiconnectivityOf(BiAdjacentGraph.Of(graph)).
Uses
the algorithm of Hopcroft and Tarjan,
without recursion.
Time: O(NodeCount + EdgeCount).
Memory: up to 8 x EdgeCount + 20 x NodeCount bytes for the result; while it runs,
8 x EdgeCount + 63 x NodeCount bytes more.
CcsOf(Graph)
Returns the connected components of graph read as undirected. Equivalent
to WccsOf(graph).
public static NodePartition CcsOf(Graph graph)
Parameters
graphGraphThe graph.
Returns
- NodePartition
The connected components.
Remarks
The two names let a call say which reading of the graph it means.
Time: O((NodeCount + EdgeCount) x alpha(NodeCount)), as for WccsOf(Graph).
Memory: up to 12 x NodeCount bytes for the result; while it runs, 9 x NodeCount bytes
more.
SccsOf(Graph)
Returns the SCCs of graph. Uses
Tarjan's algorithm,
without recursion.
public static Sccs SccsOf(Graph graph)
Parameters
graphGraphThe graph.
Returns
- Sccs
The SCCs.
Examples
Here, dependencies are the dependencies between the packages of a program, and
names holds the names of the packages. The example groups the packages by SCC, in the
order of the SCC IDs. In this order, each package comes after the packages it uses, and the
packages that use each other come together:
// The SCCs in reverse topological order, each as the names of its packages.
Sccs sccs = Components.SccsOf(dependencies);
List<List<string>> buildOrder = [];
for (int scc = 0; scc < sccs.SccCount; scc++)
{
buildOrder.Add([.. sccs.NodesOf(scc).Select(node => names[node])]);
}
// buildOrder: [[util], [cli], [json], [model, storage], [service], [ui], [app]]
Remarks
Every node is in exactly one SCC; a node that lies on no cycle is an SCC of its own. SCC IDs are in reverse topological order: every edge between two SCCs goes from the higher SCC ID to the lower one, so SCC 0 has no edges to other SCCs. The nodes of each SCC are in ascending order.
Time: O(NodeCount + EdgeCount), plus sorting the nodes of each SCC: up to
O(NodeCount log NodeCount) when one SCC holds most of the nodes.
Memory: up to 12 x NodeCount bytes for the result; while it runs, 24 x NodeCount
bytes more.
Exceptions
- ArgumentException
graphhas more than 2^30 - 29 nodes.
StrongConnectivityOf(BiAdjacentGraph)
Returns the strong bridges and strong articulation points of graph. These
are the edges and nodes whose removal splits an SCC.
public static StrongConnectivity StrongConnectivityOf(BiAdjacentGraph graph)
Parameters
graphBiAdjacentGraphThe graph.
Returns
- StrongConnectivity
The strong bridges and strong articulation points.
Remarks
The edges are followed in their direction, as by SccsOf(Graph);
StrongConnectivity describes the results. The computation also follows them
against their direction, so it takes a BiAdjacentGraph. For a plain
Graph, call Components.StrongConnectivityOf(BiAdjacentGraph.Of(graph)).
Uses the algorithm of Italiano, Laura and Santaroni, which finds both from the
dominators within each SCC, without recursion.
Time: O((NodeCount + EdgeCount) log NodeCount).
Memory: up to 28 x NodeCount bytes for the result; while it runs, about 80 x NodeCount
bytes more.
Exceptions
- ArgumentException
graphhas more than 2^30 - 30 nodes.
WccsOf(Graph)
Returns the weakly connected components of graph. These are the
components of the graph read as undirected.
public static NodePartition WccsOf(Graph graph)
Parameters
graphGraphThe graph.
Returns
- NodePartition
The weakly connected components.
Remarks
Every node is in exactly one component. Each component is a group of the result. The groups are in the order of their smallest nodes: group 0 holds node 0, group 1 the smallest node not in group 0, and so on. Uses union-find, whose cost grows with alpha(NodeCount). The inverse Ackermann function alpha is below 5 for any graph.
Time: O((NodeCount + EdgeCount) x alpha(NodeCount)).
Memory: up to 12 x NodeCount bytes for the result; while it runs, 9 x NodeCount bytes
more.