1

如何找到关于包含路径的关系方向?我需要这个来做一个考虑到关系方向的加权图搜索(用 a 衡量“错误”方向0,另见评论)。

让我们说:

START a=node({param})
MATCH a-[*]-b
WITH a, b
MATCH p = allshortestpaths(a-[*]-b)
RETURN extract(r in rels(p): flows_with_path(r)) as in_flow

如果 , flows_with_path = 1否则sp = (a)-[*0..]-[r]->[*0..]-(b)0

编辑:更正查询

4

1 回答 1

1

So, here's a way to do it with existing cypher functions. I don't promise it's super performant, but give it a shot. We're building our collection with reduce, using an accumulator tuple with a collection and the last node we looked at, so we can check that it's connected to the next node. This requires 2.0's case/when syntax--there may be a way to do it in 1.9 but it's probably even more complex.

START a=node:node_auto_index(name="Trinity") 
MATCH a-[*]-b 
WHERE a <> b
WITH distinct a,b 
MATCH p = allshortestpaths(a-[*]-b) 
RETURN extract(x in nodes(p): x.name?), // a concise representation of the path we're checking
  head(
    reduce(acc=[[], head(nodes(p))], x IN tail(nodes(p)): // pop the first node off, traverse the tail
      CASE WHEN ALL (y IN tail(acc) WHERE y-->x)  // a bit of a hack because tail(acc)-->x doesn't parse right, so I had to wrap it so I can have a bare identifier in the pattern predicate
           THEN [head(acc) + 0, x]    // add a 0 to our accumulator collection
           ELSE [head(acc) + 1, x]    // add a 1 to our accumulator collection 
      END )) AS in_line

http://console.neo4j.org/r/v0jx03

Output:

+---------------------------------------------------------------------------+
| extract(x in nodes(p): x.name?)                               | in_line   |
+---------------------------------------------------------------------------+
| ["Trinity","Morpheus"]                                        | [1]       |
| ["Trinity","Morpheus","Cypher"]                               | [1,0]     |
| ["Trinity","Morpheus","Cypher","Agent Smith"]                 | [1,0,0]   |
| ["Trinity","Morpheus","Cypher","Agent Smith","The Architect"] | [1,0,0,0] |
| ["Trinity","Neo"]                                             | [1]       |
| ["Trinity","Neo",<null>]                                      | [1,1]     |
+---------------------------------------------------------------------------+

Note: Thanks @boggle for the brainstorming session.

于 2013-07-20T00:36:28.210 回答