Package Dijkstra

Source Code of Dijkstra.AllPairsShortestPathReduce

package Dijkstra;


//TODO: weights need to be made static or global. Look into that.
import java.util.Hashtable;
import java.util.List;

import cgl.imr.base.Key;
import cgl.imr.base.ReduceOutputCollector;
import cgl.imr.base.ReduceTask;
import cgl.imr.base.SerializationException;
import cgl.imr.base.TwisterException;
import cgl.imr.base.Value;
import cgl.imr.base.impl.JobConf;
import cgl.imr.base.impl.ReducerConf;
import cgl.imr.types.BytesValue;
import cgl.imr.types.StringKey;

public class AllPairsShortestPathReduce implements ReduceTask {
 
  public static final int distance = Integer.MAX_VALUE;
  public static Hashtable<Integer, Integer> edgeWeights = new Hashtable<Integer, Integer>();
 
  static{
    // Initialize the contents of weights with Integer.MAX_VALUE.
    for (int i = 1; i <= 1000; i++)
      edgeWeights.put(i, distance);
  }
 
  public void reduce(ReduceOutputCollector collector, Key key,
      List<Value> values) throws TwisterException {

    try {
      int ID = 0;

      if (values.size() <= 0) {
        System.err.println("Reduce Input Error!");
        throw new TwisterException("Reduce input error no values.");
      }

      for (Value value : values) {
        BytesValue val = (BytesValue) value;
        Node node = new Node(val.getBytes());
       
        ID = node.getID();

        // save only the minimum distance for each edge
        for (int v : node.getEdges().keySet()) {
          if (node.getEdgeWeight(v) < edgeWeights.get(v))
            edgeWeights.put(v, node.getEdgeWeight(v));
        }
      }

      // create the new node with non-redundant edges and edge with minimum
      // distance and emit it
      Node n = new Node(ID);
      for (int i = 1; i <= 1000; i++) {
        if (edgeWeights.get(i) != Integer.MAX_VALUE)
          n.setEdgeWeight(i, edgeWeights.get(i));
      }

      collector.collect(new StringKey("" + n.getID()), new BytesValue(n
          .getBytes()));
    } catch (SerializationException e) {
      e.printStackTrace();
      throw new TwisterException(e);
    }
  }
 
  @Override
  public void close() throws TwisterException {
  }

  @Override
  public void configure(JobConf arg0, ReducerConf arg1)
      throws TwisterException {
  }


}
TOP

Related Classes of Dijkstra.AllPairsShortestPathReduce

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.