我希望使用Dijkstra算法时,最短路径不超过4个节点。意味着密码的条件是路径权重和路径数。
这是我的绘图 neo4j 命令:
CREATE (c:Node {name: "liming"})
WITH [{name:"youxue-0"},{name:"youxue-1"}] AS upNode UNWIND upNode AS per CREATE (n:Node) SET n = per
CREATE (c:Node {name: "final"})
WITH [{startName: "liming", endName: "youxue-0", cost:2},{startName: "youxue-0", endName: "youxue-1", cost:2},{startName: "youxue-1", endName: "final", cost:2}] AS relationships UNWIND relationships AS rel MATCH (start:Node {name: rel.startName}), (end:Node {name: rel.endName}) CREATE (start)-[:ROAD {cost:rel.cost}]->(end)
WITH [{name:"cw-0"}] AS upNode UNWIND upNode AS per CREATE (n:Node) SET n = per
WITH [{startName: "liming", endName: "cw-0", cost:2},{startName: "cw-0", endName: "final", cost:2}] AS relationships UNWIND relationships AS rel MATCH (start:Node {name: rel.startName}), (end:Node {name: rel.endName}) CREATE (start)-[:ROAD {cost:rel.cost}]->(end)
WITH [{name:"yiyun-0"},{name:"yiyun-1"},{name:"yiyun-2"},{name:"yiyun-3"}] AS upNode UNWIND upNode AS per CREATE (n:Node) SET n = per
WITH [{startName: "liming", endName: "yiyun-0", cost:1},{startName: "yiyun-0", endName: "yiyun-1", cost:1},{startName: "yiyun-1", endName: "yiyun-2", cost:1},{startName: "yiyun-2", endName: "yiyun-3", cost:1},{startName: "yiyun-3", endName: "final", cost:1}] AS relationships UNWIND relationships AS rel MATCH (start:Node {name: rel.startName}), (end:Node {name: rel.endName}) CREATE (start)-[:ROAD {cost:rel.cost}]->(end)
WITH [{name:"rock-0"}] AS upNode UNWIND upNode AS per CREATE (n:Node) SET n = per
WITH [{startName: "liming", endName: "rock-0", cost:1},{startName: "rock-0", endName: "final", cost:1}] AS relationships UNWIND relationships AS rel MATCH (start:Node {name: rel.startName}), (end:Node {name: rel.endName}) CREATE (start)-[:ROAD {cost:rel.cost}]->(end)
这是我的密码查询:
CALL gds.graph.project(
'yoGraph',
'Node',
'ROAD',
{
relationshipProperties: 'cost'
}
)
MATCH (source:Node {name: 'liming'}), (target:Node {name: 'final'})
CALL gds.shortestPath.yens.stream('myGraph', {
sourceNode: source,
targetNode: target,
k: 3,
relationshipWeightProperty: 'cost'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs, path
WHERE SIZE(costs) < 5
RETURN
index,
gds.util.asNode(sourceNode).name AS sourceNodeName,
gds.util.asNode(targetNode).name AS targetNodeName,
totalCost,
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
costs,
nodes(path) as path
“K”我给了3个,但只得到了2个结果:
╒═════╤══════════════╤══════════════╤═════════╤═════════════════════════════╤═══════════════╤══════════════════════════════════════════════════════════════════════╕
│index│sourceNodeName│targetNodeName│totalCost│nodeNames │costs │path │
╞═════╪══════════════╪══════════════╪═════════╪═════════════════════════════╪═══════════════╪══════════════════════════════════════════════════════════════════════╡
│0 │"liming" │"final" │2.0 │["liming", "rock-0", "final"]│[0.0, 1.0, 2.0]│[(:Node {name: "liming"}), (:Node {name: "rock-0"}), (:Node {name: "fi│
│ │ │ │ │ │ │nal"})] │
├─────┼──────────────┼──────────────┼─────────┼─────────────────────────────┼───────────────┼──────────────────────────────────────────────────────────────────────┤
│1 │"liming" │"final" │4.0 │["liming", "cw-0", "final"] │[0.0, 2.0, 4.0]│[(:Node {name: "liming"}), (:Node {name: "cw-0"}), (:Node {name: "fina│
│ │ │ │ │ │ │l"})] │
└─────┴──────────────┴──────────────┴─────────┴─────────────────────────────┴───────────────┴──────────────────────────────────────────────────────────────────────┘
我认为
WHERE
条件在结果集中有效。我希望结果是这样的:
╒═════╤══════════════╤══════════════╤═════════╤═══════════════════════════════════════════╤════════════════════╤══════════════════════════════════════════════════════════════════════╕
│index│sourceNodeName│targetNodeName│totalCost│nodeNames │costs │path │
╞═════╪══════════════╪══════════════╪═════════╪═══════════════════════════════════════════╪════════════════════╪══════════════════════════════════════════════════════════════════════╡
│0 │"liming" │"final" │2.0 │["liming", "rock-0", "final"] │[0.0, 1.0, 2.0] │[(:Node {name: "liming"}), (:Node {name: "rock-0"}), (:Node {name: "fi│
│ │ │ │ │ │ │nal"})] │
├─────┼──────────────┼──────────────┼─────────┼───────────────────────────────────────────┼────────────────────┼──────────────────────────────────────────────────────────────────────┤
│1 │"liming" │"final" │4.0 │["liming", "cw-0", "final"] │[0.0, 2.0, 4.0] │[(:Node {name: "liming"}), (:Node {name: "cw-0"}), (:Node {name: "fina│
│ │ │ │ │ │ │l"})] │
├─────┼──────────────┼──────────────┼─────────┼───────────────────────────────────────────┼────────────────────┼──────────────────────────────────────────────────────────────────────┤
│3 │"liming" │"final" │6.0 │["liming", "youxue-0", "youxue-1", "final"]│[0.0, 2.0, 4.0, 6.0]│[(:Node {name: "liming"}), (:Node {name: "youxue-0"}), (:Node {name: "│
│ │ │ │ │ │ │youxue-1"}), (:Node {name: "final"})] │
└─────┴──────────────┴──────────────┴─────────┴───────────────────────────────────────────┴────────────────────┴──────────────────────────────────────────────────────────────────────┘
有什么建议吗?
这个密码解决了我的问题。
MATCH (start:Node {name: "liming"}), (end:Node {name: "final"})
调用 apoc.path.expandConfig(start, { 关系过滤器:“道路”, 最低等级:1, 最大等级:4, 结束节点:[结束] }) 产量路径 带路径,节点(路径)AS 节点列表 WHERE 节点列表[-2].name <> "nihao" 使用路径,REDUCE(s = 0, rel IN 关系(路径) | s + rel.cost) AS TotalWeight 按总重量 ASC 排序 限制3 返回路径,总重量