ulrich
ulrich

Reputation: 3587

Dropping unconnected components of a subgraph GraphX

I have the following graph:

// Vertices
val usersTest: RDD[(VertexId, (String))] = sc.parallelize(Array((1L, ("AAA")), (2L, ("BBB")), (3L, ("CCC"))))
// Edges
val relationshipsTest: RDD[Edge[Int]] = sc.parallelize(Array(Edge(1L, 3L, 1),Edge(1L, 3L, 1),Edge(1L, 2L, 3), Edge(2L, 1L, 1), Edge(2L, 1L, 2), Edge(2L, 3L, 1),   Edge(3L, 2L, 2)))
val defaultUserTest =  "Missing"
//Creating the Graph
val graphTest = Graph(usersTest, relationshipsTest, defaultUserTest)

Which produces the following output:

(graphTest.numEdges, graphTest.numVertices)
res: (Long, Long) = (7,3)

Now, when I try to use subgraph:

val validGraphTest = graphTest.subgraph(epred = e => e.attr > 2) 

I obtain:

( validGraphTest.numEdges, validGraphTest.numVertices)
res: (Long, Long) = (1,3)

What I would like is to drop the unconnected vertices (i.e. in the example, since I have only one edge left, the desired output would be res:(Long, Long) = (1,2))

I have tried

val validCCGraphTest = validGraphTest.connectedComponents()

but ( validCCGraphTest.numEdges, validCCGraphTest.numVertices)

still produces res: (Long, Long) = (1,3)

Upvotes: 0

Views: 618

Answers (1)

zero323
zero323

Reputation: 330073

An isolated vertex with zero degree is a connected component of size one. That's why your approach doesn't work. You can try something like this:

validGraphTest
  .outerJoinVertices(validGraphTest.degrees){
    case (_, vd, Some(x)) => (vd, x)
    case (_, vd, _) => (vd, 0)
  }
  .subgraph(vpred = {case (_, (_, x)) => x > 0})
  .mapVertices{case (_, (x, _)) => x}

or a little bit more concise (although it looks like it is less efficient):

Graph(validGraphTest.degrees, validGraphTest.edges).mask(graphTest)

Upvotes: 2

Related Questions