From 343b122e12ff3e889e3121f170fad653aa420fa8 Mon Sep 17 00:00:00 2001 From: imilinovic Date: Wed, 12 Aug 2026 16:37:08 +0200 Subject: [PATCH] docs(kshortest): document the filter lambda for KSHORTEST Adds a "Constraining the expansion based on property values" section mirroring the equivalent BFS/WSP/ASP sections, with examples that run against the dataset already on the page. Replaces the "No filter lambdas" limitation with the two that remain: KSHORTEST orders by hop count and takes no weight lambda, and the three-argument `(r, n, p | ...)` form is not supported. --- .../deep-path-traversal.mdx | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/pages/advanced-algorithms/deep-path-traversal.mdx b/pages/advanced-algorithms/deep-path-traversal.mdx index 4702f04bf..5cce1e23e 100644 --- a/pages/advanced-algorithms/deep-path-traversal.mdx +++ b/pages/advanced-algorithms/deep-path-traversal.mdx @@ -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: @@ -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 \ No newline at end of file