Example 4 – triangle counting

The triangle count algorithm provides a vertex-based count of the number of triangles associated with that vertex. For instance, vertex Mike (1) is connected to Kate (5), who is connected to Sarah (2), Sarah is connected to Mike (1), and so a triangle is formed. This can be useful for route finding where triangle free minimum spanning tree graphs need to be generated for route planning.

The code to execute a triangle count and print it is simple as shown next. The graph triangleCount method is executed for the graph vertices. The result is saved in the value tCount and printed:

val tCount = graph.triangleCount().vertices
println( tCount.collect().mkString(" ") )

The results of the application job show that vertices Flo (4) and Jim (6) have no triangles, while Mike (1) and Sarah (2) have the most as expected, as they have the most relationships:

(4,0)
(6,0)
(2,4)
(1,4)
(3,2)
(5,2)
..................Content has been hidden....................

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