コード例 #1
0
  @Override
  public void compute(
      Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
      throws IOException {

    if (0 == getSuperstep()) {
      vertex.setValue(new IntWritable(Integer.MAX_VALUE));
    }

    Integer minDist = isSource(vertex) ? 0 : Integer.MAX_VALUE;
    for (IntWritable msg : messages) {
      if (msg.get() < minDist) {
        minDist = msg.get();
      }
    }

    if (minDist < vertex.getValue().get()) {
      vertex.setValue(new IntWritable(minDist));

      for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) {
        final IntWritable neighbor = edge.getTargetVertexId();
        final Integer distance = minDist + 1;

        sendMessage(neighbor, new IntWritable(distance));
      }
    }

    vertex.voteToHalt();
  }
  @Override
  public void compute(
      Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
      throws IOException {
    int currentLength = vertex.getValue().get();
    // First superstep is special, because we can simply look at the neighbors
    if (getSuperstep() == 0) {
      if (isSource(vertex)) {
        vertex.setValue(new IntWritable(0));
        sendMessageToAllEdges(vertex, vertex.getValue());
      } else {
        vertex.setValue(new IntWritable(Integer.MAX_VALUE));
      }
      vertex.voteToHalt();
      return;
    }

    boolean changed = false;
    // did we get a smaller length ?
    for (IntWritable message : messages) {
      int candidateLength = message.get() + 1;
      if (candidateLength < currentLength) {
        currentLength = candidateLength;
        changed = true;
      }
    }

    // propagate new length to the neighbors
    if (changed) {
      vertex.setValue(new IntWritable(currentLength));
      sendMessageToAllEdges(vertex, vertex.getValue());
    }
    vertex.voteToHalt();
  }
コード例 #3
0
  // public static double level=0;
  @Override
  public void compute(
      Vertex<LongWritable, DoubleWritable, FloatWritable> v, Iterable<DoubleWritable> msg)
      throws IOException {

    if (getSuperstep() == 0) {
      if (isSource(v) == true) {
        v.setValue(new DoubleWritable(0));
        //			 level++;
        sendMessageToAllEdges(v, new DoubleWritable(v.getId().get()));
      } else v.setValue(new DoubleWritable(-1));
    } else {

      for (DoubleWritable messages : msg) {

        if (v.getValue().get() == -1) {
          v.setValue(messages);
          for (Edge<LongWritable, FloatWritable> edge : v.getEdges()) {
            if (edge.getTargetVertexId().get() != v.getValue().get()) {
              sendMessage(edge.getTargetVertexId(), new DoubleWritable(v.getId().get()));
            }
          }

        } else {
          if (v.getValue().get() != messages.get()) {
            removeEdgesRequest(v.getId(), new LongWritable((long) messages.get()));
            removeEdgesRequest(new LongWritable((long) messages.get()), v.getId());
          }
        }

        /*

        	if(v.getValue().get() >  messages.get())
        	{
        		v.setValue(new DoubleWritable(getSuperstep()));
        //		changed=true;
        	}*/
      }
      if (v.getValue().get() == -1) {
        for (Edge<LongWritable, FloatWritable> edge : v.getEdges()) {
          if (edge.getTargetVertexId().get() != v.getValue().get())
            sendMessage(edge.getTargetVertexId(), new DoubleWritable(v.getId().get()));
        }
      }
      /*	if(changed)
      {
      	level++;
      	sendMessageToAllEdges(v,new DoubleWritable(level));
      	changed=false;
      }*/
    }
    v.voteToHalt();
  }
コード例 #4
0
  /**
   * Propagates the smallest vertex id to all neighbors. Will always choose to halt and only
   * reactivate if a smaller id has been sent to it.
   *
   * @param messages Iterator of messages from the previous superstep.
   * @throws java.io.IOException
   */
  @Override
  public void compute(
      Vertex<LongWritable, LongWritable, NullWritable> vertex, Iterable<LongWritable> messages)
      throws IOException {
    long currentComponent = vertex.getValue().get();

    // First superstep is special, because we can simply look at the neighbors
    if (getSuperstep() == 0) {
      for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) {
        long neighbor = edge.getTargetVertexId().get();
        if (neighbor < currentComponent) {
          currentComponent = neighbor;
        }
      }
      // Only need to send value if it is not the own id
      if (currentComponent != vertex.getValue().get()) {
        vertex.setValue(new LongWritable(currentComponent));
        for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) {
          LongWritable neighbor = edge.getTargetVertexId();
          if (neighbor.get() > currentComponent) {
            sendMessage(neighbor, vertex.getValue());
          }
        }
      }

      vertex.voteToHalt();
      return;
    }

    boolean changed = false;
    // did we get a smaller id ?
    for (LongWritable message : messages) {
      long candidateComponent = message.get();
      if (candidateComponent < currentComponent) {
        currentComponent = candidateComponent;
        changed = true;
      }
    }

    // propagate new component id to the neighbors
    if (changed) {
      vertex.setValue(new LongWritable(currentComponent));
      sendMessageToAllEdges(vertex, vertex.getValue());
    }
    vertex.voteToHalt();
  }
 @Override
 public void compute(
     Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
     throws IOException {
   if (getSuperstep() == 0) {
     // send list of this vertex's neighbors to all neighbors
     for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) {
       sendMessageToAllEdges(vertex, edge.getTargetVertexId());
     }
   } else {
     for (IntWritable message : messages) {
       // INTENTIONAL BUG: the original algorithm has these two lines, which
       // avoids the
       // NullPointerException, which the current code throws.
       // final int current = (closeMap.get(message) == null) ?
       // 0 : closeMap.get(message) + 1;
       final int current = closeMap.get(message);
       closeMap.put(message, current);
     }
     // make sure the result values are sorted and
     // packaged in an IntArrayListWritable for output
     Set<Pair> sortedResults = Sets.<Pair>newTreeSet();
     for (Map.Entry<IntWritable, Integer> entry : closeMap.entrySet()) {
       sortedResults.add(new Pair(entry.getKey(), entry.getValue()));
     }
     IntArrayListWritable outputList = new IntArrayListWritable();
     for (Pair pair : sortedResults) {
       if (pair.value > 0) {
         outputList.add(pair.key);
       } else {
         break;
       }
     }
     if (outputList.isEmpty()) {
       vertex.setValue(new IntWritable(-1));
     } else {
       vertex.setValue(outputList.get(0));
     }
   }
   vertex.voteToHalt();
 }