Triangle counting

The triangle count is another common algorithm in graph theory. Triangle in graph theory is a three node graph where each vertex is connected to each other. For example, if A and B are friends on Facebook and C is their mutual friend and A, B, C form a triangle, the graph with more triangles is tightly connected.

Triangle counting on Spark clusters requires the graph to be partitioned:

graph.partitionBy(PartitionStrategy.CanonicalRandomVertexCut$.MODULE$);   

Here we have used the CanonicalRandomVertexCut strategy to partition the graph. The TriangleCount operation on a graph can be executed as follows:

Graph<Object, string> triangleCountedGraph = graph.ops().triangleCount();   

This operation returns a graph with every vertex containing the count that is equal to the number of triangles it is associated to. Referring to the logical representation of the graph, provided in the Property Graph section, the graph contains one triangle, which is as follows:

Logical representation of Triangle of vertices

To verify, we can print vertices' properties as follows:

triangleCountedGraph.vertices().toJavaRDD().collect().forEach(System.out::println);   

The result should show that nodes 1, 2, and 3 have one associated triangle and no associated triangle to nodes 4 and 5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.154.252