org.apache.flink.graph.example.SingleSourceShortestPaths Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.graph.example;
import org.apache.flink.api.common.ProgramDescription;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.graph.Edge;
import org.apache.flink.graph.Graph;
import org.apache.flink.graph.Vertex;
import org.apache.flink.graph.example.utils.SingleSourceShortestPathsData;
import org.apache.flink.graph.spargel.MessageIterator;
import org.apache.flink.graph.spargel.MessagingFunction;
import org.apache.flink.graph.spargel.VertexUpdateFunction;
import org.apache.flink.graph.utils.Tuple3ToEdgeMap;
/**
* This example shows how to use Gelly's vertex-centric iterations.
*
* It is an implementation of the Single-Source-Shortest-Paths algorithm.
* For a gather-sum-apply implementation of the same algorithm, please refer to {@link GSASingleSourceShortestPaths}.
*
* The input file is a plain text file and must be formatted as follows:
* Edges are represented by tuples of srcVertexId, trgVertexId, distance which are
* separated by tabs. Edges themselves are separated by newlines.
* For example: 1\t2\t0.1\n1\t3\t1.4\n
defines two edges,
* edge 1-2 with distance 0.1, and edge 1-3 with distance 1.4.
*
* If no parameters are provided, the program is run with default data from
* {@link org.apache.flink.graph.example.utils.SingleSourceShortestPathsData}
*/
public class SingleSourceShortestPaths implements ProgramDescription {
public static void main(String[] args) throws Exception {
if (!parseParameters(args)) {
return;
}
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet> edges = getEdgesDataSet(env);
Graph graph = Graph.fromDataSet(edges, new InitVertices(srcVertexId), env);
// Execute the vertex-centric iteration
Graph result = graph.runVertexCentricIteration(
new VertexDistanceUpdater(), new MinDistanceMessenger(), maxIterations);
// Extract the vertices as the result
DataSet> singleSourceShortestPaths = result.getVertices();
// emit result
if (fileOutput) {
singleSourceShortestPaths.writeAsCsv(outputPath, "\n", ",");
// since file sinks are lazy, we trigger the execution explicitly
env.execute("Single Source Shortest Paths Example");
} else {
singleSourceShortestPaths.print();
}
}
// --------------------------------------------------------------------------------------------
// Single Source Shortest Path UDFs
// --------------------------------------------------------------------------------------------
@SuppressWarnings("serial")
private static final class InitVertices implements MapFunction{
private long srcId;
public InitVertices(long srcId) {
this.srcId = srcId;
}
public Double map(Long id) {
if (id.equals(srcId)) {
return 0.0;
}
else {
return Double.POSITIVE_INFINITY;
}
}
}
/**
* Function that updates the value of a vertex by picking the minimum
* distance from all incoming messages.
*/
@SuppressWarnings("serial")
public static final class VertexDistanceUpdater extends VertexUpdateFunction {
@Override
public void updateVertex(Vertex vertex, MessageIterator inMessages) {
Double minDistance = Double.MAX_VALUE;
for (double msg : inMessages) {
if (msg < minDistance) {
minDistance = msg;
}
}
if (vertex.getValue() > minDistance) {
setNewVertexValue(minDistance);
}
}
}
/**
* Distributes the minimum distance associated with a given vertex among all
* the target vertices summed up with the edge's value.
*/
@SuppressWarnings("serial")
public static final class MinDistanceMessenger extends MessagingFunction {
@Override
public void sendMessages(Vertex vertex) {
for (Edge edge : getEdges()) {
sendMessageTo(edge.getTarget(), vertex.getValue() + edge.getValue());
}
}
}
// ******************************************************************************************************************
// UTIL METHODS
// ******************************************************************************************************************
private static boolean fileOutput = false;
private static Long srcVertexId = 1l;
private static String edgesInputPath = null;
private static String outputPath = null;
private static int maxIterations = 5;
private static boolean parseParameters(String[] args) {
if(args.length > 0) {
if(args.length != 4) {
System.err.println("Usage: SingleSourceShortestPaths
© 2015 - 2025 Weber Informatics LLC | Privacy Policy