2

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?

BSMP
  • 4,596
  • 8
  • 33
  • 44
Bin Teng
  • 23
  • 4

1 Answers1

1

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)  
cheseaux
  • 5,187
  • 31
  • 50
  • In addition, I have another question ,its link address is [Question](https://stackoverflow.com/questions/45251750/how-to-campute-the-jaccard-coefficient-between-the-neighbors-collection-of-two-v), can you solve it? Thanks. – Bin Teng Jul 22 '17 at 08:07