Search code examples
datastaxgremlindatastax-enterprise-graph

How to remove cyclic vertex list from list of outer vertexes


I am new in dse graph, I want to create gremlin query which gives me list of all vertex which is linked from specified vertex but from this list I want to remove those list which are linked cyclic.

e.g.

A --> B
A --> C
A --> D
B --> A

If I have above relation then I want below vertex list as result

[C,D]

B and A should not be in above list as it has cyclic relation

I have below two separate query to find all linked vertexes and to find cyclic vertex

g.V().has('id','id').as('mainV').outE('Prerequisite').inV();

g.V().has('id','id').as('mainV').out().out().cyclicPath().path().unfold().dedup();

Could you please help me to find exact query to achieve my requirement.


Solution

  • So you basically want to filter out vertices, that have an in and an out edge to a particular vertex.

    This is your sample graph:

    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> g.addV().property(id, "A").as("a").
    ......1>   addV().property(id, "B").as("b").
    ......2>   addV().property(id, "C").as("c").
    ......3>   addV().property(id, "D").as("d").
    ......4>   addE("link").from("a").to("b").
    ......5>   addE("link").from("a").to("c").
    ......6>   addE("link").from("a").to("d").
    ......7>   addE("link").from("b").to("a").iterate()
    

    And this is the traversal you're looking for:

    gremlin> g.V().as("a").not(out().out().where(eq("a"))).not(__.in().in().where(eq("a")))
    ==>v[C]
    ==>v[D]