コード例 #1
1
ファイル: MapRunner.java プロジェクト: nate-trojian/mwpf
  public PathResult dijkstra() {
    PathResult p = new PathResult();
    Map dMap = map.newInstance();
    HashSet<Node> evaluated = new HashSet<Node>();
    HashSet<Node> unevaluated = new HashSet<Node>();
    HashMap<String, Double> dist = new HashMap<String, Double>();
    HashMap<String, String> previous = new HashMap<String, String>();
    for (int i = 0; i < dMap.getSize(); i++) {
      Node toAdd = dMap.getNode(i);
      unevaluated.add(toAdd);
      dist.put(toAdd.getName(), Double.MAX_VALUE);
      previous.put(toAdd.getName(), null);
    }
    dist.put("START", 0D);

    Node curNode = dMap.getStartNode(), temp = null;
    while (!unevaluated.isEmpty()) {
      Iterator<Node> it = unevaluated.iterator();
      curNode = it.next();
      while (it.hasNext()) {
        temp = it.next();
        if (dist.get(temp.getName()) < dist.get(curNode.getName())) curNode = temp;
      }
      evaluated.add(curNode);
      unevaluated.remove(curNode);
      if (dist.get(curNode.getName()) == Double.MAX_VALUE || dMap.getEndNode().equals(curNode))
        break;

      for (int i = 0; i < curNode.getNumDepend(); i++) {
        Node depend = curNode.getDepend(i);
        if (evaluated.contains(depend)) continue;
        double tempWeight =
            dist.get(curNode.getName())
                + dMap.getPath(curNode.getName(), depend.getName()).getWeight();
        if (tempWeight < dist.get(depend.getName())) {
          dist.put(depend.getName(), tempWeight);
          previous.put(depend.getName(), curNode.getName());
        }
      }
    }
    p.setWeight(dist.get(curNode.getName()));

    String name = curNode.getName();
    while (previous.get(curNode.getName()) != null) {
      curNode = dMap.getFromName(previous.get(curNode.getName()));
      name = curNode.getName() + "," + name;
    }
    p.setPath(name);
    return p;
  }
コード例 #2
0
ファイル: MapRunner.java プロジェクト: nate-trojian/mwpf
 public void readFile(String fn) {
   try {
     map = new Map();
     map.setStartNode(new Node("START", this));
     Scanner scan = new Scanner(new File("src/mwpf/" + fn + ".txt"));
     /*
      * 1: # of nodes (A-Z, Aa-Zz, etc)
      * 2: # of paths, p
      * 3-p+2: [A] [B] [Weight]
      */
     int numNodes = Integer.valueOf(scan.nextLine());
     for (int i = 0; i < numNodes; i++) {
       int temp = i;
       String name = "";
       int log = (int) Math.floor(Math.log(temp) / Math.log(26));
       if (log == Integer.MIN_VALUE) log = 0;
       // name = ""+((char)(65+temp/((int)Math.pow(26, log))));
       for (int j = 0; j <= log; j++) {
         name = ((char) (65 + temp % 26)) + name;
         temp = (int) Math.floor(temp / 26D) - 1;
       }
       map.addNode(new Node(name, this));
     }
     map.setEndNode(new Node("END", this));
     int numPaths = Integer.valueOf(scan.nextLine());
     for (int i = 0; i < numPaths; i++) {
       String line = scan.nextLine();
       String[] lineSplit = line.split(" ");
       if (lineSplit.length != 3) continue;
       // System.out.println(lineSplit[0] + " " + lineSplit[1]);
       map.addPath(
           new Path(
               map.getFromName(lineSplit[0]),
               map.getFromName(lineSplit[1]),
               Double.parseDouble(lineSplit[2])));
     }
     scan.close();
   } catch (Exception e) {
     System.out.println("Failed reading file: " + e);
   }
 }