简体   繁体   English

如何获得从一个节点到另一个节点的路径,包括所有其他节点以及它们之间涉及的关系

[英]How to get a path from one node to another including all other nodes and relationships involved in between

I have designed a model in Neo4j in order to get paths from one station to another including platforms/legs involved. 我在Neo4j中设计了一个模型,以便获得从一个工作站到另一个工作站(包括所涉及的平台/支腿)的路径。 The model is depicted down here. 该模型如下所示。 Basically, I need a query to take me from NBW to RD. 基本上,我需要一个查询才能将我从NBW带到RD。 also shows the platforms and legs involved. 还显示了涉及的平台和腿。 I am struggling with the query. 我正在努力查询。 I get no result. 我没有结果。 Appreciate if someone helps. 感谢有人帮助。

Here is my cypher statement: 这是我的密码声明:

MATCH p = (a:Station)-[r:Goto|can_board|can_alight|has_platfrom*0..]->(c:Station)
WHERE (a.name='NBW') 
AND c.name='RD' 
RETURN p

Model: 模型: 在此处输入图片说明

As mentioned in the comments, in Cypher you can't use a directed variable-length relationship that uses differing directions for some of the relationships. 如评论中所述,在Cypher中,您不能使用针对某些关系使用不同方向的有向可变长度关系。

However, APOC Procedures just added the ability to expand based on sequences of relationships. 但是, APOC程序刚刚添加了根据关系序列进行扩展的功能。 You can give this a try: 您可以尝试一下:

MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='THT'
CALL apoc.path.expandConfig(start, {terminatorNodes:[end], limit:1,
  relationshipFilter:'has_platform>, can_board>, goto>, can_alight>, <has_platform'}) YIELD path
RETURN path 

I added a limit so that only the first (and shortest) path to your end station will be returned. 我添加了一个限制,以便仅返回到终端站的第一个(也是最短的)路径。 Removing the limit isn't advisable, since this will continue to repeat the relationships in the expansion, going from station to station, until it finds all possible ways to get to your end station, which could hang your query. 不建议取消限制,因为这将继续在扩展中重复从站到站的关系,直到找到找到到达终端站的所有可能方式,这可能会挂起查询。

EDIT 编辑

Regarding the new model changes, the reason the above will not work is because relationship sequences can't contain a variable-length sequence within them. 关于新模型的更改,上述方法不起作用的原因是关系序列中不能包含可变长度序列。 You have 2 goto> relationships to traverse, but only one is specified in the sequence. 您有2个goto>关系可以遍历,但是序列中仅指定了一个。

Here's an alternative that doesn't use sequences, just a whitelisting of allowed relationships. 这是一种不使用序列的替代方法,只是将允许的关系列入白名单。 The spanningTree() procedure uses NODE_GLOBAL uniqueness so there will only be a single unique path to each node found (paths will not backtrack or revisit previously-visited nodes). spanningTree()过程使用NODE_GLOBAL唯一性,因此到找到的每个节点将只有一条唯一路径(路径不会回溯或重新访问以前访问的节点)。

MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='RD'
CALL apoc.path.spanningTree(start, {terminatorNodes:[end], limit:1,
  relationshipFilter:'has_platform>|can_board>|goto>|can_alight>|<has_platform'}) YIELD path
RETURN path 

Your query is directed --> and not all of the relationships between your two stations run in the same direction. 您的查询是针对--> ,并非两个工作站之间的所有关系都朝相同的方向运行。 If you remove the relationship direction you will get a result. 如果删除关系方向,您将得到结果。

Then once you have a result I think something like this could get you pointed in the right direction on extracting the particular details from the resulting path once you get that working. 然后,一旦获得结果,我认为类似的事情可能会使您指出正确的方向,以便在工作后从结果路径中提取特定细节。

Essentially I am assuming that everything you are interested in is in your path that is returned you just need to filter out the different pieces that are returned. 本质上,我假设您感兴趣的一切都在返回的路径中,您只需要过滤掉返回的不同部分即可。

As @InverseFalcon points out this query should be limited in a larger graph or it could easily run away. 正如@InverseFalcon指出的那样,此查询应限制在较大的图中,否则可能会轻易消失。

MATCH p = (a:Station)-[r:Goto|can_board|can_alight|has_platfrom*0..]-(c:Station) 
WHERE (a.name='NBW') 
AND c.name='THT' 
RETURN filter( n in nodes(p) WHERE 'Platform' in labels(n)) AS Platforms

暂无
暂无

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

相关问题 两个节点之间的最短路径与从一个节点到所有其他节点的最短路径 - Shortest path between two nodes vs shortest path from one node to all other nodes 计算从设置节点到所有其他节点的最短路径,其中某些节点被禁止从路径 - Calculating shortest path from set node to all other nodes, with some nodes forbidden from path 路径中的一个节点重复-密码(查询两个节点之间的所有路径) - duplicated one node in path - cypher (query all path between two nodes) 删除无向图中的一个节点,该节点破坏了其他两个节点之间的路径 - Removing a node in an undirected graph that destroys a path between two other nodes Neo4j:以特定的关系顺序查询两个节点之间的路径,并且仅知道最后一个节点的标签 - Neo4j: Query for a path between two nodes in a specific order of relationships and knowing only label of the last node 比 Dijikstra 更快的算法,用于查找从一个节点开始的所有节点的最短路径 - Faster Algorithm than Dijikstra for finding shortest path to all nodes starting from one node Neo4j Cypher Query:查找连接到一个节点且具有 3 个以上其他关系的所有节点 - Neo4j Cypher Query: Finding all nodes, that are connected to a node, that has more than 3 other relationships 如何获取所有连接的节点,不包括特定关系 - How to get all connected nodes, excluding specific relationships 如何检查图中的所有节点是否可以从所有其他节点访问? - How to check if all nodes in a graph are reachable from all other nodes? Networkx图:查找给定节点集中的任何节点与另一组节点之间是否存在路径 - Networkx graph: finding if path exists between any node in a given set of nodes and another set of nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM