json
ファイルedge
とvertices
タイプのグラフを作成しています。GraphxでSparkを使用した場合の最短経路パフォーマンス
私は私はこのようなgraphx
で必要とされるvertices
とedge
タイプを作成するには、これらのjson
レコードをロードし、マップのDropboxフォルダhere
にファイルを入れている:
val vertices_raw = sqlContext.read.json("path/vertices.json.gz")
val vertices = vertices_raw.rdd.map(row=> ((row.getAs[String]("toid").stripPrefix("osgb").toLong),row.getAs[Long]("index")))
val verticesRDD: RDD[(VertexId, Long)] = vertices
val edges_raw = sqlContext.read.json("path/edges.json.gz")
val edgesRDD = edges_raw.rdd.map(row=>(Edge(row.getAs[String]("positiveNode").stripPrefix("osgb").toLong, row.getAs[String]("negativeNode").stripPrefix("osgb").toLong, row.getAs[Double]("length"))))
val my_graph: Graph[(Long),Double] = Graph.apply(verticesRDD, edgesRDD).partitionBy(PartitionStrategy.RandomVertexCut)
私は、このdijkstra
を使用実装私は、2つの頂点間の最短経路を計算することが分かった:
def dijkstra[VD](g: Graph[VD, Double], origin: VertexId) = {
var g2 = g.mapVertices(
(vid, vd) => (false, if (vid == origin) 0 else Double.MaxValue, List[VertexId]())
)
for (i <- 1L to g.vertices.count - 1) {
val currentVertexId: VertexId = g2.vertices.filter(!_._2._1)
.fold((0L, (false, Double.MaxValue, List[VertexId]())))(
(a, b) => if (a._2._2 < b._2._2) a else b)
._1
val newDistances: VertexRDD[(Double, List[VertexId])] =
g2.aggregateMessages[(Double, List[VertexId])](
ctx => if (ctx.srcId == currentVertexId) {
ctx.sendToDst((ctx.srcAttr._2 + ctx.attr, ctx.srcAttr._3 :+ ctx.srcId))
},
(a, b) => if (a._1 < b._1) a else b
)
g2 = g2.outerJoinVertices(newDistances)((vid, vd, newSum) => {
val newSumVal = newSum.getOrElse((Double.MaxValue, List[VertexId]()))
(
vd._1 || vid == currentVertexId,
math.min(vd._2, newSumVal._1),
if (vd._2 < newSumVal._1) vd._3 else newSumVal._2
)
})
}
g.outerJoinVertices(g2.vertices)((vid, vd, dist) =>
(vd, dist.getOrElse((false, Double.MaxValue, List[VertexId]()))
.productIterator.toList.tail
))
}
私は2つのランダムな頂点IDの取る:
val v1 = 4000000028222916L
val v2 = 4000000031019012L
とそれらの間のパスを計算する:
val results = dijkstra(my_graph, v1).vertices.map(_._2).collect
私はstackoverflowのエラーを取得せずに私のラップトップ上でローカルにこれを計算することができません。利用可能な4つのコアのうち3つを使用していることがわかります。私はこのグラフを読み込み、Pythonのigraph
ライブラリを使って、全く同じグラフ上で、最短10パス/秒を計算することができます。これはパスを計算するのに非効率的な手段ですか?規模では、複数のノードでパスが計算されます(スタックオーバーフローエラーはありません)が、パス計算あたり30/40秒です。