Table of Contents

Class NodeMapping

Namespace
Graphity
Assembly
Graphity.Core.dll

A mapping from the node IDs of one graph to those of another, to extract a subgraph or to renumber the nodes of a graph.

public sealed class NodeMapping : IndexMapping
Inheritance
NodeMapping
Inherited Members

Examples

Here, calls holds the calls between the methods of a program, and the methods that load reaches become a graph of their own, with their names from names:

NodeMapping newToOld = NodeMapping.ForSubgraph(Graphs.ReachableFrom(calls, load));
Graph loading = Graphs.Remapped(calls, newToOld);
string[] loadingNames = new string[loading.NodeCount];
newToOld.Gather(names, loadingNames);
// loadingNames: [load, parse, read]
// loading: load -> parse, load -> read, parse -> parse

Remarks

Get(node) is the node ID that node maps to, or -1 if node is not mapped. The factories return mappings from a new graph to an old one. These map each new node to the node it comes from:

Remapped(Graph, NodeMapping) builds the new graph from the old one, and Gather carries data on the old nodes, such as their names, over to the new ones. EdgeMapping(Graph, Graph, NodeMapping) derives the EdgeMapping that carries data on edges. Inverted(int) returns the mapping from old to new, and Compose(NodeMapping) chains two mappings.

Instances are immutable and safe to share between threads.

Memory: 4 x IndexCount bytes.

Methods

Compose(NodeMapping)

Returns the mapping that applies inner first and then this mapping.

public NodeMapping Compose(NodeMapping inner)

Parameters

inner NodeMapping

The mapping to apply first.

Returns

NodeMapping

The composed mapping.

Remarks

If inner maps the nodes of a graph A to those of B, and this mapping maps B to C, the result maps A to C. A node is not mapped in the result if inner does not map it, or if this mapping does not map the node that inner maps it to. For mappings from new to old node IDs, firstToOriginal.Compose(secondToFirst) maps the nodes of a graph remapped twice back to those of the original one.

Time: O(inner.IndexCount).
Memory: 4 x inner.IndexCount bytes for the result.

Exceptions

ArgumentOutOfRangeException

inner maps a node to a node ID outside 0..IndexCount-1.

ForPermutation(ReadOnlySpan<int>)

Returns a mapping that numbers the nodes in the given order, from new to old node IDs.

public static NodeMapping ForPermutation(ReadOnlySpan<int> order)

Parameters

order ReadOnlySpan<int>

The node IDs in their new order.

Returns

NodeMapping

The mapping from new to old node IDs.

Remarks

order[newNode] is the node that gets the new node ID newNode. order holds every node ID in 0..order.Length-1 once, so the mapping maps every node. For example, NodeMapping.ForPermutation(TopOrder.Of(dag).AsSpan()) numbers the nodes of a DAG (directed acyclic graph) in topological order, so that every edge of the remapped graph goes from a lower to a higher node ID.

Time: O(order.Length).
Memory: 4 x order.Length bytes for the result; while it runs, 1 bit per node.

Exceptions

ArgumentOutOfRangeException

A node ID is not in 0..order.Length-1.

ArgumentException

A node ID occurs more than once.

ForSubgraph(SortedIntView)

Returns a mapping from the nodes of a subgraph to the selected nodes, given as a SortedIntView.

public static NodeMapping ForSubgraph(SortedIntView selected)

Parameters

selected SortedIntView

The selected node IDs.

Returns

NodeMapping

The mapping from new to old node IDs.

Remarks

The selected nodes get the new node IDs 0..selected.Count-1, in ascending order of their node IDs. A SortedIntView, as ReachableFrom(Graph, int) and NodesOf(int) return it, is taken as it is; any other list goes to ForSubgraph(params ReadOnlySpan<int>), which sorts it in a copy. As with ForSubgraph(BitArray), the node IDs are checked against a graph only when the mapping is used.

Time: O(selected.Count).
Memory: 4 x selected.Count bytes for the result.

Exceptions

ArgumentOutOfRangeException

A node ID is negative.

ArgumentException

A node ID occurs more than once.

ForSubgraph(BitArray)

Returns a mapping from the nodes of a subgraph to the selected nodes, given as the set bits of selected.

public static NodeMapping ForSubgraph(BitArray selected)

Parameters

selected BitArray

The selected node IDs.

Returns

NodeMapping

The mapping from new to old node IDs.

Remarks

The selected nodes get the new node IDs 0..k-1, in ascending order of their node IDs, where k is the number of set bits. The selected node IDs are checked against a graph only when the mapping is used: Remapped(Graph, NodeMapping) rejects one that is not a node of its graph.

Time: O(selected.Length).
Memory: 4 x k bytes for the result.

ForSubgraph(params ReadOnlySpan<int>)

Returns a mapping from the nodes of a subgraph to the selected nodes, given as node IDs.

public static NodeMapping ForSubgraph(params ReadOnlySpan<int> selected)

Parameters

selected ReadOnlySpan<int>

The selected node IDs.

Returns

NodeMapping

The mapping from new to old node IDs.

Remarks

The selected nodes get the new node IDs 0..selected.Length-1, in ascending order of their node IDs, whatever the order of the arguments; the span is sorted in a copy. Takes loose arguments, an array or a span. As with ForSubgraph(BitArray), the node IDs are checked against a graph only when the mapping is used.

Time: O(k log k), where k is selected.Length.
Memory: 4 x selected.Length bytes for the result.

Exceptions

ArgumentOutOfRangeException

A node ID is negative.

ArgumentException

A node ID occurs more than once.

Gather<T>(ReadOnlySpan<T>)

Returns values renumbered by this mapping: the value at position i of the result is values[Get(i)], or default (0 for a number) if i is not mapped.

public T[] Gather<T>(ReadOnlySpan<T> values) where T : unmanaged

Parameters

values ReadOnlySpan<T>

The value of each node of the graph that this mapping maps to.

Returns

T[]

The renumbered values, of length IndexCount.

Type Parameters

T

The element type: an unmanaged value type such as int.

Remarks

For a mapping from new to old node IDs, as the factories return it, this carries data on the nodes of the old graph over to the new graph. One generic method serves int, long, double and any other unmanaged value type. Gather<T>(ReadOnlySpan<T>, Span<T>) does the same for objects, such as names.

Time: O(IndexCount).
Memory: one element per index for the result, 4 x IndexCount bytes for int.

Exceptions

ArgumentOutOfRangeException

A node maps to a node ID outside values.

Gather<T>(ReadOnlySpan<T>, Span<T>)

Writes values renumbered by this mapping into newValues, for objects such as names.

public void Gather<T>(ReadOnlySpan<T> values, Span<T> newValues)

Parameters

values ReadOnlySpan<T>

The value of each node of the graph that this mapping maps to.

newValues Span<T>

Receives the renumbered values; length IndexCount, and not overlapping values.

Type Parameters

T

The element type, nullable or not.

Remarks

newValues[i] becomes values[Get(i)], and stays as it is if i is not mapped. References are copied, not the objects. If an exception is thrown, newValues may be partly written.

Time: O(IndexCount).

Exceptions

ArgumentException

newValues.Length != IndexCount, or the two overlap.

ArgumentOutOfRangeException

A node maps to a node ID outside values.

Inverted(int)

Returns the inverse mapping, which maps Get(node) back to node.

public NodeMapping Inverted(int targetCount)

Parameters

targetCount int

The node count of the graph that this mapping maps to.

Returns

NodeMapping

The inverse mapping, with an index count of targetCount.

Remarks

The inverse maps the targetCount nodes of the graph that this mapping maps to; a node that no node maps to is not mapped, and the inverse is then partial. For a mapping from new to old node IDs, as the factories return it, targetCount is the node count of the old graph, and the inverse gives the new node ID of an old node: newToOld.Inverted(oldGraph.NodeCount).Get(oldNode).

Time: O(IndexCount + targetCount).
Memory: 4 x targetCount bytes for the result.

Exceptions

ArgumentException

targetCount is negative or larger than Array.MaxLength, or two nodes map to the same node.

ArgumentOutOfRangeException

A node maps to targetCount or more.

Of(ReadOnlySpan<int>)

Returns the mapping that maps each node i to mapping[i], and leaves it unmapped where that is -1.

public static NodeMapping Of(ReadOnlySpan<int> mapping)

Parameters

mapping ReadOnlySpan<int>

The node ID that each node maps to, or -1.

Returns

NodeMapping

The mapping.

Remarks

Use it for mappings that the other factories do not cover, from new to old or from old to new. The span is copied. Each value must be -1 or a node ID, and no two nodes may map to the same node; without a graph, this method can only reject a value below -1. Remapped(Graph, NodeMapping) rejects a value that is not a node of its graph, and both it and Inverted(int) reject two nodes that map to the same node.

Time: O(mapping.Length).
Memory: 4 x mapping.Length bytes for the copy, which the result keeps.

Exceptions

ArgumentOutOfRangeException

A value is less than -1.

Reindex(Graph)

Returns a mapping that numbers the nodes of graph in depth-first preorder, from new to old node IDs.

public static NodeMapping Reindex(Graph graph)

Parameters

graph Graph

The graph whose nodes are numbered.

Returns

NodeMapping

The mapping from new to old node IDs.

Remarks

The search starts at node 0, follows the out-nodes of each node in ascending order, and starts again at the smallest node not yet visited until it has visited every node; each node gets the next new node ID when the search reaches it. Nodes that the search visits one after another thus get adjacent node IDs. Traversals of the remapped graph then read its arrays more in order, which can make them faster on large graphs.

Time: O(NodeCount + EdgeCount).
Memory: 4 x NodeCount bytes for the result; while it runs, 1 bit per node and up to 8 x NodeCount bytes for the stack, which grows with the deepest path.

Exceptions

ArgumentException

A depth-first path is longer than 2^30 - 29 nodes.