Recurse with calculated field


(Andrei) #1

Hi,
I am evaluating Dgraph to see if it can be used for the application I am building, however, my knowledge of Dgraph’s implementation of GraphQL is not good enough to be able to establish if this is possible:

My aim is to recursively traverse a graph starting at a vertex, performing a calculation upon a weighted facet and use that value on the next recursion.

Given the following dataset:

{
  set {
   _:andrei <name> "andrei" .
   _:janice <name> "janice" .
   _:dylan <name> "dylan" .
   _:marilyn <name> "marilyn" .
   _:andras <name> "andras" .
   _:lucy <name> "lucy" .
   _:andrei <rates> _:janice (rating=0.9) .
   _:janice <rates> _:dylan (rating=0.5) .
   _:dylan <rates> _:andras (rating=0.8) .
   _:andras <rates> _:lucy (rating=0.3) .
   _:andrei <rates> _:marilyn (rating=0.8) .
  }
}

I wish to recurse from _:andrei multiplying the @facet(rating) each time until the rating is below 0.4

_:andrei <rates> _:janice (rating=0.9) .
1 * 0.9 = 0.9
_:janice <rates> _:dylan (rating=0.5) .
0.9 * 0.5 = 0.45
_:dylan <rates> _:andras (rating=0.8) .
0.45 * 0.8 = 0.36

0.36 < 0.4 So Stop Traversal

I get that @recurse can do recursion. I just don’t understand how I can set up the variables and pass the inputs.

If someone could a) tell me if Dgraph can do this b) show me a quick and dirty sample query so I could learn how, I would be really grateful.

Thank you.


(Michel Conrado (Support Engineer)) #2

I’m not sure if you could do this, but maybe yes.

With math functions, Var block and K-Shortest Path you might get something interesting.

I’m not sure if Recurse would be your case, it’s used to penetrate and expand Nodes via a set of predicates (with filter, facets, etc.). https://docs.dgraph.io/query-language#recurse-query

check this example Flattening to get unique nodes within n steps

For your SET example, I see you want to expand just one user and relate to others until you reach a certain minimum value. Recurse would do this separately, would not relate things (But you might get something by using Var Block in sequences). In that case something like K-Shortest might be the case. But not sure exactly.

This week I’m focused on some tests with Dgraph, I can not delve into your case. But I can get your doubts to come.

Math on value variables
https://docs.dgraph.io/query-language#math-on-value-variables

Aggregation
https://docs.dgraph.io/query-language#aggregation

Var Blocks
https://docs.dgraph.io/query-language#var-blocks

Facets and Variable Propagation
https://docs.dgraph.io/query-language#facets-and-variable-propagation

Facets and Aggregation
https://docs.dgraph.io/query-language#facets-and-aggregation

K-Shortest Path Queries
https://docs.dgraph.io/query-language#k-shortest-path-queries


(Andrei) #3

Hi Michel,

Thanks for your help. I don’t think I explained the problem well enough.

I need to create a graph with weights on the edges.
I need to traverse the graph and return the product of the weights in the traversal path
So for instance:
node1 trusts node2 .5
node2 trusts node3 .5
node1 trusts node4 .4
A traversal from node 1 would return
node2 .5
node3 .25
node4 .4
Is this possible. If it isn’t, is it possible to write custom traversal code as some sort of plugin?


(system) #4

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