Finding path to node

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.

{
  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" .
  }
}

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

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.

1 Like

Perfect thanks :slight_smile:

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