cancel
Showing results for 
Search instead for 
Did you mean: 

Get all paths between two nodes via specific node

Reid
Node

We have a Neo4J database that tracks company Supply for companies. The general structure is 

````````(Company)-[:Supply_For]->(Company)`

A company can have multiple rels at the same time and each company can have multiple company supply rels.

my question is I want to find the path via two given nodes id, I tried as below pattern:

``match p = (c1:Company{id: "abc"})-[:Supply_FOR*0..2]->(c) <- [:GLF*0..2]-(c2:Company{id: "def"})  return p

but the Response Time  cannot accept, how can I get this requirement?

6 REPLIES 6

busymo16
Ninja
Ninja

Hi @Reid 
Did you get any chance to try apoc path finding algorithms (https://neo4j.com/labs/apoc/4.4/algorithms/path-finding-procedures/)? Btw, what is the issue with the Cypher path finding method that you are trying to use? Does it take a lot of time? If so, can you provide with some more information about the amount of nodes, rels, model and the configuration of Neo4j?

All the best

thank you for your reply.

I read the apoc path finding algorithms, didn't find which algo meets my requirement. 

I want is A - [Supply_FOR*0..2] ->(c)<-[Supply_FOR*0..2]-(B)

it's more like finding a path between A and B and here sometime allowed another one node C,

In general, three patterns including:

1. A -[Supply_FOR*1..4] -> (B)

2. B - [Supply_FOR*1,,4] -> (A)

3. A - [Supply_FOR*1..2] ->(c)<-[Supply_FOR*1..2]-(B)

---

The Cypher I use is 

``match p = (c1:Company{id: "abc"})-[:Supply_FOR*0..2]->(c) <- [:Supply_FOR*0..2]-(c2:Company{id: "def"})  return p limit 20`

it's too slow, many requests (30%) cost over 10 seconds.

nodes: 586 million, rels: 982 million 

BTW, nodes I need to find of A and B, maybe a super node.

neo4j info:

Version: 4.4.3
Edition: Community
Name: neo4j

Hi,
Thanks for sharing some more information about the neo4j configuration. What about the heap_size and pagecache allocation? Because the graph that you have is considered to be a big one, and it is running on Neo4j Community not on Enterprise.

Regarding the APOC you could try the following:

MATCH (c1: Company {id: "abc"})
CALL apoc.path.expandConfig(c1, { relationshipFilter: "Supply_FOR", minLevel: 0, maxLevel: 2 }) 
YIELD path
RETURN path
LIMIT 20

 And then you can use the same approach to find all possible paths, while including the second/parent Company as well.

heap_size 8G, dbms.memory.pagecache.size

I don't think this APOC meet my requirement.

Firstly, I want to find paths between A and B, this statement just have one node A.

Secondly, all paths the direction is specific.

Reid
Node

is there any suggestion?

Hi @Reid 

The query below:

MATCH (c1: Company {id: "abc"})
CALL apoc.path.expandConfig(c1, { 
    relationshipFilter: "<Supply_FOR>",
    labelFilter: "+Company",
    filterStartNode: true, 
    uniqueness: "RELATIONSHIP_PATH", 
    bfs: false
}) 
YIELD path
RETURN path 

Starts at the Company {id: "abc"} and it expands all relationships of a type Supply_FOR in all directions (either incoming or outgoing) and it gets the entire path until the last leaf node. So basically performs a depth-first-search algorithm while keeping the uniqueness of the relationship type. If this does not satisfy what you want to do, maybe you could revise your model or the requirements. I hope that this answer helps you.

Regards,

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.