/
GSAPageRank.java
129 lines (107 loc) · 4.58 KB
/
GSAPageRank.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* 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.examples;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.graph.EdgeJoinFunction;
import org.apache.flink.graph.Graph;
import org.apache.flink.graph.GraphAlgorithm;
import org.apache.flink.graph.Vertex;
import org.apache.flink.graph.gsa.ApplyFunction;
import org.apache.flink.graph.gsa.GSAConfiguration;
import org.apache.flink.graph.gsa.GatherFunction;
import org.apache.flink.graph.gsa.Neighbor;
import org.apache.flink.graph.gsa.SumFunction;
import org.apache.flink.types.LongValue;
/**
* This is an implementation of a simple PageRank algorithm, using a gather-sum-apply iteration. The
* user can define the damping factor and the maximum number of iterations.
*
* <p>The implementation assumes that each page has at least one incoming and one outgoing link.
*/
public class GSAPageRank<K>
implements GraphAlgorithm<K, Double, Double, DataSet<Vertex<K, Double>>> {
private double beta;
private int maxIterations;
/**
* Creates an instance of the GSA PageRank algorithm.
*
* <p>The implementation assumes that each page has at least one incoming and one outgoing link.
*
* @param beta the damping factor
* @param maxIterations the maximum number of iterations
*/
public GSAPageRank(double beta, int maxIterations) {
this.beta = beta;
this.maxIterations = maxIterations;
}
@Override
public DataSet<Vertex<K, Double>> run(Graph<K, Double, Double> network) throws Exception {
DataSet<Tuple2<K, LongValue>> vertexOutDegrees = network.outDegrees();
Graph<K, Double, Double> networkWithWeights =
network.joinWithEdgesOnSource(vertexOutDegrees, new InitWeights());
GSAConfiguration parameters = new GSAConfiguration();
parameters.setOptNumVertices(true);
return networkWithWeights
.runGatherSumApplyIteration(
new GatherRanks(),
new SumRanks(),
new UpdateRanks<>(beta),
maxIterations,
parameters)
.getVertices();
}
// --------------------------------------------------------------------------------------------
// Page Rank UDFs
// --------------------------------------------------------------------------------------------
@SuppressWarnings("serial")
private static final class GatherRanks extends GatherFunction<Double, Double, Double> {
@Override
public Double gather(Neighbor<Double, Double> neighbor) {
double neighborRank = neighbor.getNeighborValue();
if (getSuperstepNumber() == 1) {
neighborRank = 1.0 / this.getNumberOfVertices();
}
return neighborRank * neighbor.getEdgeValue();
}
}
@SuppressWarnings("serial")
private static final class SumRanks extends SumFunction<Double, Double, Double> {
@Override
public Double sum(Double newValue, Double currentValue) {
return newValue + currentValue;
}
}
@SuppressWarnings("serial")
private static final class UpdateRanks<K> extends ApplyFunction<K, Double, Double> {
private final double beta;
public UpdateRanks(double beta) {
this.beta = beta;
}
@Override
public void apply(Double rankSum, Double currentValue) {
setResult((1 - beta) / this.getNumberOfVertices() + beta * rankSum);
}
}
@SuppressWarnings("serial")
private static final class InitWeights implements EdgeJoinFunction<Double, LongValue> {
public Double edgeJoin(Double edgeValue, LongValue inputValue) {
return edgeValue / (double) inputValue.getValue();
}
}
}