# Run All Pairs Shortest Path on a subset of a graph projection

**URL:** <https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340>\
**Category:** Graph Data Science / Graph Analytics\
**Tags:** cypher, shortest-path\
**Created:** [May 14, 2021, 6:22pm UTC](https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340 "2021-05-14T18:22:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benstear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/benstear/32/14463_2.png) [@benstear](https://community.neo4j.com/u/benstear)\
**Post date:** [May 14, 2021, 6:22pm UTC](https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340/1 "2021-05-14T18:22:44Z")

</div>

I am using `gds.alpha.allShortestPaths.stream()` on a graph projection.

I want to run the algorithm on a subset of nodes that have a certain value for a property, but  
the docs say that the allShortestPaths algorithm only accepts a graph projection as input. I don't think running the algorithm on the entire graph projection would be possible because it consists of ~15 million nodes.

I need something like this:

`match (hgnc:Code {SAB:"HGNC"}) CALL algo.shortestPath.stream(hgnc) YIELD sourceNodeId, targetNodeId, distance WITH sourceNodeId, targetNodeId, distance WHERE gds.util.isFinite(distance) = true WITH source, target, distance WHERE source <> target RETURN source.name AS source, target.name AS target, distance`

But with the All Pairs Shortest Path algorithm obviously.

Thank you!

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 15, 2021, 8:02pm UTC](https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340/2 "2021-05-15T20:02:49Z")

</div>

Hello @benstear and welcome to the Neo4j community 🙂

You should have a look at [Cypher projection](https://neo4j.com/docs/graph-data-science/current/management-ops/cypher-projection/) but make sure the nodes you select have relationships or the algorithm won't work:

```auto
CALL gds.graph.create.cypher(
    'my-cypher-graph',
    'MATCH (n:Code {SAB:"HGNC"})) RETURN id(n) AS id',
    'MATCH (n)-->(m) RETURN id(n) AS source, id(m) AS target'
)

```

Otherwise, there is the version without graph projection using [Cypher shortestPath Functions](https://neo4j.com/docs/cypher-manual/current/clauses/match/#query-shortest-path):

```auto
MATCH (n:Code {SAB:"HGNC"})
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
WITH a, b
WHERE a <> b
MATCH p = shortestPath((a)-[*]-(b))
RETURN p

```

Or

```auto
MATCH (n:Code {SAB:"HGNC"})
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
WITH a, b
WHERE a <> b
MATCH p = allShortestPaths((a)-[*]-(b))
RETURN p

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![benstear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/benstear/32/14463_2.png) [@benstear](https://community.neo4j.com/u/benstear)\
**Post date:** [May 16, 2021, 1:23pm UTC](https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340/3 "2021-05-16T13:23:22Z")

</div>

Hi @Cobra,

Thanks so much for the response. I think what I was looking for is `shortestPath()` because I want only the single shortest path between each of my nodes.

However, I am worried that my query might still be too large. I have about 40,000 nodes I need to find the shortest paths between.

Do you know if `shortestPath()` will automatically exclude the inverse shortestPath searches?

For example if you find the shortest path between Node A and Node B then you don't need to find the shortest path between Node B and Node A again.

I think I my have to manually exclude the inverse relationships as Michael Hunger did in his post here: [All shortest paths between a set of nodes](https://community.neo4j.com/t/all-shortest-paths-between-a-set-of-nodes/241)

Thanks again.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 16, 2021, 1:27pm UTC](https://community.neo4j.com/t/run-all-pairs-shortest-path-on-a-subset-of-a-graph-projection/38340/4 "2021-05-16T13:27:34Z")

</div>

No problem 🙂

To be sure, I would recommend excluding them like Michael Hunger did in his query.

Regards,  
Cobra
