【发布时间】:2019-03-30 02:28:34
【问题描述】:
我试图在下图中找到从节点 a 到 e 的最轻路径:
结果应该是 13:
a -> b: 1
b -> c: 3
c -> d: 4
d -> e: 5 (take lighter edge)
----------
13
我尝试了几个示例(例如https://neo4j.com/docs/graph-algorithms/current/algorithms/shortest-path/),但找不到正确的查询。
MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{write:true,writeProperty:'sssp'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost
结果
╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3 │3 │5 │12.0 │
└─────────────┴────────────┴───────────┴───────────┘
和
MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{
nodeQuery:'MATCH(n:LocationNode) RETURN id(n) as id',
relationshipQuery:'MATCH(n:LocationNode)-[r:CONNECTED_TO]->(m:LocationNode) RETURN id(n) as source, id(m) as target, r.weight as weight',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost
结果
╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3 │19 │4 │14.0 │
└─────────────┴────────────┴───────────┴───────────┘
像下面这样的其他查询甚至不返回任何内容:
MATCH p=(LocationNode{name:'a'})-[:CONNECTED_TO*]->(LocationNode{name:'e'})
RETURN p as shortestPath,
REDUCE(weight=0, r in relationships(p) | weight+r.weight) AS totalDistance
我希望看到一个返回“13”作为解决方案的查询,并理想地显示选择的路径,如下所示:
我怎样才能做到这一点?
非常感谢。
【问题讨论】: