简体   繁体   English

Neo4J使用所有节点(无序)密码找到最短路径

[英]Neo4J find shortest path using all nodes (unordered) cypher

I'm not sure if this can be done in any efficient way, but i'm hoping it can be. 我不确定是否可以通过任何有效的方式完成此操作,但我希望可以做到。

I am getting a set of data with data on it that allows me to find very specific nodes. 我得到了一组数据,上面有数据,使我可以找到非常特定的节点。 However this data is not ordered in any way in terms of how the nodes are connected. 但是,这些数据没有按照节点的连接方式进行排序。

What I am trying to do is to be able to find all the nodes in neo4J (up to 7) and then say with these 7 nodes, find the path that connects them all. 我想做的是能够找到neo4J中的所有节点(最多7个),然后说出这7个节点,找到连接它们的路径。

These given nodes will be the only nodes connected in the desired path. 这些给定的节点将是在所需路径中连接的唯一节点。

basically i'm trying to get a set that looks like 基本上我想得到一套看起来像

1,2,3,4,5,6,7 1,2,3,4,5,6,7

and to be able to find 2->7->6->3<-5<-1->4 并能够找到2-> 7-> 6-> 3 <-5 <-1-> 4

any help or direction would be greatly appreciated 任何帮助或指示将不胜感激

the way I would do it is the following: 我会这样做的方式如下:

You need a starting node from where on you will query the next 7 nodes. 您需要一个起始节点,从该节点将查询接下来的7个节点。 To be able to find the very first 7 nodes I would introduce a starting root node. 为了能够找到前7个节点,我将引入一个起始根节点。 Lets call it simply :Root . 让我们简单地称之为:Root。

MATCH (:Root)-[r:NEXT*1..7]->(x)<-[]-(y) RETURN x, y

or even simpler: 甚至更简单:

MATCH (:Root)-[r:NEXT*..7]->(x)<-[]-(y) RETURN x, y

:Root of course could be any other node in your set, to get the next seven nodes from there on. :root当然可以是您集合中的任何其他节点,以便从那里获取接下来的七个节点。

Is this what you want? 这是你想要的吗?

Take a further look at the following neo4j cheat sheet, which has some great tips: 进一步看下面的neo4j备忘单,其中有一些很棒的技巧:

http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf

Regards 问候

EDIT 编辑

Ok sorry, I misunderstood you. 好的,抱歉,我误会了你。

Maybe this brings you further: 也许这会使您更进一步:

MATCH (n:Node) where n.refId in [1,2,3,4,5,6,7] 
MATCH (n2:Node) where n2.refId in [1,2,3,4,5,6,7]
MATCH p=shortestPath((q)-[:NEXT*]-(q2)) 
return collect(distinct p)

or if those numbers are node IDs than like this: 或如果这些数字是节点ID,则如下所示:

MATCH (n:Node) where id(n) in [1,2,3,4,5,6,7] 
MATCH (n2:Node) where id(n2) in [1,2,3,4,5,6,7]
MATCH p=shortestPath((q)-[:NEXT*]-(q2)) 
return collect(distinct p)

This actually returns all the paths between the given nodes as a collection. 实际上,这会将给定节点之间的所有路径作为集合返回。 So it doesn't return a single path for all those nodes. 因此,它不会为所有这些节点返回单个路径。 I am not aware of a function doing that. 我不知道这样做的功能。

However the neo4j browser displays just a single path between all those nodes desired, because of it's auto complete function. 但是,neo4j浏览器仅显示所有所需节点之间的一条路径,因为它具有自动完成功能。 So I think you would have to build your own logic in code, if you want to connect those paths to a single one. 因此,我想,如果要将这些路径连接到一条路径,就必须在代码中构建自己的逻辑。

Maybe this is at least a starting point for the problem 也许这至少是问题的起点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM