【问题标题】:GraphX - How to get all connected vertices from vertexId (not just the firsts adjacents)?GraphX - 如何从 vertexId 获取所有连接的顶点(不仅仅是第一个相邻的顶点)?
【发布时间】:2018-12-04 20:01:49
【问题描述】:

考虑这个图表:

Exemple graph

如何从一个 vertexID 获取所有连接的顶点?

例如,来自VertexId 5,它应该返回5-3-7-8-10

CollectNeighbors 只返回第一个相邻的。

我正在尝试使用pregel,但我不知道如何从特定顶点开始。我不想计算所有节点。

谢谢!

【问题讨论】:

    标签: scala apache-spark spark-graphx


    【解决方案1】:

    我刚刚注意到图表是有向的。那么您可以使用最短路径示例的代码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"))
    

    【讨论】:

    • 连接的组件似乎连接到最低的顶点 id。结果将是 (8,2), (12,2), (5,2), (10,2), (2,2), (3,2), (7,2) 因为最低的顶点是2.如何指定最小值为5而不包括(12,2)?
    • 请检查我的最新更新,我添加了一个更好的主意。
    • 经过检查,奇怪的是,它只适用于我的示例,但它似乎不适用于生成的图形,因为无论 VertexId 是什么,结果总是 100 true。逻辑上,顶点数应该小于100?
    • 确保图表是定向的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    相关资源
    最近更新 更多