Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions pages/advanced-algorithms/deep-path-traversal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,37 @@ RETURN path;

This will only return paths with a length between 2 and 4 hops (inclusive).

### Constraining the expansion based on property values

K shortest paths expansion allows an arbitrary expression filter that determines
if an expansion is allowed over a certain relationship to a certain node. The
filter is defined as a lambda function over `r` and `n`, which denotes the
relationship expanded over and the node expanded to. The paths returned are the
K shortest paths of the subgraph the filter leaves behind.

Using the dataset above, the following example allows expansion only to nodes
other than `B`, which leaves a single path from `A` to `E`:

```cypher
MATCH (a:Node {name: "A"}), (e:Node {name: "E"})
WITH a, e
MATCH path=(a)-[*KSHORTEST (r, n | n.name <> "B")]->(e)
RETURN path;
```

The filter can be combined with the path limit and the length constraints:

```cypher
MATCH (a:Node {name: "A"}), (e:Node {name: "E"})
WITH a, e
MATCH path=(a)-[*KSHORTEST 2..4 |3 (r, n | n.name <> "D")]->(e)
RETURN path;
```

Unlike the other deep path traversal algorithms, K shortest paths does not
support the three-argument `(r, n, p | ...)` form that exposes the already
collected path. Using it raises an error.

### When to use K shortest paths?

Use the K shortest paths algorithm when you need to:
Expand All @@ -893,8 +924,11 @@ Use the K shortest paths algorithm when you need to:

- **Predefined nodes**: Both source and target nodes must be matched first using
a `WITH` clause
- **No filter lambdas**: K shortest paths does not support user-defined
filtering during expansion
- **Unweighted**: Paths are ordered by the number of hops. Relationship
properties are never treated as weights, and no weight lambda is accepted
- **No accumulated path in the filter lambda**: the two-argument
`(r, n | ...)` filter is supported, the three-argument `(r, n, p | ...)` form
is not


<CommunityLinks/>