简体   繁体   English

Neo4j Cypher:检查路径中不连续节点的属性

[英]Neo4j Cypher: check attributes of not consecutive nodes in path

I have got a graph that represents several bus/train stops in different cities. 我有一张图表代表了不同城市的几个公共汽车/火车站。 Lets assume I want to go from city A (with stops a1, a2, a3...) to city Z (with stops z1, z2...) 让我们假设我想从城市A(有停止a1,a2,a3 ......)到城市Z(有停止z1,z2 ......)

There are several routes (relations) between the nodes and I want to get all paths between the start and the end node. 节点之间有几个路由(关系),我想获得起始节点和结束节点之间的所有路径。 My cost vector would be complex (travel time and waiting time and price and and and...) in reality, therefore I cannot use shortestpaths etc. I managed to write a (quite complex) query that does what I want: In general it is looking for each match with start A and end Z that is available. 实际上,我的成本向量将是复杂的(旅行时间和等待时间和价格以及......),因此我不能使用最短路径等。我设法编写了一个(非常复杂的)查询来做我想要的:一般来说正在寻找每个匹配的开始A和结束Z可用。

I try to avoid looping by filter out results with special characteristics, eg 我试图通过过滤掉具有特殊特征的结果来避免循环,例如

MATCH (from{name:'a1'}), (to{name:'z1'}),
path = (from)-[:CONNECTED_TO*0..8]->(to)    
WHERE ALL(b IN NODES(path) WHERE SINGLE(c IN NODES(path) WHERE b = c))

Now I want to avoid the possiblity to visit one city more than once, eg instead of a1-->a2-->d2-->d4-->a3-->a4-->z1 I want to get a1-->a4-->z1. 现在我想避免不止一次访问一个城市的可能性,例如,而不是a1 - > a2 - > d2 - > d4 - > a3 - > a4 - > z1我想得到a1-- > A4 - > Z1。

Therefore I have to check all nodes in the path. 因此,我必须检查路径中的所有节点。 If the value of n.city is the same for consecutive nodes, everything is fine. 如果连续节点的n.city值相同,那么一切都很好。 But If I got a path with nodes of the same city that are not consecutive, eg cityA--> cityB-->cityA I want to throw away that path. 但是如果我得到一条同一城市的节点不连续的路径,例如cityA - > cityB - > cityA我想扔掉那条路。

How can I do that? 我怎样才能做到这一点? Is something possible? 有可能吗?

I know, that is not really a beatiful approach, but I invested quite a lot of time in finding a better one without throwing away the whole data structure but I could not find one. 我知道,这不是一个美丽的方法,但我花了很多时间寻找一个更好的方法而不丢弃整个数据结构,但我找不到一个。 Its just a prototype and Neo4j is not my focus. 它只是一个原型而Neo4j不是我的重点。 I want to test some tools and products to build some knowledge. 我想测试一些工具和产品来构建一些知识。 I will go ahead with a better approach next time. 我下次会继续采用更好的方法。

Interesting question. 有趣的问题。 The important thing to observe here is that a path that never revisits a city (after leaving it) must have fewer transitions between cities than the number of distinct cities. 这里需要注意的重要一点是,从未重新访问城市的路径(离开之后)必须在城市之间的转换少于不同城市的数量。 For example: 例如:

  • AABBC (a "good" path) has 3 distinct cities and 2 transitions AABBC(“好”路径)有3个不同的城市和2个过渡
  • ABBAC (a "bad" path) also has 3 distinct cities but 3 transitions ABBAC(“坏”路径)也有3个不同的城市,但有3个过渡

With this observation in mind, the following query should work (even if the start and end nodes are the same): 考虑到这一点,以下查询应该有效(即使起始节点和结束节点相同):

MATCH path = ({name:'a1'})-[:CONNECTED_TO*0..8]->({name:'z1'})
WITH path, NODES(path) as ns
WITH path, ns,
  REDUCE(s = {cnt: 0, last: ns[0].city}, x IN ns[1..] |
    CASE WHEN x.city = s.last THEN s ELSE {cnt: s.cnt+1, last: x.city} END).cnt AS nTransitions
UNWIND ns AS node
WITH path, nTransitions, COUNT(DISTINCT node.city) AS nCities
WHERE nTransitions < nCities
RETURN path;

The REDUCE function is used to calculate the number of transitions in a path. REDUCE函数用于计算路径中的转换数。

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

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