GraphX - 如何从 vertexId 获取所有连接的顶点(而不仅仅是第一个相邻的顶点)?
GraphX - How to get all connected vertices from vertexId (not just the firsts adjacents)?
考虑这张图:
Exemple graph
如何从 vertexID 获取所有连接的顶点?
例如从VertexId 5
开始,应该return5-3-7-8-10
CollectNeighbors
只有第一个相邻的 return。
我正在尝试使用 pregel
,但我不知道如何从特定的顶点开始。我不想计算所有节点。
谢谢!
我刚刚注意到图形是有向的。那么你可以使用最短路径示例的代码here。如果特定节点的距离不是无穷大,那么您可以到达该节点。
或者你有更好的想法可以修改最短路径算法来满足你的需求
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"))
考虑这张图:
Exemple graph
如何从 vertexID 获取所有连接的顶点?
例如从VertexId 5
开始,应该return5-3-7-8-10
CollectNeighbors
只有第一个相邻的 return。
我正在尝试使用 pregel
,但我不知道如何从特定的顶点开始。我不想计算所有节点。
谢谢!
我刚刚注意到图形是有向的。那么你可以使用最短路径示例的代码here。如果特定节点的距离不是无穷大,那么您可以到达该节点。
或者你有更好的想法可以修改最短路径算法来满足你的需求
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"))