这段代码是从“使用Apache Giraph的实用图形分析”一书中借用的。
我的代码的目标是将图形从只使用单向边转换为使用双向边。
我的数据是TextDoubleDoubleAdjacencyListVertexInputFormat:格式的制表符分隔的文本文件
A 0 B 0
B 0
C 0 A 0 D 0
D 0 B 0 G 0
E 0 A 0 B 0 D 0
F 0 C 0 D 0
G 0 C 0 E 0我的最小非工作示例是:
import org.apache.giraph.GiraphRunner;
import org.apache.giraph.graph.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.util.ToolRunner;
import org.apache.giraph.edge.EdgeFactory;
public class DigraphToGraph extends BasicComputation<Text,DoubleWritable,DoubleWritable,Text>
{
static final DoubleWritable ORIG_E = new DoubleWritable (1),
NEW_E = new DoubleWritable (2);
@Override
public void compute(Vertex <Text,DoubleWritable, DoubleWritable> vertex, Iterable<Text> messages) {
if (getSuperstep () == 0)
sendMessageToAllEdges (vertex, vertex.getId ());
else {
for (Text m:messages) {
DoubleWritable edgeValue = vertex.getEdgeValue (m);
if (edgeValue == null)
vertex.addEdge (EdgeFactory.create (m, NEW_E));
else
// problem HERE: try to relabel edge
vertex.setEdgeValue (m, ORIG_E);
}
}
vertex.voteToHalt ();
}
}执行此代码后,我希望边只有1或2个值。然而,我得到的是:
G 0.0 C 0.0 E 0.0 D 2.0
E 0.0 A 0.0 B 0.0 D 0.0 G 2.0
F 0.0 C 0.0 D 0.0
D 0.0 B 0.0 G 0.0 E 2.0 B 2.0 C 2.0
B 0.0 E 2.0 D 2.0 A 2.0
C 0.0 A 0.0 D 0.0 G 2.0 B 2.0
A 0.0 B 0.0 E 2.0 C 2.0为什么我的代码不修改预先存在的边缘?
发布于 2016-10-10 22:37:17
您的输出是正确的。假设有一条值为0的从v1到v2的边。您的代码的工作方式如下: 1-如果存在从v2到v1的边,则此边的值将更新为1。2-否则,将使用值2创建从v2到v1的新边。在这两种情况下,源边的值都不会更改。
因为在您的输入数据中没有双向边,所以不执行第一种情况。因此,您的输出中没有值1。
https://stackoverflow.com/questions/39274640
复制相似问题