您可以使用 Gremlin 获取两个节点之间的路径。您可以首先找到所有路径,因此所有这些路径中都包含最短和最长的路径。这是一个示例:
如图所示,从 A 到 B 有 3 条路径(A->B、A->F->C->B、A->E->D->C-> B),但节点 F 没有值为“val1”的属性“xyz”,因此不应包含此路径。
代码:
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]
并在节点A和节点B之间遍历(这里我们不过滤属性“xyz”),所以我们得到三个路径:
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]
并添加属性“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]
因此我们得到最短路径:[A, B] 和最长路径:[A, E, D, C, B]
英语不是我的母语,所以如果有任何困惑,请随时与我联系。