1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.benchmark;
2021import org.apache.giraph.graph.BasicComputation;
22import org.apache.giraph.edge.Edge;
23import org.apache.giraph.graph.Vertex;
24import org.apache.hadoop.io.DoubleWritable;
25import org.apache.hadoop.io.LongWritable;
2627import java.io.IOException;
2829/**30 * Shortest paths algorithm.31 */32publicclassShortestPathsComputationextends BasicComputation<LongWritable,
33 DoubleWritable, DoubleWritable, DoubleWritable> {
34/** Source id. */35publicstaticfinal String SOURCE_ID =
36"giraph.shortestPathsBenchmark.sourceId";
37/** Default source id. */38publicstaticfinallong SOURCE_ID_DEFAULT = 1;
3940/**41 * Check if vertex is source from which to calculate shortest paths.42 *43 * @param vertex Vertex44 * @return True iff vertex is source for shortest paths45 */46privateboolean isSource(
47 Vertex<LongWritable, DoubleWritable, DoubleWritable> vertex) {
48return vertex.getId().get() ==
49 getConf().getLong(SOURCE_ID, SOURCE_ID_DEFAULT);
50 }
5152 @Override
53publicvoid compute(
54 Vertex<LongWritable, DoubleWritable, DoubleWritable> vertex,
55 Iterable<DoubleWritable> messages) throws IOException {
56if (getSuperstep() == 0) {
57 vertex.setValue(new DoubleWritable(Double.MAX_VALUE));
58 }
5960double minDist = isSource(vertex) ? 0d : Double.MAX_VALUE;
61for (DoubleWritable message : messages) {
62 minDist = Math.min(minDist, message.get());
63 }
6465if (minDist < vertex.getValue().get()) {
66 vertex.setValue(new DoubleWritable(minDist));
67for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) {
68double distance = minDist + edge.getValue().get();
69 sendMessage(edge.getTargetVertexId(),
70new DoubleWritable(distance));
71 }
72 }
7374 vertex.voteToHalt();
75 }
76 }