Find all paths between two vertices, only considering the vertex where propertyname = value

I have an example of using a workaround as shown below. Is orientDb supported?

1) Find all paths from node1 to node2. ONLY TRAVERSE those nodes that have the property xyz = "val1" 2) Find the shortest path. STLY TRAVERSE those nodes that have the property xyz = "val1" 3) Find the longest path. ONLY POISE those nodes that have the property xyz = "val1"

abstract class TraversGraph{
     public  Path getPath(Node src,Node dest, Propery property,Value value);
}

      

Note. Pay attention to the condition that I mentioned in the caps

+3


source to share


1 answer


You can use Gremlin to get paths between two nodes. You can find all the paths first, so the shortest and longest paths are included in all of these paths. Here is an example: enter image description here As you can see in the image, there are three paths from A to B (A-> B, A-> F-> C-> B, A-> E-> D-> C-> B), but node F does not have an "xyz" property with a value of "val1", so these paths should not be included.

code:

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> a = g.addVertex(null,[name: "A", xyz: "val1"])
==>v[0]
gremlin> b = g.addVertex(null,[name: "B", xyz: "val1"])
==>v[1]
gremlin> c = g.addVertex(null,[name: "C", xyz: "val1"])
==>v[2]
gremlin> d = g.addVertex(null,[name: "D", xyz: "val1"])
==>v[3]
gremlin> e = g.addVertex(null,[name: "E", xyz: "val1"])
==>v[4]
gremlin> f = g.addVertex(null,[name: "F"])
==>v[5]
gremlin> g.addEdge(a, b, "KNOWS")
==>e[6][0-KNOWS->1]
gremlin> g.addEdge(a, e, "KNOWS")
==>e[7][0-KNOWS->4]
gremlin> g.addEdge(a, f, "KNOWS")
==>e[8][0-KNOWS->5]
gremlin> g.addEdge(f, c, "KNOWS")
==>e[9][5-KNOWS->2]
gremlin> g.addEdge(e, d, "KNOWS")
==>e[10][4-KNOWS->3]
gremlin> g.addEdge(d, c, "KNOWS")
==>e[11][3-KNOWS->2]
gremlin> g.addEdge(c, b, "KNOWS")
==>e[12][2-KNOWS->1]

      

And moving between node A and node B (here we are not filtering the "xyz" property), so we get three paths:

gremlin> a.out('KNOWS').loop(1){it.loops<100}{true}.has('name', 'B').path{it.name}
==>[A, B]
==>[A, F, C, B]
==>[A, E, D, C, B]

      



And add a property filter "xyz"

gremlin> a.out('KNOWS').loop(1){it.loops<100 && it.object.hasNot('xyz', null)}{true}.has('name', 'B').path{it.name}
==>[A, B]
==>[A, E, D, C, B]

      

Thus, we get the shortest path: [A, B] and the longest path: [A, E, D, C, B]

English is not my first language, so if any confusion feel free to contact me.

+6


source







All Articles