# Finding path to node

**URL:** <https://discuss.dgraph.io/t/finding-path-to-node/5702>\
**Category:** Users\
**Created:** [December 26, 2019, 1:10pm UTC](https://discuss.dgraph.io/t/finding-path-to-node/5702 "2019-12-26T13:10:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor9](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@victor9](https://discuss.dgraph.io/u/victor9)\
**Post date:** [December 26, 2019, 1:10pm UTC](https://discuss.dgraph.io/t/finding-path-to-node/5702/1 "2019-12-26T13:10:06Z")

</div>

I’m working on a project where I need to find the route between two nodes. So I’m wondering is dgraph a good solution? And if it is how would you do that?

Lets say I have the data below. I’d like to only show the “route” leading to test5.

```auto
{
  set {
    _:1 <name> "test1" .
    _:1 <next> _:2 .
    
    _:2 <name> "test2" .
    _:2 <next> _:3 .
    _:2 <next> _:4 .
    
    _:3 <name> "test3" .
    _:3 <next> _:5 .
    
    _:4 <name> "test4" .
    
    _:5 <name> "test5" .
  }
}

```

---

<div class="post-metadata">

**Author:** ![imkleats](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/imkleats/32/2332_2.png) [@imkleats](https://discuss.dgraph.io/u/imkleats)\
**Post date:** [December 26, 2019, 3:26pm UTC](https://discuss.dgraph.io/t/finding-path-to-node/5702/2 "2019-12-26T15:26:26Z")

</div>

Hi there, Victor. It sounds like a K Shortest Path Query might suit your needs.

```auto
A as var(func: eq(name, "test1")) # Obtain uid of your starting node
B as var(func: eq(name, "test5")) # Obtain uid of your ending node

path as shortest(from: uid(A), to: uid(B)) {
   next # Specifying the predicate(s) potentially traversed in your path
}

path(func: uid(path)) {
   name 
}

```

This is an adaptation of the examples provided in the documentation. You might also look at the docs to see if any of the optional arguments (numpaths and depth) or weighting of edge traversal by facet values are useful to you. Hope this helps.

---

<div class="post-metadata">

**Author:** ![victor9](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@victor9](https://discuss.dgraph.io/u/victor9)\
**Post date:** [December 27, 2019, 7:18am UTC](https://discuss.dgraph.io/t/finding-path-to-node/5702/3 "2019-12-27T07:18:49Z")

</div>

> [@imkleats](#):
>
> A as var(func: eq(name, “test1”)) # Obtain uid of your starting node B as var(func: eq(name, “test5”)) # Obtain uid of your ending node path as shortest(from: uid(A), to: uid(B)) { next # Specifying the predicate(s) potentially traversed in your path } path(func: uid(path)) { name }

Perfect thanks 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [January 26, 2020, 7:18am UTC](https://discuss.dgraph.io/t/finding-path-to-node/5702/4 "2020-01-26T07:18:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
