Class NodePartition
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
A partition of the nodes of a graph into groups, such as its components.
public class NodePartition : IndexPartition
- Inheritance
-
NodePartition
- Derived
- Inherited Members
Remarks
Get(node) returns the group of node, or -1 if it is in no group, and
NodesOf(int) the nodes of a group in ascending order. Components returns
partitions into components, in which every node is in exactly one group: an
Sccs for the strongly connected components, a plain NodePartition
for the weakly connected ones. Of(Graph, ReadOnlySpan<int>, int) takes groups of the caller's choice, which may
leave nodes out.
A partition holds the graph whose nodes it partitions. Condense() builds the
condensed graph, with a node for each group and an edge between two groups wherever an edge of
the graph runs between them. EdgePartition(Graph, NodePartition) builds the
EdgePartition that tells which edges of the graph are behind each edge of the
condensed graph. The members for groups of any kind, such as
GroupCount, SizeOf(int) and Reduce,
come from IndexPartition.
Instances are immutable and safe to share between threads. No other assembly can derive from this class; Sccs is its only subclass.
Memory: 4 x (NodeCount + MappedCount + GroupCount) bytes, where MappedCount is the number of nodes that belong to a group; the graph is shared, not copied.
Properties
NodeCount
Gets the number of nodes of the partitioned graph, in a group or not. Equivalent to IndexCount.
public int NodeCount { get; }
Property Value
Remarks
Time: O(1).
Methods
Condense()
Returns the condensed graph, with a node for each group and an edge between two groups wherever an edge of the partitioned graph runs between them.
public Graph Condense()
Returns
- Graph
The condensed graph.
Remarks
The node IDs of the condensed graph are the group IDs. It has the edge g -> h if a
node in group g has an out-node in group h and g != h. Edges within a
group and edges from or to a node in no group are left out. The edges from one group to
another become a single edge. EdgePartition(Graph, NodePartition) tells which edges are behind
each edge of the condensed graph. For an Sccs, the condensed graph is acyclic.
The condensed graph is built anew on every call, not cached: keep the result to use it more than once.
Time: O(NodeCount + EdgeCount + GroupCount), plus O(d log d) for each group with d
out-nodes in the result.
Memory: 4 x (GroupCount + condensedEdgeCount) bytes for the result, where
condensedEdgeCount is its edge count; while it runs, 1 bit per group and up to
6 x condensedEdgeCount bytes more.
NodesOf(int)
Returns the nodes in group groupId, in ascending order. Equivalent to
IndicesOf(groupId).
public SortedIntView NodesOf(int groupId)
Parameters
groupIdintGroup ID in
0..GroupCount-1.
Returns
- SortedIntView
The nodes in the group.
Remarks
The result is a view of this partition's own array, not a copy.
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
groupIdis out of range.
Of(Graph, ReadOnlySpan<int>, int)
Returns a partition of the nodes of graph into groups of the caller's
choice.
public static NodePartition Of(Graph graph, ReadOnlySpan<int> groupOf, int groupCount)
Parameters
graphGraphThe graph whose nodes are partitioned.
groupOfReadOnlySpan<int>The group of each node, or -1; length
graph.NodeCount.groupCountintThe number of groups, at most as many as a graph can have nodes.
Returns
- NodePartition
The partition.
Examples
Here, packageOf holds the package of each method of calls as a node ID of the
package graph dependencies, and the condensed graph shows which packages call which:
NodePartition byPackage =
NodePartition.Of(calls, packageOf, dependencies.NodeCount);
Graph packageCalls = byPackage.Condense();
// packageCalls: app -> [cli, ui, service], ui -> [model],
// service -> [model, storage]
Remarks
groupOf[node] is the group of each node, in 0..groupCount-1, or -1 for a node
in no group; the span is copied. A group may have no nodes: it becomes a node without edges
in Condense(), so that group IDs from a numbering of the caller stay valid.
Time: O(NodeCount + groupCount).
Memory: 4 x (NodeCount + MappedCount + groupCount) bytes for the result.
Exceptions
- ArgumentOutOfRangeException
A group is not in
-1..groupCount-1.- ArgumentException
groupOf.Length != graph.NodeCount, orgroupCountis negative or larger than a graph can have nodes.