public PathResult myAlgorithm() { PathResult p = new PathResult(); map.orderInRows(); for (int i = 1; i < map.getNumRows() - 1; i++) { map.condense(i); } String name = map.getPath("START", "END").getName(); if (name.equals("")) name = "START,END"; else name = "START," + name + ",END"; p.setPath(name); p.setWeight(map.getPath("START", "END").getWeight()); return p; }
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 PathResult bruteSearch(Map map, Node curNode) { PathResult p = new PathResult(); if (curNode.getName().equals("END")) { p.setPath("END"); return p; } PathResult[] depends = new PathResult[curNode.getNumDepend()]; for (int i = 0; i < curNode.getNumDepend(); i++) { Path path = map.getPath(curNode.getName(), curNode.getDepend(i).getName()); depends[i] = (new PathResult(path.getA().getName() + ",", path.getWeight())) .append(bruteSearch(map, curNode.getDepend(i))); // System.out.println(i + " " + depends[i]); } int minIndex = 0; for (int i = 1; i < depends.length; i++) { if (depends[i].getWeight() < depends[minIndex].getWeight()) minIndex = i; } return depends[minIndex]; }