Breadth First Search
Glossary
 Directed

Directed trait. The algorithm is welldefined on a directed graph.
 Undirected

Undirected trait. The algorithm is welldefined on an undirected graph.
 Homogeneous

Homogeneous trait. The algorithm will treat all nodes and relationships in its input graph(s) similarly, as if they were all of the same type. If multiple types of nodes or relationships exist in the graph, this must be taken into account when analysing the results of the algorithm.
 Heterogeneous

Heterogeneous trait. The algorithm has the ability to distinguish between nodes and/or relationships of different types.
 Weighted

Weighted trait. The algorithm supports configuration to set node and/or relationship properties to use as weights. These values can represent cost, time, capacity or some other domainspecific properties, specified via the nodeWeightProperty, nodeProperties and relationshipWeightProperty configuration parameters. The algorithm will by default consider each node and/or relationship as equally important.
1. Introduction
The Breadth First Search algorithm is a graph traversal algorithm that given a start node visits nodes in order of increasing distance, see https://en.wikipedia.org/wiki/Breadthfirst_search. A related algorithm is the Depth First Search algorithm, Depth First Search. This algorithm is useful for searching when the likelihood of finding the node searched for decreases with distance. There are multiple termination conditions supported for the traversal, based on either reaching one of several target nodes, reaching a maximum depth, exhausting a given budget of traversed relationship cost, or just traversing the whole graph. The output of the procedure contains information about which nodes were visited and in what order.
2. Syntax
CALL gds.bfs.stream(
graphName: string,
configuration: map
)
YIELD
sourceNode: int,
nodeIds: int,
path: Path
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

Integer 

yes 
The number of concurrent threads used for running the algorithm. Also provides the default value for 'readConcurrency' and 'writeConcurrency'. 

Integer 

yes 
The number of concurrent threads used for writing the result (applicable in WRITE mode). 

Boolean 

yes 
If disabled the progress percentage will not be logged. 
Name  Type  Default  Optional  Description 

sourceNode 
Integer 

no 
The node id of the node where to start the traversal. 
targetNodes 
List of Integer 

yes 
Ids for target nodes. Traversal terminates when any target node is visited. 
maxDepth 
Integer 

yes 
The maximum distance from the source node at which nodes are visited. 
Name  Type  Description 

sourceNode 
Integer 
The node id of the node where to start the traversal. 
nodeIds 
List of Integer 
The ids of all nodes that were visited during the traversal. 
path 
Path 
A path containing all the nodes that were visited during the traversal. 
CALL gds.bfs.mutate(
graphName: string,
configuration: map
)
YIELD
relationshipsWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
mutateMillis: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

mutateRelationshipType 
String 

no 
The relationship type used for the new relationships written to the projected graph. 
Name  Type  Default  Optional  Description 

sourceNode 
Integer 

no 
The node id of the node where to start the traversal. 
targetNodes 
List of Integer 

yes 
Ids for target nodes. Traversal terminates when any target node is visited. 
maxDepth 
Integer 

yes 
The maximum distance from the source node at which nodes are visited. 
Name  Type  Description 

preProcessingMillis 
Integer 
Milliseconds for preprocessing the graph. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
postProcessingMillis 
Integer 
Unused. 
mutateMillis 
Integer 
Milliseconds for adding relationships to the projected graph. 
relationshipsWritten 
Integer 
The number of relationships that were added. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.bfs.stats(
graphName: string,
configuration: map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 
Name  Type  Default  Optional  Description 

sourceNode 
Integer 

no 
The node id of the node where to start the traversal. 
targetNodes 
List of Integer 

yes 
Ids for target nodes. Traversal terminates when any target node is visited. 
maxDepth 
Integer 

yes 
The maximum distance from the source node at which nodes are visited. 
Name  Type  Description 

preProcessingMillis 
Integer 
Milliseconds for preprocessing the graph. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
postProcessingMillis 
Integer 
Unused. 
configuration 
Map 
The configuration used for running the algorithm. 
3. Examples
In this section we will show examples of running the Breadth First Search algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small graph of a handful nodes connected in a particular pattern. The example graph looks like this:
Consider the graph projected by the following Cypher statement:
CREATE
(nA:Node {name: 'A'}),
(nB:Node {name: 'B'}),
(nC:Node {name: 'C'}),
(nD:Node {name: 'D'}),
(nE:Node {name: 'E'}),
(nA)[:REL]>(nB),
(nA)[:REL]>(nC),
(nB)[:REL]>(nE),
(nC)[:REL]>(nD)
CALL gds.graph.project('myGraph', 'Node', 'REL')
In the following examples we will demonstrate using the Breadth First Search algorithm on this graph.
3.1. Memory Estimation
First off, we will estimate the cost of running the algorithm using the estimate
procedure.
This can be done with any execution mode.
We will use the stream
mode in this example.
Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have.
When you later actually run the algorithm in one of the execution modes the system will perform an estimation.
If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited.
To read more about this, see Automatic estimation and execution blocking.
For more details on estimate
in general, see Memory Estimation.
MATCH (source:Node {name: 'A'})
CALL gds.bfs.stream.estimate('myGraph', {
sourceNode: source
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
RETURN nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
nodeCount  relationshipCount  bytesMin  bytesMax  requiredMemory 

5 
4 
536 
536 
"536 Bytes" 
3.2. Stream
In the stream
execution mode, the algorithm returns the path in traversal order for each relationship.
This allows us to inspect the results directly or postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
MATCH (source:Node{name:'A'})
CALL gds.bfs.stream('myGraph', {
sourceNode: source
})
YIELD path
RETURN path
If we do not specify any of the options for early termination, the algorithm will traverse the entire graph.
In the image below we can see the traversal order of the nodes, marked by relationship type NEXT
:
MATCH (a:Node{name:'A'}), (d:Node{name:'D'}), (e:Node{name:'E'})
WITH id(a) AS source, [id(d), id(e)] AS targetNodes
CALL gds.bfs.stream('myGraph', {
sourceNode: source,
targetNodes: targetNodes
})
YIELD path
RETURN path
In the image below we can see the traversal order of the nodes, marked by relationship type NEXT
.
It is notable that the D
node is not present in the picture, this is because the algorithm reached the target node E
first and terminated the execution, leaving D
unvisited.
MATCH (source:Node{name:'A'})
CALL gds.bfs.stream('myGraph', {
sourceNode: source,
maxDepth: 1
})
YIELD path
RETURN path
In the image below we can see the traversal order of the nodes, marked by relationship type NEXT
.
Nodes D
and E
were not visited since they are at distance 2 from node A
.
3.3. Mutate
The mutate
execution mode updates the named graph with new relationships.
The path returned from the Breadth First Search algorithm is a line graph, where the nodes appear in the order they were visited by the algorithm.
The relationship type has to be configured using the mutateRelationshipType
option.
The mutate
mode is especially useful when multiple algorithms are used in conjunction.
For more details on the mutate
mode in general, see Mutate.
Breadth First Search mutate
supports the same early termination conditions as the stream
mode.
mutate
mode:MATCH (source:Node{name:'A'})
CALL gds.bfs.mutate('myGraph', {
sourceNode: source,
mutateRelationshipType: 'BFS'
})
YIELD relationshipsWritten
RETURN relationshipsWritten
relationshipsWritten 

4 
After executing the above query, the inmemory graph will be updated with new relationships of type BFS
.
The relationships produced are always directed, even if the input graph is undirected. 
Was this page helpful?