Skip to main content

algo.SSpaths

The algo.SSpaths procedure returns all shortest paths from a source node to multiple reachable nodes, subject to constraints like cost, path length, and number of paths to return.

Syntax

CALL algo.SSpaths({
sourceNode: <node>,
relTypes: [<relationship_type>],
weightProp: <property>, // optional
costProp: <property>, // optional
maxCost: <int>, // optional
maxLen: <int>, // optional
relDirection: "outgoing", // or "incoming", "both"
pathCount: <int>
})
YIELD path, pathWeight, pathCost

Parameters

NameTypeDescription
sourceNodeNodeStarting node
relTypesArrayList of relationship types to follow
weightPropStringProperty to minimize along the path (e.g., dist, time)
costPropStringProperty to constrain the total value (optional)
maxCostIntegerUpper bound on total cost (optional)
maxLenIntegerMax number of relationships in the path (optional)
relDirectionStringTraversal direction (outgoing, incoming, both)
pathCountIntegerNumber of paths to return (0 = all shortest, 1 = default, n = max number of results)

Returns

NameTypeDescription
pathPathDiscovered path from source to target
pathWeightIntegerSum of the weightProp across the path
pathCostIntegerSum of the costProp across the path (if used)

Examples:

Lets take this Road Network Graph as an example:

Road network

Example: All Shortest Paths by Distance (up to 10 km)

MATCH (a:City{name:'A'})
CALL algo.SSpaths({
sourceNode: a,
relTypes: ['Road'],
costProp: 'dist',
maxCost: 10,
pathCount: 1000
})
YIELD path, pathCost
RETURN pathCost, [n in nodes(path) | n.name] AS pathNodes
ORDER BY pathCost

Expected Result:

pathCostpathNodes
2[A, D]
3[A, B]
6[A, D, C]
7[A, D, E]
8[A, B, D]
8[A, C]
10[A, B, E]

Example: Top 5 Shortest Paths from A by Distance

MATCH (a:City{name:'A'})
CALL algo.SSpaths({
sourceNode: a,
relTypes: ['Road'],
weightProp: 'dist',
pathCount: 5
})
YIELD path, pathWeight, pathCost
RETURN pathWeight, pathCost, [n in nodes(path) | n.name] AS pathNodes
ORDER BY pathWeight

Expected Result:

pathWeightpathCostpathNodes
21[A, D]
31[A, B]
62[A, D, C]
72[A, D, E]
81[A, C]