UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Eccentricity Centrality
    • Betweenness Centrality
    • Bridges
    • Articulation Points
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • KNN
    • Vector Similarity
    • Bipartite Graph
    • HyperANF
    • Weakly Connected Components (WCC)
    • Strongly Connected Components (SCC)
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Triangle Count
    • Clique Count
    • k-Core
    • k-Truss
    • p-Cohesion
    • Induced Subgraph
    • Topological Sort
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Dijkstra's Shortest Path
    • A* Shortest Path
    • Yen's K-Shortest Paths
    • Shortest Path (BFS)
    • Delta-Stepping SSSP
    • Shortest Path Faster Algorithm (SPFA)
    • All-Pairs Shortest Path (APSP)
    • Minimum Spanning Tree (MST)
    • K-Spanning Tree
    • Steiner Tree
    • Prize-Collecting Steiner Tree (PCST)
    • Minimum Cost Flow
    • Maximum Flow
    • K-Hop Fast
    • Longest Path (DAG)
    • Random Walk
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Same Community
    • Louvain
    • Leiden
    • Modularity Optimization
    • Label Propagation
    • HANP
    • SLPA
    • k-Means
    • HDBSCAN
    • K-1 Coloring
    • Modularity
    • Conductance
    • Max k-Cut
      • Node2Vec
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Pathfinding

Breadth-First Search (BFS)

Overview

Graph traversal is a search technique used to systematically visit and explore all the nodes in a graph. Its primary goal is to reveal and examine the structure and connections of the graph. There are two common strategies for graph traversal:

  • Breadth-First Search (BFS)
  • Depth-First Search (DFS)

The BFS algorithm explores a graph level by level and proceeds as follows:

  1. Create a queue (first in, first out) to keep track of visited nodes.
  2. Start from a selected node, enqueue it and mark as visited.
  3. Dequeue a node from the front of the queue, enqueue all its unvisited neighbors into the queue and mark them as visited.
  4. Repeat step 3 until the queue is empty.

The following example demonstrates BFS traversal starting from node A, assuming neighbors are visited in alphabetical order (A–Z):

Considerations

  • Only nodes within the same connected component as the start node will be traversed. Nodes in other connected components are excluded from the traversal results.

Example Graph

GQL
INSERT (A:default {_id: "A"}), (B:default {_id: "B"}),
       (C:default {_id: "C"}), (D:default {_id: "D"}),
       (E:default {_id: "E"}), (F:default {_id: "F"}),
       (G:default {_id: "G"}),
       (A)-[:default]->(B), (A)-[:default]->(D),
       (B)-[:default]->(E), (C)-[:default]->(A),
       (E)-[:default]->(F), (F)-[:default]->(C),
       (G)-[:default]->(D)

Parameters

NameTypeDefaultDescription
startNodeSTRING/Required. Starting node _id.
maxDepthINT-1Maximum depth to traverse (-1 = unlimited).
directionSTRINGoutEdge direction: in, out, or both.

Run Mode

Returns:

ColumnTypeDescription
nodeIdSTRINGNode identifier (_id)
depthINTDepth from start node
parentSTRINGParent node in BFS tree
GQL
CALL algo.bfs({
  startNode: "A"
}) YIELD nodeId, depth, parent

Result:

nodeIddepthparent
A0
D1A
B1A
E2B
F3E
C4F

Stream Mode

Returns the same columns as run mode, streamed for memory efficiency.

GQL
CALL algo.bfs.stream({
  startNode: "A",
  maxDepth: 2
}) YIELD nodeId, depth
RETURN nodeId, depth

Result:

nodeIddepth
A0
D1
B1
E2

Stats Mode

Returns:

ColumnTypeDescription
nodeCountINTTotal number of nodes visited
maxDepthINTMaximum depth reached from start node
GQL
CALL algo.bfs.stats({
  startNode: "A"
}) YIELD nodeCount, maxDepth

Result:

nodeCountmaxDepth
64