Package Dijkstra

Source Code of Dijkstra.GraphSearch

package Dijkstra;

import java.util.Hashtable;
import java.util.List;
import java.util.ArrayList;
import org.safehaus.uuid.UUIDGenerator;

import cgl.imr.base.KeyValuePair;
import cgl.imr.base.SerializationException;
import cgl.imr.base.TwisterModel;
import cgl.imr.base.TwisterMonitor;
import cgl.imr.base.impl.JobConf;
import cgl.imr.client.TwisterDriver;
import cgl.imr.types.BytesValue;
import cgl.imr.types.StringKey;

/* CODE FOR TESTING WITHOUT CHECKING FOR COVERGENCE
-----------------------------------------------
for(int m: newGraph.keySet()){
System.out.print(m+" ");
for(int n: newGraph.get(m).getEdges().keySet())
System.out.print(n+","+newGraph.get(m).getEdgeWeight(n)+"| ");
System.out.println();
}
*/

public class GraphSearch {

  public static int graphsize;
  private Graph graph;
  private UUIDGenerator uuidGen = UUIDGenerator.getInstance();

  public GraphSearch(Graph graph) {
    this.graph = graph;
  }

  public void driveMapReduce(int numberOfMapTasks, int numberOfReduceTasks)
      throws Exception {

    long beforeTime = System.currentTimeMillis();

    System.out.println("Number of Map tasks: " + numberOfMapTasks
        + "Number of Map tasks: " + numberOfReduceTasks);

    JobConf jobConf = new JobConf("Dijkstra Shortest Path"
        + uuidGen.generateTimeBasedUUID());

    jobConf.setMapperClass(AllPairsShortestPathMap.class);
    jobConf.setReducerClass(AllPairsShortestPathReduce.class);
    jobConf.setCombinerClass(AllPairsShortestPathCombiner.class);
    jobConf.setNumMapTasks(numberOfMapTasks);
    jobConf.setNumReduceTasks(numberOfReduceTasks);

    TwisterModel mrDriver = new TwisterDriver(jobConf);
    mrDriver.configureMaps();

    int loopCount = 0;
    TwisterMonitor monitor = null;

    for (loopCount = 0;; loopCount++) {

      monitor = mrDriver.runMapReduce(getKeyValuesForMap(numberOfMapTasks));
      monitor.monitorTillCompletion();

      Hashtable<Integer, Node> newGraph = ((AllPairsShortestPathCombiner) mrDriver
          .getCurrentCombiner()).getResults();

      System.out.println(" Loop Count: " + loopCount);

      if (true) {
        int converged = 1;

        for (int m : this.graph.getNodes().keySet())
          for (int n : this.graph.getNodes().get(m).getEdges()
              .keySet())
            if (this.graph.getNodes().get(m).getEdgeWeight(n) != newGraph
                .get(m).getEdgeWeight(n))
              converged = 0;
        if (converged == 1) {
          System.out.println("Converged!");
          break;
        } else
          System.out.println("Not yet Converged!");
      }

      // reset nodes of the graph
      this.graph.setNodes(newGraph);

    }

    this.graph.storeToFile("graph-out");

    double timeInSeconds = ((double) (System.currentTimeMillis() - beforeTime)) / 1000;

    System.out.println("Total Time for All pairs shortest path : "
        + timeInSeconds);
    System.out.println("Total loop count : " + (loopCount));

    // This closes the broker connections
    mrDriver.close();
  }

  private List<KeyValuePair> getKeyValuesForMap(int numMaps) {

    int count = 0;
    List<KeyValuePair> keyValues = new ArrayList<KeyValuePair>();
    List<Graph> subgraphs = graph.getSubGraphs(numMaps);

    for (Graph x : subgraphs) {
      System.out.println("\nGraph" + count
          + " contains the following nodes");
      for (int y : x.getNodes().keySet())
        System.out.print(y + "| ");
      count++;
    }
    StringKey key = null;
    BytesValue value = null;

    int keyNo = 0;
    for (Graph g : subgraphs) {
      key = new StringKey("" + keyNo);
      try {
        value = new BytesValue(g.getBytes());
      } catch (SerializationException e) {
        e.printStackTrace();
      }
      keyValues.add(new KeyValuePair(key, value));
      keyNo++;
    }

    return keyValues;
  }

  public static void main(String[] args) throws Exception  {

    Graph graph = new Graph();
    int numMapTasks = Integer.parseInt(args[0]);
    int numReduceTasks = Integer.parseInt(args[1]);
   
    if (args.length != 3) {
      System.out
          .println("Usage: [map task number][reduce task number][graph file name]");
      System.exit(-1);
    }
    String graphFile = args[2];
    graph.loadFromFile(graphFile);
   
    System.out.println("Total no.of vertices: " + graph.getNodes().size());

    GraphSearch gs = new GraphSearch(graph);
    gs.driveMapReduce(numMapTasks, numReduceTasks);

    System.exit(0);
  }
}
TOP

Related Classes of Dijkstra.GraphSearch

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.