Spark Scala GraphX:两个顶点之间的最短路径 [英] Spark Scala GraphX: Shortest path between two vertices

查看:635
本文介绍了Spark Scala GraphX:两个顶点之间的最短路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spark GraphX(Scala)中有向图G.我想找到从已知顶点v1开始要到达另一个顶点v2的应该交叉的边数.换句话说,我需要从顶点v1到顶点v2的最短路径,该路径以边的数量计算(不使用边的权重).

我正在查看 GraphX文档,但我无法找到一种方法来做到这一点.如果它具有树形结构,则为了计算图的深度也需要这样做.他们是这样做的简单方法吗?

解决方案

要使用Spark GraphX查找顶点之间的最短路径,请使用GraphX documentation, but I wasn't able to find a method to do it. This is also needed in order to compute the depth of the graph if it has a tree structure. Is their an easy way to do this?

解决方案

To find the shortest path between vertices using Spark GraphX, there is the ShortestPaths object, which is member of the org.apache.spark.graphx.lib.

Assuming you have a GraphX graph in g and you want to find the shortest path between the vertices with ids v1 and v2, you can do the following:

import org.apache.spark.graphx._
import org.apache.spark.graphx.lib.ShortestPaths

val result = ShortestPaths.run(g, Seq(v2))

val shortestPath = result               // result is a graph
  .vertices                             // we get the vertices RDD
  .filter({case(vId, _) => vId == v1})  // we filter to get only the shortest path from v1
  .first                                // there's only one value
  ._2                                   // the result is a tuple (v1, Map)
  .get(v2)                              // we get its shortest path to v2 as an Option object

The ShortestPaths GraphX algorithm returns a graph where the vertices RDD contains tuples in the format (vertexId, Map(target -> shortestPath). This graph will contain all vertices of the original graph, and their shortest paths to all target vertices passed in the Seq argument of the algorithm.

In your case, you want the shortest path between two specific vertices, so in the code above I show how to call the algorithm with only one target (v2), and then I filter the result to get only the shortest path starting from the desired vertex (v1).

这篇关于Spark Scala GraphX:两个顶点之间的最短路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆