Bin Teng
Bin Teng

Reputation: 23

How to compute the sum of degree of two vertexs in each edge in graphx

I have a graph like this:

val vertexArray = Array(
      (1L, ("Alice", 28)),
      (2L, ("Bob", 27)),
      (3L, ("Charlie", 65)),
      (4L, ("David", 42)),
      (5L, ("Ed", 55)))                               
val edges = sc.parallelize(Array(
                 Edge(1L, 2L, ""), 
                 Edge(1L, 3L, ""), 
                 Edge(2L, 4L, ""),
                 Edge(3L, 5L, ""),
                 Edge(2L, 3L, "")))
val graph = Graph(vertexArray, edges)

I want to get the sum of degrees of two vertices in each edge. For example, the node 1L has 2 neighbors and node 2L has 3 neighbors, then the result which i want to get is "1L, 2L, 5".The whole result is:

"1L, 2L, 5"
"1L, 3L, 5"
"2L, 4L, 4"
"3L, 5L, 4",
"2L, 3L, 6"

How can I implement it in GraphX?

Upvotes: 2

Views: 428

Answers (1)

cheseaux
cheseaux

Reputation: 5315

First you can replace each vertex property with its total degree

val graphDegrees = graph.outerJoinVertices(graph.degrees)((_,_,deg) => deg.get)

And finally, map each triplet and sum up the two vertices' degree

val graphSum = graphDegrees.mapTriplets(t => t.srcAttr + t.dstAttr)

We can check the result by printing the edges

graphSum.edges.collect.foreach(println)

Which gives

Edge(1,2,5)
Edge(1,3,5)
Edge(2,4,4)
Edge(2,3,6)
Edge(3,5,4)  

Upvotes: 1

Related Questions