Github user ssaumitra commented on a diff in the pull request: https://github.com/apache/flink/pull/1250#discussion_r41696647 --- Diff: flink-libraries/flink-gelly/src/main/java/org/apache/flink/graph/library/TriangleCounter.java --- @@ -0,0 +1,339 @@ +/* + * 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. + */ + +package org.apache.flink.graph.library; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.functions.ReduceFunction; +import org.apache.flink.api.common.functions.FlatMapFunction; +import org.apache.flink.api.common.functions.JoinFunction; +import org.apache.flink.api.common.functions.GroupReduceFunction; +import org.apache.flink.api.common.operators.Order; +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.functions.FunctionAnnotation; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.api.java.tuple.Tuple4; +import org.apache.flink.graph.Edge; +import org.apache.flink.graph.Graph; +import org.apache.flink.graph.GraphAlgorithm; +import org.apache.flink.types.NullValue; +import org.apache.flink.util.Collector; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * This function returns number of triangles present in the input graph. + * A triangle consists of three edges that connect three vertices with each other. + * <p> + * <p> + * The basic algorithm works as follows: + * It groups all edges that share a common vertex and builds triads, i.e., triples of vertices + * that are connected by two edges. Finally, all triads are filtered for which no third edge exists + * that closes the triangle. + * <p> + * <p> + * For a group of <i>n</i> edges that share a common vertex, the number of built triads is quadratic <i>((n*(n-1))/2)</i>. + * Therefore, an optimization of the algorithm is to group edges on the vertex with the smaller output degree to + * reduce the number of triads. + * This implementation extends the basic algorithm by computing output degrees of edge vertices and + * grouping on edges on the vertex with the smaller degree. + */ + +public class TriangleCounter<K extends Comparable<K>, VV, EV> implements GraphAlgorithm<K, VV, EV, DataSet<Integer>> { + @Override + public DataSet<Integer> run(Graph<K, VV, EV> input) throws Exception { + + DataSet<Edge<K, EV>> edges = input.getEdges(); + + // annotate edges with degrees + DataSet<EdgeWithDegrees<K>> edgesWithDegrees = edges.flatMap(new EdgeDuplicator<K, EV>()) + .groupBy(0).sortGroup(1, Order.ASCENDING).reduceGroup(new DegreeCounter<K, EV>()) + .groupBy(EdgeWithDegrees.V1, EdgeWithDegrees.V2).reduce(new DegreeJoiner<K>()); + + // project edges by degrees + DataSet<Edge<K, NullValue>> edgesByDegree = edgesWithDegrees.map(new EdgeByDegreeProjector<K>()); + // project edges by vertex id + DataSet<Edge<K, NullValue>> edgesById = edgesByDegree.map(new EdgeByIdProjector<K>()); + + DataSet<Integer> triangles = edgesByDegree + // build triads + .groupBy(EdgeWithDegrees.V1).sortGroup(EdgeWithDegrees.V2, Order.ASCENDING) + .reduceGroup(new TriadBuilder()) + // filter triads + .join(edgesById).where(Triad.V2, Triad.V3).equalTo(0, 1).with(new TriadFilter<K>()); --- End diff -- Just FYI, vertices of Triad and edgesWithDegrees are referred with V1 and V2. And those of edges are referred with indices 0 and 1. IMHO, org.apache.flink.graph.Edge can also have static variables V1, V2 so that group operators are more readable.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---