# Finding common complete path between multiple input nodes

**URL:** <https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352>\
**Category:** Cypher\
**Tags:** browser, cypher\
**Created:** [May 8, 2020, 10:27pm UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352 "2020-05-08T22:27:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiriti.chinthapalli](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kiriti.chinthapalli/32/9385_2.png) [@kiriti.chinthapalli](https://community.neo4j.com/u/kiriti.chinthapalli)\
**Post date:** [May 8, 2020, 10:27pm UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/1 "2020-05-08T22:27:28Z")

</div>

Hello,  
I am pretty new to CYPHER and NEO4J.  
My requirement is to find the relations of an array of input nodes.  
Example:  
 ![graph db](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/5/5898e8cc35d4594d3a39382750bce7af25117dd7.png)

I am using the following query,

```auto
WITH ["A", "B","F"] AS names
  UNWIND names AS nn
    MATCH (n {name: nn})
    WITH collect(n) AS nds
  
  UNWIND nds AS n1
    UNWIND nds AS n2
    WITH nds, n1, n2 WHERE id(n1) > id(n2)
      MATCH path = allShortestPaths((n1)-[*]-(n2))
      WITH nds, path WHERE ALL(n IN nds WHERE n IN nodes(path))
  RETURN path ORDER BY length(path) ASC

```

getting the following output which is correct,  
 ![output1](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/a/aaf4183d420f6ccacb4495088c3b36d4fe74cee0.png)

But when I gave input ["A", "B","F","H"] in the query I am getting empty output which is not desired.  
The desired output is similar to the above mentioned output but also containing A-\>H relation.  
Please let me know where I am going wrong and what needs to be done.

Thanks in advance,  
Krishna Kiriti

---

<div class="post-metadata">

**Author:** ![mdfrenchman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mdfrenchman/32/6094_2.png) [@mdfrenchman](https://community.neo4j.com/u/mdfrenchman)\
**Post date:** [May 9, 2020, 2:14am UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/2 "2020-05-09T02:14:04Z")

</div>

Hi @kiriti.chinthapalli,

In your example Node G is in between A and H, but is not in your allowed set of values.

The `WHERE all` near the end of the query requires all nodes in the path to be in the NDA array.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![kiriti.chinthapalli](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kiriti.chinthapalli/32/9385_2.png) [@kiriti.chinthapalli](https://community.neo4j.com/u/kiriti.chinthapalli)\
**Post date:** [May 9, 2020, 6:53am UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/3 "2020-05-09T06:53:45Z")

</div>

But the same query works for the inputs "A","B", "H'.

WITH ["A", "B","H"] AS names  
UNWIND names AS nn  
MATCH (n {name: nn})  
WITH collect(n) AS nds

UNWIND nds AS n1  
UNWIND nds AS n2  
WITH nds, n1, n2 WHERE id(n1) \> id(n2)  
MATCH path = allShortestPaths((n1)--(n2))  
WITH nds, path WHERE ALL(n IN nds WHERE n IN nodes(path))  
RETURN path ORDER BY length(path) ASC

Output:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/6/6f681badfa9ea3a0c854d05619f94ff208593199.png)

---

<div class="post-metadata">

**Author:** ![anthapu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anthapu/32/4113_2.png) [@anthapu](https://community.neo4j.com/u/anthapu)\
**Post date:** [May 9, 2020, 10:29pm UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/4 "2020-05-09T22:29:08Z")

</div>

If you follow your logic this is what is happening

A -\> B  
A -\> F  
A -\> H  
B -\> A  
B -\> F  
B -\> H  
..

If you follow these paths you can see that there is not a single path where all nodes you have selected are present ( A, B, F, H)

That's why it is returning empty path.

For A, B, H the for path matching is B -\> H you have all of your selected nodes, that's why you are seeing results.

---

<div class="post-metadata">

**Author:** ![kiriti.chinthapalli](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kiriti.chinthapalli/32/9385_2.png) [@kiriti.chinthapalli](https://community.neo4j.com/u/kiriti.chinthapalli)\
**Post date:** [May 11, 2020, 4:40pm UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/5 "2020-05-11T16:40:13Z")

</div>

Thanks for the reply. so what I am doing here just retrieving a single common path then. so it will not return multiple paths.  
What to do to achieve my requirement ?

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [May 14, 2020, 12:59am UTC](https://community.neo4j.com/t/finding-common-complete-path-between-multiple-input-nodes/18352/6 "2020-05-14T00:59:07Z")

</div>

I'm not sure I'm following this.

The reason why you weren't getting results with A, B, F, and H was because there is no single common path that can connect them all.

A path only has a single start node, and a single end node, and it cannot reuse relationships in the path (relationship isomorphism). Because of this, F and H are dead ends. Once you reach there, the path cannot continue. So you can go `B-A-F`, or `B-A-G-H`, or `F-A-G-H` (or the reverse of any of those). But you cannot go `B-A-F-A-G-H` because it reuses the relationship between A and F.

So you cannot return the graph you want with a single path. It would require an approach that generates multiple paths, and the graph formed by those multiple paths would include all 4 nodes.
