Computes the layers of nodes constructed by a directed or undirected breadth-first search.
Inheritance Hierarchy

Remarks

The first of these layers contains all coreNodes from which a breadth-first search to the other nodes starts.

Each layer i contains nodes that have a path of length i to at least one of the coreNodes. Nodes that are unreachable because there is no path to them, or the path's length exceeds layerCount have a layer index of -1 instead.

The search can be directed or undirected. In a directed search the edges can be traversed either forwards (from source to target) or backwards.

Examples

// configure the algorithm
const algorithm = new Bfs({
  // specify the nodes of the 0-th layer to start from
  coreNodes: centerNode,
})
// run the algorithm
const result = algorithm.run(graph)
// add labels to indicate the layer
result.nodeLayerIds.forEach(({ key: node, value: id }) =>
  graph.addLabel(node, String(id)),
)

See Also

Developer's Guide

API

bfs

Members

No filters for this type

Constructors

Parameters

Properties

Gets or sets a collection of nodes to start from.
These nodes form the 0th layer.
conversionfinal
Gets or sets the number of layers to return.
This setting can improve performance if not all layers are needed. Setting 0 as value (the default) will return all layers.
final
Gets or sets the collection of edges which define a subset of the graph for the algorithms to work on.

If nothing is set, all edges of the graph will be processed.

If only the excludes are set, all edges in the graph except those provided in the excludes are processed.

Note that edges which start or end at nodes which are not in the subgraphNodes are automatically not considered by the algorithm.

ItemCollection<T> instances may be shared among algorithm instances and will be (re-)evaluated upon (re-)execution of the algorithm.

The edges provided here must be part of the graph which is passed to the run method.
conversionfinal

Examples

Performing the breadth-first search on a subset of the graph
// configure the algorithm
const algorithm = new Bfs({
  // note that at least one core node must be included in the SubgraphNodes
  coreNodes: centerNode,
  // Ignore edges without target arrow heads
  subgraphEdges: {
    excludes: (edge: IEdge): boolean =>
      edge.style instanceof PolylineEdgeStyle &&
      edge.style.targetArrow instanceof Arrow &&
      edge.style.targetArrow.type === ArrowType.NONE,
  },
})
// run the algorithm
const result = algorithm.run(graph)
// add labels to indicate the layer
result.nodeLayerIds.forEach(({ key, value }) =>
  graph.addLabel(key, String(value)),
)
Gets or sets the collection of nodes which define a subset of the graph for the algorithms to work on.

If nothing is set, all nodes of the graph will be processed.

If only the excludes are set, all nodes in the graph except those provided in the excludes are processed.

ItemCollection<T> instances may be shared among algorithm instances and will be (re-)evaluated upon (re-)execution of the algorithm.

The nodes provided here must be part of the graph which is passed to the run method.
At least one of the nodes in coreNodes must be included in subgraphNodes if subgraphNodes are set.
conversionfinal

Examples

Performing the breadth-first search on a subset of the graph
// configure the algorithm
const algorithm = new Bfs({
  // note that at least one core node must be included in the SubgraphNodes
  coreNodes: centerNode,
  subgraphNodes: {
    // only consider elliptical nodes in the graph
    includes: (node: INode): boolean =>
      node.style instanceof ShapeNodeStyle &&
      node.style.shape === ShapeNodeShape.ELLIPSE,
    // but ignore the first node, regardless of its shape
    excludes: graph.nodes.first()!,
  },
})
// run the algorithm
const result = algorithm.run(graph)
// add labels to indicate the layer
result.nodeLayerIds.forEach(({ key, value }) =>
  graph.addLabel(key, String(value)),
)
Considering only selected nodes
algorithm.subgraphNodes = graphComponent.selection.nodes
Ignoring all group nodes
algorithm.subgraphNodes.excludes = (n) => graph.isGroupNode(n)
Considering selected nodes but ignoring group nodes
algorithm.subgraphNodes = graphComponent.selection.nodes
algorithm.subgraphNodes.excludes = (n) => graph.isGroupNode(n)
Gets or sets a value indicating whether to follow only incoming, only outgoing, BOTH only incoming and only outgoing, or any edges independent of their direction.

When BOTH is used, the node layers for SUCCESSOR and for PREDECESSOR are calculated and for each node the smaller of both layers is used. This may result in some layers staying empty for the combined layer assignment.

Default is UNDIRECTED.

conversionfinal

Methods

Returns the layers of nodes constructed by a breadth-first search.
The result obtained from this algorithm is a snapshot which is no longer valid once the graph has changed, e.g. by adding or removing nodes or edges.
final

Parameters

graph: IGraph
The input graph to run the algorithm on.

Return Value

BfsResult
A BfsResult containing the calculated node layers.

Throws

Exception ({ name: 'InvalidOperationError' })
if no coreNodes are provided.
Exception ({ name: 'InvalidOperationError' })
If the algorithm can't create a valid result due to an invalid graph structure or wrongly configured properties.

Complexity

O(|V| + |E|)