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
    • FastRP
    • GraphSAGE
    • HashGNN
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Pathfinding

All-Pairs Shortest Path (APSP)

Overview

The All-Pairs Shortest Path (APSP) algorithm computes the shortest path distance between every pair of reachable nodes in the graph. Unlike single-source algorithms (e.g., Delta-Stepping, SPFA) that compute distances from one source, APSP computes distances between all pairs simultaneously.

Concepts

All-Pairs Shortest Path

For a graph with N nodes, there are up to N×(N-1) directed pairs or N×(N-1)/2 undirected pairs. APSP computes the shortest path distance for each reachable pair. Unreachable pairs (in disconnected graphs) are excluded from results.

This implementation runs parallel BFS from every node to compute all pairwise distances. It is best suited for small to medium graphs due to the O(V²+VE) computational cost.

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 {value: 2}]->(B), (A)-[:default {value: 4}]->(F),
       (B)-[:default {value: 3}]->(C), (B)-[:default {value: 3}]->(D),
       (B)-[:default {value: 6}]->(F), (D)-[:default {value: 2}]->(E),
       (D)-[:default {value: 2}]->(F), (E)-[:default {value: 3}]->(G),
       (F)-[:default {value: 1}]->(E)

Parameters

NameTypeDefaultDescription
directionSTRINGoutEdge direction for traversal: out, in, or both.
weightSTRING/Edge property to use as weight. If unset, all edges have unit weight.

Run Mode

Returns:

ColumnTypeDescription
sourceSTRINGSource node identifier (_id)
targetSTRINGTarget node identifier (_id)
distanceFLOATShortest path distance
GQL
CALL algo.apsp({weight: "value"}) YIELD source, target, distance

Result:

sourcetargetdistance
AB2
AF4
AC5
AD5
AE5
AG8
BC3
BD3
BF5
BE5
BG8
DE2
DF2
DG5
EG3
FE1
FG4

Stream Mode

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

GQL
CALL algo.apsp.stream({weight: "value"}) YIELD source, target, distance
FILTER source = "A"
RETURN source, target, distance

Result:

sourcetargetdistance
AE5
AD5
AG8
AF4
AC5
AB2

Stats Mode

Returns:

ColumnTypeDescription
pairCountINTTotal number of reachable source-target pairs
maxDistanceFLOATMaximum shortest path distance across all pairs
GQL
CALL algo.apsp.stats({weight: "value"}) YIELD pairCount, maxDistance

Result:

pairCountmaxDistance
178