Table of Contents

Class BiAdjacentGraph

Namespace
Graphity
Assembly
Graphity.Core.dll

A Graph that also holds the in-nodes of every node, for algorithms that follow edges against their direction.

public sealed class BiAdjacentGraph : Graph, IEquatable<Graph>
Inheritance
BiAdjacentGraph
Implements
Inherited Members

Remarks

Of(Graph) builds the in-adjacency of a graph - the in-nodes of every node. The result has the node IDs and edge indices of the graph and shares the out-nodes of every node, called its out-adjacency. Algorithms that need in-nodes, such as Dominators, take a BiAdjacentGraph, so that the in-adjacency is built once and serves all of them. Transposed() reverses every edge without copying, and UniAdjacentGraph() drops the in-adjacency again.

The in-nodes of a node are in ascending order, like its out-nodes. A BiAdjacentGraph equals a Graph with the same nodes and edges. Instances are immutable and safe to share between threads.

Memory: 8 x (NodeCount + EdgeCount) bytes, half of it for the out-adjacency, which is shared with the graph it was built from.

Methods

Degree(int)

Returns the degree of node. Equivalent to InDegree(node) + OutDegree(node).

public int Degree(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The degree.

Remarks

It counts edges, not neighbors: a self-loop counts twice, and so do the edges node -> v and v -> node, although v is one neighbor.

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

HasInNode(int, int)

Returns whether the edge inNode -> node exists.

public bool HasInNode(int node, int inNode)

Parameters

node int

Node ID in 0..NodeCount-1.

inNode int

Node ID; a value out of range yields false.

Returns

bool

true if the edge exists.

Remarks

Time: O(log InDegree(node)).

Exceptions

ArgumentOutOfRangeException

node is out of range.

HasNeighbor(int, int)

Returns whether neighbor is a neighbor of node. Equivalent to HasOutNode(node, neighbor) || HasInNode(node, neighbor).

public bool HasNeighbor(int node, int neighbor)

Parameters

node int

Node ID in 0..NodeCount-1.

neighbor int

Node ID; a value out of range yields false.

Returns

bool

true if neighbor is an in-node or an out-node of node.

Remarks

Time: O(log OutDegree(node) + log InDegree(node)).

Exceptions

ArgumentOutOfRangeException

node is out of range.

InDegree(int)

Returns the in-degree of node. That is the number of edges with node as target.

public int InDegree(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The in-degree.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

InNodes(int)

Returns the in-nodes of node in ascending order. These are the sources of the edges with node as target.

public SortedIntView InNodes(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

SortedIntView

The in-nodes.

Remarks

The result is a view of this graph's own array, not a copy.

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

IsRootNode(int)

Returns whether node is a root node. Equivalent to InDegree(node) == 0.

public bool IsRootNode(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

bool

true if no edge has node as target.

Remarks

RootNodes(BiAdjacentGraph) returns all root nodes.

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

Neighbors(int)

Returns the neighbors of node in ascending order and without duplicates. These are its in-nodes and out-nodes.

public SortedIntView Neighbors(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

SortedIntView

The neighbors.

Remarks

Unlike InNodes(int) and OutNodes(int), which are views, the result is a copy, merged on every call.

Time: O(InDegree(node) + OutDegree(node)).
Memory: up to 4 x (InDegree(node) + OutDegree(node)) bytes for the result; while it runs, up to as much again if a neighbor is both an in-node and an out-node.

Exceptions

ArgumentOutOfRangeException

node is out of range.

Of(Graph)

Returns a BiAdjacentGraph with the nodes and edges of graph, building its in-adjacency.

public static BiAdjacentGraph Of(Graph graph)

Parameters

graph Graph

The graph to build the in-adjacency for.

Returns

BiAdjacentGraph

The graph with in-adjacency.

Remarks

Node IDs and edge indices stay the same, and the out-adjacency is shared with graph, not copied. The in-adjacency is built anew on every call, not cached: keep the result to use it more than once. If graph already is a BiAdjacentGraph, returns it.

Time: O(NodeCount + EdgeCount), or O(1) if already bi-adjacent.
Memory: +4 x (NodeCount + EdgeCount) bytes, or none if already bi-adjacent.

Transposed()

Returns the transposed graph as a view of this graph's arrays. The transposed graph has every edge reversed.

public BiAdjacentGraph Transposed()

Returns

BiAdjacentGraph

The transposed graph.

Examples

Here, the packages of a program that depend on model, directly or indirectly, are the ones reachable from it against the direction of the dependencies:

int model = 4;
BiAdjacentGraph usedBy = BiAdjacentGraph.Of(dependencies).Transposed();
SortedIntView affected = Graphs.ReachableFrom(usedBy, model);
// affected: [0, 2, 3, 4, 5] - app, ui, service, model and storage

Remarks

Node IDs stay the same, edge indices do not: edge e of the transposed graph is in general not the reverse of edge e of this graph. TransposedEdgeIndex(int) translates one edge index, and TransposedEdgeMapping(Graph) translates all of them.

A plain Graph needs its in-adjacency first: BiAdjacentGraph.Of(graph).Transposed().

Time: O(1).

TransposedEdgeIndex(int)

Returns the edge index that edge edgeIndex has in the transposed graph.

public int TransposedEdgeIndex(int edgeIndex)

Parameters

edgeIndex int

Edge index in 0..EdgeCount-1.

Returns

int

The edge index in the transposed graph.

Remarks

Called on Transposed(), it translates back: Transposed().TransposedEdgeIndex(TransposedEdgeIndex(e)) == e. To translate every edge, TransposedEdgeMapping(Graph) needs one pass instead of a search per edge; it maps from the transposed graph to this one.

Time: O(log NodeCount).

Exceptions

ArgumentOutOfRangeException

edgeIndex is out of range.

UniAdjacentGraph()

Returns a Graph with the same nodes and edges, without the in-adjacency.

public override Graph UniAdjacentGraph()

Returns

Graph

A graph over the out-adjacency of this graph.

Remarks

The result shares the out-adjacency of this graph; nothing is copied. Once only the result is kept, the in-adjacency can be garbage-collected.

Time: O(1).