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

Shortest Path (BFS)

Overview

The Shortest Path (BFS) algorithm finds the shortest path between a source node and a target node in an unweighted graph using bidirectional breadth-first search. By searching simultaneously from both the source and target, it meets in the middle for significant speedup on large graphs compared to a single-direction BFS.

Concepts

Why BFS for Shortest Paths

In unweighted graphs, BFS naturally finds the shortest path because it explores nodes level by level — the first time it reaches a node is always via the fewest hops. This makes BFS the optimal choice for unweighted shortest path problems, while other algorithms like Dijkstra's algorithm is needed for weighted graphs.

Bidirectional BFS

Standard BFS explores outward from the source node layer by layer until reaching the target. Bidirectional BFS improves on this by running two BFS searches in parallel — one from the source and one from the target. The search terminates when the two frontiers meet, typically exploring far fewer nodes than a single-direction search.

At each step, the algorithm expands the smaller frontier first, ensuring balanced exploration.

Considerations

  • This algorithm requires the compute engine topology. Run ALTER GRAPH <graphName> SET COMPUTE ENABLED before using it.
  • The algorithm treats all edges as unweighted (each edge has cost 1).
  • Returns distance -1 if no path exists between source and target.

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]->(F),
       (B)-[:default]->(C), (B)-[:default]->(D),
       (B)-[:default]->(F), (D)-[:default]->(E),
       (D)-[:default]->(F), (E)-[:default]->(G),
       (F)-[:default]->(E)

Parameters

NameTypeDefaultDescription
sourceSTRING/Required. Source node _id.
targetSTRING/Required. Target node _id.
directionSTRINGoutEdge direction: in, out, or both.
maxDepthINT10Maximum search depth.

Run Mode

Returns:

ColumnTypeDescription
distanceINTShortest path distance (-1 if no path exists)
pathSTRINGShortest path as node _ids (e.g., "A -> B -> D")
GQL
CALL algo.shortest_bfs({
  source: "A",
  target: "G"
}) YIELD distance, path

Result:

distancepath
3A -> F -> E -> G

Stats Mode

Returns:

ColumnTypeDescription
distanceINTShortest path distance (-1 if no path exists)
nodesExploredINTTotal nodes explored during search
GQL
CALL algo.shortest_bfs.stats({
  source: "A",
  target: "G"
}) YIELD distance, nodesExplored

Result:

distancenodesExplored
37