0

This code is borrowed from the book "Practical Graph Analytics with Apache Giraph".

My code's goal is to transform a graph from using only one-direction edge into using two-direction edges.

My data is a tab-delimited text file to 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

My minimal non-working example is:

  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 ();
  }
}

After executing this code I was expecting to edges to have only 1 or 2 values. However, what I get is:

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

Why my code is not modyfing pre-existing edges?

TylerH
  • 19,065
  • 49
  • 65
  • 86
Marc Kees
  • 156
  • 2
  • 12
  • I also tried to use: `removeEdgesRequest(vertex.getId(),m); addEdgeRequest(vertex.getId(),EdgeFactory.create(m,ORIG_E));` but it didn't work. – Marc Kees Sep 01 '16 at 16:36

1 Answers1

0

Your output is correct. Suppose there is an edge from v1 to v2 with value 0. Your code works as follows: 1- if there is an edge from v2 to v1, then the value of this edge will be updated to 1. 2- otherwise, a new edge will be created from v2 to v1 with the value 2. In both cases, the source edge value wont be changed.

Since in your input data there is no bi-directional edge, the first case do not executed. As a result, there is no value 1 in your output.