如何使用Gremlin查找包含在一组N个顶点中的对中的特定长度的所有路径

时间:2015-05-03 15:39:40

标签: graph orientdb gremlin

我想将N个顶点作为输入,然后我想返回那些N个顶点中所有对之间的特定长度的所有路径。怎么能在格雷姆林做到这一点?

一些解释 - 有这个图(用路径表示):

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

() node
-[]- relation

例如,这些应该是(n,m,y)

中长度为3的路径
(n)-[r1]-(n1)-[r2]-(n2)-[r3]-(m)
(y)-[r4]-(n1)-[r2]-(n2)-[r3]-(m)
(n)-[r1]-(n1)-[r2]-(n1)-[r4]-(y)

这是我的2个顶点的Gremlin示例:

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

由于

1 个答案:

答案 0 :(得分:3)

以下是使用Tinkergraph玩具图的示例:

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]]