如何使用密码计算节点之间的距离/跳跃/长度(#关系)?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用密码计算节点之间的距离/跳跃/长度(#关系)?相关的知识,希望对你有一定的参考价值。

我正在尝试确定图中节点之间的“距离”。使用示例电影数据库,我想返回与凯文·培根(Kevin Bacon)相距遥远的演员节点。使用下面的图片可以有效地找到所需的内容。

如何将其构建到密码查询中?这似乎有可能,因为我的cypher-foo还不是很先进,我只是想不出一种方法:(

MATCH p=(bacon:Person {name:"Kevin Bacon"})-[*1..5]-(hollywood)
WHERE hollywood.name in (['Helen Hunt', 'Ed Harris'])
RETURN p

FYI-我的neo4j数据库是v4.0

Example Distance metric

答案

此查询:

MATCH p=(bacon:Person {name:"Kevin Bacon"})-[*1..5]-(hollywood)
WHERE hollywood.name IN ['Helen Hunt', 'Ed Harris']
UNWIND
  REDUCE(s = [], i IN RANGE(0, LENGTH(p)) |
    s + {dist: i, node: NODES(p)[i]}
  ) AS data
RETURN data.dist AS distance, COLLECT(DISTINCT data.node) AS nodes

返回此结果:

╒══════════╤═════════════════════════════════════════════════════════════════════╕
│"distance"│"nodes"                                                              │
╞══════════╪═════════════════════════════════════════════════════════════════════╡
│0         │[{"name":"Kevin Bacon","born":1958}]                                 │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│1         │[{"title":"Apollo 13","tagline":"Houston, we have a problem.","releas│
│          │ed":1995},{"title":"A Few Good Men","tagline":"In the heart of the na│
│          │tion's capital, in a courthouse of the U.S. government, one man will │
│          │stop at nothing to keep his honor, and one will stop at nothing to fi│
│          │nd the truth.","released":1992},{"title":"Frost/Nixon","tagline":"400│
│          │ million people were waiting for the truth.","released":2008}]       │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│2         │[{"name":"Tom Hanks","born":1956},{"name":"Ed Harris","born":1950},{"│
│          │name":"Bill Paxton","born":1955},{"name":"Cuba Gooding Jr.","born":19│
│          │68},{"name":"Jack Nicholson","born":1937},{"name":"Ron Howard","born"│
│          │:1954}]                                                              │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│3         │[{"title":"Cast Away","tagline":"At the edge of the world, his journe│
│          │y begins.","released":2000},{"title":"Twister","tagline":"Don't Breat│
│          │he. Don't Look Back.","released":1996},{"title":"As Good as It Gets",│
│          │"tagline":"A comedy from the heart that goes for the throat.","releas│
│          │ed":1997},{"title":"Apollo 13","tagline":"Houston, we have a problem.│
│          │","released":1995}]                                                  │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│4         │[{"name":"Helen Hunt","born":1963},{"name":"Ed Harris","born":1950}] │
└──────────┴─────────────────────────────────────────────────────────────────────┘

[请注意,电影“ Apollo 13”出现在结果中的距离为1和3。而“ Ed Harris”出现在结果中的距离为2和4。这是因为存在多条长度从(“凯文·培根”到这两个节点。

以上是关于如何使用密码计算节点之间的距离/跳跃/长度(#关系)?的主要内容,如果未能解决你的问题,请参考以下文章

点与线线与线之间的位置关系

LinkedIn之类的网站如何有效地显示用户之间的关系距离1、2和3。我们可以对 neo4j 密码做同样的事情吗?

如何找到树上一组节点之间的最大距离?

arcgis怎么批量计算两点之间的距离

计算字符串的所有 1-hamming 距离邻居的最快方法?

linux如何计算两个numa节点之间的numa距离(任何numa库的内部逻辑,如libnuma..etc)?