Neo4J common neighbors of a set nodes in a path

帅比萌擦擦* 提交于 2019-12-11 14:54:17

问题


My network consists of nodes and the relationship is a numeric number. Think of it as a set of cities, and the relationship is the whether there is a road and if so how far is it.

I have path from my neo4j query, wonder how I can find the neighbor of this path given the following condition. These neighbors should be neighbor to more than one node in the path. In the following picture, I have tried to illustrate what I mean. My path looks like the blue star below. I would like to find the green nodes. These green nodes, are connected to two or more nodes in the path. I have draw a few of these green nodes.

As an output I would like to have a path that include the blue path as well as the green ones.


EDIT


My original path looks like

If I use the suggested solution by @NonameCurious, I will have

As you can see the result is a group of nodes which there is no relationship. I assume it is because the query only returns nodes. However, I would like to have the connection between those "neighbors" with the original path be displayed on top of the original path.


回答1:


How about this:

WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)--(node) WHERE NOT a IN nodes
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a

I am assuming path is given.

UPDATE:

If you need to filter relationships, you can use something like this:

WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)-[r]-(node) WHERE NOT a IN nodes AND r.score > 27
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a


来源:https://stackoverflow.com/questions/59213115/neo4j-common-neighbors-of-a-set-nodes-in-a-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!