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; }
public MapRunner() { Scanner scan = new Scanner(System.in); String fileName = scan.next(); if (fileName.equals("rGen")) { int numNodes = scan.nextInt(); generateRandom(numNodes); } scan.close(); readFile(fileName); if (map == null) throw new NullPointerException(); Timer timer = new Timer(); // Set up brute force map before we do anything to map Map bruteMap = map.newInstance(); // Dijkstra's Algorithm timer.start(); System.out.println("DIJKSTRA'S: " + dijkstra()); timer.printMicro(); // Actually do the algorithm here timer.start(); System.out.println("MY ALGORITHM: " + myAlgorithm()); timer.printMicro(); // Brute force timer.start(); System.out.println("BRUTE FORCE: " + bruteSearch(bruteMap, bruteMap.getStartNode())); timer.printMicro(); }