Class BfsDistances
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
The distances of the nodes of a graph from one or several sources, with a shortest path to every node reached.
public sealed class BfsDistances
- Inheritance
-
BfsDistances
- Inherited Members
Examples
Here, calls are the calls between the methods of a program, and the search finds a shortest
chain of calls from main to parse:
BfsDistances fromMain = BfsDistances.Of(calls, main);
int depth = fromMain.DistanceOf(parse);
ImmutableArray<int> chain = fromMain.PathTo(parse);
// depth: 3
// chain: [0, 2, 3, 6] - main -> run -> load -> parse
Remarks
The sources are the nodes where the search starts. The distance of a node is the number of edges
on a shortest path to it from the nearest source. A source has distance 0, and a node that no
source reaches has distance -1. Edges are followed in their direction. For distances against their
direction, pass the transposed graph: BiAdjacentGraph.Of(graph).Transposed().
Uses a breadth-first search that takes the sources in list order and the out-nodes of each node in ascending order. For the reachable nodes alone, ReachableFrom(Graph, ReadOnlySpan<int>) needs less memory.
Instances are immutable and safe to share between threads.
Memory: 8 x NodeCount bytes: the distance of each node and the node before it on its shortest path.
Properties
MaxDistance
Gets the largest distance of a node that a source reaches, or -1 if there are no sources.
public int MaxDistance { get; }
Property Value
Remarks
Time: O(1).
Methods
DistanceOf(int)
Returns the distance of node, or -1 if no source reaches it. The distance
is the number of edges on a shortest path to node from the nearest source.
public int DistanceOf(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- int
The distance, or -1.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
IsReachable(int)
Returns whether a source reaches node. Equivalent to
DistanceOf(node) >= 0.
public bool IsReachable(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
Of(Graph, int)
Returns the distances of the nodes of graph from source.
Equivalent to Of(graph, [source]).
public static BfsDistances Of(Graph graph, int source)
Parameters
Returns
- BfsDistances
The distances.
Remarks
Time: O(NodeCount + EdgeCount).
Memory: as for Of(Graph, ReadOnlySpan<int>).
Exceptions
- ArgumentOutOfRangeException
sourceis out of range.
Of(Graph, ReadOnlySpan<int>)
Returns the distances of the nodes of graph from the nearest of
sources.
public static BfsDistances Of(Graph graph, ReadOnlySpan<int> sources)
Parameters
graphGraphThe graph to search.
sourcesReadOnlySpan<int>Node IDs, each in
0..NodeCount-1.
Returns
- BfsDistances
The distances.
Remarks
A source given twice counts once, and with no sources, no node is reachable.
Time: O(sources.Length + NodeCount + EdgeCount).
Memory: 8 x NodeCount bytes for the result; while it runs, 4 x NodeCount bytes more.
Exceptions
- ArgumentOutOfRangeException
A source is out of range.
PathTo(int)
Returns a shortest path to node from the nearest source, or an empty list
if no source reaches node.
public ImmutableArray<int> PathTo(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- ImmutableArray<int>
The node IDs of the path, or an empty list.
Remarks
The path has DistanceOf(node) + 1 nodes, with the source first and
node last. Of several shortest paths, this method returns the one that the
search finds first.
Time: O(DistanceOf(node)).
Memory: 4 x (DistanceOf(node) + 1) bytes for the result.
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.