# ShortestPath

**URL:** https://discuss.dgraph.io/t/shortestpath/16113
**Category:** Users
**Tags:** dql
**Created:** [November 21, 2021, 8:58am UTC](https://discuss.dgraph.io/t/shortestpath/16113 "2021-11-21T08:58:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![farid](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@farid](https://discuss.dgraph.io/u/farid)
#### Post date: [November 21, 2021, 8:58am UTC](https://discuss.dgraph.io/t/shortestpath/16113/1 "2021-11-21T08:58:20Z")

</div>

when doing the shortest path, how can I return all the “names” the path has traversed through, rather than just getting the UID.  
In my use case I need to find different dept of interconnections between entities.

another question would be if I want to find the with numpaths of 2 or greater, but for 3 nodes  
somehow numpaths does not work in this scenario

```auto
{
A as var(func: eq(name, “Alice”))
E as var(func: eq(name, “Ethan”))
C as var(func: eq(name, “Chow”))

AEpath as shortest(from: uid(A), to: uid(E), numpaths: 2) {
friend
}

ACpath as shortest(from: uid(A), to: uid(C), numpaths: 2) {
friend
}

CEpath as shortest(from: uid(C), to: uid(E), numpaths: 2) {
friend
}

sp1path(func: uid(AEpath)) {
name
uid
}

sp2path(func: uid(ACpath)) {
name
uid
}

sp3path(func: uid(CEpath)) {
name
uid
}

}

.......................The mutation.................
{
  set {
    _:a <friend> _:b .
    _:b <friend> _:c .
    _:c <friend> _:d .
    _:a <friend> _:d .
    _:d <friend> _:e .
    _:a <friend> _:e .
    _:a <friend> _:f .
    _:a <name> "Alice" .
    _:a <dgraph.type> "Person" .
    _:b <name> "Bob" .
    _:b <dgraph.type> "Person" .
    _:c <name> "Chow" .
    _:c <dgraph.type> "Person" .
    _:d <name> "Danny" .
    _:d <dgraph.type> "Person" .
    _:e <name> "Ethan" .
    _:e <dgraph.type> "Person" .
    _:f <name> "Francis" .
    _:f <dgraph.type> "Person" .
   
  }
}

```

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [November 21, 2021, 4:06pm UTC](https://discuss.dgraph.io/t/shortestpath/16113/2 "2021-11-21T16:06:39Z")

</div>

> [@farid](#):
>
> when doing the shortest path, how can I return all the “names” the path has traversed through, rather than just getting the UID.

In the `_path_` block? you can’t.

> [@farid](#):
>
> another question would be if I want to find the with numpaths of 2 or greater, but for 3 nodes  
> somehow numpaths does not work in this scenario

Not sure what you mean. Can you explain?

---

<div class="post-metadata">

### Author: ![farid](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@farid](https://discuss.dgraph.io/u/farid)
#### Post date: [November 25, 2021, 1:19am UTC](https://discuss.dgraph.io/t/shortestpath/16113/3 "2021-11-25T01:19:31Z")

</div>

I was trying to find the shortest path between three nodes  
A,B,C

seems dgraph is not able to compute multiple shortest in a single go, it has to be queried individually.  
please do let me know if my understanding is correct.

---

<div class="post-metadata">

### Author: ![iluminae](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/iluminae/32/7367_2.png) [@iluminae](https://discuss.dgraph.io/u/iluminae)
#### Post date: [November 25, 2021, 2:14am UTC](https://discuss.dgraph.io/t/shortestpath/16113/4 "2021-11-25T02:14:24Z")

</div>

Correct, shortest path can be only run now between 2 uids right now.

_feature request time_ - if you have an algorithm in mind go ahead and put in a feature suggestion. Be sure to explain the algorithm and/or UX. Some times the hardest part is getting a good UX on a feature.

Currently if you say “the shortest path between 3 nodes” I am not sure what that would mean or what the response would look like.

---

<div class="post-metadata">

### Author: ![farid](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@farid](https://discuss.dgraph.io/u/farid)
#### Post date: [November 25, 2021, 3:37am UTC](https://discuss.dgraph.io/t/shortestpath/16113/5 "2021-11-25T03:37:12Z")

</div>

Well in my use case, if i have 3 nodes and i want to find the interconnection between them  
say Nodes A,B and C

I would need to find the below paths:  
A–\>B  
B–\>A \*  
A–\>C  
C–\>A \*  
B–\>C  
C–\>B \*

(\*)assuming no reverse relation is used)

If i could do this in a single query…would be great, but i would now need to loop and query multiple times to find the interconnections between them.

---

<div class="post-metadata">

### Author: ![iluminae](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/iluminae/32/7367_2.png) [@iluminae](https://discuss.dgraph.io/u/iluminae)
#### Post date: [November 25, 2021, 5:06am UTC](https://discuss.dgraph.io/t/shortestpath/16113/6 "2021-11-25T05:06:49Z")

</div>

Multiple blocks, but still single network request, which on its own is pretty cool.

Note than in your original post you can union your three query blocks together:

```auto
paths(func: uid(AEpath,CEpath,ACpath)) {
  name
  uid
}

```

Not that that saves you from having the 3 shortest path blocks.
