简体   繁体   中英

Compute all the relationships between 2 nodes

I am trying to calculate all the relationships between 2 nodes:

For me the shortestpath , allshortestpaths and apoc.algo.dijkstra work perfectly. But these don't fetch all relationships between 2 nodes.

This is my query which works very fast:

MATCH (s:Stop)--(st:Stoptime), (e:Stop)--(et:Stoptime)    
WHERE s.name IN [ 'Schlump', 'U Schlump'] 
    AND e.name IN ['Hauptbahnhof Süd', 'HBF/Steintorwall' , 'Hamburg Hbf']
        AND st.arrival_time < et.departure_time 

MATCH p = allshortestpaths((st)-[r:PRECEDES*]->(et))
RETURN p

But when I remove the allshortestpaths and see all relationships, it takes forever.

I tried breaking the query into multiple queries as displayed below but it is also taking a lot of time.

MATCH (s:Stop)--(st:Stoptime), (e:Stop)--(et:Stoptime)    

MATCH p1 = (st)-[r1*..4]-(st2:Stoptime),
 p2 = (st2:Stoptime)-[r2*..4]-(st3:Stoptime),
 p3 = (st4:Stoptime)-[r3*..4]-(st5:Stoptime),
 p4 = (st5:Stoptime)-[r4*..4]-(et:Stoptime)
 WHERE s.name IN [ 'Schlump', 'U Schlump'] 
    AND e.name IN ['Hauptbahnhof Süd', 'HBF/Steintorwall' , 'Hamburg Hbf']
 AND all(x1 in nodes(p1) WHERE (x1:Stoptime)) 
 AND all(x2 in nodes(p2) WHERE (x2:Stoptime)) 
  AND all(x3 in nodes(p3) WHERE (x3:Stoptime)) 
   AND all(x4 in nodes(p4) WHERE (x4:Stoptime)) 
RETURN r1, r2, r3, r4

What should I do? How can I find all relationships between certain nodes?

It depends on the graph structure if it is feasible to return all paths between two nodes.

If you have circular structure or a dense graph, there will be very large number of paths between two nodes. See this question for more insights: Find all paths between two graph nodes

A graph of a public transport system will be very dense I assume. Thus, returning all paths between two stations does not work because of graph theory (not because of unoptimized queries).

You have to figure out why exactly you need all paths and if you can formulate that query differently or with some constraints (eg maximim length). Also, your graph struture might not be suitable to answer your question if you acutally need all paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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