0

I have the following graph:

enter image description here

Created using:

g.addV("Person").property(T.id, "1").as("p1")
        .addV("Person").property(T.id, "2").as("p2")
        .addV("Person").property(T.id, "3").as("p3")
        .addV("Person").property(T.id, "4").as("p4")
        .addV("Person").property(T.id, "5").as("p5")
        .addV("Person").property(T.id, "6").as("p6")
        .addV("Person").property(T.id, "7").as("p7")
        .addE("TalksTo").from("p1").to("p2").property(T.id, "a")
        .addE("TalksTo").from("p2").to("p3").property(T.id, "b")
        .addE("TalksTo").from("p3").to("p2").property(T.id, "c")
        .addE("TalksTo").from("p2").to("p4").property(T.id, "d")
        .addE("TalksTo").from("p4").to("p5").property(T.id, "e")
        .addE("TalksTo").from("p5").to("p7").property(T.id, "f")
        .addE("TalksTo").from("p6").to("p5").property(T.id, "g")
        .addE("TalksTo").from("p3").to("p6").property(T.id, "h")

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?

1 Answer 1

0

You want to use simplePath() filter.

......1>   repeat(inE('TalksTo').outV().simplePath()).
......2>     until(
......3>       or(
......4>         inE().hasLabel("TalksTo").count().is(0),
......5>         loops().is(10))).
......6>   path().by(T.id)
==>[7,f,5,e,4,d,2,a,1]
==>[7,f,5,g,6,h,3,b,2,a,1]

To get the exact path as posted in your question , you can directly hop to the vertex instead of first getting an edge and then the vertex :

......1>   repeat(in('TalksTo').simplePath()).
......2>     until(
......3>       or(
......4>         inE().hasLabel("TalksTo").count().is(0),
......5>         loops().is(10))).
......6>   path().by(T.id)
==>[7,5,4,2,1]
==>[7,5,6,3,2,1]
2
  • Is there any way to get extra information from this query - such as which edges were skipped to break cycles, without running a new query? Perhaps by the way of slightly modifying the existing query?
    – spc16670
    Commented Jun 11 at 20:07
  • simplePath() is vertex-centric, not edge-centric. So it's looking to see which nodes were already traversed per traverser and ensuring that those nodes are not crossed again. If you wanted to see all partial paths that were taken as part of the query execution, you could add an emit() before path() and that would return all partial paths and the completed paths. Commented Jun 12 at 13:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.