1. 程式人生 > >Spark元件之GraphX學習16--最短路徑ShortestPaths

Spark元件之GraphX學習16--最短路徑ShortestPaths

1解釋

求圖中的最短路徑,更多的請見參考【3】,這篇寫的很詳細

2.程式碼:

/**
 * @author xubo
 * ref http://spark.apache.org/docs/1.5.2/graphx-programming-guide.html
 * time 20160503
 */

package org.apache.spark.graphx.learning

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.graphx.Graph
import org.apache.spark.graphx.Graph.graphToGraphOps
import org.apache.spark.graphx.lib.ShortestPaths

object ShortPaths {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("ShortPaths").setMaster("local[4]")
    val sc = new SparkContext(conf)

    // 測試的真實結果,後面用於對比
    val shortestPaths = Set(
      (1, Map(1 -> 0, 4 -> 2)), (2, Map(1 -> 1, 4 -> 2)), (3, Map(1 -> 2, 4 -> 1)),
      (4, Map(1 -> 2, 4 -> 0)), (5, Map(1 -> 1, 4 -> 1)), (6, Map(1 -> 3, 4 -> 1)))

    // 構造無向圖的邊序列
    val edgeSeq = Seq((1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (4, 5), (4, 6)).flatMap {
      case e => Seq(e, e.swap)
    }

    // 構造無向圖
    val edges = sc.parallelize(edgeSeq).map { case (v1, v2) => (v1.toLong, v2.toLong) }
    val graph = Graph.fromEdgeTuples(edges, 1)

    // 要求最短路徑的點集合
    val landmarks = Seq(1, 4).map(_.toLong)

    // 計算最短路徑
    val results = ShortestPaths.run(graph, landmarks).vertices.collect.map {
      case (v, spMap) => (v, spMap.mapValues(i => i))
    }

    val shortestPath1 = ShortestPaths.run(graph, landmarks)
    // 與真實結果對比
    println("\ngraph edges");
    println("edges:");
    graph.edges.collect.foreach(println)
    //    graph.edges.collect.foreach(println)
    println("vertices:");
    graph.vertices.collect.foreach(println)
    //    println("triplets:");
    //    graph.triplets.collect.foreach(println)
    println();

    println("\n shortestPath1");
    println("edges:");
    shortestPath1.edges.collect.foreach(println)
    println("vertices:");
    shortestPath1.vertices.collect.foreach(println)
    //    println("vertices:")

    assert(results.toSet == shortestPaths)
    println("results.toSet:" + results.toSet);
    println("end");

    sc.stop()
  }
}

圖分析:其實是無向圖,但是儲存的時候GraphX存的是有向圖


3.結果:

分析:返回的是

(1,Map(1 -> 0, 4 -> 2))
(5,Map(1 -> 1, 4 -> 1))
(6,Map(4 -> 1, 1 -> 3))
節點的屬性存的是到某幾點的最短路徑,比如
(1,Map(1 -> 0, 4 -> 2))

表明的是1節點到1節點路徑為0,到4節點為2

同理

(6,Map(4 -> 1, 1 -> 3))
6號節點到4為1,到1為3,途中可以看得出來

全部結果:

graph edges
edges:
Edge(1,2,1)
Edge(1,5,1)
Edge(2,1,1)
Edge(2,3,1)
Edge(2,5,1)
Edge(3,2,1)
Edge(5,1,1)
Edge(3,4,1)
Edge(4,3,1)
Edge(5,2,1)
Edge(4,5,1)
Edge(4,6,1)
Edge(5,4,1)
Edge(6,4,1)
vertices:
(4,1)
(1,1)
(5,1)
(6,1)
(2,1)
(3,1)


 shortestPath1
edges:
Edge(1,2,1)
Edge(1,5,1)
Edge(2,1,1)
Edge(2,3,1)
Edge(2,5,1)
Edge(3,2,1)
Edge(5,1,1)
Edge(3,4,1)
Edge(4,3,1)
Edge(5,2,1)
Edge(4,5,1)
Edge(4,6,1)
Edge(5,4,1)
Edge(6,4,1)
vertices:
(4,Map(4 -> 0, 1 -> 2))
(1,Map(1 -> 0, 4 -> 2))
(5,Map(1 -> 1, 4 -> 1))
(6,Map(4 -> 1, 1 -> 3))
(2,Map(1 -> 1, 4 -> 2))
(3,Map(4 -> 1, 1 -> 2))
results.toSet:Set((1,Map(1 -> 0, 4 -> 2)), (5,Map(1 -> 1, 4 -> 1)), (2,Map(1 -> 1, 4 -> 2)), (6,Map(4 -> 1, 1 -> 3)), (4,Map(4 -> 0, 1 -> 2)), (3,Map(4 -> 1, 1 -> 2)))
end

如果改為全部節點,則為:

vertices:
(4,Map(5 -> 1, 1 -> 2, 6 -> 1, 2 -> 2, 3 -> 1, 4 -> 0))
(1,Map(5 -> 1, 1 -> 0, 6 -> 3, 2 -> 1, 3 -> 2, 4 -> 2))
(5,Map(5 -> 0, 1 -> 1, 6 -> 2, 2 -> 1, 3 -> 2, 4 -> 1))
(6,Map(5 -> 2, 1 -> 3, 6 -> 0, 2 -> 3, 3 -> 2, 4 -> 1))
(2,Map(5 -> 1, 1 -> 1, 6 -> 3, 2 -> 0, 3 -> 1, 4 -> 2))
(3,Map(5 -> 2, 1 -> 2, 6 -> 2, 2 -> 1, 3 -> 0, 4 -> 1))

results.toSet:Set((6,Map(5 -> 2, 1 -> 3, 6 -> 0, 2 -> 3, 3 -> 2, 4 -> 1)), (4,Map(5 -> 1, 1 -> 2, 6 -> 1, 2 -> 2, 3 -> 1, 4 -> 0)), (3,Map(5 -> 2, 1 -> 2, 6 -> 2, 2 -> 1, 3 -> 0, 4 -> 1)), (2,Map(5 -> 1, 1 -> 1, 6 -> 3, 2 -> 0, 3 -> 1, 4 -> 2)), (1,Map(5 -> 1, 1 -> 0, 6 -> 3, 2 -> 1, 3 -> 2, 4 -> 2)), (5,Map(5 -> 0, 1 -> 1, 6 -> 2, 2 -> 1, 3 -> 2, 4 -> 1)))



參考

【1】 http://spark.apache.org/docs/1.5.2/graphx-programming-guide.html

【3】http://blog.csdn.net/zcf1002797280/article/details/50007913