我如何在尊重边缘方向的同时找到所有可到达的顶点



有一个官方解决方案(收据(来查找所有连接的组件。但我需要相同的,但要考虑到边缘的方向。

所以我需要从某个节点获取所有节点,反之亦然 - 找到我可以从中选择顶点的所有节点。

我的代码示例是用 Java 编写的。

// My test graph
    TinkerGraph graph = TinkerGraph.open();
    GraphTraversalSource g = graph.traversal();
        g
            .addV().property(T.id, 1).as("1")
            .addV().property(T.id, 2).as("2")
            .addV().property(T.id, 3).as("3")
            .addV().property(T.id, 4).as("4")
            .addV().property(T.id, 5).as("5")
            .addV().property(T.id, 6).as("6")
            .addE("link").from("1").to("2")
            .addE("link").from("1").to("3")
            .addE("link").from("1").to("4")
            .addE("link").from("2").to("3")
            .addE("link").from("3").to("4")
            .addE("link").from("4").to("5")
            .addE("link").from("4").to("6")
            .addE("link").from("5").to("2")
            .iterate();

例如

for vertex 1 I expect the result [1, 2, 3, 4, 5, 6]
for vertices 2, 3, 4, 5 I expect [2, 3, 4, 5, 6]
for vertex 6 I expect [6]

反之亦然的任务:

for vertex 6 result [1, 2, 3, 4, 5]
for vertex 1 result [1] 

可能吗?这似乎是一项常见的任务,但我找不到有关它的问题。人们只想找到连接的组件。提前谢谢你!

对于单个起始顶点,它就像这样简单:

g.V(startId).
  emit().
    repeat(out("link").dedup()).
  dedup()

现在,对于一次所有顶点(或一组顶点(,您将执行以下操作:

g.V().project("vertex","result").
        by().
        by(emit().
             repeat(out("link").dedup()).
           dedup().fold())

对示例图形的查询

gremlin> (1..6).collect { i ->
           result = g.V(i).emit().repeat(out("link").dedup()).dedup().id().toList()
           "for vertex ${i} result ${result}"
         }
==>for vertex 1 result [1, 2, 3, 4, 5, 6]
==>for vertex 2 result [2, 3, 4, 5, 6]
==>for vertex 3 result [3, 4, 5, 6, 2]
==>for vertex 4 result [4, 5, 6, 2, 3]
==>for vertex 5 result [5, 2, 3, 4, 6]
==>for vertex 6 result [6]
gremlin> g.V().project("vertex","result").
                 by(id).
                 by(emit().
                      repeat(out("link").dedup()).
                    dedup().id().fold())
==>[vertex:1,result:[1,2,3,4,5,6]]
==>[vertex:2,result:[2,3,4,5,6]]
==>[vertex:3,result:[3,4,5,6,2]]
==>[vertex:4,result:[4,5,6,2,3]]
==>[vertex:5,result:[5,2,3,4,6]]
==>[vertex:6,result:[6]]

最新更新