4

我试图在我的 Neo4j 数据库中找到两个相距最远的节点。出于分析的目的,我将两个节点之间的最短距离视为它们之间的距离。因此,最远的两个节点之间将有最长的最短路径。我正在使用 Cypher 中的以下语法来查找最短节点。

给定 Neo4j 示例文档http://docs.neo4j.org/chunked/milestone/query-match.html#match-shortest-path中所示的两个节点,我可以运行以下 Cypher 查询。

MATCH p = shortestPath((martin:Person)-[*..15]-(oliver:Person))
WHERE martin.name = 'Martin Sheen' AND oliver.name = 'Oliver Stone'
RETURN p

我的数据库有超过 1/2 百万个节点。蛮力方式显然需要很长时间。有没有简单或更快的方法来获得这两个节点?

[作为一个额外的皱纹..图表是加权的,但这个细节可以忽略。]

4

1 回答 1

5

If I'm reading this correctly, you want all-pairs shortest path. This will give you a list with each node as a source and the shortest path to every other node. While it does do it by weight, you can simple use a weight of 1 for everything.

You'll have to implement this yourself in Java as Cypher doesn't have anything for this.

于 2013-10-19T19:51:24.983 回答