我试图在下图中找到从节点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”作为解决方案,并且理想情况下会显示如下所示的所选路径:
我该如何实现?
非常感谢您。
答案 0 :(得分:1)
此查询:
MATCH p=(a:LocationNode{name:'a'})-[:CONNECTED_TO*]->(e:LocationNode{name:'e'})
WITH p, REDUCE(s=0, r IN RELATIONSHIPS(p) | s + r.weight) AS totalWeight
RETURN p, totalWeight
ORDER BY totalWeight
LIMIT 1
返回此结果:
╒══════════════════════════════════════════════════════════════════════╤═════════════╕
│"p" │"totalWeight"│
╞══════════════════════════════════════════════════════════════════════╪═════════════╡
│[{"name":"a"},{"weight":1},{"name":"b"},{"name":"b"},{"weight":3},{"na│13 │
│me":"c"},{"name":"c"},{"weight":4},{"name":"d"},{"name":"d"},{"weight"│ │
│:5},{"name":"e"}] │ │
└──────────────────────────────────────────────────────────────────────┴─────────────┘
在neo4j浏览器中,如果禁用Connect result nodes选项(在“浏览器设置”窗格的底部,您可以通过单击左侧面板中的齿轮图标来显示该选项),则可视化效果为:< / p>