Search code examples
gremlinconnected-components

Gremlin connected components prints one by one


I'm newbie to the gremlin QL, My requirement to generate the connected components on huge graph. I tried the below query but it's printing as a group of values but I need to print one by one.

Connected components Query:

g.V().emit(cyclicPath().or().not(both())).repeat(both()).until(cyclicPath()).path().aggregate("p").unfold().dedup().map(__.as("v").select("p").unfold().filter(unfold().where(eq("v"))).unfold().dedup().order().by(id).fold()).dedup()
[v[89826185]]
[v[89826188], v[89826189], v[89826190], v[89826191], v[89826192], v[89826193], v[89826194]]
[v[89826195], v[89826196], v[89826198]]

I need to print the values like below way. min-id of group(list) to each element of the group(list).

Ex:

89826188 89826189
89826188 89826190
89826188 89826191
89826188 89826192
89826188 89826193
89826188 89826194
89826188 89826188 (self)

Solution

  • You could do that in your application's code. Doing it at the query level will only blow up the result size, but here you go:

    g.V().
      emit(cyclicPath().or().not(both())).
        repeat(both()).
        until(cyclicPath()).
      path().aggregate("p").
      unfold().dedup().
      map(__.as("v").select("p").unfold().
          filter(unfold().where(eq("v"))).
          unfold().dedup().
          order().
            by(id).
          fold()).
      dedup().as("list").
      unfold().
      map(union(select("list").
                  by(limit(local, 1)),
                identity()).
          id().fold())
    

    It's basically the same query, I only added the final map() step to reformat the result.