考虑此图:
示例图
如何从vertexID获取所有连接的顶点?
例如,从VertexId 5
,它应该返回5-3-7-8-10
CCD_ 2仅返回第一个相邻的。
我正在尝试使用pregel
,但我不知道如何从特定的顶点开始。我不想计算所有的节点。
谢谢!
我刚刚注意到图是有向的。那么您可以在这里使用最短路径示例的代码。如果一个特定节点的距离不是无穷大,那么你就可以到达这个节点。
或者有一个更好的想法,你可以修改最短路径算法来满足你的需求。
import org.apache.spark.graphx.{Graph, VertexId}
import org.apache.spark.graphx.util.GraphGenerators
// A graph with edge attributes containing distances
val graph: Graph[Long, Double] =
GraphGenerators.logNormalGraph(sc, numVertices = 100).mapEdges(e => e.attr.toDouble)
val sourceId: VertexId = 42 // The ultimate source
// Initialize the graph such that all vertices except the root have canReach = false.
val initialGraph: Graph[Boolean, Double] = graph.mapVertices((id, _) => id == sourceId)
val sssp = initialGraph.pregel(false)(
(id, canReach, newCanReach) => canReach || newCanReach, // Vertex Program
triplet => { // Send Message
if (triplet.srcAttr && !triplet.dstAttr) {
Iterator((triplet.dstId, true))
} else {
Iterator.empty
}
},
(a, b) => a || b // Merge Message
)
println(sssp.vertices.collect.mkString("n"))