Search code examples
graphorientdbgremlin

How to find all paths up to specific length among pairs contained in a set of N vertices with Gremlin


I would like to have as an input N vertices, then I would like to return all the paths up to specific length that are between all the pairs among those N vertices. How can this be done in Gremlin?

Some explanation - having this graph (represented in paths):

(n)-[r1]-(n1)-[r2]-(n2)-[r3]-(m)-[r5]-(n3)
(y)-[r4]-(n1)-[r2]-(n2)-[r3]-(m)-[r6]-(n4)

() node
-[]- relation

E.g these should be the paths up to length 3 among (n,m,y)

(n)-[r1]-(n1)-[r2]-(n2)-[r3]-(m)
(y)-[r4]-(n1)-[r2]-(n2)-[r3]-(m)
(n)-[r1]-(n1)-[r2]-(n1)-[r4]-(y)

This is my Gremlin example for 2 Vertices:

g = new OrientGraph("remote:localhost/graphdb")
v = g.v('#12:110')
y = g.v('#12:109')
hops = 3
v
    .as('looop')
        .inE.has('label','EdgeClass')
        .outV.has('@class','NodeClass')
        .outE.has('label','EdgeClass')
        .inV.except([v]).dedup()
    .loop('looop'){it.loops<hops}{it.object.rid==y.rid}.path

Thanks


Solution

  • Here's a sample using the Tinkergraph toy graph:

    gremlin> vertices = g.v(2,3,5).toSet()
    ==>v[2]
    ==>v[3]
    ==>v[5]
    gremlin> vertices._().as("x").bothE().bothV().simplePath().loop("x") {it.loops <= 3} {it.object in vertices}.simplePath().path() {it} {it.label}
    ==>[v[2], knows, v[1], created, v[3]]
    ==>[v[2], knows, v[1], knows, v[4], created, v[3]]
    ==>[v[2], knows, v[1], knows, v[4], created, v[5]]
    ==>[v[3], created, v[4], created, v[5]]
    ==>[v[3], created, v[1], knows, v[2]]
    ==>[v[3], created, v[4], knows, v[1], knows, v[2]]
    ==>[v[3], created, v[1], knows, v[4], created, v[5]]
    ==>[v[5], created, v[4], created, v[3]]
    ==>[v[5], created, v[4], knows, v[1], created, v[3]]
    ==>[v[5], created, v[4], knows, v[1], knows, v[2]]