本人最近研究关于spark的图模型,spark最近才接触,scala语言也不是很熟,论坛里有没有高手帮忙解答一下关于最短路径的,官方文档我也看了,但貌似没有关于最短路径的,那个shortestpaths源码,也没怎么看明白,而且一下实现办法也只是展示了源点到目标顶点的距离,我想用spark做出最短路径的,最好是有代码案例的,结果带中间节点的,不知哪位大神做过这方面的研究,帮帮忙

解决方案 »

  1.   

    不知道楼主解决了么,我这给你个例子:
    import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.graphx.{EdgeDirection, VertexId, Graph}
    import org.apache.spark.graphx.util.GraphGenerators
    object Pregel_SSSP {
      def main(args: Array[String]) {
        val conf = new SparkConf().setAppName("Pregel_SSSP")
        val sc = new SparkContext(conf)
        // A graph with edge attributes containing distances
        val graph: Graph[Long, Double] =
          GraphGenerators.logNormalGraph(sc, numVertices = 5).mapEdges(e => e.attr.toDouble)
        graph.edges.foreach(println)
        val sourceId: VertexId = 0 // The ultimate source    // Initialize the graph such that all vertices except the root have distance infinity.
        val initialGraph : Graph[(Double, List[VertexId]), Double] = graph.mapVertices((id, _) => if (id == sourceId) (0.0, List[VertexId](sourceId)) else (Double.PositiveInfinity, List[VertexId]()))    val sssp = initialGraph.pregel((Double.PositiveInfinity, List[VertexId]()), Int.MaxValue, EdgeDirection.Out)(      // Vertex Program
          (id, dist, newDist) => if (dist._1 < newDist._1) dist else newDist,      // Send Message
          triplet => {
            if (triplet.srcAttr._1 < triplet.dstAttr._1 - triplet.attr ) {
              Iterator((triplet.dstId, (triplet.srcAttr._1 + triplet.attr , triplet.srcAttr._2 :+ triplet.dstId)))
            } else {
              Iterator.empty
            }
          },
          //Merge Message
          (a, b) => if (a._1 < b._1) a else b)
        println(sssp.vertices.collect.mkString("\n"))
      }
    }
      

  2.   

    基于spark 1.3的
      

  3.   

    你好,这是求单源最短路径,怎么才能并行求多源路径。我尝试把他写成方法调用,然后把需要求得点对生产rdd,再map调用单源方法,但rdd不能嵌套rdd。另一方面,我感觉他肯定是可以并行的,因为求每一个结点单源路径和其他结点不相关,只是不知道怎么并行。急求!!!
      

  4.   


    注意下单源的实现思路,就是迭代过程中记录下当前的最短距离,现在变成了多源,就需要分别记录多个路径的相对最短距离,attr里就需要一个映射关系。并不是简单的并行对每一个点对执行单源的算法和数据结构就可以的。