Neptune Gremlin how to break cycles when collecting paths

  Kiến thức lập trình

I have the following graph:

Created using:

    Vertex p1 = g.addV("Person")
            .property(T.id, "1")
            .next();
    Vertex p2 = g.addV("Person")
            .property(T.id, "2")
            .next();
    Vertex p3 = g.addV("Person")
            .property(T.id, "3")
            .next();
    Vertex p4 = g.addV("Person")
            .property(T.id, "4")
            .next();
    Vertex p5 = g.addV("Person")
            .property(T.id, "5")
            .next();
    Vertex p6 = g.addV("Person")
            .property(T.id, "6")
            .next();
    Vertex p7 = g.addV("Person")
            .property(T.id, "7")
            .next();


    g.addE("TalksTo").from(p1).to(p2)
            .property(T.id, "a")
            .next();
    g.addE("TalksTo").from(p2).to(p3)
            .property(T.id, "b")
            .next();
    g.addE("TalksTo").from(p3).to(p2)
            .property(T.id, "c")
            .next();
    g.addE("TalksTo").from(p2).to(p4)
            .property(T.id, "d")
            .next();
    g.addE("TalksTo").from(p4).to(p5)
            .property(T.id, "e")
            .next();
    g.addE("TalksTo").from(p5).to(p7)
            .property(T.id, "f")
            .next();
    g.addE("TalksTo").from(p6).to(p5)
            .property(T.id, "g")
            .next();
    g.addE("TalksTo").from(p3).to(p6)
            .property(T.id, "h")
            .next();

I would like to traverse that graph to get the following paths:

7 -> 5 -> 4 -> 2 -> 1
7 -> 5 -> 6 -> 3 -> 2 -> 1

Essentially breaking up the cycle between 2 and 1.

I have tried with the following query:

g.V("7")
.repeat(
    as("source").inE("TalksTo").outV().where(neq("source"))
 )
.until(or(
    inE().hasLabel("TalksTo").count().is(0),
    loops().is(10),
    cyclicPath()
 ))
.path()
.toList();

But it does not yield the results I am looking for – do I need to consider subgraphs to solve this?

LEAVE A COMMENT