Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

neo4j - Merge Duplicate Paths

I am trying to compare and merge identical paths. I am following along with this example, but it is only comparing each element in a path to a constant.

My problem is as follows. Consider three graphs

a->b->c->d
b->c->d
a->b->c->e

Created by the query CREATE p = (:x {name: 'a'}) -[:next]-> (:y {name: 'b'})-[:next]-> (:x {name: 'c'})-[:next]-> (:y {name: 'd'}), q = (:y {name: 'b'})-[:next]-> (:x {name: 'c'})-[:next]-> (:y {name: 'd'}), r = (:x {name: 'a'}) -[:next]-> (:y {name: 'b'})-[:next]-> (:x {name: 'c'})-[:next]-> (:y {name: 'e'}) RETURN *

I want to find and then merge all equivalent paths so that I get:

a->b->c->d
      |
      v
      e

My current idea for an approach is using a query to collect all identical paths in lists and then merging the overlapping nodes and relationships in the paths. For example, I want a query to get

[ 
  [ [b-c-d, b-c-d], [a-b-c, a-b-c] ] // comparison of path 1 to paths 2 and 3
  [ [b-c, b-c] ] // comparison of paths 2 and 3
]

Then I want to extract all unique nodes and merge them using apoc.refactor.mergeNodes. Followed by apoc.refactor.mergeRelationships on relationships connecting the now identical nodes.

Is this the correct approach? How would I construct a query to obtain the overlapping paths?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...