|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.graphx.lib |
| 19 | + |
| 20 | +import org.apache.spark.graphx._ |
| 21 | +import scala.reflect.ClassTag |
| 22 | + |
| 23 | +/** |
| 24 | + * Computes shortest paths to the given set of landmark vertices, returning a graph where each |
| 25 | + * vertex attribute is a map containing the shortest-path distance to each reachable landmark. |
| 26 | + */ |
| 27 | +object ShortestPaths { |
| 28 | + /** Stores a map from the vertex id of a landmark to the distance to that landmark. */ |
| 29 | + type SPMap = Map[VertexId, Int] |
| 30 | + |
| 31 | + private def makeMap(x: (VertexId, Int)*) = Map(x: _*) |
| 32 | + |
| 33 | + private def incrementMap(spmap: SPMap): SPMap = spmap.map { case (v, d) => v -> (d + 1) } |
| 34 | + |
| 35 | + private def addMaps(spmap1: SPMap, spmap2: SPMap): SPMap = |
| 36 | + (spmap1.keySet ++ spmap2.keySet).map { |
| 37 | + k => k -> math.min(spmap1.getOrElse(k, Int.MaxValue), spmap2.getOrElse(k, Int.MaxValue)) |
| 38 | + }.toMap |
| 39 | + |
| 40 | + /** |
| 41 | + * Computes shortest paths to the given set of landmark vertices. |
| 42 | + * |
| 43 | + * @tparam ED the edge attribute type (not used in the computation) |
| 44 | + * |
| 45 | + * @param graph the graph for which to compute the shortest paths |
| 46 | + * @param landmarks the list of landmark vertex ids. Shortest paths will be computed to each |
| 47 | + * landmark. |
| 48 | + * |
| 49 | + * @return a graph where each vertex attribute is a map containing the shortest-path distance to |
| 50 | + * each reachable landmark vertex. |
| 51 | + */ |
| 52 | + def run[ED: ClassTag](graph: Graph[_, ED], landmarks: Seq[VertexId]): Graph[SPMap, ED] = { |
| 53 | + val spGraph = graph.mapVertices { (vid, attr) => |
| 54 | + if (landmarks.contains(vid)) makeMap(vid -> 0) else makeMap() |
| 55 | + } |
| 56 | + |
| 57 | + val initialMessage = makeMap() |
| 58 | + |
| 59 | + def vertexProgram(id: VertexId, attr: SPMap, msg: SPMap): SPMap = { |
| 60 | + addMaps(attr, msg) |
| 61 | + } |
| 62 | + |
| 63 | + def sendMessage(edge: EdgeTriplet[SPMap, _]): Iterator[(VertexId, SPMap)] = { |
| 64 | + val newAttr = incrementMap(edge.srcAttr) |
| 65 | + if (edge.dstAttr != addMaps(newAttr, edge.dstAttr)) Iterator((edge.dstId, newAttr)) |
| 66 | + else Iterator.empty |
| 67 | + } |
| 68 | + |
| 69 | + Pregel(spGraph, initialMessage)(vertexProgram, sendMessage, addMaps) |
| 70 | + } |
| 71 | +} |
0 commit comments