Github user dkuppitz commented on a diff in the pull request:

    https://github.com/apache/tinkerpop/pull/531#discussion_r95638679
  
    --- Diff: docs/src/recipes/connected-components.asciidoc ---
    @@ -0,0 +1,82 @@
    +////
    +Licensed to the Apache Software Foundation (ASF) under one or more
    +contributor license agreements.  See the NOTICE file distributed with
    +this work for additional information regarding copyright ownership.
    +The ASF licenses this file to You under the Apache License, Version 2.0
    +(the "License"); you may not use this file except in compliance with
    +the License.  You may obtain a copy of the License at
    +
    +  http://www.apache.org/licenses/LICENSE-2.0
    +
    +Unless required by applicable law or agreed to in writing, software
    +distributed under the License is distributed on an "AS IS" BASIS,
    +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +See the License for the specific language governing permissions and
    +limitations under the License.
    +////
    +[[connected-components]]
    +Connected Components
    +--------------------
    +
    +Gremlin can be used to find 
link:https://en.wikipedia.org/wiki/Connected_component_(graph_theory)[connected 
components]
    +in a graph. Consider the following graph which has three connected 
components:
    +
    +image:connected-components.png[width=600]
    +
    +[gremlin-groovy]
    +----
    +g.addV(id, "A").as("a").
    +  addV(id, "B").as("b").
    +  addV(id, "C").as("c").
    +  addV(id, "D").as("d").
    +  addV(id, "E").as("e").
    +  addV(id, "F").
    +  addE("link").from("a").to("b").
    +  addE("link").from("b").to("c").
    +  addE("link").from("d").to("e").iterate()
    +----
    +
    +One way to detect the various subgraphs would be to do something like this:
    +
    +[gremlin-groovy,existing]
    +----
    
+g.V().emit(cyclicPath().or().not(both())).repeat(both()).until(cyclicPath()).  
<1>
    +  aggregate("p").by(path()).cap("p").                                      
    <2>
    +  unfold().limit(local, 1).dedup().                                        
    <3>
    +  map(__.as("v").select("p").unfold().                                     
    <4>
    +         filter(unfold().where(eq("v"))).
    +         unfold().dedup().order().by(id).fold()
    +  ).dedup()                                                                
    <5>
    +----
    +
    +<1> Iterate all vertices and repeatedly traverse over both incoming and 
outgoing edges (TinkerPop doesn't support
    +unidirectional graphs directly so it must be simulated by ignoring the 
direction with `both`). Note the use of `emit`
    +prior to `repeat` as this allows for return of a single length path.
    +<2> Aggregate the emitted vertices to "p" and get their path information. 
Calling `cap` at the end will push the
    +aggregate path list into the traversal. It is within these paths that the 
list of connected components will be
    +identified. Obviously the paths list are duplicative in the sense that 
they contains different paths travelled over
    +the same vertices.
    +<3> Unroll the elements in the path list with `unfold` and grab the first 
vertex in each path and `dedup`.
    +<4> Use the first vertex in each path to filter against the paths stored 
in "p". When a path is found that has the
    +vertex in it, dedup the vertices in the path, order it by the identifier. 
Each path output from this `map` step
    +represents a connected component.
    +<5> The connected component list is duplicative given the nature of the 
paths in "p", but now that the vertices within
    +the paths are ordered, a final `dedup` will make the list of connective 
components unique.
    +
    +NOTE: This is a nice example of where running smaller pieces of a large 
Gremlin statement make it easier to see what
    +is happening at each step. Consider running this example one line at a 
time (or perhaps even in a step at a time) to
    +see the output at each point.
    +
    +While the above approach returns results nicely, the traversal doesn't 
appear to work with OLAP. A less efficient
    +approach, but one more suited for OLAP execution looks quite similar but 
does not use `dedup` as heavily (thus
    +`GraphComputer` is forced to analyze far more paths):
    +
    +[gremlin-groovy,existing]
    +----
    
+g.withComputer().V().emit(cyclicPath().or().not(both())).repeat(both()).until(cyclicPath()).
    --- End diff --
    
    I'm pretty sure that's not working in `tp32` (OLAP).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to