TriangleCounting

A triangle is created if two neighbors of a vertex are connected by an edge. In other words, a user will create a triangle with the two friends who are friends with each other.

Graph has a function triangleCount(), which computes the triangles in the graph.

The following is the code used to count the triangles in the graph by first invoking the triangleCount function and then by joining the triangles with the vertices (users) to generate the output of each user and the triangle the user belongs to:

scala> val triangleCounts = graph.triangleCount.vertices
triangleCounts: org.apache.spark.graphx.VertexRDD[Int] = VertexRDDImpl[3365] at RDD at VertexRDD.scala:57

scala> triangleCounts.take(10)
res171: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((4,0), (6,1), (8,1), (10,1), (2,1), (1,1), (3,1), (7,0), (9,0), (5,0))

scala> val triangleCountsPerUser = users.join(triangleCounts).map { case(id, (User(x,y), k)) => ((x,y), k) }
triangleCountsPerUser: org.apache.spark.rdd.RDD[((String, String), Int)] = MapPartitionsRDD[3371] at map at <console>:153

scala> triangleCountsPerUser.collect.mkString(" ")
res170: String =
((Liz,Doctor),0)
((Beth,Accountant),1) //1 count means this User is part of 1 triangle
((Mary,Cashier),1) //1 count means this User is part of 1 triangle
((Ken,Librarian),1) //1 count means this User is part of 1 triangle
((Mark,Doctor),1) //1 count means this User is part of 1 triangle
((John,Accountant),1) //1 count means this User is part of 1 triangle
((Sam,Lawyer),1) //1 count means this User is part of 1 triangle
((Larry,Engineer),0)
((Dan,Doctor),0)
((Eric,Accountant),0)

The diagram of the two triangles we just computed in the preceding code shows the two triangles, (John, Mark, Sam) and (Ken, Mary, Beth):

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

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