[34/42] tinkerpop git commit: Add "centrality" recipes. CTR
Add "centrality" recipes. CTR Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/e790e56a Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/e790e56a Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/e790e56a Branch: refs/heads/TINKERPOP-1278 Commit: e790e56aaa2118b07e36f2579502786bd9d79cc0 Parents: b36b42f Author: Stephen MalletteAuthored: Fri Jun 10 16:50:35 2016 -0400 Committer: Stephen Mallette Committed: Fri Jun 10 16:50:35 2016 -0400 -- docs/src/recipes/centrality.asciidoc | 138 + docs/src/recipes/index.asciidoc | 2 + docs/static/images/betweeness-example.png | Bin 0 -> 8465 bytes 3 files changed, 140 insertions(+) -- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e790e56a/docs/src/recipes/centrality.asciidoc -- diff --git a/docs/src/recipes/centrality.asciidoc b/docs/src/recipes/centrality.asciidoc new file mode 100644 index 000..cb3ce12 --- /dev/null +++ b/docs/src/recipes/centrality.asciidoc @@ -0,0 +1,138 @@ + +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. + +[[centrality]] +Centrality +-- + +There are many measures of link:https://en.wikipedia.org/wiki/Centrality[centrality] which are meant to help identify +the most important vertices in a graph. As these measures are common in graph theory, this section attempts to +demonstrate how some of these different indicators can be calculated using Gremlin. + +[[degree-centrality]] +Degree Centrality +~ + +link:https://en.wikipedia.org/wiki/Centrality#Degree_centrality[Degree centrality] is a measure of the number of +edges associated to each vertex. + +[gremlin-groovy,modern] + +g.V().bothE().group().by(otherV()).by(count()) <1> +g.V().inE().group().by(inV()).by(count()) <2> +g.V().outE().group().by(outV()).by(count()) <3> +g.V().group().by().by(outE().count()) <4> + + +<1> Calculation of degree centrality which counts all incident edges on each vertex to include those that are both +incoming and outgoing. +<2> Calculation of in-degree centrality which only counts incoming edges to a vertex. +<3> Calculation of out-degree centrality which only counts outgoing edges from a vertex. +<4> Same calculation as the previous traversal, but includes all vertices, even those with a zero + +[[betweeness-centrality]] +Betweeness Centrality +~ + +link:https://en.wikipedia.org/wiki/Betweenness_centrality[Betweeness centrality] is a measure of the number of times +a vertex is found between the < > of each vertex pair in a graph. Consider the following +graph for demonstration purposes: + +image:betweeness-example.png[width=600] + +[gremlin-groovy ] + +a = graph.addVertex('name','a') +b = graph.addVertex('name','b') +c = graph.addVertex('name','c') +d = graph.addVertex('name','d') +e = graph.addVertex('name','e') +a.addEdge('next',b) +b.addEdge('next',c) +c.addEdge('next',d) +d.addEdge('next',e) +g.withSack(0).V().repeat(both().simplePath()).emit().path().<1> + group().by(project("a","b").by(limit(local, 1)). <2> + by(tail(local, 1))). + by(order().by(count(local))). <3> + select(values).as("shortestPaths"). <4> + V().as("v").map(select("shortestPaths").unfold(). <5> + filter(unfold().where(eq("v"))).count()). + sack(sum).barrier(normSack).sack().as("betweeness"). <6> + select("v","betweeness") + + +<1> Defines a Gremlin link:http://tinkerpop.apache.org/docs/x.y.z/reference/#sack-step[sack] with a value of zero, +which represents the initial betweeness score for each vertex, and traverses on both incoming and outgoing edges +avoiding < >. +<2> Group each path by the first and last vertex. +<3> Reduce
[2/6] tinkerpop git commit: Add "centrality" recipes. CTR
Add "centrality" recipes. CTR Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/e790e56a Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/e790e56a Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/e790e56a Branch: refs/heads/TINKERPOP-1331 Commit: e790e56aaa2118b07e36f2579502786bd9d79cc0 Parents: b36b42f Author: Stephen MalletteAuthored: Fri Jun 10 16:50:35 2016 -0400 Committer: Stephen Mallette Committed: Fri Jun 10 16:50:35 2016 -0400 -- docs/src/recipes/centrality.asciidoc | 138 + docs/src/recipes/index.asciidoc | 2 + docs/static/images/betweeness-example.png | Bin 0 -> 8465 bytes 3 files changed, 140 insertions(+) -- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e790e56a/docs/src/recipes/centrality.asciidoc -- diff --git a/docs/src/recipes/centrality.asciidoc b/docs/src/recipes/centrality.asciidoc new file mode 100644 index 000..cb3ce12 --- /dev/null +++ b/docs/src/recipes/centrality.asciidoc @@ -0,0 +1,138 @@ + +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. + +[[centrality]] +Centrality +-- + +There are many measures of link:https://en.wikipedia.org/wiki/Centrality[centrality] which are meant to help identify +the most important vertices in a graph. As these measures are common in graph theory, this section attempts to +demonstrate how some of these different indicators can be calculated using Gremlin. + +[[degree-centrality]] +Degree Centrality +~ + +link:https://en.wikipedia.org/wiki/Centrality#Degree_centrality[Degree centrality] is a measure of the number of +edges associated to each vertex. + +[gremlin-groovy,modern] + +g.V().bothE().group().by(otherV()).by(count()) <1> +g.V().inE().group().by(inV()).by(count()) <2> +g.V().outE().group().by(outV()).by(count()) <3> +g.V().group().by().by(outE().count()) <4> + + +<1> Calculation of degree centrality which counts all incident edges on each vertex to include those that are both +incoming and outgoing. +<2> Calculation of in-degree centrality which only counts incoming edges to a vertex. +<3> Calculation of out-degree centrality which only counts outgoing edges from a vertex. +<4> Same calculation as the previous traversal, but includes all vertices, even those with a zero + +[[betweeness-centrality]] +Betweeness Centrality +~ + +link:https://en.wikipedia.org/wiki/Betweenness_centrality[Betweeness centrality] is a measure of the number of times +a vertex is found between the < > of each vertex pair in a graph. Consider the following +graph for demonstration purposes: + +image:betweeness-example.png[width=600] + +[gremlin-groovy ] + +a = graph.addVertex('name','a') +b = graph.addVertex('name','b') +c = graph.addVertex('name','c') +d = graph.addVertex('name','d') +e = graph.addVertex('name','e') +a.addEdge('next',b) +b.addEdge('next',c) +c.addEdge('next',d) +d.addEdge('next',e) +g.withSack(0).V().repeat(both().simplePath()).emit().path().<1> + group().by(project("a","b").by(limit(local, 1)). <2> + by(tail(local, 1))). + by(order().by(count(local))). <3> + select(values).as("shortestPaths"). <4> + V().as("v").map(select("shortestPaths").unfold(). <5> + filter(unfold().where(eq("v"))).count()). + sack(sum).barrier(normSack).sack().as("betweeness"). <6> + select("v","betweeness") + + +<1> Defines a Gremlin link:http://tinkerpop.apache.org/docs/x.y.z/reference/#sack-step[sack] with a value of zero, +which represents the initial betweeness score for each vertex, and traverses on both incoming and outgoing edges +avoiding < >. +<2> Group each path by the first and last vertex. +<3> Reduce