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
Constructors
Properties
Gets or sets a collection of nodes to start from.
0 as value (the default) will return all layers.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.
Examples
// 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.
Examples
// 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)),
)algorithm.subgraphNodes = graphComponent.selection.nodesalgorithm.subgraphNodes.excludes = (n) => graph.isGroupNode(n)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.
Methods
Parameters
- graph: IGraph
- The input graph to run the algorithm on.
Return Value
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.