GraphPath objects containing the source node and ordered steps (edge + node pairs).
Shortest Paths
Shortest path algorithms find paths with minimum total weight. Use BFS for unweighted graphs or Dijkstra for weighted graphs.hasPath
- Reachability testing
- Validating connections before path computation
Example
getShortestPath
undefined if unreachable.
Complexity:
- O(V + E) for unweighted graphs (BFS)
- O((V + E) log V) for weighted graphs (Dijkstra)
- Finding the shortest route between two points
- Navigation and routing
- Dependency resolution
Example
Weighted graphs
getShortestPaths
- O(V + E) for unweighted graphs
- O((V + E) log V) for weighted graphs
- Finding all alternative routes of equal length
- Analyzing path redundancy
- Building path tables
Example
genShortestPaths
getShortestPaths() for the full array.
When to use:
- Processing paths without loading all into memory
- Early termination when finding acceptable path
Example
getAllPairsShortestPaths
'dijkstra'(default): Runs Dijkstra from each source. O(V * (V + E) log V)'floyd-warshall': Dynamic programming. O(V³)
- Precomputing all-pairs distances
- Network diameter calculation
- Building complete routing tables
- Dijkstra: O(V² + VE log V) for sparse graphs
- Floyd-Warshall: O(V³) always, better for dense graphs
Example
Using Floyd-Warshall
Simple Paths
Simple paths are acyclic paths that don’t revisit nodes. These algorithms use DFS backtracking.getSimplePath
undefined if unreachable.
Complexity: O(V + E) in best case, exponential in worst case
When to use:
- Finding any path without cycles
- When shortest path might contain cycles (in directed graphs with negative weights)
Example
getSimplePaths
- Enumerating all possible routes
- Finding alternative paths
- Network reliability analysis
Example
genSimplePaths
- Processing paths one at a time
- Early termination when finding acceptable path
- Avoiding memory issues with many paths
Example
Path Operations
joinPaths
- Combining path segments
- Building longer routes from cached sub-paths
Example
Options
PathOptions
from- Source node ID. Default:graph.initialNodeId, else the sole node with in-degree 0to- Target node ID. If omitted, returns paths to all reachable nodesgetWeight- Edge weight function. Default: every edge has weight 1. Used by Dijkstra’s algorithm when provided
SinglePathOptions
from- Source node ID. Default:graph.initialNodeId, else the sole node with in-degree 0to- Target node ID. Required for single-path queriesgetWeight- Edge weight function. Default: every edge has weight 1
AllPairsShortestPathsOptions
algorithm- Algorithm to use. Default:'dijkstra'. Use'floyd-warshall'for dense graphsgetWeight- Edge weight function. Default: every edge has weight 1
Path Types
GraphPath
source- The source node where this path beginssteps- Ordered steps from source to target.path.steps.at(-1)?.nodeis the final node. Empty steps means source-only path
GraphStep
edge- Edge traversed to reach this nodenode- Node reached after traversing the edge