> ## Documentation Index
> Fetch the complete documentation index at: https://new.docs.falkordb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# algo.SSpaths

> Explore all shortest paths from a single source node with weight, cost, and length constraints.

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

```cypher theme={null}
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

| Name           | Type    | Description                                                                          |
| -------------- | ------- | ------------------------------------------------------------------------------------ |
| `sourceNode`   | Node    | Starting node                                                                        |
| `relTypes`     | Array   | List of relationship types to follow                                                 |
| `weightProp`   | String  | Property to minimize along the path (e.g., `dist`, `time`)                           |
| `costProp`     | String  | Property to constrain the total value (optional)                                     |
| `maxCost`      | Integer | Upper bound on total cost (optional)                                                 |
| `maxLen`       | Integer | Max number of relationships in the path (optional)                                   |
| `relDirection` | String  | Traversal direction (`outgoing`, `incoming`, `both`)                                 |
| `pathCount`    | Integer | Number of paths to return (0 = all shortest, 1 = default, n = max number of results) |

## Returns

| Name         | Type    | Description                                   |
| ------------ | ------- | --------------------------------------------- |
| `path`       | Path    | Discovered path from source to target         |
| `pathWeight` | Integer | Sum of the weightProp across the path         |
| `pathCost`   | Integer | Sum of the costProp across the path (if used) |

## Examples

Let's take this Road Network Graph as an example:

<img src="https://mintcdn.com/falkordb-core/4RKo-4HTPunDsB0h/images/road_network.png?fit=max&auto=format&n=4RKo-4HTPunDsB0h&q=85&s=a88be799c06f603415412df18a27ea97" alt="Road network" width="1672" height="1178" data-path="images/road_network.png" />

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

```cypher theme={null}
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:

| pathCost | pathNodes  |
| -------- | ---------- |
| `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

```cypher theme={null}
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:

| pathWeight | pathCost | pathNodes  |
| ---------- | -------- | ---------- |
| `2`        | `1`      | \[A, D]    |
| `3`        | `1`      | \[A, B]    |
| `6`        | `2`      | \[A, D, C] |
| `7`        | `2`      | \[A, D, E] |
| `8`        | `1`      | \[A, C]    |

## Frequently Asked Questions

<AccordionGroup>
  <Accordion title="How does algo.SSpaths differ from algo.SPpaths?">
    **algo.SSpaths** finds paths from a single source to *multiple* destinations (all reachable nodes). **[algo.SPpaths](/algorithms/sppath)** finds paths between a specific source-target pair. Use SSpaths for exploration and SPpaths for point-to-point routing.
  </Accordion>

  <Accordion title="Can I limit how many paths are returned?">
    Yes. Use the `pathCount` parameter. Set it to a specific number (e.g. `pathCount: 5`) for the top results, or `0` to return all shortest paths.
  </Accordion>

  <Accordion title="How do I constrain path results by distance or cost?">
    Use `maxCost` with `costProp` to set an upper bound. For example, `costProp: 'dist', maxCost: 10` returns only paths with total distance ≤ 10.
  </Accordion>

  <Accordion title="What does the maxLen parameter do?">
    It limits the maximum number of *relationships* (hops) in any returned path. This prevents very long paths from being explored, improving performance on dense graphs.
  </Accordion>

  <Accordion title="Is weightProp required?">
    No. If omitted, all edges are treated as having equal weight. The algorithm then finds paths with the fewest hops rather than minimal weighted distance.
  </Accordion>
</AccordionGroup>
